Repository navigation
Expand file tree
/
Copy pathBasicOTA.ino
More file actions
102 lines (80 loc) · 2.13 KB
/
Copy pathBasicOTA.ino
File metadata and controls
102 lines (80 loc) · 2.13 KB
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
#define DEBUG 1
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include "password.h" //Declare const char *wifi_password = "XXXXXXXX"; const char *wifi_ssid = "XXXXXXXX";
#ifdef DEBUG
#include <WiFiUdp.h>
unsigned int localPort = 514;
IPAddress syslogServer(192, 168, 1, 7);
WiFiUDP udp;
void sendUdpSyslog(String msgtosend)
{
unsigned int msg_length = msgtosend.length();
byte* p = (byte*)malloc(msg_length);
memcpy(p, (char*) msgtosend.c_str(), msg_length);
udp.beginPacket(syslogServer, 514);
udp.write("esp8266-OnlyOTA ");
udp.write(p, msg_length);
udp.endPacket();
free(p);
}
#endif
ESP8266WiFiMulti WiFiMulti;
WiFiClient wclient;
String UdpBuffer;
void CheckOTA()
{
if((WiFiMulti.run() == WL_CONNECTED)) {
#ifdef DEBUG
sendUdpSyslog("Flash Size In Byte:");
UdpBuffer=ESP.getFlashChipRealSize();
sendUdpSyslog(UdpBuffer);
#endif
t_httpUpdate_return ret = ESPhttpUpdate.update("http://192.168.1.5/ota/ota.php","0");
switch(ret) {
case HTTP_UPDATE_FAILED:
#ifdef DEBUG
char chArray[200] ;
sprintf(chArray,"HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
sendUdpSyslog(String(chArray));
#endif
break;
case HTTP_UPDATE_NO_UPDATES:
#ifdef DEBUG
sendUdpSyslog("HTTP_UPDATE_NO_UPDATES");
#endif
break;
case HTTP_UPDATE_OK:
#ifdef DEBUG
sendUdpSyslog("HTTP_UPDATE_OK");
#endif
break;
}
}
}
void setup()
{
WiFiMulti.addAP(wifi_ssid, wifi_password);
int Attempt = 0;
while (WiFiMulti.run() != WL_CONNECTED) {
delay(100);
Attempt++;
if (Attempt == 200)
{
ESP.restart();
}
}
#ifdef DEBUG
udp.begin(localPort);
sendUdpSyslog(WiFi.localIP().toString());
#endif
CheckOTA();
// delay(10000);
}
void loop()
{
delay(50);
}