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
79DynamicJsonDocument config (5 *1024 );// 5 KB
810DynamicJsonDocument secret (1 *1024 );// 1 KB
11+ MQTTClient mqtt (1 *1024 );
12+ WiFiClient wifi;// needed to stay on global scope
913NowApp espnow;
1014
1115uint32_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+
1728void 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