OTAClass Class
OTAClass Class
Description
A class used for managing OTA update service on Ameba.
Syntax
class OTAClass
Members
Public Constructors | |
The public constructor should not be used as this class is intended to be a singleton class. Access member functions using the object instance named OTA. |
Public Methods | |
OTAClass::beginArduinoMdnsService | Start the MDNS service for OTA |
OTAClass::endArduinoMdnsService | Stop the MDNS service for OTA |
OTAClass::beginLocal | Start the local OTA service |
OTAClass::setOtaAddress | Set the flash address to store the OTA image |
OTAClass::setRecoverPin | Set the recovery pin which will allow booting from the old image |
OTAClass::beginArduinoMdnsService
Description
Start the MDNS service for OTA.
Syntax
int beginArduinoMdnsService(char *device_name, uint16_t port);
Parameters
device_name: device name to show on MDNS service
port: port number for the OTA service
Returns
0: if MDNS service init success
-1: if MDNS service init fail
Example Code
Example: ota_basic
This example shows how to update the Ameba image via Arduino IDE.
(1) Download this sketch via USB for the first time
(2) Reset Ameba. It will connect to the network, enable mDNS, and wait for the client connected.
(3) Modify this sketch. (Ex. the version number)
(4) Select network port in “Tools” -> “Port” -> “Arduino at 192.168.1.238 (Ameba RTL8195A)”. Then upload.
(5) After download finish, Ameba would reboot. You can see the changes have been updated.
#include <WiFi.h> #include <OTA.h> char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password #define MY_VERSION_NUMBER 1 #define OTA_PORT 5000 #if defined(BOARD_RTL8195A) #define RECOVER_PIN 18 #elif defined(BOARD_RTL8710) #define RECOVER_PIN 17 #else #define RECOVER_PIN 18 #endif void setup() { printf("This is version %d\r\n\r\n", MY_VERSION_NUMBER); printf("Try to connect to %s\r\n", ssid); while (WiFi.begin(ssid, pass) != WL_CONNECTED) { printf("Failed. Wait 1s and retry...\r\n"); delay(1000); } printf("Connected to %s\r\n", ssid); // These setting only needed at first time download from usb. And it doesn't needed at next OTA. #if MY_VERSION_NUMBER == 1 // This set the flash address that store the OTA image. Skip this setting would use default setting which is DEFAULT_OTA_ADDRESS OTA.setOtaAddress(DEFAULT_OTA_ADDRESS); // This set the recover pin. Boot device with pull up this pin (Eq. connect pin to 3.3V) would make device boot from version 1 OTA.setRecoverPin(RECOVER_PIN); #endif // Broadcast mDNS service at OTA_PORT that makes Arduino IDE find Ameba device OTA.beginArduinoMdnsService("MyAmeba", OTA_PORT); // Listen at OTA_PORT and wait for client (Eq. Arduino IDE). Client would send OTA image and make a update. if (OTA.beginLocal(OTA_PORT) < 0) { printf("OTA failed!!\r\n"); } } void loop() { delay(1000); }
Notes and Warnings
Configures and registers the MDNS service required for the Arduino IDE to discover and recognize Ameba OTA.
OTAClass::endArduinoMdnsService
Description
Stop the MDNS service for OTA.
Syntax
int endArduinoMdnsService(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
NA
Notes and Warnings
NA
OTAClass::beginLocal
Description
Start the local OTA service.
Syntax
int beginLocal(uint16_t port, bool reboot_when_success);
Parameters
port: port number for the OTA service
reboot_when_successs: reboot after OTA update is done, default = true
Returns
0: if OTA success
-1: if OTA fail
Example Code
Example: ota_basic
The details of the code are provided in the previous section of OTAClass::beginArduinoMdnsService.
Notes and Warnings
NA
OTAClass::setOtaAddress
Description
Set the flash address to store the OTA image.
Syntax
int setOtaAddress(uint32_t address);
Parameters
address: Flash address for storing the OTA image
Returns
The function returns nothing.
Example Code
Example: ota_basic
The details of the code are provided in the previous section of OTAClass::beginArduinoMdnsService.
Notes and Warnings
Default OTA address = 0x80000.
OTAClass::setRecoverPin
Description
Set the recovery pin which will allow booting from the old image.
Syntax
Int setRecoverPin(uint32_t pin1, uint32_t pin2);
Parameters
pin1: PIN for recovery
pin2: PIN for recovery, default = unused
Returns
The function returns nothing.
Example Code
Example: ota_basic
Notes and Warnings
Connect recovery pin to 3.3V to boot from the old image.