|
1 | | -#include "Esp8266.h" |
2 | | - |
3 | | -/** |
4 | | - * Implementation of the Esp8266 class. |
5 | | -*/ |
6 | | -Esp8266::Esp8266(char *ssidName, char *pwd, char *mqttServer, MsgServiceArduino *msgARD):client(espClient) |
7 | | -{ |
8 | | - this->ssidName = ssidName; |
9 | | - this->pwd = pwd; |
10 | | - this->msgARD = msgARD; |
11 | | - |
12 | | - client.setServer(mqttServer, 1883); |
13 | | - client.setCallback(std::bind(&Esp8266::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); |
14 | | -} |
15 | | - |
16 | | -void Esp8266::callback(char* topic, byte* payload, unsigned int length){ |
17 | | - String message = String((char*)payload); |
18 | | - this->msgARD->sendMsg(String(topic) + ":" + message.substring(0, length)); |
19 | | - message = ""; |
20 | | -} |
21 | | - |
22 | | -void Esp8266::connecting() |
23 | | -{ |
24 | | - WiFi.begin(ssidName, pwd); |
25 | | - Serial.print("Connecting..."); |
26 | | - while (WiFi.status() != WL_CONNECTED) |
27 | | - { |
28 | | - delay(500); |
29 | | - Serial.print("."); |
30 | | - } |
31 | | - WiFi.setSleepMode(WIFI_NONE_SLEEP); |
32 | | -} |
33 | | - |
34 | | -void Esp8266::reconnect(){ |
35 | | - while (!client.connected()) { |
36 | | - Serial.print("Attempting MQTT connection..."); |
37 | | - String clientId = "ESP8266Client-"; |
38 | | - clientId += String(random(0xffff), HEX); |
39 | | - if (client.connect(clientId.c_str())) { |
40 | | - Serial.println("connected"); |
41 | | - client.subscribe(VENTILATION); |
42 | | - client.subscribe(IRRIGATION); |
43 | | - client.subscribe(LUMINOSITY); |
44 | | - client.subscribe(TEMPERATURE); |
45 | | - } else { |
46 | | - Serial.print("failed, rc="); |
47 | | - Serial.print(client.state()); |
48 | | - Serial.println(" try again in 5 seconds"); |
49 | | - // Wait 5 seconds before retrying |
50 | | - delay(5000); |
51 | | - } |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -void Esp8266::processIncomingMessages(){ |
56 | | - if(!client.connected()){ |
57 | | - this->reconnect(); |
58 | | - } |
59 | | - client.loop(); |
60 | | -} |
61 | | - |
62 | | -void Esp8266::sendData(char* topic, String msg){ |
63 | | - if (WiFi.status() != WL_CONNECTED) |
64 | | - { |
65 | | - Serial.println("Not connecting"); |
66 | | - connecting(); |
67 | | - } |
68 | | - if (WiFi.status() == WL_CONNECTED) |
69 | | - { |
70 | | - client.publish(topic, msg.c_str()); |
71 | | - } |
72 | | -} |
73 | | - |
74 | | -bool Esp8266::isConnected() |
75 | | -{ |
76 | | - return WiFi.status() == WL_CONNECTED; |
77 | | -} |
| 1 | +#include "Esp8266.h" |
| 2 | + |
| 3 | +/** |
| 4 | + * Implementation of the Esp8266 class. |
| 5 | +*/ |
| 6 | +Esp8266::Esp8266(char *ssidName, char *pwd, char *mqttServer, MsgServiceArduino *msgARD, String greenhouseId):client(espClient) |
| 7 | +{ |
| 8 | + this->ssidName = ssidName; |
| 9 | + this->pwd = pwd; |
| 10 | + this->msgARD = msgARD; |
| 11 | + |
| 12 | + client.setServer(mqttServer, 1883); |
| 13 | + client.setCallback(std::bind(&Esp8266::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); |
| 14 | +} |
| 15 | + |
| 16 | +void Esp8266::callback(char* topic, byte* payload, unsigned int length){ |
| 17 | + String message = String((char*)payload); |
| 18 | + //message = "greenhouseID,action" |
| 19 | + String id = String(strtok((char*)payload, ",")); |
| 20 | + String action = String((char *)strtok(NULL, ",")); |
| 21 | + if (id == this->greenhouseId) |
| 22 | + { |
| 23 | + this->msgARD->sendMsg(String(topic) + ":" + action); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void Esp8266::connecting() |
| 28 | +{ |
| 29 | + WiFi.begin(ssidName, pwd); |
| 30 | + Serial.print("Connecting..."); |
| 31 | + while (WiFi.status() != WL_CONNECTED) |
| 32 | + { |
| 33 | + delay(500); |
| 34 | + Serial.print("."); |
| 35 | + } |
| 36 | + WiFi.setSleepMode(WIFI_NONE_SLEEP); |
| 37 | +} |
| 38 | + |
| 39 | +void Esp8266::reconnect(){ |
| 40 | + while (!client.connected()) { |
| 41 | + Serial.print("Attempting MQTT connection..."); |
| 42 | + String clientId = "ESP8266Client-"; |
| 43 | + clientId += String(random(0xffff), HEX); |
| 44 | + if (client.connect(clientId.c_str())) { |
| 45 | + Serial.println("connected"); |
| 46 | + client.subscribe(VENTILATION); |
| 47 | + client.subscribe(IRRIGATION); |
| 48 | + client.subscribe(LUMINOSITY); |
| 49 | + client.subscribe(TEMPERATURE); |
| 50 | + } else { |
| 51 | + Serial.print("failed, rc="); |
| 52 | + Serial.print(client.state()); |
| 53 | + Serial.println(" try again in 5 seconds"); |
| 54 | + // Wait 5 seconds before retrying |
| 55 | + delay(5000); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void Esp8266::processIncomingMessages(){ |
| 61 | + if(!client.connected()){ |
| 62 | + this->reconnect(); |
| 63 | + } |
| 64 | + client.loop(); |
| 65 | +} |
| 66 | + |
| 67 | +void Esp8266::sendData(char* topic, String msg){ |
| 68 | + if (WiFi.status() != WL_CONNECTED) |
| 69 | + { |
| 70 | + Serial.println("Not connecting"); |
| 71 | + connecting(); |
| 72 | + } |
| 73 | + if (WiFi.status() == WL_CONNECTED) |
| 74 | + { |
| 75 | + client.publish(topic, msg.c_str()); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +bool Esp8266::isConnected() |
| 80 | +{ |
| 81 | + return WiFi.status() == WL_CONNECTED; |
| 82 | +} |
0 commit comments