Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
mqtt.ino
Go to the documentation of this file.
1
5#include <ELClient.h>
6#include <ELClientCmd.h>
7#include <ELClientMqtt.h>
8
9// Initialize a connection to esp-link using the normal hardware serial port both for
10// SLIP and for debug messages.
12
13// Initialize CMD client (for GetTime)
15
16// Initialize the MQTT client
17ELClientMqtt mqtt(&esp);
18
19// Callback made from esp-link to notify of wifi status changes
20// Here we just print something out for grins
21void wifiCb(void* response) {
22 ELClientResponse *res = (ELClientResponse*)response;
23 if (res->argc() == 1) {
24 uint8_t status;
25 res->popArg(&status, 1);
26
27 if(status == STATION_GOT_IP) {
28 Serial.println("WIFI CONNECTED");
29 } else {
30 Serial.print("WIFI NOT READY: ");
31 Serial.println(status);
32 }
33 }
34}
35
37
38// Callback when MQTT is connected
39void mqttConnected(void* response) {
40 Serial.println("MQTT connected!");
41 mqtt.subscribe("/esp-link/1");
42 mqtt.subscribe("/hello/world/#");
43 //mqtt.subscribe("/esp-link/2", 1);
44 //mqtt.publish("/esp-link/0", "test1");
45 connected = true;
46}
47
48// Callback when MQTT is disconnected
49void mqttDisconnected(void* response) {
50 Serial.println("MQTT disconnected");
51 connected = false;
52}
53
54// Callback when an MQTT message arrives for one of our subscriptions
55void mqttData(void* response) {
56 ELClientResponse *res = (ELClientResponse *)response;
57
58 Serial.print("Received: topic=");
59 String topic = res->popString();
60 Serial.println(topic);
61
62 Serial.print("data=");
63 String data = res->popString();
64 Serial.println(data);
65}
66
67void mqttPublished(void* response) {
68 Serial.println("MQTT published");
69}
70
71void setup() {
72 Serial.begin(115200);
73 Serial.println("EL-Client starting!");
74
75 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
76 // callbacks to the wifi status change callback. The callback gets called with the initial
77 // status right after Sync() below completes.
78 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
79 bool ok;
80 do {
81 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
82 if (!ok) Serial.println("EL-Client sync failed!");
83 } while(!ok);
84 Serial.println("EL-Client synced!");
85
86 // Set-up callbacks for events and initialize with es-link.
87 mqtt.connectedCb.attach(mqttConnected);
88 mqtt.disconnectedCb.attach(mqttDisconnected);
89 mqtt.publishedCb.attach(mqttPublished);
90 mqtt.dataCb.attach(mqttData);
91 mqtt.setup();
92
93 //Serial.println("ARDUINO: setup mqtt lwt");
94 //mqtt.lwt("/lwt", "offline", 0, 0); //or mqtt.lwt("/lwt", "offline");
95
96 Serial.println("EL-MQTT ready");
97}
98
99static int count;
100static uint32_t last;
101
102void loop() {
103 esp.Process();
104
105 if (connected && (millis()-last) > 4000) {
106 Serial.println("publishing");
107 char buf[12];
108
109 itoa(count++, buf, 10);
110 mqtt.publish("/esp-link/1", buf);
111
112 itoa(count+99, buf, 10);
113 mqtt.publish("/hello/world/arduino", buf);
114
115 uint32_t t = cmd.GetTime();
116 Serial.print("Time: "); Serial.println(t);
117
118 last = millis();
119 }
120}
Definitions for ELClientCmd.
Definitions for ELClientMqtt.
Definitions for ELClient.
@ STATION_GOT_IP
Definition ELClient.h:51
String popString()
Extract a string from the response packet.
int16_t popArg(void *data, uint16_t maxLen)
Extract an argument from the response packet.
void mqttConnected(void *response)
Definition mqtt.ino:39
void wifiCb(void *response)
Definition mqtt.ino:21
void mqttData(void *response)
Definition mqtt.ino:55
void setup()
Definition mqtt.ino:71
void mqttPublished(void *response)
Definition mqtt.ino:67
bool connected
Definition mqtt.ino:36
ELClientCmd cmd & esp
Definition mqtt.ino:14
ELClient esp & Serial
Definition mqtt.ino:11
void mqttDisconnected(void *response)
Definition mqtt.ino:49
void loop()
Definition mqtt.ino:102