Skip to content

Commit d09be29

Browse files
committed
added mqtt to the example
1 parent 9e6654f commit d09be29

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

09_esp_now/data/config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
22
"wifi":{
3-
"mode":"STA",
3+
"mode":"AP",
44
"channel":1
55
},
66
"espnow":{
7+
},
8+
"mqtt" : {
9+
"host":"10.0.0.42",
10+
"port":1883,
11+
"client_id":"esp_test3_now"
712
}
813
}

09_esp_now/platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ framework = arduino
44
lib_deps =
55
66
7+
[email protected] #MQTT 256dpi/arduino-mqtt
78
lib_ldf_mode = deep+
89
[env:esp32dev]
910

09_esp_now/src/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#include <Arduino.h>
22
#include <ArduinoJson.h>
3+
#include <WiFi.h>
4+
#include <MQTT.h>
35

46
#include <ConfigUtils.h>
57
#include <EspNowWrapper.h>
68

79
DynamicJsonDocument config(5*1024);//5 KB
810
DynamicJsonDocument secret(1*1024);//1 KB
11+
MQTTClient mqtt(1*1024);
12+
WiFiClient wifi;//needed to stay on global scope
913
NowApp espnow;
1014

1115
uint32_t cycle_count = 0;
@@ -14,6 +18,13 @@ void meshMessage(String &payload,String from){
1418
Serial.printf("RX> from(%s) => [%s]\n",from.c_str(),payload.c_str());
1519
}
1620

21+
void mqtt_start(DynamicJsonDocument &config){
22+
mqtt.begin(config["mqtt"]["host"],config["mqtt"]["port"], wifi);
23+
if(mqtt.connect(config["mqtt"]["client_id"])){
24+
Serial.println("mqtt>connected");
25+
}
26+
}
27+
1728
void setup() {
1829

1930
Serial.begin(115200);
@@ -23,6 +34,18 @@ void setup() {
2334
load_json(secret,"/secret.json");
2435
timelog("config loaded");
2536

37+
String ssid = secret["wifi"]["access_point"];
38+
String pass = secret["wifi"]["password"];
39+
WiFi.begin(ssid.c_str(),pass.c_str());
40+
while (WiFi.status() != WL_CONNECTED) {
41+
Serial.print(".");
42+
delay(1000);
43+
}
44+
timelog("config loaded");
45+
mqtt_start(config);
46+
mqtt.publish("espnow","test");
47+
mqtt.loop();
48+
2649
espnow.start(config,secret);
2750
espnow.onMessage(meshMessage);
2851

@@ -34,6 +57,8 @@ void loop() {
3457
cycle_count++;
3558
Serial.printf("\n\n");
3659
timelog("loop start cycle ("+String(cycle_count)+")");
60+
mqtt.publish("espnow","Mqtt("+String(cycle_count)+")");
61+
delay(1000);
3762
espnow.print_info();
3863
espnow.broadcast("Hello Everyone ("+String(cycle_count)+")");
3964
delay(5000);

0 commit comments

Comments
 (0)