led
This commit is contained in:
parent
f05fbac50d
commit
32f94dfaf1
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
src/build/
|
||||
config.csv
|
||||
config.bin
|
1
src/.gitignore
vendored
1
src/.gitignore
vendored
@ -1 +0,0 @@
|
||||
build/
|
@ -39,15 +39,14 @@ void deviceInit() {
|
||||
void app_main(void)
|
||||
{
|
||||
deviceInit();
|
||||
|
||||
// it is important to initialize the counter before the gpios
|
||||
// it is important to initialize the counter before the gpios
|
||||
counterInit();
|
||||
|
||||
gpioInit();
|
||||
networkInit(isGpioForceProv());
|
||||
timesyncInit();
|
||||
|
||||
sinksenderInit();
|
||||
gpioInit();
|
||||
gpioLedBlink(50);
|
||||
networkInit(isGpioForceProv());
|
||||
gpioLedBlink(25);
|
||||
timesyncInit();
|
||||
|
||||
/* Start main application now */
|
||||
while (1) {
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "counter.h"
|
||||
#include "timesync.h"
|
||||
#include "sinkStruct.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include <driver/timer.h>
|
||||
#include <esp_log.h>
|
||||
@ -69,6 +70,7 @@ static void counterZeroCrossingAveragerTask(void *arg) {
|
||||
} else {
|
||||
ESP_LOGI(TAG, "now it is settled");
|
||||
settled = true;
|
||||
gpioLedOn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,36 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "gpio.h"
|
||||
#include "counter.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
|
||||
static const char *TAG = "gpio";
|
||||
|
||||
|
||||
static uint8_t ledBlinkLevel = 0;
|
||||
static uint8_t ledLastState = 0;
|
||||
|
||||
static void gpioLedTask(void *arg) {
|
||||
static uint8_t cnt = 0;
|
||||
while (1) {
|
||||
if (ledBlinkLevel != 0) {
|
||||
if (cnt == ledBlinkLevel) {
|
||||
cnt = 0;
|
||||
ledLastState ^= 0x01;
|
||||
gpio_set_level(GPIO_BUILTIN_LED, ledLastState);
|
||||
} else {
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
void gpioInit() {
|
||||
ESP_LOGI(TAG, "Initializing gpios");
|
||||
|
||||
@ -27,12 +49,37 @@ void gpioInit() {
|
||||
io_conf.pull_down_en = 0;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.pin_bit_mask = (1ULL << GPIO_BUILTIN_LED);
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
io_conf.pull_up_en = 0;
|
||||
io_conf.pull_down_en = 0;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
gpio_install_isr_service(0);
|
||||
gpio_isr_handler_add(GPIO_ZERO_CROSSING, counterZeroCrossingISR, NULL);
|
||||
|
||||
xTaskCreate(gpioLedTask, "gpio_led_task", 1024, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
bool isGpioForceProv() {
|
||||
int r = gpio_get_level(GPIO_FORCE_PROV);
|
||||
ESP_LOGI(TAG, "forceProv pin is %d", r);
|
||||
return (r != 0);
|
||||
}
|
||||
}
|
||||
|
||||
void gpioLedOn() {
|
||||
ledBlinkLevel = 0;
|
||||
ledLastState = 1;
|
||||
gpio_set_level(GPIO_BUILTIN_LED, ledLastState);
|
||||
}
|
||||
|
||||
void gpioLedOff() {
|
||||
ledBlinkLevel = 0;
|
||||
ledLastState = 0;
|
||||
gpio_set_level(GPIO_BUILTIN_LED, ledLastState);
|
||||
}
|
||||
|
||||
void gpioLedBlink(uint8_t f) {
|
||||
ledBlinkLevel = f;
|
||||
}
|
||||
|
@ -2,12 +2,16 @@
|
||||
#define _GPIO_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#define GPIO_BUILTIN_LED 2
|
||||
#define GPIO_FORCE_PROV 5
|
||||
#define GPIO_ZERO_CROSSING 26
|
||||
|
||||
void gpioInit();
|
||||
bool isGpioForceProv();
|
||||
|
||||
void gpioLedOn();
|
||||
void gpioLedOff();
|
||||
void gpioLedBlink(uint8_t f);
|
||||
|
||||
#endif // _GPIO_H_
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "sinkSender.h"
|
||||
#include "sinkStruct.h"
|
||||
#include "sha256.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
@ -20,22 +21,15 @@
|
||||
static const char *TAG = "ss";
|
||||
extern char VERSION[];
|
||||
|
||||
const uint32_t CONFIG_MAGIC = 0xaffe0002;
|
||||
|
||||
static const char DEFAULT_SINKSERVER[] = "sink.hottis.de";
|
||||
static const uint16_t DEFAULT_SINKPORT = 20169;
|
||||
static const char SINK_SERVER[] = "sink.hottis.de";
|
||||
static const uint16_t SINK_PORT = 20169;
|
||||
|
||||
static const char DEFAULT_DEVICE_ID[] = "MainsCnt0x";
|
||||
static char deviceId[16];
|
||||
static const char DEFAULT_SHAREDSECRET[] = "1234567890123456789012345678901";
|
||||
static char sharedSecret[32];
|
||||
|
||||
typedef struct __attribute__((__packed__)) {
|
||||
uint32_t magic;
|
||||
char sinkServer[48];
|
||||
uint16_t sinkPort;
|
||||
char deviceId[16];
|
||||
char sharedSecret[32];
|
||||
} sinksenderConfig_t;
|
||||
|
||||
sinksenderConfig_t config;
|
||||
|
||||
|
||||
extern xQueueHandle minuteBufferQueue;
|
||||
@ -48,15 +42,15 @@ static void sinksenderSend(t_minuteBuffer *minuteBuffer) {
|
||||
minuteBuffer->s.version = strtoll(VERSION, NULL, 16);
|
||||
|
||||
memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));
|
||||
strcpy(minuteBuffer->s.deviceId, config.deviceId);
|
||||
strcpy(minuteBuffer->s.deviceId, deviceId);
|
||||
|
||||
memcpy(minuteBuffer->s.hash, config.sharedSecret, SHA256_BLOCK_SIZE);
|
||||
memcpy(minuteBuffer->s.hash, sharedSecret, SHA256_BLOCK_SIZE);
|
||||
SHA256_CTX ctx;
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, minuteBuffer->b, sizeof(minuteBuffer->b));
|
||||
sha256_final(&ctx, minuteBuffer->s.hash);
|
||||
|
||||
struct hostent *hptr = gethostbyname(config.sinkServer);
|
||||
struct hostent *hptr = gethostbyname(SINK_SERVER);
|
||||
if (hptr) {
|
||||
if (hptr->h_addrtype == AF_INET) {
|
||||
char *sinkAddr = hptr->h_addr_list[0];
|
||||
@ -68,7 +62,7 @@ static void sinksenderSend(t_minuteBuffer *minuteBuffer) {
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(config.sinkPort);
|
||||
servaddr.sin_port = htons(SINK_PORT);
|
||||
memcpy(&servaddr.sin_addr.s_addr, sinkAddr, 4);
|
||||
|
||||
ssize_t res = sendto(sockfd, minuteBuffer->b, sizeof(minuteBuffer->b),
|
||||
@ -86,7 +80,7 @@ static void sinksenderSend(t_minuteBuffer *minuteBuffer) {
|
||||
ESP_LOGE(TAG, "unknown address type: %d", hptr->h_addrtype);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "sinkserver %s couldn't be resolved: %d", config.sinkServer, h_errno);
|
||||
ESP_LOGE(TAG, "sinkserver %s couldn't be resolved: %d", SINK_SERVER, h_errno);
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,8 +89,10 @@ static void sinksenderExecTask(void *arg) {
|
||||
if (minuteBufferQueue) {
|
||||
static t_minuteBuffer minuteBuffer;
|
||||
if (xQueueReceive(minuteBufferQueue, &minuteBuffer, portMAX_DELAY) == pdPASS) {
|
||||
gpioLedOff();
|
||||
ESP_LOGI(TAG, "Got a buffer from queue");
|
||||
sinksenderSend(&minuteBuffer);
|
||||
gpioLedOn();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -107,33 +103,38 @@ void sinksenderInit() {
|
||||
|
||||
ESP_LOGI(TAG, "About to load sink sender configuration");
|
||||
nvs_handle_t nvsHandle;
|
||||
if (ESP_OK != nvs_open("config", NVS_READWRITE, &nvsHandle)) {
|
||||
ESP_LOGE(TAG, "Unable to open nvs namespace config, use default values");
|
||||
strcpy(config.sinkServer, DEFAULT_SINKSERVER);
|
||||
config.sinkPort = DEFAULT_SINKPORT;
|
||||
strcpy(config.deviceId, DEFAULT_DEVICE_ID);
|
||||
strcpy(config.sharedSecret, DEFAULT_SHAREDSECRET);
|
||||
if (ESP_OK != nvs_open("sink", NVS_READWRITE, &nvsHandle)) {
|
||||
ESP_LOGE(TAG, "Unable to open nvs namespace sink, use default values");
|
||||
strcpy(deviceId, DEFAULT_DEVICE_ID);
|
||||
strcpy(sharedSecret, DEFAULT_SHAREDSECRET);
|
||||
} else {
|
||||
size_t s;
|
||||
esp_err_t err = nvs_get_blob(nvsHandle, "sinkSender", (void*)&config, &s);
|
||||
esp_err_t err;
|
||||
|
||||
err = nvs_get_str(nvsHandle, "deviceId", NULL, &s);
|
||||
ESP_LOGI(TAG, "1. err: %d, len: %d", err, s);
|
||||
err = nvs_get_str(nvsHandle, "deviceId", deviceId, &s);
|
||||
ESP_LOGI(TAG, "2. err: %d, len: %d", err, s);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "sinkSender configuration loaded");
|
||||
ESP_LOGI(TAG, "deviceId: %s", deviceId);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Get result is %d", err);
|
||||
strcpy(deviceId, DEFAULT_DEVICE_ID);
|
||||
ESP_LOGI(TAG, "deviceId not configured, use default");
|
||||
}
|
||||
if (config.magic != CONFIG_MAGIC) {
|
||||
ESP_LOGW(TAG, "No configuration found, write it");
|
||||
config.magic = CONFIG_MAGIC;
|
||||
strcpy(config.sinkServer, DEFAULT_SINKSERVER);
|
||||
config.sinkPort = DEFAULT_SINKPORT;
|
||||
strcpy(config.deviceId, DEFAULT_DEVICE_ID);
|
||||
strcpy(config.sharedSecret, DEFAULT_SHAREDSECRET);
|
||||
err = nvs_set_blob(nvsHandle, "sinkSender", (void*)&config, sizeof(config));
|
||||
ESP_LOGW(TAG, "Set result is %d", err);
|
||||
err = nvs_commit(nvsHandle);
|
||||
ESP_LOGW(TAG, "Commit result is %d", err);
|
||||
err = nvs_get_str(nvsHandle, "sharedSecret", NULL, &s);
|
||||
ESP_LOGI(TAG, "1. err: %d, len: %d", err, s);
|
||||
err = nvs_get_str(nvsHandle, "sharedSecret", sharedSecret, &s);
|
||||
ESP_LOGI(TAG, "2. err: %d, len: %d", err, s);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "sharedSecret: %s", sharedSecret);
|
||||
} else {
|
||||
strcpy(sharedSecret, DEFAULT_SHAREDSECRET);
|
||||
ESP_LOGI(TAG, "sharedSecret not configured, use default");
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "finally deviceId: %s", deviceId);
|
||||
ESP_LOGI(TAG, "finally sharedSecret: %s", sharedSecret);
|
||||
|
||||
xTaskCreate(sinksenderExecTask, "sinksender_exec_task", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "timesync.h"
|
||||
#include "gpio.h"
|
||||
#include <stdbool.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_sntp.h>
|
||||
@ -17,7 +18,10 @@ void timesyncCallback(struct timeval *tv) {
|
||||
|
||||
ESP_LOGI(TAG, "time is synchronized now: %u s %u ms", current_seconds, current_milliseconds);
|
||||
|
||||
synchronized = true;
|
||||
if (!synchronized) {
|
||||
gpioLedBlink(10);
|
||||
synchronized = true;
|
||||
}
|
||||
}
|
||||
|
||||
void timesyncInit() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user