Skip to content

Commit f923fce

Browse files
committed
add mqtt example
1 parent 5c21145 commit f923fce

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

espduino/examples/mqtt/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
HOST ?= esp-link
2+
LIBRARYPATH = $(ARDUINODIR)/libraries ../../..
3+
LIBRARIES = espduino
4+
CPPFLAGS =
5+
SERIALDEV = net:$(HOST):2323
6+
include ../arduino.mk
7+
8+
run: upload size
9+
nc $(HOST) 23

espduino/examples/mqtt/mqtt.ino

+16-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* \author
55
* Tuan PM <[email protected]>
66
*/
7-
#include <SoftwareSerial.h>
7+
//#include <SoftwareSerial.h>
88
#include <espduino.h>
99
#include <mqtt.h>
1010

11-
SoftwareSerial debugPort(2, 3); // RX, TX
12-
ESP esp(&Serial, &debugPort, 4);
11+
//SoftwareSerial debugPort(2, 3); // RX, TX
12+
ESP esp(&Serial, &Serial, 4);
1313
MQTT mqtt(&esp);
1414
boolean wifiConnected = false;
1515

@@ -21,7 +21,7 @@ void wifiCb(void* response)
2121
if(res.getArgc() == 1) {
2222
res.popArgs((uint8_t*)&status, 4);
2323
if(status == STATION_GOT_IP) {
24-
debugPort.println("WIFI CONNECTED");
24+
Serial.println("Wifi connected");
2525
mqtt.connect("yourserver.com", 1883, false);
2626
wifiConnected = true;
2727
//or mqtt.connect("host", 1883); /*without security ssl*/
@@ -35,7 +35,7 @@ void wifiCb(void* response)
3535

3636
void mqttConnected(void* response)
3737
{
38-
debugPort.println("Connected");
38+
Serial.println("MQTT connected");
3939
mqtt.subscribe("/topic/0"); //or mqtt.subscribe("topic"); /*with qos = 0*/
4040
mqtt.subscribe("/topic/1");
4141
mqtt.subscribe("/topic/2");
@@ -50,36 +50,35 @@ void mqttData(void* response)
5050
{
5151
RESPONSE res(response);
5252

53-
debugPort.print("Received: topic=");
53+
Serial.print("Received: topic=");
5454
String topic = res.popString();
55-
debugPort.println(topic);
55+
Serial.println(topic);
5656

57-
debugPort.print("data=");
57+
Serial.print("data=");
5858
String data = res.popString();
59-
debugPort.println(data);
59+
Serial.println(data);
6060

6161
}
6262
void mqttPublished(void* response)
6363
{
6464

6565
}
6666
void setup() {
67-
Serial.begin(19200);
68-
debugPort.begin(19200);
67+
Serial.begin(115200);
6968
esp.enable();
7069
delay(500);
7170
esp.reset();
7271
delay(500);
7372
while(!esp.ready());
7473

75-
debugPort.println("ARDUINO: setup mqtt client");
74+
Serial.println("ARDUINO: setup mqtt client");
7675
if(!mqtt.begin("DVES_duino", "admin", "Isb_C4OGD4c3", 120, 1)) {
77-
debugPort.println("ARDUINO: fail to setup mqtt");
76+
Serial.println("ARDUINO: fail to setup mqtt");
7877
while(1);
7978
}
8079

8180

82-
debugPort.println("ARDUINO: setup mqtt lwt");
81+
Serial.println("ARDUINO: setup mqtt lwt");
8382
mqtt.lwt("/lwt", "offline", 0, 0); //or mqtt.lwt("/lwt", "offline");
8483

8584
/*setup mqtt events */
@@ -89,18 +88,18 @@ void setup() {
8988
mqtt.dataCb.attach(&mqttData);
9089

9190
/*setup wifi*/
92-
debugPort.println("ARDUINO: setup wifi");
91+
Serial.println("ARDUINO: setup wifi");
9392
esp.wifiCb.attach(&wifiCb);
9493

9594
esp.wifiConnect("DVES_HOME","wifipassword");
9695

9796

98-
debugPort.println("ARDUINO: system started");
97+
Serial.println("ARDUINO: system started");
9998
}
10099

101100
void loop() {
102101
esp.process();
103102
if(wifiConnected) {
104103

105104
}
106-
}
105+
}

espduino/mqtt.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MQTT {
2323
FP<void, void*> publishedCb;
2424
FP<void, void*> dataCb;
2525

26-
26+
2727
MQTT(ESP *esp);
2828
boolean begin(const char* client_id, const char* user, const char* pass, uint16_t keep_alive, boolean clean_seasion);
2929
boolean lwt(const char* topic, const char* message, uint8_t qos, uint8_t retain);
@@ -38,4 +38,4 @@ class MQTT {
3838
void publish(const char* topic, char* data);
3939

4040
};
41-
#endif
41+
#endif

espduino/rest.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ boolean REST::begin(const char* host, uint16_t port, boolean security)
2323
if(security)
2424
sec = 1;
2525
restCb.attach(this, &REST::restCallback);
26-
26+
2727
uint16_t crc = esp->request(CMD_REST_SETUP, (uint32_t)&restCb, 1, 3);
2828
crc = esp->request(crc,(uint8_t*)host, strlen(host));
2929
crc = esp->request(crc,(uint8_t*)&port, 2);
@@ -45,6 +45,8 @@ void REST::request(const char* path, const char* method, const char* data, int l
4545
{
4646
if(remote_instance == 0)
4747
return;
48+
//Serial.print("REST ri=");
49+
//Serial.println(remote_instance, 16);
4850
uint16_t crc;
4951
if(len > 0)
5052
crc = esp->request(CMD_REST_REQUEST, 0, 0, 5);
@@ -57,7 +59,7 @@ void REST::request(const char* path, const char* method, const char* data, int l
5759
crc = esp->request(crc,(uint8_t*)&len, 2);
5860
crc = esp->request(crc,(uint8_t*)data, len);
5961
}
60-
62+
6163
esp->request(crc);
6264
}
6365
void REST::request(const char* path, const char* method, const char* data)
@@ -131,7 +133,7 @@ uint16_t REST::getResponse(char* data, uint16_t maxLen)
131133
data[len < maxLen ? len : maxLen-1] = 0;
132134
return esp->return_value;
133135
}
134-
135-
136+
137+
136138
return 0;
137139
}

0 commit comments

Comments
 (0)