Skip to content

Commit

Permalink
Merge pull request #35 from jomjol/rolling
Browse files Browse the repository at this point in the history
Rolling
  • Loading branch information
jomjol authored Oct 14, 2020
2 parents 1633b74 + 21d07be commit 2614481
Show file tree
Hide file tree
Showing 27 changed files with 410 additions and 104 deletions.
Binary file added .DS_Store
Binary file not shown.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@ A 3d-printable housing can be found here: https://www.thingiverse.com/thing:4571



##### Rolling - (2020-09-28)
##### Rolling - (2020-10-13)

* Implementation of user and password for MQTT Authentication (see `config.ini`)

2020-10-04

* First simple MQTT Client - to be configured in `config.ini` (see example)



2020-09-29

* Implementation of HTML-Version (thanks to phlupp)

* ESP32 Temperature is now written correctly to log file

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

Expand Down
Binary file added code/.DS_Store
Binary file not shown.
18 changes: 16 additions & 2 deletions code/lib/jomjol_flowcontroll/ClassFlowAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "ClassLogFile.h"

bool debugdetailanalog = false;

ClassFlowAnalog::ClassFlowAnalog()
{
isLogImage = false;
Expand Down Expand Up @@ -147,6 +149,8 @@ bool ClassFlowAnalog::doFlow(string time)
return false;
};

if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doFlow nach Alignment");

doNeuralNetwork(time);

return true;
Expand All @@ -167,15 +171,15 @@ 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;
}

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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions code/lib/jomjol_flowcontroll/ClassFlowControll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions code/lib/jomjol_flowcontroll/ClassFlowControll.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ClassFlowDigit.h"
#include "ClassFlowAnalog.h"
#include "ClassFlowPostProcessing.h"
#include "ClassFlowMQTT.h"


class ClassFlowControll :
Expand Down
122 changes: 122 additions & 0 deletions code/lib/jomjol_flowcontroll/ClassFlowMQTT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#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;
user = "";
password = "";
}

ClassFlowMQTT::ClassFlowMQTT(std::vector<ClassFlow*>* lfc)
{
uri = "";
topic = "";
clientname = "watermeter";
OldValue = "";
flowpostprocessing = NULL;
user = "";
password = "";

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]) == "USER") && (zerlegt.size() > 1))
{
this->user = zerlegt[1];
}
if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
{
this->password = zerlegt[1];
}
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, user, password);
}

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;
}
25 changes: 25 additions & 0 deletions code/lib/jomjol_flowcontroll/ClassFlowMQTT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#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;
std::string user, password;


public:
ClassFlowMQTT();
ClassFlowMQTT(std::vector<ClassFlow*>* lfc);
bool ReadParameter(FILE* pfile, string& aktparamgraph);
bool doFlow(string time);
string name(){return "ClassFlowMQTT";};
};

6 changes: 3 additions & 3 deletions code/lib/jomjol_image_proc/CFindTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
82 changes: 82 additions & 0 deletions code/lib/jomjol_mqtt/interface_mqtt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#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, std::string _user, std::string _password){
esp_mqtt_client_config_t mqtt_cfg = {
.uri = _mqttURI.c_str(),
.client_id = _clientid.c_str(),
};

if (_user.length() && _password.length()){
mqtt_cfg.username = _user.c_str();
mqtt_cfg.password = _password.c_str();
printf("Connect to MQTT: %s, %s", mqtt_cfg.username, mqtt_cfg.password);
};

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 changes: 4 additions & 0 deletions code/lib/jomjol_mqtt/interface_mqtt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <string>

void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = "");
void MQTTPublish(std::string _key, std::string _content);
11 changes: 11 additions & 0 deletions code/lib/jomjol_tfliteclass/CTfLiteClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 2614481

Please sign in to comment.