Skip to content

Commit f41fa99

Browse files
committed
major changes v1.2
1 parent 2fcac09 commit f41fa99

File tree

7 files changed

+248
-24
lines changed

7 files changed

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

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <ESP8266WiFi.h>
2-
#include <whesp8266.h>
2+
#include <ThingESP.h>
33

4-
WH_ESP8266 DeviceClient;
4+
ThingESP8266 thing("username", "project_name", "credentials");
55

66
int LED = LED_BUILTIN;
77

@@ -10,13 +10,10 @@ void setup()
1010
Serial.begin(115200);
1111

1212
pinMode(LED, OUTPUT);
13-
digitalWrite(LED, 1);
1413

15-
DeviceClient.SetDevice("username", "project_name", "credentials");
14+
thing.SetWiFi("wifi_ssid", "wifi_password");
1615

17-
DeviceClient.SetWiFi("ssid", "pass");
18-
19-
DeviceClient.initDevice();
16+
thing.initDevice();
2017

2118
}
2219

@@ -49,6 +46,6 @@ String HandleResponse(String query)
4946
void loop()
5047
{
5148

52-
DeviceClient.Handle();
49+
thing.Handle();
5350

5451
}

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.0.1
2+
version=1.2.0
33
author=SiddheshNan <hello@siddhesh.me>
44
maintainer=SiddheshNan <hello@siddhesh.me>
55
sentence=Arduino library for the ThingsESP Platform.

src/ThingESP.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define MQTT_MAX_PACKET_SIZE 1024
2+
#if defined(ESP8266)
3+
#include "ThingESP_8266.cpp"
4+
#elif defined (ESP32)
5+
#include "ThingESP_32.cpp"
6+
#endif

src/whesp8266.cpp renamed to src/ThingESP_32.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
#if defined(ESP8266)
22
#include <ESP8266WiFi.h>
3-
#else
3+
#elif defined (ESP32)
44
#include <WiFi.h>
55
#endif
66
#include "PubSubClient/PubSubClient.h"
77
#include "ArduinoJson.h"
8-
//#include <string.h>
9-
//WiFiClient espClient;
10-
//PubSubClient client(espClient);
11-
8+
129
String HandleResponse(String query);
1310

14-
class WH_ESP8266
11+
class ThingESP32
1512
{
1613
public:
17-
WH_ESP8266()
14+
ThingESP32(String username, String deviceName, String password)
1815
{
19-
WiFiClient espClient;
16+
WiFiClient espClient;
2017
PubSubClient client(espClient);
2118
this->client = client;
22-
};
23-
24-
void SetDevice(String username, String deviceName, String password)
25-
{
2619
this->Username = username;
2720
this->DeviceName = deviceName;
2821
this->Password = password;
29-
}
22+
};
23+
24+
3025
void SetWiFi(const char *ssID, const char *ssID_password)
3126
{
3227
this->ssid = ssID;
@@ -174,7 +169,7 @@ class WH_ESP8266
174169

175170
this->client.setServer(this->mqttServer, this->mqttPort);
176171
this->client.setCallback([this](char *topic, byte *payload, unsigned int length) {
177-
this->callback(topic, payload, length);
172+
callback(topic, payload, length);
178173
});
179174
}
180175
};

src/ThingESP_8266.cpp

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#if defined(ESP8266)
2+
#include <ESP8266WiFi.h>
3+
#elif defined (ESP32)
4+
#include <WiFi.h>
5+
#endif
6+
7+
#include "PubSubClient/PubSubClient.h"
8+
#include "ArduinoJson.h"
9+
10+
String HandleResponse(String query);
11+
12+
class ThingESP8266
13+
{
14+
public:
15+
ThingESP8266(String username, String deviceName, String password)
16+
{
17+
WiFiClient espClient;
18+
PubSubClient client(espClient);
19+
this->client = client;
20+
this->Username = username;
21+
this->DeviceName = deviceName;
22+
this->Password = password;
23+
};
24+
25+
26+
void SetWiFi(const char *ssID, const char *ssID_password)
27+
{
28+
this->ssid = ssID;
29+
this->ssid_password = ssID_password;
30+
}
31+
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+
62+
void initDevice()
63+
{
64+
// this->client = client;
65+
this->topic = this->DeviceName + "/" + this->Username;
66+
this->outname = this->DeviceName + "@" + this->Username;
67+
68+
this->char_DeviceName = this->DeviceName.c_str();
69+
this->char_Password = this->Password.c_str();
70+
this->char_outname = this->outname.c_str();
71+
this->char_topic = this->topic.c_str();
72+
this->initiated = true;
73+
74+
this->setupIT();
75+
}
76+
77+
void Handle()
78+
{
79+
if (!client.connected())
80+
{
81+
while (!client.connected())
82+
{
83+
Serial.print("Attempting connection...");
84+
if (client.connect(this->char_outname, this->char_outname, this->char_Password))
85+
{
86+
Serial.println("connected");
87+
client.subscribe(this->char_topic);
88+
}
89+
else
90+
{
91+
Serial.print("failed, rc=");
92+
Serial.print(this->client.state());
93+
Serial.println(" try again in 5 seconds");
94+
delay(5000);
95+
}
96+
}
97+
}
98+
this->client.loop();
99+
}
100+
101+
void publishMSG(const char *info)
102+
{
103+
client.publish(this->char_topic, info);
104+
}
105+
106+
void SetHost(const char *host)
107+
{
108+
this->mqttServer = host;
109+
}
110+
111+
private:
112+
String Username;
113+
String DeviceName;
114+
String Password;
115+
116+
bool initiated = false;
117+
118+
const char *ssid;
119+
const char *ssid_password;
120+
121+
const char *mqttServer = "thingesp.siddhesh.me";
122+
int mqttPort = 1893;
123+
124+
String topic;
125+
String outname;
126+
const char *char_DeviceName;
127+
const char *char_Password;
128+
const char *char_outname;
129+
const char *char_topic;
130+
131+
PubSubClient client;
132+
133+
void callback(char *topic, byte *payload, unsigned int length)
134+
{
135+
String srr;
136+
Serial.println();
137+
Serial.print("Message arrived [");
138+
Serial.print(topic);
139+
Serial.print("] ");
140+
Serial.println();
141+
for (int i = 0; i < length; i++)
142+
{
143+
Serial.print((char)payload[i]);
144+
srr.concat((char)payload[i]);
145+
}
146+
this->logic(srr);
147+
}
148+
149+
void setupIT()
150+
{
151+
152+
Serial.println();
153+
Serial.print("Connecting to ");
154+
Serial.println(ssid);
155+
156+
WiFi.begin(ssid, ssid_password);
157+
158+
while (WiFi.status() != WL_CONNECTED)
159+
{
160+
delay(500);
161+
Serial.print(".");
162+
}
163+
164+
randomSeed(micros());
165+
166+
Serial.println("");
167+
Serial.println("WiFi connected");
168+
Serial.println("IP address: ");
169+
Serial.println(WiFi.localIP());
170+
171+
this->client.setServer(this->mqttServer, this->mqttPort);
172+
this->client.setCallback([this](char *topic, byte *payload, unsigned int length) {
173+
callback(topic, payload, length);
174+
});
175+
}
176+
};

src/whesp8266.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)