Skip to content

Commit 19c89fa

Browse files
committed
Aggiunto esempio MQTT
1 parent 680823a commit 19c89fa

File tree

2 files changed

+160
-3
lines changed

2 files changed

+160
-3
lines changed

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
+#Files
1+
#Files
2+
3+
#Directories
24

3-
#Directorys
4-
/Examples/BasicMeter_with_MQTT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <MQTTManager.h>
2+
#include <WebServer.h>
3+
#include <PowerMeter.h>
4+
5+
WiFiClient espClient;
6+
PubSubClient client(espClient);
7+
const char* mosquittoServer = "test.mosquitto.org";
8+
String topics[2] = {"test/topic1",
9+
"test/topic2"};
10+
11+
WebServer server(80);
12+
13+
MQTTManager connectionManager;
14+
15+
const char* ssid = "YOUR_SSID";
16+
const char* pass = "YOUR_PASS";
17+
18+
//PowerMeter
19+
SemaphoreHandle_t semaforo;
20+
TaskHandle_t powerMeterTask;
21+
22+
PowerMeter powerMeter;
23+
24+
uint32_t t0 = 0;
25+
26+
void powerMeasureFunction(void *vParameters);
27+
28+
29+
void rebootCallback() {
30+
Serial.println("Rebooot timeee!!...");
31+
}
32+
33+
void callback(char* topic, byte* message, unsigned int length) {
34+
35+
Serial.print("Message arrived on topic: ");
36+
Serial.print(topic);
37+
Serial.print(". Message: ");
38+
39+
char messageChar[length];
40+
String messageTemp;
41+
String toSend;
42+
char toSendChar[12];
43+
44+
for (int i = 0; i < length; i++) {
45+
Serial.print((char)message[i]);
46+
messageTemp += (char)message[i];
47+
messageChar[i] += (char)message[i];
48+
}
49+
Serial.println();
50+
51+
}
52+
53+
void setup() {
54+
//Start serial
55+
Serial.begin(115200);
56+
57+
//Creatin a variable to store version of the project
58+
String ver = String(__FILE__) + " Time: " + String(__DATE__) + " " + String(__TIME__);
59+
Serial.println("Version: " + ver);
60+
61+
//Setting version on WebServer /IP_ADD/info
62+
connectionManager.setVersion(ver);
63+
//Setting server
64+
// true -> whitDefaultHomePage
65+
// set olso /IP_ADD/reboot for call rebootCallback and after reboots the core
66+
// and /IP_ADD/rebootOnly thats reboot ESP32 whitout calling callBack
67+
connectionManager.setServer(&server, true);
68+
//Connect to a WiFi for the first time
69+
connectionManager.startWiFi(ssid, pass);
70+
//For enable WPS may use:
71+
//connectionManager.startConnection(bool whitWPS = true);
72+
73+
//Start WebServer and OTA
74+
connectionManager.startWebServer();
75+
connectionManager.setOTAHostname("ESP32");
76+
connectionManager.startOTA();
77+
78+
//Setting rebootCallback and reboot options
79+
connectionManager.setOnRebootCallback(rebootCallback);
80+
connectionManager.setRebootOptions(false, true, false, true);
81+
82+
//Print the hostname
83+
Serial.println("You can reach me also at: " + connectionManager.getOTAHostname() + ".local/");
84+
85+
//Setting MQTT server
86+
connectionManager.setMQTTServer(&client, "ESP32", mosquittoServer);
87+
connectionManager.setTopics(topics, 2);
88+
connectionManager.setCallback(callback);
89+
connectionManager.startMQTT();
90+
91+
//PowerMeter
92+
//Printing the ADC values
93+
powerMeter.printADCValues();
94+
Serial.println();
95+
96+
//Setting offsets
97+
//powerMeter.setOffsets();
98+
99+
//Disable debug
100+
powerMeter.setDebug(false);
101+
102+
//Creating a variable to store version of the project
103+
String ver = String(__FILE__) + " Time: " + String(__DATE__) + " " + String(__TIME__);
104+
Serial.println("Version: " + ver);
105+
106+
//Creating Mutex
107+
semaforo = xSemaphoreCreateMutex();
108+
109+
//Creating measure task
110+
xTaskCreatePinnedToCore(
111+
powerMeasureFunction, /* Task function. */
112+
"MEASURE TASK", /* name of task. */
113+
10000, /* Stack size of task */
114+
NULL, /* parameter of the task */
115+
1, /* priority of the task */
116+
&powerMeterTask, /* Task handle to keep track of created task */
117+
1);
118+
}
119+
120+
void powerMeasureFunction(void *vParameters) {
121+
while (true) {
122+
xSemaphoreTake(semaforo, portMAX_DELAY);
123+
powerMeter.loop();
124+
xSemaphoreGive(semaforo);
125+
}
126+
vTaskDelete(NULL);
127+
}
128+
129+
void loop() {
130+
//this update everything, connection LED (default on-board led), connection BUTTON (default on-board BOOT button), servers...
131+
//ATTENTION: in case of MQTT connection falliture can block the prosess for several seconds
132+
connectionManager.loop();
133+
134+
//Sending data to MQTT
135+
if (millis() - t0 > 1000) {
136+
t0 = millis();
137+
xSemaphoreTake(semaforo, portMAX_DELAY);
138+
Measure values = powerMeter.getMeasure();
139+
xSemaphoreGive(semaforo);
140+
Serial.println(values.toString());
141+
sendViaMQTT(&values);
142+
}
143+
}
144+
145+
void sendViaMQTT(Measure *values){
146+
char str[10];
147+
sprintf(str, "%.2f", values->rmsVoltage);
148+
client.publish("wattmetro/RMSvoltage", str);
149+
sprintf(str, "%.2f", values->realPower);
150+
client.publish("wattmetro/realPower", str);
151+
sprintf(str, "%.2f", values->rmsCurrent);
152+
client.publish("wattmetro/RMScurrent", str);
153+
sprintf(str, "%.2f", values->realPowerSec);
154+
client.publish("wattmetro/realPowerSec", str);
155+
sprintf(str, "%.2f", values->rmsCurrentSec);
156+
client.publish("wattmetro/RMScurrentSec", str);
157+
}

0 commit comments

Comments
 (0)