|
@@ -5,120 +5,44 @@
|
|
|
#include "esp_system.h"
|
|
|
#include "esp_spi_flash.h"
|
|
|
#include "freertos/queue.h"
|
|
|
-#include "driver/ledc.h"
|
|
|
-#include "driver/pcnt.h"
|
|
|
#include "esp_attr.h"
|
|
|
#include "esp_log.h"
|
|
|
#include "config.h"
|
|
|
#include "wifi.h"
|
|
|
-#include "uart.h"
|
|
|
#include "serial.h"
|
|
|
-#include "ssd1306.h"
|
|
|
-#include "ssd1306_driver.h"
|
|
|
+#include "displayAndSend.h"
|
|
|
+#include "pcnt_functions.h"
|
|
|
|
|
|
-// Externs
|
|
|
-extern void ledc_init(void);
|
|
|
-extern void init_pcnt_unit();
|
|
|
-extern double getkWh(uint32_t *bigCnt, int32_t *cnt);
|
|
|
-
|
|
|
-extern void mqtt_init();
|
|
|
-extern void sendMQTTMessage(const char * topic, const char * data);
|
|
|
-
|
|
|
-extern void sendHTTPMessage(const double inData, const uint32_t bigCnt, const int32_t cnt);
|
|
|
-
|
|
|
-void displayTask(void *pvParameters);
|
|
|
|
|
|
// Chip info:
|
|
|
// This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
|
|
|
|
|
|
-/** Electric meter:
|
|
|
- * 10000 impulses per kWh
|
|
|
- * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
|
|
|
- *
|
|
|
- * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
|
|
|
- * So we need some other way of counting higher numbers that will last for one h
|
|
|
- * Probably we need to use interrupts !
|
|
|
- */
|
|
|
|
|
|
void app_main(void)
|
|
|
{
|
|
|
ESP_LOGI("MAIN", "HomeEnergyMeter ESP32");
|
|
|
|
|
|
-
|
|
|
- init_pcnt_unit();
|
|
|
- ledc_init();
|
|
|
#ifdef WIFI_ENABLED
|
|
|
initWifi(); // Init WIFI
|
|
|
#endif
|
|
|
+
|
|
|
#ifdef MQTT_ENABLED
|
|
|
mqtt_init();
|
|
|
#endif
|
|
|
|
|
|
-#ifdef SERIAL_ENABLED
|
|
|
- initUart();
|
|
|
- xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL);
|
|
|
+#ifdef CCFG_PCNT
|
|
|
+ initPCNT();
|
|
|
#endif
|
|
|
|
|
|
- char dataStr[100];
|
|
|
- double kWh = 0.0;
|
|
|
- uint32_t bigCnt = 0;
|
|
|
- int32_t cnt = 0;
|
|
|
-
|
|
|
- xTaskCreate(displayTask, "OLED-Task", 1024*10, NULL, 2, NULL);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- while( true ) {
|
|
|
-
|
|
|
-
|
|
|
- kWh = getkWh(&bigCnt, &cnt);
|
|
|
-
|
|
|
- sprintf(dataStr,"%.5f",kWh);
|
|
|
-
|
|
|
- // @TODO This is changed for testing
|
|
|
-#ifdef MQTT_ENABLED
|
|
|
- sendMQTTMessage("/sensors/TEST/energy/electricalTotal", dataStr);
|
|
|
+#ifdef SERIAL_ENABLED
|
|
|
+ initSerial();
|
|
|
#endif
|
|
|
|
|
|
-#ifdef WIFI_ENABLED
|
|
|
- sendHTTPMessage(kWh, bigCnt, cnt);
|
|
|
-#endif
|
|
|
|
|
|
- ESP_LOGI("MAIN", "%.4f %u %d",kWh,bigCnt,cnt);
|
|
|
+ initDisplayAndSend();
|
|
|
|
|
|
- vTaskDelay(60000 / portTICK_PERIOD_MS);
|
|
|
- }
|
|
|
- esp_restart();
|
|
|
+ vTaskDelete(NULL);
|
|
|
}
|
|
|
|
|
|
|
|
|
-void displayTask(void *pvParameters) {
|
|
|
-
|
|
|
-#ifdef ENABLE_SSD1306
|
|
|
|
|
|
- i2c_master_init();
|
|
|
- oled_ssd1306_init();
|
|
|
-
|
|
|
- vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
- SSD1306_SetPosition(4,18);
|
|
|
- SSD1306_DrawVertLine(29,0,128);
|
|
|
- SSD1306_Puts("Home Energy Meter", &Font_7x10,SSD1306_COLOR_WHITE);
|
|
|
- SSD1306_SetPosition(4,31);
|
|
|
- SSD1306_Puts("v1.0", &Font_7x10,SSD1306_COLOR_WHITE);
|
|
|
- SSD1306_UpdateScreen();
|
|
|
- vTaskDelay(3000 / portTICK_PERIOD_MS);
|
|
|
-
|
|
|
- SSD1306_Clear();
|
|
|
- SSD1306_SetPosition(0,0);
|
|
|
- SSD1306_Puts("Temp A:", &Font_7x10,SSD1306_COLOR_WHITE);
|
|
|
- SSD1306_SetPosition(0,11);
|
|
|
- SSD1306_Puts("Temp B:", &Font_7x10,SSD1306_COLOR_WHITE);
|
|
|
- SSD1306_SetPosition(0,22);
|
|
|
- SSD1306_Puts("ADC Vpp:", &Font_7x10,SSD1306_COLOR_WHITE);
|
|
|
- SSD1306_SetPosition(0,33);
|
|
|
- SSD1306_Puts("Pulses:", &Font_7x10,SSD1306_COLOR_WHITE);
|
|
|
- SSD1306_UpdateScreen();
|
|
|
-
|
|
|
-#endif
|
|
|
- vTaskDelete(NULL);
|
|
|
-}
|