Skip to content

Commit 9c6770a

Browse files
committed
fixed ESP32 reset bug & added more examples
1 parent 9598f79 commit 9c6770a

File tree

6 files changed

+178
-86
lines changed

6 files changed

+178
-86
lines changed

examples/ESP32-example/ESP32-example.ino renamed to examples/ESP32-basic/ESP32-basic.ino

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
ThingESP32 thing("username", "project_name", "credentials");
55

6-
int LED = 16;
6+
int LED = 2;
77

88
void setup()
99
{
@@ -23,17 +23,17 @@ String HandleResponse(String query)
2323
{
2424

2525
if (query == "led on") {
26-
digitalWrite(LED, 0);
26+
digitalWrite(LED, 1);
2727
return "Done: LED Turned ON";
2828
}
2929

3030
else if (query == "led off") {
31-
digitalWrite(LED, 1);
31+
digitalWrite(LED, 0);
3232
return "Done: LED Turned OFF";
3333
}
3434

3535
else if (query == "led status")
36-
return digitalRead(LED) ? "LED is OFF" : "LED is ON";
36+
return digitalRead(LED) ? "LED is ON" : "LED is OFF";
3737

3838

3939
else return "Your query was invalid..";
@@ -45,7 +45,5 @@ String HandleResponse(String query)
4545

4646
void loop()
4747
{
48-
4948
thing.Handle();
50-
5149
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <WiFi.h>
2+
#include <ThingESP.h>
3+
4+
ThingESP32 thing("username", "project_name", "credentials");
5+
6+
int LED = 2;
7+
8+
unsigned long previousMillis = 0;
9+
const long INTERVAL = 6000;
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
15+
pinMode(LED, OUTPUT);
16+
17+
thing.SetWiFi("wifi_ssid", "wifi_password");
18+
19+
thing.initDevice();
20+
}
21+
22+
String HandleResponse(String query)
23+
{
24+
25+
if (query == "led on")
26+
{
27+
digitalWrite(LED, 1);
28+
return "Done: LED Turned ON";
29+
}
30+
31+
else if (query == "led off")
32+
{
33+
digitalWrite(LED, 0);
34+
return "Done: LED Turned OFF";
35+
}
36+
37+
else if (query == "led status")
38+
return digitalRead(LED) ? "LED is ON" : "LED is OFF";
39+
40+
else
41+
return "Your query was invalid..";
42+
}
43+
44+
void loop()
45+
{
46+
47+
// if (millis() - previousMillis >= INTERVAL)
48+
// {
49+
// previousMillis = millis();
50+
// String msg = digitalRead(LED) ? "LED is ON" : "LED is OFF";
51+
// thing.sendMsg("PHONE_NUMBER", msg);
52+
// }
53+
54+
thing.Handle();
55+
}

examples/ESP8266-example/ESP8266-example.ino renamed to examples/ESP8266-basic/ESP8266-basic.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,5 @@ String HandleResponse(String query)
4545

4646
void loop()
4747
{
48-
4948
thing.Handle();
50-
5149
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <ESP8266WiFi.h>
2+
#include <ThingESP.h>
3+
4+
ThingESP8266 thing("username", "project_name", "credentials");
5+
6+
int LED = LED_BUILTIN;
7+
8+
unsigned long previousMillis = 0;
9+
const long INTERVAL = 6000;
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
15+
pinMode(LED, OUTPUT);
16+
17+
thing.SetWiFi("wifi_ssid", "wifi_password");
18+
19+
thing.initDevice();
20+
}
21+
22+
String HandleResponse(String query)
23+
{
24+
25+
if (query == "led on")
26+
{
27+
digitalWrite(LED, 0);
28+
return "Done: LED Turned ON";
29+
}
30+
31+
else if (query == "led off")
32+
{
33+
digitalWrite(LED, 1);
34+
return "Done: LED Turned OFF";
35+
}
36+
37+
else if (query == "led status")
38+
return digitalRead(LED) ? "LED is OFF" : "LED is ON";
39+
40+
else
41+
return "Your query was invalid..";
42+
}
43+
44+
void loop()
45+
{
46+
// if (millis() - previousMillis >= INTERVAL)
47+
// {
48+
// previousMillis = millis();
49+
// String msg = digitalRead(LED) ? "LED is OFF" : "LED is ON";
50+
// thing.sendMsg("PHONE_NUMBER", msg);
51+
// }
52+
53+
thing.Handle();
54+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ThingESP
2-
version=1.2.1
2+
version=1.2.2
33
author=SiddheshNan <hello@siddhesh.me>
44
maintainer=SiddheshNan <hello@siddhesh.me>
55
sentence=Arduino library for the ThingsESP Platform.

src/ThingESP_32.cpp

Lines changed: 64 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,32 @@
1+
#define MQTT_MAX_PACKET_SIZE 1024
12
#if defined(ESP8266)
23
#include <ESP8266WiFi.h>
3-
#elif defined (ESP32)
4+
#elif defined(ESP32)
45
#include <WiFi.h>
56
#endif
67
#include "PubSubClient/PubSubClient.h"
78
#include "ArduinoJson.h"
89

9-
1010
String HandleResponse(String query);
1111

1212
class ThingESP32
1313
{
1414
public:
15-
ThingESP32(String username, String deviceName, String password)
15+
ThingESP32(String username, String deviceName, String password) : g_client(espClient)
1616
{
17-
WiFiClient espClient;
18-
PubSubClient client(espClient);
19-
this->client = client;
2017
this->Username = username;
2118
this->DeviceName = deviceName;
2219
this->Password = password;
2320
};
2421

25-
2622
void SetWiFi(const char *ssID, const char *ssID_password)
2723
{
2824
this->ssid = ssID;
2925
this->ssid_password = ssID_password;
3026
}
3127

32-
void logic(String data)
33-
{
34-
DynamicJsonDocument data_in(1024);
35-
DynamicJsonDocument data_out(1024);
36-
deserializeJson(data_in, data);
37-
38-
if (data_in["action"] == "query")
39-
{
40-
data_out["msg_id"] = data_in["msg_id"];
41-
data_out["action"] = "returned_api_response";
42-
String query = data_in["query"];
43-
query.toLowerCase();
44-
data_out["returned_api_response"] = HandleResponse(query);
45-
String outdata;
46-
serializeJson(data_out, outdata);
47-
publishMSG(outdata.c_str());
48-
}
49-
};
50-
51-
void sendMsg(String number, String msg)
52-
{
53-
DynamicJsonDocument data_out(1024);
54-
data_out["action"] = "device_call";
55-
data_out["to_number"] = number;
56-
data_out["msg"] = msg;
57-
String outdata;
58-
serializeJson(data_out, outdata);
59-
publishMSG(outdata.c_str());
60-
}
61-
6228
void initDevice()
6329
{
64-
// this->client = client;
6530
this->topic = this->DeviceName + "/" + this->Username;
6631
this->outname = this->DeviceName + "@" + this->Username;
6732

@@ -71,41 +36,66 @@ class ThingESP32
7136
this->char_topic = this->topic.c_str();
7237
this->initiated = true;
7338

74-
this->setupIT();
39+
Serial.println();
40+
Serial.print("Connecting to ");
41+
Serial.println(ssid);
42+
Serial.println(mqttServer);
43+
44+
WiFi.begin(ssid, ssid_password);
45+
46+
while (WiFi.status() != WL_CONNECTED)
47+
{
48+
delay(500);
49+
Serial.print(".");
50+
}
51+
52+
randomSeed(micros());
53+
54+
Serial.println("");
55+
Serial.println("WiFi connected");
56+
Serial.println("IP address: ");
57+
Serial.println(WiFi.localIP());
58+
59+
g_client.setServer(this->mqttServer, this->mqttPort);
60+
g_client.setCallback([this](char *topic, byte *payload, unsigned int length) {
61+
callback(topic, payload, length);
62+
});
7563
}
7664

7765
void Handle()
7866
{
79-
if (!client.connected())
67+
if (!g_client.connected())
8068
{
81-
while (!client.connected())
69+
while (!g_client.connected())
8270
{
71+
delay(10);
8372
Serial.print("Attempting connection...");
84-
if (client.connect(this->char_outname, this->char_outname, this->char_Password))
73+
if (g_client.connect(this->char_outname, this->char_outname, this->char_Password))
8574
{
8675
Serial.println("connected");
87-
client.subscribe(this->char_topic);
76+
g_client.subscribe(this->char_topic);
8877
}
8978
else
9079
{
9180
Serial.print("failed, rc=");
92-
Serial.print(this->client.state());
81+
Serial.print(g_client.state());
9382
Serial.println(" try again in 5 seconds");
9483
delay(5000);
9584
}
9685
}
9786
}
98-
this->client.loop();
87+
g_client.loop();
9988
}
10089

101-
void publishMSG(const char *info)
102-
{
103-
client.publish(this->char_topic, info);
104-
}
105-
106-
void SetHost(const char *host)
90+
void sendMsg(String number, String msg)
10791
{
108-
this->mqttServer = host;
92+
DynamicJsonDocument data_out(1024);
93+
data_out["action"] = "device_call";
94+
data_out["to_number"] = number;
95+
data_out["msg"] = msg;
96+
String outdata;
97+
serializeJson(data_out, outdata);
98+
publishMSG(outdata.c_str());
10999
}
110100

111101
private:
@@ -119,17 +109,19 @@ class ThingESP32
119109
const char *ssid_password;
120110

121111
const char *mqttServer = "thingesp.siddhesh.me";
122-
112+
123113
int mqttPort = 1893;
124114

125115
String topic;
126116
String outname;
117+
127118
const char *char_DeviceName;
128119
const char *char_Password;
129120
const char *char_outname;
130121
const char *char_topic;
131122

132-
PubSubClient client;
123+
WiFiClient espClient;
124+
PubSubClient g_client;
133125

134126
void callback(char *topic, byte *payload, unsigned int length)
135127
{
@@ -144,35 +136,30 @@ class ThingESP32
144136
srr.concat((char)payload[i]);
145137
}
146138
Serial.print(srr);
147-
this->logic(srr);
139+
onMessage(srr);
148140
}
149141

150-
void setupIT()
142+
void onMessage(String data)
151143
{
144+
DynamicJsonDocument data_in(1024);
145+
DynamicJsonDocument data_out(1024);
146+
deserializeJson(data_in, data);
152147

153-
Serial.println();
154-
Serial.print("Connecting to ");
155-
Serial.println(ssid);
156-
Serial.println(mqttServer);
157-
158-
WiFi.begin(ssid, ssid_password);
159-
160-
while (WiFi.status() != WL_CONNECTED)
148+
if (data_in["action"] == "query")
161149
{
162-
delay(500);
163-
Serial.print(".");
150+
data_out["msg_id"] = data_in["msg_id"];
151+
data_out["action"] = "returned_api_response";
152+
String query = data_in["query"];
153+
query.toLowerCase();
154+
data_out["returned_api_response"] = HandleResponse(query);
155+
String outdata;
156+
serializeJson(data_out, outdata);
157+
publishMSG(outdata.c_str());
164158
}
159+
}
165160

166-
randomSeed(micros());
167-
168-
Serial.println("");
169-
Serial.println("WiFi connected");
170-
Serial.println("IP address: ");
171-
Serial.println(WiFi.localIP());
172-
173-
this->client.setServer(this->mqttServer, this->mqttPort);
174-
this->client.setCallback([this](char *topic, byte *payload, unsigned int length) {
175-
callback(topic, payload, length);
176-
});
161+
void publishMSG(const char *info)
162+
{
163+
g_client.publish(this->char_topic, info);
177164
}
178165
};

0 commit comments

Comments
 (0)