Browse Source

factor common code

main
Nicolas Massé 4 years ago
parent
commit
c651bd968f
  1. 2
      main/CMakeLists.txt
  2. 2
      main/common.c
  3. 16
      main/common.h
  4. 9
      main/main.c
  5. 36
      main/mqtt.c
  6. 1
      main/mqtt.h
  7. 34
      main/sntp.c
  8. 1
      main/sntp.h
  9. 40
      main/wifi.c
  10. 1
      main/wifi.h

2
main/CMakeLists.txt

@ -1,2 +1,2 @@
idf_component_register(SRCS "main.c" "tic.c" "wifi.c" "mqtt.c" "sntp.c" "libteleinfo.cpp" "libteleinfo/src/LibTeleinfo.cpp"
idf_component_register(SRCS "main.c" "tic.c" "wifi.c" "mqtt.c" "sntp.c" "common.c" "libteleinfo.cpp" "libteleinfo/src/LibTeleinfo.cpp"
INCLUDE_DIRS ".")

2
main/common.c

@ -0,0 +1,2 @@
#include "common.h"
volatile EventGroupHandle_t services_event_group;

16
main/common.h

@ -0,0 +1,16 @@
#ifndef __COMMON_H__
#define __COMMON_H__
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#define WIFI_CONNECTED_BIT BIT0
#define MQTT_CONNECTED_BIT BIT1
#define TIME_SYNC_BIT BIT2
extern volatile EventGroupHandle_t services_event_group;
#define WAIT_FOR(flags) while ((xEventGroupWaitBits(services_event_group, flags, pdFALSE, pdTRUE, portMAX_DELAY) & (flags)) != (flags)) {}
#endif

9
main/main.c

@ -13,6 +13,7 @@
#include "wifi.h"
#include "mqtt.h"
#include "sntp.h"
#include "common.h"
// Embedded via component.mk
extern const uint8_t cacert_pem_start[] asm("_binary_cacert_pem_start");
@ -26,11 +27,13 @@ void app_main(void) {
ESP_ERROR_CHECK(esp_tls_init_global_ca_store());
ESP_ERROR_CHECK(esp_tls_set_global_ca_store((const unsigned char *) cacert_pem_start, cacert_pem_end - cacert_pem_start));
// Create an event group to keep track of service readiness
services_event_group = xEventGroupCreate();
wifi_init_sta();
wifi_wait_for_online();
WAIT_FOR(WIFI_CONNECTED_BIT);
mqtt_init();
sntp_start();
mqtt_wait_for_readiness();
sntp_wait_for_readiness();
WAIT_FOR(MQTT_CONNECTED_BIT|TIME_SYNC_BIT);
tic_uart_init();
}

36
main/mqtt.c

@ -4,7 +4,6 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
@ -16,17 +15,11 @@
#include "esp_tls.h"
#include "esp_ota_ops.h"
#include <sys/param.h>
#include "common.h"
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_mqtt_event_group;
static esp_mqtt_client_config_t mqtt_cfg;
static esp_mqtt_client_handle_t client;
/* The event group allows multiple bits for each event, but we only care about one event:
* - we are connected to the MQTT server
*/
#define MQTT_CONNECTED_BIT BIT0
static const char *MQTT_LOGGER = "mqtt";
void mqtt_publish_data(char* key, char* value) {
@ -57,11 +50,11 @@ esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
switch (event->event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(MQTT_LOGGER, "MQTT_EVENT_CONNECTED");
xEventGroupSetBits(s_mqtt_event_group, MQTT_CONNECTED_BIT);
xEventGroupSetBits(services_event_group, MQTT_CONNECTED_BIT);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(MQTT_LOGGER, "MQTT_EVENT_DISCONNECTED");
xEventGroupClearBits(s_mqtt_event_group, MQTT_CONNECTED_BIT);
xEventGroupClearBits(services_event_group, MQTT_CONNECTED_BIT);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(MQTT_LOGGER, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
@ -110,8 +103,6 @@ char* get_nvs_string(nvs_handle_t nvs, char* key) {
}
void mqtt_init(void) {
s_mqtt_event_group = xEventGroupCreate();
nvs_handle_t nvs;
esp_err_t err = nvs_open("mqtt", NVS_READONLY, &nvs);
if (err != ESP_OK) {
@ -131,24 +122,3 @@ void mqtt_init(void) {
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client);
}
void mqtt_wait_for_readiness(void) {
/* Waiting until the connection is established (MQTT_CONNECTED_BIT).
* The bits are set by mqtt_event_handler_cb() (see above)
*/
EventBits_t bits = xEventGroupWaitBits(s_mqtt_event_group,
MQTT_CONNECTED_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & MQTT_CONNECTED_BIT) {
ESP_LOGI(MQTT_LOGGER, "Connected to the MQTT server!");
} else {
ESP_LOGE(MQTT_LOGGER, "UNEXPECTED EVENT");
}
//vEventGroupDelete(s_mqtt_event_group);
}

1
main/mqtt.h

@ -2,7 +2,6 @@
#define __MQTT_H__
void mqtt_init();
void mqtt_wait_for_readiness(void);
void mqtt_publish_data(char* key, char* value);
void mqtt_publish_alert(uint8_t value);

34
main/sntp.c

@ -3,21 +3,13 @@
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_sntp.h"
#include "sntp.h"
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_sntp_event_group;
/* The event group allows multiple bits for each event, but we only care about one event:
* - time is synchronized
*/
#define TIME_SYNC_BIT BIT0
#include "common.h"
static const char *SNTP_LOGGER = "sntp";
@ -32,12 +24,11 @@ void sntp_callback(struct timeval *tv) {
ESP_LOGI(SNTP_LOGGER, "Time synchronized: %s", strftime_buf);
}
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
xEventGroupSetBits(s_sntp_event_group, TIME_SYNC_BIT);
xEventGroupSetBits(services_event_group, TIME_SYNC_BIT);
}
}
void sntp_start() {
s_sntp_event_group = xEventGroupCreate();
ESP_LOGI(SNTP_LOGGER, "Initializing SNTP...");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
@ -45,24 +36,3 @@ void sntp_start() {
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
sntp_init();
}
void sntp_wait_for_readiness(void) {
/* Waiting until the connection is established (MQTT_CONNECTED_BIT).
* The bits are set by mqtt_event_handler_cb() (see above)
*/
EventBits_t bits = xEventGroupWaitBits(s_sntp_event_group,
TIME_SYNC_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & TIME_SYNC_BIT) {
ESP_LOGI(SNTP_LOGGER, "Time is synchronized!");
} else {
ESP_LOGE(SNTP_LOGGER, "UNEXPECTED EVENT");
}
//vEventGroupDelete(s_sntp_event_group);
}

1
main/sntp.h

@ -2,6 +2,5 @@
#define __SNTP_H__
void sntp_start();
void sntp_wait_for_readiness(void);
#endif

40
main/wifi.c

@ -1,7 +1,6 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
@ -12,14 +11,7 @@
#include "lwip/sys.h"
#include "wifi.h"
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about one event:
* - we are connected to the AP with an IP
*/
#define WIFI_CONNECTED_BIT BIT0
#include "common.h"
static const char *WIFI_LOGGER = "wifi";
@ -29,20 +21,18 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_LOGI(WIFI_LOGGER,"Connection to the AP failed!");
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
xEventGroupClearBits(services_event_group, WIFI_CONNECTED_BIT);
ESP_LOGI(WIFI_LOGGER, "Retrying to connect to the AP...");
esp_wifi_connect();
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(WIFI_LOGGER, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
xEventGroupSetBits(services_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void) {
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
@ -69,27 +59,3 @@ void wifi_init_sta(void) {
ESP_LOGI(WIFI_LOGGER, "wifi_init_sta finished.");
}
void wifi_wait_for_online(void) {
/* Waiting until the connection is established (WIFI_CONNECTED_BIT).
* The bits are set by wifi_event_handler() (see above)
*/
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(WIFI_LOGGER, "Connected to Wifi!");
} else {
ESP_LOGE(WIFI_LOGGER, "UNEXPECTED EVENT");
}
/* The event will not be processed after unregister */
//ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
//ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
//vEventGroupDelete(s_wifi_event_group);
}

1
main/wifi.h

@ -2,6 +2,5 @@
#define __WIFI_H__
void wifi_init_sta(void);
void wifi_wait_for_online(void);
#endif

Loading…
Cancel
Save