-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP_OTA_Github.ino
178 lines (161 loc) · 5.48 KB
/
ESP_OTA_Github.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
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <WiFiClientSecure.h>
#include <CertStoreBearSSL.h>
BearSSL::CertStore certStore;
#include <time.h>
const String FirmwareVer={"2.0"};//{"1.8"};
#define URL_fw_Version "https://raw.githubusercontent.com/soheilpaper/ota_github_blink/main/version.txt"
#define URL_fw_Bin "https://github.com/soheilpaper/ota_github_blink/raw/main/BlinkWithoutDelay_ino_esp8285.bin" //"https://raw.githubusercontent.com/programmer131/otaFiles/master/firmware.bin"
const char* host = "raw.githubusercontent.com";
const int httpsPort = 443;
// DigiCert High Assurance EV Root CA
const char trustRoot[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j
ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDA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DfzAIJ9VNep
+OkuE6N36B9K
-----END CERTIFICATE-----
)EOF";
X509List cert(trustRoot);
extern const unsigned char caCert[] PROGMEM;
extern const unsigned int caCertLen;
const char* ssid = "Adsl momo"; //"home_wifi";
const char* password = "9155253009"; //"helloworld";
void setClock() {
// Set time via NTP, as required for x.509 validation
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
/*
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
*/
}
void FirmwareUpdate()
{
WiFiClientSecure client;
client.setTrustAnchors(&cert);
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return;
}
client.print(String("GET ") + URL_fw_Version + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
//Serial.println("Headers received");
break;
}
}
String payload = client.readStringUntil('\n');
payload.trim();
if(payload.equals(FirmwareVer) )
{
Serial.println("Device already on latest firmware version");
}
else
{
Serial.println("New firmware detected");
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
t_httpUpdate_return ret = ESPhttpUpdate.update(client, URL_fw_Bin);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", 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 connect_wifi();
unsigned long previousMillis_2 = 0;
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 60000;
const long mini_interval = 1000;
void repeatedCall(){
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis) >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
setClock();
FirmwareUpdate();
}
if ((currentMillis - previousMillis_2) >= mini_interval) {
static int idle_counter=0;
previousMillis_2 = currentMillis;
Serial.print(" Active fw version:");
Serial.println(FirmwareVer);
Serial.print("Idle Loop....");
Serial.println(idle_counter++);
if(idle_counter%2==0)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
if(WiFi.status() == !WL_CONNECTED)
connect_wifi();
}
}
void setup()
{
Serial.begin(115200);
Serial.println("");
Serial.println("Start");
WiFi.mode(WIFI_STA);
connect_wifi();
setClock();
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
repeatedCall();
delay (1000);
Serial.println("Connecting to FirmwareUpdate");
FirmwareUpdate();
Serial.println("end of FirmwareUpdate");
}
void connect_wifi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("O");
}
Serial.println("Connected to WiFi");
}