161 lines
4.1 KiB
C
161 lines
4.1 KiB
C
#include <espressif/esp_common.h>
|
|
#include <esp8266.h>
|
|
#include <esp/uart.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <FreeRTOS.h>
|
|
#include <task.h>
|
|
#include <ssid_config.h>
|
|
#include <httpd/httpd.h>
|
|
#include <sys/time.h>
|
|
|
|
#include <lwip/err.h>
|
|
#include <lwip/sockets.h>
|
|
#include <lwip/sys.h>
|
|
#include <lwip/netdb.h>
|
|
#include <lwip/dns.h>
|
|
|
|
#include "system.h"
|
|
#include "wifi.h"
|
|
#include "web.h"
|
|
#include "mqtt.h"
|
|
#include "lux.h"
|
|
|
|
#include <espressif/esp_common.h>
|
|
#include <esp/uart.h>
|
|
|
|
#include "rboot-api.h"
|
|
|
|
#define LED_PIN 2
|
|
#define SWITCH_PIN 2
|
|
|
|
/* Add extras/sntp component to makefile for this include to work */
|
|
#include <sntp.h>
|
|
#include <time.h>
|
|
|
|
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
|
|
"2.pool.ntp.org", "3.pool.ntp.org"
|
|
|
|
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
|
|
#define UNUSED_ARG(x) (void)x
|
|
|
|
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
|
|
|
|
|
|
SemaphoreHandle_t time_available_semaphore;
|
|
|
|
|
|
time_t day_seconds();
|
|
|
|
|
|
void sntp_tsk(void *pvParameters) {
|
|
const char *servers[] = {SNTP_SERVERS};
|
|
UNUSED_ARG(pvParameters);
|
|
|
|
while (!uxSemaphoreGetCount(wifi_available_semaphore)) {
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
printf("waitfi\n");
|
|
}
|
|
|
|
/* Wait until we have joined AP and are assigned an IP */
|
|
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
|
|
vTaskDelayMs(100);
|
|
}
|
|
|
|
/* Start SNTP */
|
|
printf("Starting SNTP... ");
|
|
/* SNTP will request an update each 5 minutes */
|
|
sntp_set_update_delay(5 * 60000);
|
|
/* Set GMT+1 zone, daylight savings off */
|
|
const struct timezone tz = {1 * 60, 1};
|
|
/* SNTP initialization */
|
|
sntp_initialize(&tz);
|
|
/* Servers must be configured right after initialization */
|
|
sntp_set_servers(servers, sizeof(servers) / sizeof(char *));
|
|
printf("SNTP DONE!\n");
|
|
|
|
xSemaphoreGive(time_available_semaphore);
|
|
|
|
/* Print date and time each 5 seconds */
|
|
while (1) {
|
|
vTaskDelayMs(5000);
|
|
time_t ts = time(NULL);
|
|
int t = ts;
|
|
printf("TIME: %d %d %s", t, (int) day_seconds(), ctime(&ts));
|
|
}
|
|
}
|
|
|
|
|
|
void gpio_intr_handler(uint8_t gpio_num);
|
|
|
|
void manual_switch(void);
|
|
|
|
void buttonIntTask(void *pvParameters)
|
|
{
|
|
printf("Waiting for button press interrupt on gpio %d...\r\n", SWITCH_PIN);
|
|
QueueHandle_t *tsqueue = (QueueHandle_t *)pvParameters;
|
|
gpio_set_interrupt(SWITCH_PIN, int_type, gpio_intr_handler);
|
|
|
|
uint32_t last = 0;
|
|
while(1) {
|
|
uint32_t button_ts;
|
|
xQueueReceive(*tsqueue, &button_ts, portMAX_DELAY);
|
|
button_ts *= portTICK_PERIOD_MS;
|
|
if(last < button_ts-200) {
|
|
manual_switch();
|
|
//printf("Button interrupt fired at %dms\r\n", button_ts);
|
|
last = button_ts;
|
|
}
|
|
}
|
|
}
|
|
|
|
static QueueHandle_t tsqueue;
|
|
|
|
void gpio_intr_handler(uint8_t gpio_num)
|
|
{
|
|
uint32_t now = xTaskGetTickCountFromISR();
|
|
xQueueSendToBackFromISR(tsqueue, &now, NULL);
|
|
}
|
|
|
|
void register_app(void);
|
|
|
|
void user_init(void) {
|
|
uart_set_baud(0, 115200);
|
|
printf("SDK version: %s\n", sdk_system_get_sdk_version());
|
|
|
|
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
|
|
|
|
system_init_config();
|
|
|
|
wifi_available_semaphore = xSemaphoreCreateBinary();
|
|
time_available_semaphore = xSemaphoreCreateBinary();
|
|
|
|
gpio_enable(SWITCH_PIN, GPIO_INPUT);
|
|
|
|
tsqueue = xQueueCreate(2, sizeof(uint32_t));
|
|
|
|
xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL);
|
|
|
|
//xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL);
|
|
|
|
//xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
|
|
|
|
xTaskCreate(&sntp_tsk, "SNTP", 512, NULL, 1, NULL);
|
|
|
|
//xTaskCreate(buttonIntTask, "buttonIntTask", 256, &tsqueue, 2, NULL);
|
|
|
|
register_app();
|
|
|
|
gpio_enable(LED_PIN, GPIO_OUTPUT);
|
|
gpio_write(LED_PIN, true);
|
|
rboot_config conf = rboot_get_config();
|
|
printf("\r\n\r\nOTA Basic demo.\r\nCurrently running on flash slot %d / %d.\r\n\r\n",
|
|
conf.current_rom, conf.count);
|
|
|
|
printf("Image addresses in flash:\r\n");
|
|
for (int i = 0; i < conf.count; i++) {
|
|
printf("%c%d: offset 0x%08x\r\n", i == conf.current_rom ? '*' : ' ', i, conf.roms[i]);
|
|
}
|
|
|
|
printf("profit!\n");
|
|
}
|