Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
update.cpp
Go to the documentation of this file.
1
13#include <Arduino.h>
14#include <WiFi.h>
15#include <WiFiMulti.h>
16#include <HTTPClient.h>
17#include <HTTPUpdate.h>
18#include <tinyxml2.h>
19#include <MQTTMailer.hpp>
20
21#include <MQTTMailer.hpp>
22
23using namespace tinyxml2;
24using namespace std;
25
30#define OTA_SERVER "192.168.50.10"
31extern const bool USB_DEBUG;
33
44
45 //Serial.println("\tchecking available firmware version");
46 string fw_version_url = string("http://") + string(OTA_SERVER) + string("/current_version.txt");
47 if(USB_DEBUG) Serial.printf("\tchecking new fw version @ %s\n", fw_version_url.c_str());
48
50 httpClient.begin( fw_version_url.c_str() );
51
52 int httpCode = httpClient.GET();
53
54 if ( httpCode == 200 ) {
55 string fw_filename = httpClient.getString().c_str();
56 string fw_version = fw_filename.substr(fw_filename.length()-10, 6).c_str();
57
58 if(fw_filename.length() < 10) return "failure";
59
60 if(USB_DEBUG){
61 Serial.print("\tgot version string: \"");
62 Serial.print(fw_version.c_str());
63 Serial.println("\"");
64 Serial.printf("\tfrom: \'%s\'\n", fw_filename.c_str());
65 }
66
67 int version_major = stoi(fw_filename.substr(fw_filename.length()-9, 1));
68 int version_minor = stoi(fw_filename.substr(fw_filename.length()-7, 1));
69 int version_patch = stoi(fw_filename.substr(fw_filename.length()-5, 1));
70
71 MQTTMailer instance = MQTTMailer::getInstance();
72 std::string topic = "datagator/ota_status/" + std::string(WiFi.macAddress().c_str());
73 std::string msg = "{\"STATUS_MSG\": \"version\", \"SERVER_FW_VERSION\": \"" + fw_version +
74 "\", \"DEVICE_FW_VERSION\": \"v" + to_string(VERSION_MAJOR) +
75 "." + to_string(VERSION_MINOR) +
76 "." + to_string(VERSION_PATCH) +
77 "\"}";
78 instance.mailMessage(&mqtt_client, topic, msg);
79
80
81 if(USB_DEBUG) Serial.printf("\tv_maj: %d, v_min: %d, v_patch: %d\n", version_major, version_minor, version_patch);
82
83 // if version doesn't match, update
84 if(version_minor != VERSION_MINOR || version_major != VERSION_MAJOR || version_patch != VERSION_PATCH){
85 return fw_filename;
86 }else{
87 return "";
88 }
89
90
91 }else{
92 if(USB_DEBUG){
93 Serial.print("\thttp connection failed with code ");
94 Serial.print(httpCode);
95 Serial.print(": ");
96 Serial.println(httpClient.errorToString(httpCode));
97 }
98 return "";
99 }
100}
101
111void update_progress(int cur, int total){
112 if(USB_DEBUG) Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total);
113}
114
122 if(USB_DEBUG) Serial.println("CALLBACK: HTTP update process started");
123 MQTTMailer instance = MQTTMailer::getInstance();
124 std::string topic = "datagator/ota_status/" + std::string(WiFi.macAddress().c_str());
125 std::string msg = "{\"STATUS_MSG\": \"started\"}";
126 instance.mailMessage(&mqtt_client, topic, msg);
127
128}
129
137 if(USB_DEBUG) Serial.println("update finished");
138 MQTTMailer instance = MQTTMailer::getInstance();
139 std::string topic = "datagator/ota_status/" + std::string(WiFi.macAddress().c_str());
140 std::string msg = "{\"STATUS_MSG\": \"finished\"}";
141 instance.mailMessage(&mqtt_client, topic, msg);
142
143}
144
145
153void update_error(int err) {
154 if(USB_DEBUG) Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err);
155 MQTTMailer instance = MQTTMailer::getInstance();
156 std::string topic = "datagator/ota/" + std::string(WiFi.macAddress().c_str());
157 std::string msg = "{\"STATUS_MSG\": \"error\"}";
158 instance.mailMessage(&mqtt_client, topic, msg);
159}
160
180
181 if(file_name != ""){
182
183 for(int i = 0; i < 3; i++){
184 // wait for WiFi connection
185 if(USB_DEBUG) Serial.println("starting update attempt");
186
188 if(USB_DEBUG) Serial.println("client created");
189 httpUpdate.onStart(update_started);
191 httpUpdate.onProgress(update_progress);
192 httpUpdate.onError(update_error);
193
194 string fw_url = string("http://") + string(OTA_SERVER) + string("/") + file_name;
195 if(USB_DEBUG) Serial.println(fw_url.c_str());
196
198 }
199 }
200}
Converts MQTTMail objects into viable MQTT messages so that they can be published.
Definition MQTTMailer.hpp:60
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31
const bool USB_DEBUG
USB serial debugging enabled.
Definition main.cpp:62
void update_finished()
Log update process finished.
Definition update.cpp:136
void update_progress(int cur, int total)
Prints update process progress to the serial interface.
Definition update.cpp:111
void update_error(int err)
Log update process errored out.
Definition update.cpp:153
PubSubClient mqtt_client
MQTT client object for logging.
string new_firmware_version_available()
Check if server has different firmware version.
Definition update.cpp:43
void update_started()
Send status message to the MQTT broker that the update process started.
Definition update.cpp:121
void attempt_update()
Attempts OTA firmware update from server.
Definition update.cpp:178
#define OTA_SERVER
Over-The-Air update server address.
Definition update.cpp:30