-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalve_ota.ino
129 lines (103 loc) · 3.72 KB
/
valve_ota.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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <PubSubClient.h> //Get it from here: https://github.com/knolleary/pubsubclient
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include "secrets.h"
unsigned char certificates_esp8266_bin_crt[] = BIN_CRT;
unsigned int certificates_esp8266_bin_crt_len = BIN_CRT_LEN;
unsigned char certificates_esp8266_bin_key[] = BIN_KEY;
unsigned int certificates_esp8266_bin_key_len = BIN_KEY_LEN;
unsigned char certificates_esp8266_bin_CA[] = BIN_CA;
unsigned int certificates_esp8266_bin_CA_len = BIN_CA_LEN;
// callback header
void callback(char* topic, byte* payload, unsigned int length);
WiFiClientSecure espClient;
PubSubClient client(AWS_ENDPOINT, 8883, callback, espClient);
void checkForUpdate() {
// wait for WiFi connection
if (WiFi.status() == WL_CONNECTED) {
t_httpUpdate_return ret = ESPhttpUpdate.update(FW_UPDATE_URL);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
if (String(topic) == FW_UPDATE_TOPIC) {
Serial.println("fetching firmware update...");
return checkForUpdate();
}
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
const char* desired = root["desired"];
if (String(desired) == "open") {
Serial.println("desired: open");
digitalWrite(SOLENOID_PIN, HIGH);
client.publish(UPDATE_TOPIC, "{\"state\":{\"reported\": {\"valve\": \"open\"}}}");
} else {
Serial.println("desired: closed");
digitalWrite(SOLENOID_PIN, LOW);
client.publish(UPDATE_TOPIC, "{\"state\":{\"reported\": {\"valve\": \"closed\"}}}");
}
}
void setup() {
Serial.begin(115200);
Serial.println("Booting");
pinMode(SOLENOID_PIN, OUTPUT);
// should read from AWS
digitalWrite(SOLENOID_PIN, LOW);
WiFiManager wifiManager;
wifiManager.autoConnect();
Serial.println("connected...Hello :)");
espClient.setCACert(certificates_esp8266_bin_CA, certificates_esp8266_bin_CA_len);
espClient.setCertificate(certificates_esp8266_bin_crt, certificates_esp8266_bin_crt_len);
espClient.setPrivateKey(certificates_esp8266_bin_key, certificates_esp8266_bin_key_len);
client.setCallback(callback);
client.publish(UPDATE_TOPIC, "{\"state\":{\"reported\": {\"valve\": \"closed\"}}}");
client.subscribe(DELTA_TOPIC);
client.subscribe(FW_UPDATE_TOPIC);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
client.subscribe(DELTA_TOPIC);
client.subscribe(FW_UPDATE_TOPIC);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}