This commit is contained in:
j3d1 2021-11-16 20:19:02 +01:00
parent bce33fa836
commit 084161eba9
7 changed files with 108 additions and 77 deletions

View file

@ -1,91 +1,30 @@
#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 "ntp.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);
@ -128,19 +67,20 @@ void user_init(void) {
system_init_config();
wifi_available_semaphore = xSemaphoreCreateBinary();
wan_available_semaphore = xSemaphoreCreateBinary();
time_available_semaphore = xSemaphoreCreateBinary();
gpio_enable(SWITCH_PIN, GPIO_INPUT);
//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(&httpd_task, "httpd_task", 2048, NULL, 2, NULL);
//xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
xTaskCreate(&sntp_tsk, "SNTP", 512, NULL, 1, NULL);
xTaskCreate(&sntp_task, "sntp_task", 512, NULL, 1, NULL);
//xTaskCreate(buttonIntTask, "buttonIntTask", 256, &tsqueue, 2, NULL);
@ -148,6 +88,8 @@ void user_init(void) {
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);
@ -158,4 +100,5 @@ void user_init(void) {
}
printf("profit!\n");
*/
}

View file

@ -13,9 +13,9 @@
#include <time.h>
#include <semphr.h>
#define DAYTIME(h, m, s) (h*3600+m*60+s)
#include "ntp.h"
extern SemaphoreHandle_t time_available_semaphore;
#define DAYTIME(h, m, s) (h*3600+m*60+s)
struct {
time_t sunrise_start = DAYTIME(8, 30, 0);
@ -204,7 +204,7 @@ extern "C" void manual_switch() {
printf("NIGHT_S\n");
}
printf("%lld | %lld %lld %lld %lld\n", rt,
printf("%ld | %ld %ld %ld %ld\n", rt,
settings.sunrise_start,
settings.sunrise_end,
settings.sunset_time,
@ -282,6 +282,11 @@ extern "C" void manual_switch() {
vTaskDelay(1000 / portTICK_PERIOD_MS);
/*time_t ts = time(nullptr);
int t = ts;
printf("TIME: %d %d %s", t, (int) day_seconds(), ctime(&ts));*/
}
}

53
firmware/ntp.cpp Normal file
View file

@ -0,0 +1,53 @@
//
// Created by jedi on 04.11.21.
//
#include "ntp.h"
#include "wifi.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Add extras/sntp component to makefile for this include to work */
#include <sntp.h>
#include <time.h>
#ifdef __cplusplus
}
#endif
#include <stdio.h>
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
SemaphoreHandle_t time_available_semaphore = nullptr;
void sntp_task(void *pvParameters) {
const char *servers[] = {SNTP_SERVERS};
(void) pvParameters;
while (!uxSemaphoreGetCount(wan_available_semaphore)) {
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
/* 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 */
vTaskDelete(nullptr);
}

23
firmware/ntp.h Normal file
View file

@ -0,0 +1,23 @@
//
// Created by jedi on 04.11.21.
//
#ifndef FIRMWARE_NTP_H
#define FIRMWARE_NTP_H
#include <FreeRTOS.h>
#include <semphr.h>
#ifdef __cplusplus
extern "C" {
#endif
extern SemaphoreHandle_t time_available_semaphore;
void sntp_task(void *pvParameters);
#ifdef __cplusplus
}
#endif
#endif //FIRMWARE_NTP_H

View file

@ -422,14 +422,10 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
extern "C" void httpd_task(void *pvParameters) {
(void) pvParameters;
while (!uxSemaphoreGetCount(wifi_available_semaphore))
while (!uxSemaphoreGetCount(wifi_available_semaphore)) {
vTaskDelay(500 / portTICK_PERIOD_MS);
/*tCGI pCGIs[] = {
{"/gpio", (tCGIHandler) gpio_cgi_handler},
};
}
// register handlers and start the server
http_set_cgi_handlers(pCGIs, sizeof(pCGIs) / sizeof(pCGIs[0]));*/
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
httpd_init();

View file

@ -22,9 +22,9 @@ extern "C" {
#include <espressif/esp_softap.h>
#include <dhcpserver.h>
const char *wificfg_default_ssid = "fiatlux_%02X%02X%02X";
const char *wificfg_default_ssid = "fiatlux-noctis_%02X%02X%02X";
const char *wificfg_default_password = "fiatlux02";
const char *wificfg_default_hostname = "fiatlux-%02x%02x%02x";
const char *wificfg_default_hostname = "fiatlux-noctis-%02x%02x%02x";
char *wifi_sta_ssid = nullptr;
char *wifi_sta_password = nullptr;
@ -32,6 +32,7 @@ char *wifi_ap_ssid = nullptr;
char *wifi_ap_password = nullptr;
SemaphoreHandle_t wifi_available_semaphore = nullptr;
SemaphoreHandle_t wan_available_semaphore = nullptr;
[[noreturn]] static void dns_task(void *pvParameters) {
printf("run dns task\n");
@ -361,9 +362,18 @@ extern "C" void wifi_task(void *pvParameters) {
//gpio_enable(LED_PIN, GPIO_OUTPUT);
//gpio_write(LED_PIN, true);
printf("wifi wait for ip\n");
xSemaphoreGive(wifi_available_semaphore);
/* Wait until we have joined AP and are assigned an IP */
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelay(250 / portTICK_PERIOD_MS);
}
xSemaphoreGive(wan_available_semaphore);
//monitor loop connection here
printf("wifi task done\n");
vTaskDelete(nullptr);

View file

@ -13,6 +13,7 @@ extern "C" {
#endif
extern SemaphoreHandle_t wifi_available_semaphore;
extern SemaphoreHandle_t wan_available_semaphore;
void wifi_task(void *pvParameters);