-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilityInformation.ino
126 lines (111 loc) · 3.82 KB
/
utilityInformation.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
#include <Arduino.h>
#include <MAGELLAN_MQTT.h>
#include <MAGELLAN_WiFi_SETTING.h> //optional you can using wifi connect with your own function
WiFiClient WiFi_client;
MAGELLAN_MQTT magel(WiFi_client);
String SSID = "set_your_ssid";
String PASS = "set_your_password";
String thingIdentifier = "set_your_thing_identifier";
String thingSecret = "set_your_thing_secret";
int checkStatusUpdate = UNKNOWN;
void setup()
{
Serial.begin(115200);
WiFiSetting.SSID = SSID; //optional wifi connection
WiFiSetting.PASS = PASS; //optional wifi connection
connectWiFi(WiFiSetting); //optional wifi connection
magel.OTA.autoUpdate(false); // this function ENABLED by default unless you set FALSE
setting.ThingIdentifier = thingIdentifier;
setting.ThingSecret = thingSecret;
setting.clientBufferSize = defaultOTABuffer; // set buffer size compatible for OTA
magel.begin(setting);
magel.getServerConfig("autoUpdate", [](String resp){
if(resp == "1")
{
magel.OTA.autoUpdate(true);
}
else{
magel.OTA.autoUpdate(false);
}
//magel.OTA.getAutoUpdate() is return TRUE if set autoUpdate, FALSE if set manualUpdate
magel.clientConfig.add("autoUpdateMode", ((magel.OTA.getAutoUpdate())? "ENABLE" : "DISABLE"));
magel.clientConfig.save(); // update client config from device to thing optional
});
magel.getControl([](String key, String value){
if(key == "executeUpdate")
{
magel.control.ACK("executeUpdate", value);
if(value == "1")
{
magel.OTA.executeUpdate(); // executeUpdate OTA
}
}
else // acknowledge other control
{
magel.control.ACK(key, value);
}
});
// prepare sensor for add widget control
magel.report.send("executeUpdate", "0");
}
void loop()
{
magel.loop();
reconnectWiFi(magel);
magel.subscribes([](){
magel.subscribe.serverConfig(PLAINTEXT);
magel.subscribe.control(PLAINTEXT);
checkStatusUpdate = magel.OTA.checkUpdate(); // checkUpdate once time after connect and after reconnect
// subscribe function here!
});
magel.interval(10,[](){ //time interval function inside every 10000 millis
// doing function something every 10 sec here!
switch (checkStatusUpdate)
{
case UP_TO_DATE:
Serial.print(F("checkStatusUpdate: "));
Serial.println("# UP_TO_DATE");
break;
case OUT_OF_DATE:
Serial.print(F("checkStatusUpdate: "));
Serial.println(F("# OUT_OF_DATE"));
break;
default:
Serial.print(F("checkStatusUpdate: "));
Serial.println("# UNKNOWN");
break;
}
// you can use get information about OTA with this function too
switch (magel.OTA.utility().firmwareIsUpToDate)
{
case UP_TO_DATE:
Serial.print(F("checkStatusUpdate: "));
Serial.println("# UP_TO_DATE");
break;
case OUT_OF_DATE:
Serial.print(F("checkStatusUpdate: "));
Serial.println(F("# OUT_OF_DATE"));
break;
default:
Serial.print(F("checkStatusUpdate: "));
Serial.println("# UNKNOWN");
break;
}
Serial.print("# checksum: ");
//boolean TRUE meaning information in magel.OTA.utility is already get data about OTA
Serial.println(magel.OTA.utility().isReadyOTA);
Serial.print("# firmware name: ");
Serial.println(magel.OTA.utility().firmwareName);
Serial.print("# firmware version: ");
Serial.println(magel.OTA.utility().firmwareVersion);
Serial.print("# firmware size: ");
Serial.println(magel.OTA.utility().firmwareTotalSize);
Serial.print("# checksumAlgorithm: ");
Serial.println(magel.OTA.utility().checksumAlgorithm);
Serial.print("# checksum: ");
Serial.println(magel.OTA.utility().checksum);
Serial.print("# Device information: ");
// if get "null" in value because this device never OTA before
Serial.println(magel.OTA.readDeviceInfo());
});
}