Thomas Chef пре 3 година
родитељ
комит
b7883ac0aa
7 измењених фајлова са 142 додато и 6 уклоњено
  1. 3 1
      main/CMakeLists.txt
  2. 12 0
      main/Kconfig.projbuild
  3. 4 3
      main/config.h
  4. 1 1
      main/led_test_inout.c
  5. 11 0
      main/main.c
  6. 108 0
      main/mqtt.c
  7. 3 1
      main/wifi.h

+ 3 - 1
main/CMakeLists.txt

@@ -1,2 +1,4 @@
-idf_component_register(SRCS "wifi.c" "pcnt_functions.c" "main.c" "led_test_inout.c" "pcnt_functions.c" "wifi.c"
+idf_component_register(SRCS "mqtt.c" "wifi.c"
+"pcnt_functions.c" "main.c" "led_test_inout.c"
+"pcnt_functions.c" "wifi.c" "mqtt.c"
                     INCLUDE_DIRS "")

+ 12 - 0
main/Kconfig.projbuild

@@ -12,6 +12,18 @@ menu "HomeEnergyMeter Application Configuration"
         help
             WiFi password (WPA or WPA2) to use.
 
+    config ESP_MQTT_PASSWORD
+        string "MQTT Password"
+        default "mypassword"
+        help
+            MQTT password
+
+    config ESP_MQTT_UNAME
+        string "MQTT User"
+        default "myuser"
+        help
+            MQTT Username
+
     config ESP_PULSES_PER_KWH
         string "Pulses per kWh"
         default "10000"

+ 4 - 3
main/config.h

@@ -1,9 +1,10 @@
 
-#define PULSES_PER_KWH      10000
+#define PULSES_PER_KWH      1000
 
 // These defines configures which code to generate (to save download time during development)
-//#define CCFG_GEN_PULSE      // For testing purposes only (LED-Pulse 14Hz)
-//#define CCFG_PCNT           // pcnt-code that counts pulses
+#define CCFG_GEN_PULSE      // For testing purposes only (LED-Pulse 14Hz)
+#define CCFG_PCNT           // pcnt-code that counts pulses
+#define WIFI_ENABLED
 
 
 #define PCNT_INPUT_SIG_IO   4  // Pulse Input GPIO

+ 1 - 1
main/led_test_inout.c

@@ -19,7 +19,7 @@ void ledc_init(void)
     ledc_timer.speed_mode       = LEDC_LOW_SPEED_MODE;
     ledc_timer.timer_num        = LEDC_TIMER_0;
     ledc_timer.duty_resolution  = LEDC_TIMER_13_BIT;
-    ledc_timer.freq_hz          = 14;   // Should be 14 for some kind of max kWh
+    ledc_timer.freq_hz          = 14;   // Should be 2 for some kind of max kWh
     ledc_timer.clk_cfg          = LEDC_AUTO_CLK;
     ledc_timer_config(&ledc_timer);
 

+ 11 - 0
main/main.c

@@ -17,6 +17,9 @@ extern void ledc_init(void);
 extern void init_pcnt_unit();
 extern float getkWh();
 
+extern void mqtt_init();
+extern void sendMQTTMessage(const char * topic, const char * data);
+
 // Chip info:
 // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
 
@@ -38,13 +41,21 @@ void app_main(void)
 #ifdef WIFI_ENABLED
     initWifi();             // Init WIFI
 #endif
+    vTaskDelay(5000 / portTICK_PERIOD_MS);
+    mqtt_init();
 
     float kWh;
 
     while( true ) {
 
+        char dataStr[100];
+
         kWh = getkWh();
 
+        sprintf(dataStr,"%f",kWh);
+
+        sendMQTTMessage("/sensors/energy/electricalTotal", dataStr);
+
         printf("%f\n",kWh);
         vTaskDelay(1000 / portTICK_PERIOD_MS);
     }

+ 108 - 0
main/mqtt.c

@@ -0,0 +1,108 @@
+/* MQTT (over TCP) Example
+
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include "esp_wifi.h"
+#include "esp_system.h"
+#include "nvs_flash.h"
+#include "esp_event.h"
+#include "esp_netif.h"
+//#include "protocol_examples_common.h"
+
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/semphr.h"
+#include "freertos/queue.h"
+
+#include "lwip/sockets.h"
+#include "lwip/dns.h"
+#include "lwip/netdb.h"
+
+#include "esp_log.h"
+#include "mqtt_client.h"
+
+static const char *TAG = "MQTT";
+static esp_mqtt_client_handle_t client;
+static bool connected = false;
+
+
+static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
+{
+    esp_mqtt_client_handle_t client = event->client;
+    int msg_id;
+    // your_context_t *context = event->context;
+    switch (event->event_id) {
+        case MQTT_EVENT_CONNECTED:
+            connected = true;
+            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
+            break;
+        case MQTT_EVENT_DISCONNECTED:
+            connected = false;
+            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
+            break;
+
+        case MQTT_EVENT_PUBLISHED:
+            ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
+            break;
+        case MQTT_EVENT_DATA:
+            ESP_LOGI(TAG, "MQTT_EVENT_DATA");
+            printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
+            printf("DATA=%.*s\r\n", event->data_len, event->data);
+            break;
+        case MQTT_EVENT_ERROR:
+            ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
+            break;
+        default:
+            ESP_LOGI(TAG, "Other event id:%d", event->event_id);
+            break;
+    }
+    return ESP_OK;
+}
+
+static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
+    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
+    mqtt_event_handler_cb(event_data);
+}
+
+
+
+void mqtt_init(void)
+{
+    esp_mqtt_client_config_t mqtt_cfg = {
+        .uri = "mqtt://192.168.1.110:1883",
+        .password = CONFIG_ESP_MQTT_PASSWORD,
+        .username = CONFIG_ESP_MQTT_UNAME
+    };
+
+    client = esp_mqtt_client_init(&mqtt_cfg);
+    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
+    esp_mqtt_client_start(client);
+
+/*    while( 1 ) {
+        int msg_id;
+        msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
+        ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
+        vTaskDelay(1000 / portTICK_PERIOD_MS);
+    }*/
+}
+
+void sendMQTTMessage(const char * topic, const char * data) {
+    if( connected ) {
+        int msg_id;
+        msg_id = esp_mqtt_client_publish(client, topic, data, 0, 1, 0);
+        ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
+    }
+    else {
+        ESP_LOGI(TAG, "Not connected to MQTT");
+    }
+}
+

+ 3 - 1
main/wifi.h

@@ -1,13 +1,15 @@
 #ifndef __WIFI__
 #define __WIFI__
 
+#include "config.h"
+
 void initWifi(void);
 
 extern int latestRSSIValue;
 extern int commIsUpAndRunning; // Global info
 extern int latestWiFiChannel;
 
-//#define WIFI_ENABLED
+
 
 
 #endif