Эх сурвалжийг харах

Rolling 2020-10-04

Initial MQTT
jomjol 5 жил өмнө
parent
commit
70a88088f2

+ 16 - 2
code/lib/jomjol_flowcontroll/ClassFlowAnalog.cpp

@@ -12,6 +12,8 @@
 
 #include "ClassLogFile.h"
 
+bool debugdetailanalog = false;
+
 ClassFlowAnalog::ClassFlowAnalog()
 {
     isLogImage = false;
@@ -147,6 +149,8 @@ bool ClassFlowAnalog::doFlow(string time)
         return false;
     };
 
+    if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doFlow nach Alignment");
+
     doNeuralNetwork(time);
 
     return true;
@@ -167,7 +171,7 @@ bool ClassFlowAnalog::doAlignAndCut(string time)
     CAlignAndCutImage *caic = new CAlignAndCutImage(input);
 
     if (!caic->ImageOkay()){
-        LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut not okay!");
+        if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut not okay!");
         delete caic;
         return false;
     }
@@ -175,7 +179,7 @@ bool ClassFlowAnalog::doAlignAndCut(string time)
     if (input_roi.length() > 0){
         img_roi = new CImageBasis(input_roi);
         if (!img_roi->ImageOkay()){
-            LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut ImageRoi not okay!");
+            if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut ImageRoi not okay!");
             delete caic;
             delete img_roi;
             return false;
@@ -190,6 +194,13 @@ bool ClassFlowAnalog::doAlignAndCut(string time)
         caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
 
         rs = new CResizeImage(output);
+        if (!rs->ImageOkay()){
+            if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut CResizeImage(output);!");
+            delete caic;
+            delete rs;
+            return false;
+        }
+
         rs->Resize(modelxsize, modelysize);
         ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
         ioresize = FormatFileName(ioresize);
@@ -248,8 +259,11 @@ bool ClassFlowAnalog::doNeuralNetwork(string time)
         f1 = 0; f2 = 0;
 
 #ifndef OHNETFLITE
+//        LogFile.WriteToFile("ClassFlowAnalog::doNeuralNetwork vor CNN tflite->LoadInputImage(ioresize)");
         tflite->LoadInputImage(ioresize);
         tflite->Invoke();
+        if (debugdetailanalog) LogFile.WriteToFile("Nach Invoke");
+
 
         f1 = tflite->GetOutputValue(0);
         f2 = tflite->GetOutputValue(1);

+ 5 - 0
code/lib/jomjol_flowcontroll/ClassFlowControll.cpp

@@ -20,6 +20,9 @@ std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _
     if (_stepname.compare("[Analog]") == 0){
         _classname = "ClassFlowAnalog";
     }
+    if (_stepname.compare("[MQTT]") == 0){
+        _classname = "ClassFlowMQTT";
+    }
 //    std::string zw = "Classname: " + _classname + "\n";
 //    printf(zw.c_str());
 
@@ -80,6 +83,8 @@ ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
         cfc = new ClassFlowAnalog(&FlowControll);
     if (toUpper(_type).compare("[DIGITS]") == 0)
         cfc = new ClassFlowDigit(&FlowControll);
+    if (toUpper(_type).compare("[MQTT]") == 0)
+        cfc = new ClassFlowMQTT(&FlowControll);
     if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
     {
         cfc = new ClassFlowPostProcessing(&FlowControll); 

+ 1 - 0
code/lib/jomjol_flowcontroll/ClassFlowControll.h

@@ -8,6 +8,7 @@
 #include "ClassFlowDigit.h"
 #include "ClassFlowAnalog.h"
 #include "ClassFlowPostProcessing.h"
+#include "ClassFlowMQTT.h"
 
 
 class ClassFlowControll :

+ 110 - 0
code/lib/jomjol_flowcontroll/ClassFlowMQTT.cpp

@@ -0,0 +1,110 @@
+#include "ClassFlowMQTT.h"
+#include "Helper.h"
+
+#include "interface_mqtt.h"
+#include "ClassFlowPostProcessing.h"
+
+#include <time.h>
+
+static const char* TAG2 = "example";
+
+ClassFlowMQTT::ClassFlowMQTT()
+{
+    uri = "";
+    topic = "";
+    clientname = "watermeter";
+    OldValue = "";
+    flowpostprocessing = NULL;    
+}
+
+ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
+{
+    uri = "";
+    topic = "";
+    clientname = "watermeter";
+    OldValue = "";
+    flowpostprocessing = NULL;
+
+    ListFlowControll = lfc;
+
+    for (int i = 0; i < ListFlowControll->size(); ++i)
+    {
+        if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
+        {
+            flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
+        }
+    }
+
+}
+
+bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
+{
+    std::vector<string> zerlegt;
+
+    aktparamgraph = trim(aktparamgraph);
+
+    if (aktparamgraph.size() == 0)
+        if (!this->GetNextParagraph(pfile, aktparamgraph))
+            return false;
+
+    if (toUpper(aktparamgraph).compare("[MQTT]") != 0)       // Paragraph passt nich zu MakeImage
+        return false;
+
+    while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
+    {
+        zerlegt = this->ZerlegeZeile(aktparamgraph);
+        if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
+        {
+            this->uri = zerlegt[1];
+        }
+        if ((toUpper(zerlegt[0]) == "TOPIC") && (zerlegt.size() > 1))
+        {
+            this->topic = zerlegt[1];
+        }
+        if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
+        {
+            this->clientname = zerlegt[1];
+        }
+
+    }
+
+    if ((uri.length() > 0) && (topic.length() > 0)) 
+    {
+        MQTTInit(uri, clientname);
+    }
+   
+    return true;
+}
+
+
+bool ClassFlowMQTT::doFlow(string zwtime)
+{
+    std::string result;
+    string zw = "";
+    
+    if (flowpostprocessing)
+    {
+        result =  flowpostprocessing->getReadoutParam(false, true);
+    }
+    else
+    {
+        for (int i = 0; i < ListFlowControll->size(); ++i)
+        {
+            zw = (*ListFlowControll)[i]->getReadout();
+            if (zw.length() > 0)
+            {
+                if (result.length() == 0)
+                    result = zw;
+                else
+                    result = result + "\t" + zw;
+            }
+        }
+    }
+
+    MQTTPublish(topic, result);
+
+    OldValue = result;
+
+
+    return true;
+}

+ 24 - 0
code/lib/jomjol_flowcontroll/ClassFlowMQTT.h

@@ -0,0 +1,24 @@
+#pragma once
+#include "ClassFlow.h"
+
+#include "ClassFlowPostProcessing.h"
+
+#include <string>
+
+class ClassFlowMQTT :
+    public ClassFlow
+{
+protected:
+    std::string uri, topic, clientname;
+    std::string OldValue;
+	ClassFlowPostProcessing* flowpostprocessing;    
+
+
+public:
+    ClassFlowMQTT();
+    ClassFlowMQTT(std::vector<ClassFlow*>* lfc);
+    bool ReadParameter(FILE* pfile, string& aktparamgraph);
+    bool doFlow(string time);
+    string name(){return "ClassFlowMQTT";};
+};
+

+ 3 - 3
code/lib/jomjol_image_proc/CFindTemplate.cpp

@@ -391,11 +391,11 @@ CImageBasis::CImageBasis(std::string _image)
     channels = 3;
     externalImage = false;
     filename = _image;
-    long freebefore = esp_get_free_heap_size();
+//    long freebefore = esp_get_free_heap_size();
 
     rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
-    if (rgb_image == NULL)
-        LogFile.WriteToFile("Image Load failed:" + _image + " FreeHeapSize before: " + to_string(freebefore) + " after: " + to_string(esp_get_free_heap_size()));
+//    if (rgb_image == NULL)
+//        LogFile.WriteToFile("Image Load failed:" + _image + " FreeHeapSize before: " + to_string(freebefore) + " after: " + to_string(esp_get_free_heap_size()));
     //    printf("CImageBasis after load\n");
     //    printf("w %d, h %d, b %d, c %d", this->width, this->height, this->bpp, this->channels);
 }

+ 76 - 0
code/lib/jomjol_mqtt/interface_mqtt.cpp

@@ -0,0 +1,76 @@
+#include "interface_mqtt.h"
+
+
+#include "esp_log.h"
+#include "mqtt_client.h"
+#include "ClassLogFile.h"
+
+static const char *TAG = "interface_mqtt";
+
+bool debugdetail = true;
+
+// #define CONFIG_BROKER_URL "mqtt://192.168.178.43:1883"
+
+esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
+
+bool mqtt_connected = false;
+esp_mqtt_client_handle_t client = NULL;
+
+void MQTTPublish(std::string _key, std::string _content){
+    if (client && mqtt_connected) {
+        int msg_id;
+        std::string zw;
+        msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, 0);
+        zw = "sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content;
+        if (debugdetail) LogFile.WriteToFile(zw);
+        ESP_LOGI(TAG, "sent publish successful in MQTTPublish, msg_id=%d, %s, %s", msg_id, _key.c_str(), _content.c_str());
+    }
+    else {
+        ESP_LOGI(TAG, "Problem with Publish, client=%d, mqtt_connected %d", (int) client, (int) mqtt_connected);
+    }
+}
+
+
+static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
+{
+    switch (event->event_id) {
+        case MQTT_EVENT_CONNECTED:
+            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
+            mqtt_connected = true;
+            break;
+        case MQTT_EVENT_DISCONNECTED:
+            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((esp_mqtt_event_handle_t) event_data);
+}
+
+void MQTTInit(std::string _mqttURI, std::string _clientid){
+    esp_mqtt_client_config_t mqtt_cfg = {
+        .uri = _mqttURI.c_str(),
+        .client_id = _clientid.c_str(),
+    };
+
+    client = esp_mqtt_client_init(&mqtt_cfg);
+    esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client);
+    esp_mqtt_client_start(client);
+}

+ 4 - 0
code/lib/jomjol_mqtt/interface_mqtt.h

@@ -0,0 +1,4 @@
+#include <string>
+
+void MQTTInit(std::string _mqttURI, std::string _clientid);
+void MQTTPublish(std::string _key, std::string _content);

+ 11 - 0
code/lib/jomjol_tfliteclass/CTfLiteClass.cpp

@@ -2,8 +2,12 @@
 
 #include "bitmap_image.hpp"
 
+#include "ClassLogFile.h"
+
 #include <sys/stat.h>
 
+bool debugdetailtflite = false;
+
 float CTfLiteClass::GetOutputValue(int nr)
 {
     TfLiteTensor* output2 = this->interpreter->output(0);
@@ -109,7 +113,11 @@ void CTfLiteClass::Invoke()
 
 bool CTfLiteClass::LoadInputImage(std::string _fn)
 {
+    std::string zw = "ClassFlowAnalog::doNeuralNetwork nach Load Image: " + _fn;
+//    LogFile.WriteToFile(zw);
     bitmap_image image(_fn);
+    if (debugdetailtflite) LogFile.WriteToFile(zw);
+
     unsigned int w = image.width();
     unsigned int h = image.height();
     unsigned char red, green, blue;
@@ -135,6 +143,9 @@ bool CTfLiteClass::LoadInputImage(std::string _fn)
 //                printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
 
             }
+    
+    if (debugdetailtflite) LogFile.WriteToFile("Nach dem Laden in input");
+
     return true;
 }
 

+ 1 - 1
code/platformio.ini

@@ -23,7 +23,7 @@ board_build.embed_files  =
 ;board_build.partitions = partitions_singleapp.csv
 board_build.partitions = partition.csv
 
-lib_deps = jomjol_helper, connect_wlan, conversions, driver, sensors, jomjol_image_proc, jomjol_controlcamera, jomjol_flowcontroll, jomjol_tfliteclass, tfmicro, jomjol_fileserver_ota, jomjol_time_sntp, jomjol_logfile
+lib_deps = jomjol_helper, connect_wlan, conversions, driver, sensors, jomjol_image_proc, jomjol_controlcamera, jomjol_flowcontroll, jomjol_tfliteclass, tfmicro, jomjol_fileserver_ota, jomjol_time_sntp, jomjol_logfile, jomjol_mqtt
 monitor_speed = 115200
 
 debug_tool = esp-prog

+ 8 - 45
code/src/main.cpp

@@ -5,63 +5,26 @@
 
 #include "driver/gpio.h"
 #include "sdkconfig.h"
-//#include "version.h"
 
-#include "ClassLogFile.h"
-
-
-//#include "esp_wifi.h"
-//#include "protocol_examples_common.h"
-
-#include "connect_wlan.h"
-
-#include <esp_http_server.h>
-
-#include "lwip/err.h"
-#include "lwip/sockets.h"
-#include "lwip/sys.h"
-#include "lwip/netdb.h"
-#include "lwip/dns.h"
-
-
-#include <sys/unistd.h>
-#include <sys/stat.h>
-#include "esp_log.h"
-#include "esp_system.h"
-#include "esp_event.h"
-#include "esp_event_loop.h"
+// SD-Card ////////////////////
 #include "nvs_flash.h"
-#include "esp_err.h"
 #include "esp_vfs_fat.h"
+#include "sdmmc_cmd.h"
 #include "driver/sdmmc_host.h"
 #include "driver/sdmmc_defs.h"
-#include "sdmmc_cmd.h"
+///////////////////////////////
+
+#include "ClassLogFile.h"
+
+#include "connect_wlan.h"
 
-#include "server_main.h"
-#include "server_camera.h"
 #include "server_tflite.h"
 #include "server_file.h"
 #include "server_ota.h"
 #include "time_sntp.h"
 #include "ClassControllCamera.h"
-
-
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "freertos/event_groups.h"
-
-#include "freertos/FreeRTOS.h"
-
-// SD-Card
-#include "nvs_flash.h"
-#include "esp_vfs_fat.h"
-#include "sdmmc_cmd.h"
-
 #include "server_main.h"
 #include "server_camera.h"
-#include "ClassControllCamera.h"
-#include "connect_wlan.h"
-#include "time_sntp.h"
 
 static const char *TAGMAIN = "connect_wlan_main";
 
@@ -130,7 +93,7 @@ extern "C" void app_main()
     vTaskDelay( xDelay );   
 //    LogFile.WriteToFile("Startsequence 07");  
     setup_time();
-    LogFile.WriteToFile("======================== Main Started ================================");
+    LogFile.WriteToFile("============================== Main Started =======================================");
     LogFile.SwitchOnOff(false);
 
     std::string zw = gettimestring("%Y%m%d-%H%M%S");

+ 2 - 2
code/src/version.cpp

@@ -1,4 +1,4 @@
-const char* GIT_REV="6e26fa6";
+const char* GIT_REV="f8e8c75";
 const char* GIT_TAG="";
 const char* GIT_BRANCH="rolling";
-const char* BUILD_TIME="2020-09-29 18:38";
+const char* BUILD_TIME="2020-10-04 08:06";

+ 2 - 2
code/version.cpp

@@ -1,4 +1,4 @@
-const char* GIT_REV="6e26fa6";
+const char* GIT_REV="f8e8c75";
 const char* GIT_TAG="";
 const char* GIT_BRANCH="rolling";
-const char* BUILD_TIME="2020-09-29 18:38";
+const char* BUILD_TIME="2020-10-04 08:06";

BIN
firmware/bootloader.bin


BIN
firmware/firmware.bin


+ 4 - 0
sd-card/config/config.ini

@@ -38,6 +38,10 @@ MaxRateValue = 0.1
 ErrorMessage = True
 CheckDigitIncreaseConsistency = False
 
+;[MQTT]
+;Uri = mqtt://IP-MQTT-SERVER:1883
+;Topic = /watermeter/readout
+;ClientID = watermeter
 
 [AutoTimer]
 AutoStart= True