-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonoff-iot.ino
165 lines (142 loc) · 3.89 KB
/
sonoff-iot.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "FS.h"
#include "sonoff_secrets.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
int relay = 12;
int timezone = -3;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "a.st1.ntp.br", timezone * 3600);
WiFiClientSecure secureWiFiEspClient;
void callback(char *topic, byte *payload, unsigned int length)
{
StaticJsonDocument<128> pubSubTopicJsonNotification;
deserializeJson(pubSubTopicJsonNotification, payload);
bool hasCarArrived = pubSubTopicJsonNotification["hasCarArrived"];
if (hasCarArrived)
{
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
digitalWrite(LED_BUILTIN, LOW);
delay(5 * 60 * 1000);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
digitalWrite(LED_BUILTIN, HIGH);
}
}
void setup_wifi()
{
secureWiFiEspClient.setBufferSizes(512, 512);
WiFi.begin(WIFI_USER, WIFI_PSWD);
timeClient.begin();
while (!timeClient.update())
{
timeClient.forceUpdate();
}
secureWiFiEspClient.setX509Time(timeClient.getEpochTime());
}
PubSubClient client(AWS_ENDPOINT, MQTT_PORT, callback, secureWiFiEspClient);
void reconnect()
{
while (!client.connected())
{
if (client.connect(AWS_IOT_THING_ID))
{
publishMessageWhenReconnectsToBroker(timeClient.getFormattedDate());
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
char buf[256];
secureWiFiEspClient.getLastSSLError(buf, 256);
Serial.print("WiFiClientSecure SSL error: ");
Serial.println(buf);
delay(5000);
}
}
}
void SPIFFSLoading()
{
if (!SPIFFS.begin())
{
Serial.println("Failed to mount file system");
return;
}
Serial.print("Heap: ");
Serial.println(ESP.getFreeHeap());
File cert = SPIFFS.open("/cert.der", "r");
if (!cert)
{
Serial.println("Failed to open cert file");
}
else
Serial.println("Success to open cert file");
delay(1000);
// Load private key file
File private_key = SPIFFS.open("/key.der", "r");
if (!private_key)
{
Serial.println("Failed to open private cert file");
}
else
Serial.println("Success to open private cert file");
delay(1000);
// Load CA file
File ca = SPIFFS.open("/rootCA.der", "r");
if (!ca)
{
Serial.println("Failed to open ca ");
}
else
Serial.println("Success to open ca");
delay(1000);
if (secureWiFiEspClient.loadCertificate(cert))
Serial.println("cert loaded");
else
Serial.println("cert not loaded");
if (secureWiFiEspClient.loadPrivateKey(private_key))
Serial.println("private key loaded");
else
Serial.println("private key not loaded");
if (secureWiFiEspClient.loadCACert(ca))
Serial.println("ca loaded");
else
Serial.println("ca failed");
}
void reconnectClient()
{
if (!client.connected())
{
reconnect();
}
}
void setup()
{
Serial.begin(115200);
setup_wifi();
delay(1000);
SPIFFSLoading();
reconnectClient();
}
void publishMessageWhenReconnectsToBroker(String zonedDateTime)
{
char reconnectMessage[192];
StaticJsonDocument<192> pubSubJsonSerializable;
pubSubJsonSerializable["message"] = AWS_IOT_RECONNECT_MESSAGE;
pubSubJsonSerializable["sender"] = AWS_IOT_THING_ID;
pubSubJsonSerializable["onlineStatus"] = true;
pubSubJsonSerializable["timeArrived"] = zonedDateTime;
serializeJson(pubSubJsonSerializable, reconnectMessage);
client.subscribe(AWS_IOT_CORE_TOPIC);
client.publish(AWS_IOT_CORE_STATUS_CHECK_TOPIC, reconnectMessage);
Serial.print("Connected");
}
void loop()
{
client.loop();
reconnectClient();
}