Skip to content

Commit

Permalink
version 1.0.1 enhance OTA feature and fix issue
Browse files Browse the repository at this point in the history
update library properties
  • Loading branch information
AIS-DeviceIntegration authored Dec 1, 2022
1 parent aec1df8 commit 5f76330
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 14 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# Magellan
Magellan IoT Platform Library/SDK for ESP32 and ESP82666
# MAGELLAN_SIM7600E
41 changes: 41 additions & 0 deletions examples/example_MQTT/credential/credential.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <Arduino.h>
#include <MAGELLAN_MQTT.h>
#include <MAGELLAN_WiFi_SETTING.h> //optional you can using wifi connect with your own functionn
#define REGEN_BY_CONDITION false
WiFiClient WiFi_client;
MAGELLAN_MQTT magel(WiFi_client);

String SSID = "set_your_ssid";
String PASS = "set_your_password";

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.begin(setting);

Serial.println("# Read Credential 1st time");
Serial.println("ThingIdentifier: "+ magel.credential.getThingIdentifier());
Serial.println("ThingSecret: "+ magel.credential.getThingSecret());
Serial.println("# Regenerate Credential");
magel.credential.regenerate(REGEN_BY_CONDITION); // if set true == will regenerate only credential activated, false regenerate without condition
Serial.println("# Read Credential 2nd time (After Regenerate)");
Serial.println("ThingIdentifier: "+ magel.credential.getThingIdentifier());
Serial.println("ThingSecret: "+ magel.credential.getThingSecret());
Serial.println("PreviousThingIdentifier: "+ magel.credential.getPreviousThingIdentifier());
Serial.println("PreviousThingSecret: "+ magel.credential.getPreviousThingSecret());
Serial.println("# Recovery Credential(swarp credential old to current)");
magel.credential.recovery();
Serial.println("ThingIdentifier: "+ magel.credential.getThingIdentifier());
Serial.println("ThingSecret: "+ magel.credential.getThingSecret());
Serial.println("PreviousThingIdentifier: "+ magel.credential.getPreviousThingIdentifier());
Serial.println("PreviousThingSecret: "+ magel.credential.getPreviousThingSecret());
}

void loop()
{
magel.loop();
}
3 changes: 1 addition & 2 deletions src/MAGELLAN_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ support esp32, esp8266
Author:(POC Device Magellan team)
Create Date: 25 April 2022.
Modified: 1 september 2022.
Released for private usage.
*/

#include "MAGELLAN_MQTT.h"
Expand Down Expand Up @@ -1216,4 +1215,4 @@ boolean MAGELLAN_MQTT::OnTheAir::start()
fileSys.writeFile("/prv_credentialFile.json", buff_crrnt.c_str()); // writePreviousCredential from current
return fileSys.writeFile("/credentialFile.json", buff_prev.c_str()); //writeCredential from previous

}
}
1 change: 0 additions & 1 deletion src/MAGELLAN_MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ support esp32, esp8266
Author:(POC Device Magellan team)
Create Date: 25 April 2022.
Modified: 1 september 2022.
Released for private usage.
*/
#ifndef MAGELLAN_MQTT_H
#define MAGELLAN_MQTT_H
Expand Down
49 changes: 45 additions & 4 deletions src/utils/MAGELLAN_MQTT_device_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ support esp32, esp8266
Author:(POC Device Magellan team)
Create Date: 25 April 2022.
Modified: 1 september 2022.
Released for private usage.
*/

#include "MAGELLAN_MQTT_device_core.h"
Expand Down Expand Up @@ -82,6 +81,8 @@ DynamicJsonDocument *Attribute_MQTT_core::docSensor = new DynamicJsonDocument(10

OTA_INFO MAGELLAN_MQTT_device_core::OTA_info;

boolean attemp_download_1 = false;
boolean attemp_download_2 = false;


String b2str(byte* payload, unsigned int length) // convert byte* to String
Expand Down Expand Up @@ -272,17 +273,20 @@ boolean pub_Download(unsigned int fw_chunkpart, size_t chunk_size)

boolean pub_UpdateProgress(String FOTA_State, String description)
{
delay(3000);
String topic = "api/v2/thing/"+attr.ext_Token+"/fotaupdateprogress/req/?FOTAState="+FOTA_State;
boolean Pub_status = false;
if(description.indexOf("description") != -1)
{
Pub_status = attr.mqtt_client->publish(topic.c_str(), description.c_str());
Pub_status = attr.mqtt_client->publish(topic.c_str(), description.c_str());
Serial.println(F("-------------------------------"));
Serial.println("# STATE OTA Description: "+ description);
Serial.println(F("-------------------------------"));
}
else{
Pub_status = attr.mqtt_client->publish(topic.c_str(), "");
Pub_status = attr.mqtt_client->publish(topic.c_str(), "");
}

String Debug = (Pub_status == true)? "Success" : "Failure";
Expand Down Expand Up @@ -657,6 +661,8 @@ void updateFirmware(uint8_t *data, size_t len)

String fw_infoInFIleSys;
JsonObject fw_last = configOTAFile.readObjectLastedOTA();
int bufferFW_size = fw_last["sizefirmware"];

fw_last.remove("namefirmware");
fw_last.remove("sizefirmware");
fw_last.remove("checksumAlgorithm");
Expand All @@ -665,11 +671,21 @@ void updateFirmware(uint8_t *data, size_t len)
fw_last.remove("versionfirmware");

serializeJson(fw_last, fw_infoInFIleSys);

if(fw_infoInFIleSys.indexOf("null") == -1)
{
pubClientConfig(fw_infoInFIleSys);
}
else if ((bufferFW_v.length() > 4 || bufferFW_v.indexOf("null") == -1) && (fw_infoInFIleSys.indexOf("null") != -1)) // handle if fw version !null but some key value found null is still pub client config
{
pubClientConfig(fw_infoInFIleSys);
}

else if ((bufferFW_v.indexOf("null") != -1) && (bufferFW_size > 0))
{
pubClientConfig(fw_infoInFIleSys);
}


// write new data only success OTA

// Serial.println("#Debug: "+ configOTAFile.readConfigFileOTA());
Expand All @@ -694,6 +710,7 @@ void updateFirmware(uint8_t *data, size_t len)
configOTAFile.saveSuccessOrFail("fail");
// readFS_pubClientConfig();
}
delay(5000);
ESP.restart();
}

