Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TINY_GSM_MODEM_SARAR4 // My modem is SIM7000
- #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb or full HTTP response wont be received
- #define powerPin SARA_ENABLE
- #define enablePin SARA_TX_ENABLE
- #define R4XX
- //static Sodaq_R4X r4x;
- //static Sodaq_SARA_R4XX_OnOff saraR4xxOnOff;
- bool modemstat = false;
- //uint32_t getNow();
- const int urat = 7; // LTE Cat 1 dual mode not possible
- const int cid = 1 ; // cell id set to 1
- const int mnoProfile = 31; // Deutsche Telekom
- const int bandmask = 524420;
- const int bandmaskRATind = 0;
- const char * FOperator = "20404";
- const char* APN = "";
- const char* gprsUser = "";
- const char* gprsPass = "";
- #define MODEM_STREAM Serial1
- #define MODEMBAUD 115200
- #include <TinyGsmClient.h> // https://github.com/vshymanskyy/TinyGSM
- #include <SDU.h> // prepends to this sketch a 'second stage SD bootloader'
- #include <Arduino.h>
- //#include "Sodaq_R4X.h"
- #include "Sodaq_wdt.h"
- #include "SD.h"
- #define SerialAT Serial1 // Set serial for AT commands (to the module)
- TinyGsm modem(SerialAT); // sets GSM modem to serial1
- TinyGsmClient GSMclient(modem); // client for GSM
- String fwImageURL = "/update.bin"; //example of your .bin location MUST BE HTTP SITE!!! NOT HTTPS
- const int port = 4402; // HTTP = 80
- const char server[] = "78.47.186.254"; // head server address (MUST BE HTTP!)
- long contentLength; // How many bytes of data the .bin is
- bool isValidContentType = false; // checks if the .bin is valid
- /*---------------------------SETUP-----------------------------*/
- void setup() {
- Serial.begin(9600); // Start serial monitor at a baud rate of 9600
- SerialUSB.println("\n------STARTING-UP------");
- init_GSM();
- GSMOTA();
- }
- void init_GSM() {
- #ifdef powerPin
- pinMode(powerPin, OUTPUT); // Turn the nb-iot module on
- digitalWrite(powerPin, HIGH);
- #endif
- #ifdef enablePin
- pinMode(enablePin, OUTPUT); // Set state to active
- digitalWrite(enablePin, HIGH);
- #endif // enablePin
- #if defined(R4XX)
- pinMode(SARA_R4XX_TOGGLE, OUTPUT);
- digitalWrite(SARA_R4XX_TOGGLE, LOW);
- delay(2000);
- pinMode(SARA_R4XX_TOGGLE, INPUT);
- #endif
- MODEM_STREAM.begin(MODEMBAUD); debug2("modem stream initialized");
- while (1) {
- MODEM_STREAM.write("AT\r\n"); debug2("modem stream written");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString();
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- } debug2("modem has replied OK");
- sodaq_wdt_reset();
- bool success;
- debug2("pins updated");
- debug2("attempting to connect");
- /*while(1) {
- MODEM_STREAM.write("AT+CMEE=2\r\n"); debug2("AT CMEE written");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString();
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }*/ debug2("modem has replied OK");
- // while(1) {
- // String netstat = "AT+UGPIOC="+ String(NETWORK_STATUS_GPIO_ID) + ",255\r\n";
- // const char * brx = netstat.c_str();
- // MODEM_STREAM.write(brx); debug2("NETWORK LED written");
- // sodaq_wdt_safe_delay(650);
- // String r;
- // if (MODEM_STREAM.available()) {
- // r = MODEM_STREAM.readString();
- // if ( r.indexOf("OK") >= 0 ) break;;
- // }
- // } debug2("modem has replied OK");
- //
- while (1) {
- MODEM_STREAM.write("ATE0\r\n"); debug2("ATE0 written");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString();
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- MODEM_STREAM.write("AT+CFUN?\r\n"); debug2("AT CFUN written");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- MODEM_STREAM.write("AT+COPS=2\r\n"); debug2("AT COPS written");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- String umnoprof = "AT+UMNOPROF=" + String(mnoProfile) + "\r\n";
- const char * brx = umnoprof.c_str();
- MODEM_STREAM.write(brx); debug2("AT mnoProfile written : " + umnoprof);
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- String urats = "AT+URAT=" + String(urat) + "\r\n";
- const char * brx = urats.c_str();
- MODEM_STREAM.write(brx); debug2("AT urat written : " + urats);
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- String bandm = "AT+UBANDMASK=" + String(bandmaskRATind) + "," + String(bandmask) + "\r\n";
- const char * brx = bandm.c_str();
- MODEM_STREAM.write(brx); debug2("AT bandmask written : " + bandm);
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- MODEM_STREAM.write("AT+COPS=0,2\r\n"); debug2("AT COPS (again) written");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- MODEM_STREAM.write("AT+CGDCONT?\r\n"); debug2("AT APN checked");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- while (1) {
- MODEM_STREAM.write("AT+CSQ\r\n"); debug2("AT signal quality checked");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- }
- int i = 0;
- while (1) {
- i = i + 1;
- MODEM_STREAM.write("AT+CGACT=1,1\r\n"); debug2("AT GPRS attached");
- sodaq_wdt_safe_delay(650);
- String r;
- if (MODEM_STREAM.available()) {
- r = MODEM_STREAM.readString(); debug2(r);
- if ( r.indexOf("OK") >= 0 ) break;;
- }
- if (i > 10) break;
- }
- debug2("successful in APN connect ....");
- }
- void connectClient() { // Connect to your client server, for example
- while (!!!GSMclient.connect(server, port)) {
- Serial.print("*");
- }
- }
- /*-----------------------------LOOP-------------------------------*/
- void loop() {
- }
- /*-------------------------GET-UPDATE-----------------------------*/
- // used to extract header value from headers for ota update
- String getHeaderValue(String header, String headerName) {
- return header.substring(strlen(headerName.c_str()));
- }
- void GSMOTA() {
- const uint32_t clientReadTimeout = 5000;
- SerialUSB.println("\n♦ ♦ UPDATE AVALIBLE ♦ ♦\n");
- connectClient(); // Connect to your client
- if (GSMclient.connect(server, port)) {
- SerialUSB.println("Fetching bin file at: " + String(fwImageURL)); // tells where its going to get the .bin and the name its looking for
- // Get the contents of the bin file
- GSMclient.print(String("GET ") + fwImageURL + " HTTP/1.1\r\n" +
- "Host: " + String(server) + "\r\n" +
- "Cache-Control: no-cache\r\n" +
- "Connection: close\r\n\r\n");
- unsigned long timeout = millis();
- while (GSMclient.available() == 0) {
- if (millis() - timeout > 5000) {
- SerialUSB.println("Client Timeout !");
- GSMclient.stop();
- return;
- }
- }
- // Once the response is available start reading reply
- while (GSMclient.available()) {
- // read line till /n
- String line = GSMclient.readStringUntil('\n');
- // remove space, to check if the line is end of headers
- line.trim();
- // if the the line is empty, this is end of headers
- // break the while and feed the remaining client to the Update.writeStream();
- if (!line.length()) {
- // headers ended
- break; // and get the OTA update started
- }
- debug2(line);
- // Check if the HTTP Response is 200 if not break and Exit Update
- if (line.startsWith("HTTP/1.1")) {
- if (line.indexOf("200") < 0) {
- SerialUSB.println("Got a non 200 status code from server. Exiting OTA Update.");
- break;
- }
- }
- // extracting headers starting with content length
- if (line.startsWith("Content-Length: ")) {
- contentLength = atol((getHeaderValue(line, "Content-Length: ")).c_str());
- SerialUSB.println("Got " + String(contentLength) + " bytes from server");
- }
- // Next, the content type
- if (line.startsWith("Content-Type: ")) {
- String contentType = getHeaderValue(line, "Content-Type: ");
- SerialUSB.println("Got " + contentType + " payload.");
- if (contentType == "application/octet-stream") {
- isValidContentType = true;
- }
- }
- }
- } else {
- SerialUSB.println("Connection to " + String(server) + " failed. Please check your setup");
- }
- // Check what is the contentLength and if content type is `application/octet-stream`
- SerialUSB.println("contentLength : " + String(contentLength) + ", isValidContentType : " + String(isValidContentType));
- // check contentLength and content type
- if (contentLength && isValidContentType) {
- uint32_t readLength = 0;
- if(!SD.begin(10)) { debug2("SD FAILURE ::: ");
- return;
- }
- File file = SD.open("UPDATE.bin", O_CREAT | O_WRITE);
- if (!file) {
- GSMclient.stop(); debug2("Could not create bin file. Can't continue with update.");
- return;
- }
- uint32_t clientReadStartTime = millis();
- SerialUSB.println(F("Reading response data"));
- clientReadStartTime = millis();
- printPercent(readLength, contentLength);
- // uint32_t readLength = 0;
- while (readLength < contentLength && GSMclient.connected() && millis() - clientReadStartTime < clientReadTimeout) {
- while (GSMclient.available()) {
- uint8_t c = GSMclient.read();
- SerialUSB.print((c)); // Uncomment this to show data
- file.write(c);
- readLength++;
- if (readLength % (contentLength / 13) == 0) {
- printPercent(readLength, contentLength);
- }
- clientReadStartTime = millis();
- }
- }
- printPercent(readLength, contentLength);
- file.close();
- GSMclient.stop();
- #ifdef __AVR__
- wdt_enable(WDTO_15MS);
- while (true);
- #else
- NVIC_SystemReset();
- #endif
- }
- // Check if there is enough to OTA Update
- }
- void printPercent(uint32_t readLength, uint32_t contentLength) {
- // If we know the total length
- if (contentLength != (uint32_t) - 1) {
- SerialUSB.print("\r ");
- SerialUSB.print((100.0 * readLength) / contentLength);
- SerialUSB.print('%');
- } else {
- SerialUSB.println(readLength);
- }
- }
- void debug2(String s) {
- SerialUSB.println(s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement