Skip to content

Commit 2614481

Browse files
authored
Merge pull request #35 from jomjol/rolling
Rolling
2 parents 1633b74 + 21d07be commit 2614481

27 files changed

+410
-104
lines changed

.DS_Store

10 KB
Binary file not shown.

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,21 @@ A 3d-printable housing can be found here: https://www.thingiverse.com/thing:4571
2727

2828

2929

30-
##### Rolling - (2020-09-28)
30+
##### Rolling - (2020-10-13)
31+
32+
* Implementation of user and password for MQTT Authentication (see `config.ini`)
33+
34+
2020-10-04
35+
36+
* First simple MQTT Client - to be configured in `config.ini` (see example)
37+
38+
39+
40+
2020-09-29
41+
42+
* Implementation of HTML-Version (thanks to phlupp)
43+
44+
* ESP32 Temperature is now written correctly to log file
3145

3246
* based on v2.2.1 (2020-09-28)
3347

code/.DS_Store

6 KB
Binary file not shown.

code/lib/jomjol_flowcontroll/ClassFlowAnalog.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "ClassLogFile.h"
1414

15+
bool debugdetailanalog = false;
16+
1517
ClassFlowAnalog::ClassFlowAnalog()
1618
{
1719
isLogImage = false;
@@ -147,6 +149,8 @@ bool ClassFlowAnalog::doFlow(string time)
147149
return false;
148150
};
149151

152+
if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doFlow nach Alignment");
153+
150154
doNeuralNetwork(time);
151155

152156
return true;
@@ -167,15 +171,15 @@ bool ClassFlowAnalog::doAlignAndCut(string time)
167171
CAlignAndCutImage *caic = new CAlignAndCutImage(input);
168172

169173
if (!caic->ImageOkay()){
170-
LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut not okay!");
174+
if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut not okay!");
171175
delete caic;
172176
return false;
173177
}
174178

175179
if (input_roi.length() > 0){
176180
img_roi = new CImageBasis(input_roi);
177181
if (!img_roi->ImageOkay()){
178-
LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut ImageRoi not okay!");
182+
if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut ImageRoi not okay!");
179183
delete caic;
180184
delete img_roi;
181185
return false;
@@ -190,6 +194,13 @@ bool ClassFlowAnalog::doAlignAndCut(string time)
190194
caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
191195

192196
rs = new CResizeImage(output);
197+
if (!rs->ImageOkay()){
198+
if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut CResizeImage(output);!");
199+
delete caic;
200+
delete rs;
201+
return false;
202+
}
203+
193204
rs->Resize(modelxsize, modelysize);
194205
ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
195206
ioresize = FormatFileName(ioresize);
@@ -248,8 +259,11 @@ bool ClassFlowAnalog::doNeuralNetwork(string time)
248259
f1 = 0; f2 = 0;
249260

250261
#ifndef OHNETFLITE
262+
// LogFile.WriteToFile("ClassFlowAnalog::doNeuralNetwork vor CNN tflite->LoadInputImage(ioresize)");
251263
tflite->LoadInputImage(ioresize);
252264
tflite->Invoke();
265+
if (debugdetailanalog) LogFile.WriteToFile("Nach Invoke");
266+
253267

254268
f1 = tflite->GetOutputValue(0);
255269
f2 = tflite->GetOutputValue(1);

code/lib/jomjol_flowcontroll/ClassFlowControll.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _
2020
if (_stepname.compare("[Analog]") == 0){
2121
_classname = "ClassFlowAnalog";
2222
}
23+
if (_stepname.compare("[MQTT]") == 0){
24+
_classname = "ClassFlowMQTT";
25+
}
2326
// std::string zw = "Classname: " + _classname + "\n";
2427
// printf(zw.c_str());
2528

@@ -80,6 +83,8 @@ ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
8083
cfc = new ClassFlowAnalog(&FlowControll);
8184
if (toUpper(_type).compare("[DIGITS]") == 0)
8285
cfc = new ClassFlowDigit(&FlowControll);
86+
if (toUpper(_type).compare("[MQTT]") == 0)
87+
cfc = new ClassFlowMQTT(&FlowControll);
8388
if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
8489
{
8590
cfc = new ClassFlowPostProcessing(&FlowControll);

code/lib/jomjol_flowcontroll/ClassFlowControll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ClassFlowDigit.h"
99
#include "ClassFlowAnalog.h"
1010
#include "ClassFlowPostProcessing.h"
11+
#include "ClassFlowMQTT.h"
1112

1213

1314
class ClassFlowControll :
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "ClassFlowMQTT.h"
2+
#include "Helper.h"
3+
4+
#include "interface_mqtt.h"
5+
#include "ClassFlowPostProcessing.h"
6+
7+
#include <time.h>
8+
9+
static const char* TAG2 = "example";
10+
11+
ClassFlowMQTT::ClassFlowMQTT()
12+
{
13+
uri = "";
14+
topic = "";
15+
clientname = "watermeter";
16+
OldValue = "";
17+
flowpostprocessing = NULL;
18+
user = "";
19+
password = "";
20+
}
21+
22+
ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
23+
{
24+
uri = "";
25+
topic = "";
26+
clientname = "watermeter";
27+
OldValue = "";
28+
flowpostprocessing = NULL;
29+
user = "";
30+
password = "";
31+
32+
ListFlowControll = lfc;
33+
34+
for (int i = 0; i < ListFlowControll->size(); ++i)
35+
{
36+
if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
37+
{
38+
flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
39+
}
40+
}
41+
42+
}
43+
44+
bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
45+
{
46+
std::vector<string> zerlegt;
47+
48+
aktparamgraph = trim(aktparamgraph);
49+
50+
if (aktparamgraph.size() == 0)
51+
if (!this->GetNextParagraph(pfile, aktparamgraph))
52+
return false;
53+
54+
if (toUpper(aktparamgraph).compare("[MQTT]") != 0) // Paragraph passt nich zu MakeImage
55+
return false;
56+
57+
while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
58+
{
59+
zerlegt = this->ZerlegeZeile(aktparamgraph);
60+
if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
61+
{
62+
this->user = zerlegt[1];
63+
}
64+
if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
65+
{
66+
this->password = zerlegt[1];
67+
}
68+
if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
69+
{
70+
this->uri = zerlegt[1];
71+
}
72+
if ((toUpper(zerlegt[0]) == "TOPIC") && (zerlegt.size() > 1))
73+
{
74+
this->topic = zerlegt[1];
75+
}
76+
if ((toUpper(zerlegt[0]) == "CLIENTID") && (zerlegt.size() > 1))
77+
{
78+
this->clientname = zerlegt[1];
79+
}
80+
81+
}
82+
83+
if ((uri.length() > 0) && (topic.length() > 0))
84+
{
85+
MQTTInit(uri, clientname, user, password);
86+
}
87+
88+
return true;
89+
}
90+
91+
92+
bool ClassFlowMQTT::doFlow(string zwtime)
93+
{
94+
std::string result;
95+
string zw = "";
96+
97+
if (flowpostprocessing)
98+
{
99+
result = flowpostprocessing->getReadoutParam(false, true);
100+
}
101+
else
102+
{
103+
for (int i = 0; i < ListFlowControll->size(); ++i)
104+
{
105+
zw = (*ListFlowControll)[i]->getReadout();
106+
if (zw.length() > 0)
107+
{
108+
if (result.length() == 0)
109+
result = zw;
110+
else
111+
result = result + "\t" + zw;
112+
}
113+
}
114+
}
115+
116+
MQTTPublish(topic, result);
117+
118+
OldValue = result;
119+
120+
121+
return true;
122+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include "ClassFlow.h"
3+
4+
#include "ClassFlowPostProcessing.h"
5+
6+
#include <string>
7+
8+
class ClassFlowMQTT :
9+
public ClassFlow
10+
{
11+
protected:
12+
std::string uri, topic, clientname;
13+
std::string OldValue;
14+
ClassFlowPostProcessing* flowpostprocessing;
15+
std::string user, password;
16+
17+
18+
public:
19+
ClassFlowMQTT();
20+
ClassFlowMQTT(std::vector<ClassFlow*>* lfc);
21+
bool ReadParameter(FILE* pfile, string& aktparamgraph);
22+
bool doFlow(string time);
23+
string name(){return "ClassFlowMQTT";};
24+
};
25+

code/lib/jomjol_image_proc/CFindTemplate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,11 @@ CImageBasis::CImageBasis(std::string _image)
391391
channels = 3;
392392
externalImage = false;
393393
filename = _image;
394-
long freebefore = esp_get_free_heap_size();
394+
// long freebefore = esp_get_free_heap_size();
395395

396396
rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
397-
if (rgb_image == NULL)
398-
LogFile.WriteToFile("Image Load failed:" + _image + " FreeHeapSize before: " + to_string(freebefore) + " after: " + to_string(esp_get_free_heap_size()));
397+
// if (rgb_image == NULL)
398+
// LogFile.WriteToFile("Image Load failed:" + _image + " FreeHeapSize before: " + to_string(freebefore) + " after: " + to_string(esp_get_free_heap_size()));
399399
// printf("CImageBasis after load\n");
400400
// printf("w %d, h %d, b %d, c %d", this->width, this->height, this->bpp, this->channels);
401401
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "interface_mqtt.h"
2+
3+
4+
#include "esp_log.h"
5+
#include "mqtt_client.h"
6+
#include "ClassLogFile.h"
7+
8+
static const char *TAG = "interface_mqtt";
9+
10+
bool debugdetail = true;
11+
12+
// #define CONFIG_BROKER_URL "mqtt://192.168.178.43:1883"
13+
14+
esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
15+
16+
bool mqtt_connected = false;
17+
esp_mqtt_client_handle_t client = NULL;
18+
19+
void MQTTPublish(std::string _key, std::string _content){
20+
if (client && mqtt_connected) {
21+
int msg_id;
22+
std::string zw;
23+
msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, 0);
24+
zw = "sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content;
25+
if (debugdetail) LogFile.WriteToFile(zw);
26+
ESP_LOGI(TAG, "sent publish successful in MQTTPublish, msg_id=%d, %s, %s", msg_id, _key.c_str(), _content.c_str());
27+
}
28+
else {
29+
ESP_LOGI(TAG, "Problem with Publish, client=%d, mqtt_connected %d", (int) client, (int) mqtt_connected);
30+
}
31+
}
32+
33+
34+
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
35+
{
36+
switch (event->event_id) {
37+
case MQTT_EVENT_CONNECTED:
38+
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
39+
mqtt_connected = true;
40+
break;
41+
case MQTT_EVENT_DISCONNECTED:
42+
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
43+
break;
44+
case MQTT_EVENT_PUBLISHED:
45+
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
46+
break;
47+
case MQTT_EVENT_DATA:
48+
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
49+
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
50+
printf("DATA=%.*s\r\n", event->data_len, event->data);
51+
break;
52+
case MQTT_EVENT_ERROR:
53+
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
54+
break;
55+
default:
56+
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
57+
break;
58+
}
59+
return ESP_OK;
60+
}
61+
62+
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
63+
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
64+
mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data);
65+
}
66+
67+
void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password){
68+
esp_mqtt_client_config_t mqtt_cfg = {
69+
.uri = _mqttURI.c_str(),
70+
.client_id = _clientid.c_str(),
71+
};
72+
73+
if (_user.length() && _password.length()){
74+
mqtt_cfg.username = _user.c_str();
75+
mqtt_cfg.password = _password.c_str();
76+
printf("Connect to MQTT: %s, %s", mqtt_cfg.username, mqtt_cfg.password);
77+
};
78+
79+
client = esp_mqtt_client_init(&mqtt_cfg);
80+
esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client);
81+
esp_mqtt_client_start(client);
82+
}

0 commit comments

Comments
 (0)