Expand All @@ -715,6 +732,8 @@ void hook_FW_download(String topic, uint8_t* payload, unsigned int length)
attr.prv_cb_timeout_millis = millis();
updateFirmware(payload, length);
attr.fw_count_chunk++;
attemp_download_1 = false;
attemp_download_2 = false;
pub_Download(attr.fw_count_chunk, attr.chunk_size);

if(!attr.inProcessOTA)
Expand Down Expand Up @@ -2325,6 +2344,7 @@ void MAGELLAN_MQTT_device_core::activeOTA(size_t chunk_size, boolean useChecksum
else{
String fw_infoInFIleSys;
JsonObject fw_last = configOTAFile.readObjectLastedOTA();
int bufferFW_size = fw_last["sizefirmware"];
fw_last.remove("namefirmware");
fw_last.remove("sizefirmware");
fw_last.remove("checksumAlgorithm");
Expand All @@ -2337,6 +2357,14 @@ void MAGELLAN_MQTT_device_core::activeOTA(size_t chunk_size, boolean useChecksum
{
this->reportClientConfig(fw_infoInFIleSys);
}
else if ((bufferFW_v.length() > 4 || bufferFW_v.indexOf("null") == -1) && (fw_infoInFIleSys.indexOf("null") != -1)) // handle if fw version !null but some key value found null is still pub client config
{
this->reportClientConfig(fw_infoInFIleSys);
}
else if ((bufferFW_v.indexOf("null") != -1) && (bufferFW_size > 0))
{
this->reportClientConfig(fw_infoInFIleSys);
}
}

}
Expand All @@ -2352,17 +2380,30 @@ void MAGELLAN_MQTT_device_core::setChunkSize(size_t Chunksize)
}

// unsigned long prv_cb_timeout_millis = 0;

void checkTimeoutReq_fw_download()
{
if(attr.checkTimeout_request_download_fw)
{
unsigned long differentTime = millis() - attr.prv_cb_timeout_millis;
if(differentTime > 60000 && !attemp_download_1)
{
Serial.println("#Attemp resume download 1 after checktimeout 1 minute");
pub_Download(attr.fw_count_chunk, attr.chunk_size);
attemp_download_1 = true;
}
if(differentTime > 120000 && !attemp_download_2)
{
Serial.println("#Attemp resume download 2 after checktimeout 2 minute");
pub_Download(attr.fw_count_chunk, attr.chunk_size);
attemp_download_2 = true;
}
if(differentTime > attr.timeout_req_download_fw)
{
pub_UpdateProgress("FAILED","{\"errordescription\":\"Timeout from request firmware download (version. "+ MAGELLAN_MQTT_device_core::OTA_info.firmwareVersion+")\"}");
configOTAFile.saveSuccessOrFail("fail");
// readFS_pubClientConfig();
Serial.println(F("#device must restart timeout from request firmware dowload 2 minute"));
Serial.println("#device must restart timeout from request firmware dowload "+String(attr.timeout_req_download_fw/60000)+" minute");
delay(5000);
ESP.restart();
}
Expand Down Expand Up @@ -2626,4 +2667,4 @@ boolean MAGELLAN_MQTT_device_core::unregisterResponseHeartbeat(int format){
Serial.println("# Response type: "+ respType);
return Sub_status;
}
////////////////// Unsub //////////
////////////////// Unsub //////////
5 changes: 2 additions & 3 deletions src/utils/MAGELLAN_MQTT_device_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ support esp8266, esp32
Author:(POC Device Magellan team)
Create Date: 25 April 2022.
Modified: 21 september 2022.
Released for private usage.
*/
#ifndef MAGELLAN_MQTT_DEVICE_CORE_h
#define MAGELLAN_MQTT_DEVICE_CORE_h
Expand All @@ -45,8 +44,8 @@ Released for private usage.
#define _default_OverBufferSize defaultOTABuffer
#endif
#include "Attribute_MQTT_core.h"
//version dev v1.0.0
#define lib_version "v1.0.0"
//version dev v1.1.1
#define lib_version "v1.0.1"
//Response Payload format some function can be set.
#define PLAINTEXT 0 //Plaintext
#define JSON 1 //Json
Expand Down
2 changes: 1 addition & 1 deletion src/utils/manageConfigOTAFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ boolean manageConfigOTAFile::saveSuccessOrFail(String stateOTA)
{
JsonObject bufferProfile = readObjectConfigFileOTA();

StaticJsonDocument<256> docsBuffer = bufferProfile;
StaticJsonDocument<512> docsBuffer = bufferProfile;
bufferProfile.remove("Code");
docsBuffer["status"] = stateOTA.c_str();
String ProfileUpdate;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/manageConfigOTAFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class manageConfigOTAFile
private:
const char* configOTAFilePath = "/configOTAFile.json"; //save every event init fail success
const char* lastedOTAPath = "/lastedOTAFile.json"; //save only success
StaticJsonDocument<256> OTAdoc;
StaticJsonDocument<512> OTAdoc;
public:
void beginFileSystem();
boolean checkFileOTA();
Expand Down

0 comments on commit 5f76330

Please sign in to comment.