Advertisement
seans

Arduino OTA test.

Oct 4th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.96 KB | None | 0 0
  1.  
  2. #define TINY_GSM_MODEM_SARAR4                           // My modem is SIM7000
  3. #define TINY_GSM_RX_BUFFER   1024                 // Set RX buffer to 1Kb or full HTTP response wont be received
  4. #define powerPin SARA_ENABLE
  5. #define enablePin SARA_TX_ENABLE
  6. #define R4XX
  7. //static Sodaq_R4X r4x;
  8. //static Sodaq_SARA_R4XX_OnOff saraR4xxOnOff;
  9.  
  10. bool modemstat = false;
  11. //uint32_t getNow();
  12. const int urat = 7;                                                    // LTE Cat 1 dual mode not possible
  13. const int cid = 1 ;                                                   // cell id set to 1
  14. const int mnoProfile = 31;                                                // Deutsche Telekom
  15. const int bandmask = 524420;
  16. const int bandmaskRATind = 0;
  17. const char * FOperator = "20404";
  18.  
  19. const char* APN = "";
  20. const char* gprsUser = "";
  21. const char* gprsPass = "";
  22. #define MODEM_STREAM Serial1
  23. #define MODEMBAUD 115200
  24. #include <TinyGsmClient.h>                        // https://github.com/vshymanskyy/TinyGSM
  25. #include <SDU.h> // prepends to this sketch a 'second stage SD bootloader'
  26.  
  27. #include <Arduino.h>
  28. //#include "Sodaq_R4X.h"
  29. #include "Sodaq_wdt.h"
  30. #include "SD.h"
  31.  
  32.  
  33. #define SerialAT  Serial1                         // Set serial for AT commands (to the module)
  34. TinyGsm modem(SerialAT);                          // sets GSM modem to serial1
  35. TinyGsmClient GSMclient(modem);                   // client for GSM
  36.  
  37.  
  38.  
  39. String fwImageURL     = "/update.bin";   //example of your .bin location MUST BE HTTP SITE!!! NOT HTTPS
  40. const int port        = 4402;                               // HTTP = 80
  41. const char server[]   = "78.47.186.254";                     //  head server address (MUST BE HTTP!)
  42.  
  43. long contentLength;                                       // How many bytes of data the .bin is
  44. bool isValidContentType = false;                          // checks if the .bin is valid
  45.  
  46.  
  47. /*---------------------------SETUP-----------------------------*/
  48.  
  49. void setup() {
  50.   Serial.begin(9600);                                         // Start serial monitor at a baud rate of 9600
  51.   SerialUSB.println("\n------STARTING-UP------");
  52.   init_GSM();
  53.   GSMOTA();
  54.  
  55. }
  56.  
  57.  
  58. void init_GSM() {
  59.  
  60.  
  61.  
  62.  
  63.  
  64. #ifdef powerPin
  65.   pinMode(powerPin, OUTPUT);                                              // Turn the nb-iot module on
  66.   digitalWrite(powerPin, HIGH);
  67. #endif
  68.  
  69. #ifdef enablePin
  70.   pinMode(enablePin, OUTPUT);                                             // Set state to active
  71.   digitalWrite(enablePin, HIGH);
  72. #endif // enablePin
  73.  
  74. #if defined(R4XX)
  75.   pinMode(SARA_R4XX_TOGGLE, OUTPUT);
  76.   digitalWrite(SARA_R4XX_TOGGLE, LOW);
  77.   delay(2000);
  78.   pinMode(SARA_R4XX_TOGGLE, INPUT);
  79. #endif
  80.  
  81.   MODEM_STREAM.begin(MODEMBAUD);                                            debug2("modem stream initialized");
  82.  
  83.   while (1) {
  84.     MODEM_STREAM.write("AT\r\n");                                         debug2("modem stream written");
  85.     sodaq_wdt_safe_delay(650);
  86.     String r;
  87.     if (MODEM_STREAM.available())   {
  88.       r = MODEM_STREAM.readString();
  89.       if ( r.indexOf("OK") >= 0 ) break;;
  90.     }
  91.   }                                                         debug2("modem has replied OK");
  92.  
  93.   sodaq_wdt_reset();
  94.   bool success;
  95.  
  96.  
  97.  
  98.   debug2("pins updated");
  99.   debug2("attempting to connect");
  100.  
  101.  
  102.   /*while(1) {
  103.         MODEM_STREAM.write("AT+CMEE=2\r\n");                                      debug2("AT CMEE written");
  104.         sodaq_wdt_safe_delay(650);
  105.         String r;
  106.         if (MODEM_STREAM.available())   {
  107.           r = MODEM_STREAM.readString();
  108.           if ( r.indexOf("OK") >= 0 ) break;;
  109.     }
  110.     }*/                                                         debug2("modem has replied OK");
  111.  
  112.   //  while(1) {
  113.   //    String netstat = "AT+UGPIOC="+ String(NETWORK_STATUS_GPIO_ID) + ",255\r\n";
  114.   //    const char * brx = netstat.c_str();
  115.   //    MODEM_STREAM.write(brx);                                            debug2("NETWORK LED written");
  116.   //    sodaq_wdt_safe_delay(650);
  117.   //    String r;
  118.   //    if (MODEM_STREAM.available())   {
  119.   //      r = MODEM_STREAM.readString();
  120.   //      if ( r.indexOf("OK") >= 0 ) break;;
  121.   //    }
  122.   //  }                                                         debug2("modem has replied OK");
  123.   //
  124.  
  125.   while (1) {
  126.     MODEM_STREAM.write("ATE0\r\n");                                         debug2("ATE0 written");
  127.     sodaq_wdt_safe_delay(650);
  128.     String r;
  129.     if (MODEM_STREAM.available())   {
  130.       r = MODEM_STREAM.readString();
  131.       if ( r.indexOf("OK") >= 0 ) break;;
  132.     }
  133.   }
  134.  
  135.   while (1) {
  136.     MODEM_STREAM.write("AT+CFUN?\r\n");                                       debug2("AT CFUN written");
  137.     sodaq_wdt_safe_delay(650);
  138.     String r;
  139.     if (MODEM_STREAM.available())   {
  140.       r = MODEM_STREAM.readString();                                        debug2(r);
  141.       if ( r.indexOf("OK") >= 0 ) break;;
  142.     }
  143.   }
  144.  
  145.   while (1) {
  146.     MODEM_STREAM.write("AT+COPS=2\r\n");                                      debug2("AT COPS written");
  147.     sodaq_wdt_safe_delay(650);
  148.     String r;
  149.     if (MODEM_STREAM.available())   {
  150.       r = MODEM_STREAM.readString();                                        debug2(r);
  151.       if ( r.indexOf("OK") >= 0 ) break;;
  152.     }
  153.   }
  154.  
  155.  
  156.   while (1) {
  157.     String umnoprof = "AT+UMNOPROF=" + String(mnoProfile) + "\r\n";
  158.     const char * brx = umnoprof.c_str();
  159.     MODEM_STREAM.write(brx);                                            debug2("AT mnoProfile written : " + umnoprof);
  160.     sodaq_wdt_safe_delay(650);
  161.     String r;
  162.     if (MODEM_STREAM.available())   {
  163.       r = MODEM_STREAM.readString();                                        debug2(r);
  164.       if ( r.indexOf("OK") >= 0 ) break;;
  165.     }
  166.   }
  167.  
  168.  
  169.   while (1) {
  170.     String urats = "AT+URAT=" + String(urat) + "\r\n";
  171.     const char * brx = urats.c_str();
  172.     MODEM_STREAM.write(brx);                                            debug2("AT urat written : " + urats);
  173.     sodaq_wdt_safe_delay(650);
  174.     String r;
  175.     if (MODEM_STREAM.available())   {
  176.       r = MODEM_STREAM.readString();                                        debug2(r);
  177.       if ( r.indexOf("OK") >= 0 ) break;;
  178.     }
  179.   }
  180.  
  181.  
  182.   while (1) {
  183.     String bandm = "AT+UBANDMASK=" + String(bandmaskRATind) + "," + String(bandmask) + "\r\n";
  184.     const char * brx = bandm.c_str();
  185.     MODEM_STREAM.write(brx);                                            debug2("AT bandmask written : " + bandm);
  186.     sodaq_wdt_safe_delay(650);
  187.     String r;
  188.     if (MODEM_STREAM.available())   {
  189.       r = MODEM_STREAM.readString();                                        debug2(r);
  190.       if ( r.indexOf("OK") >= 0 ) break;;
  191.     }
  192.   }
  193.  
  194.   while (1) {
  195.     MODEM_STREAM.write("AT+COPS=0,2\r\n");                                      debug2("AT COPS (again) written");
  196.     sodaq_wdt_safe_delay(650);
  197.     String r;
  198.     if (MODEM_STREAM.available())   {
  199.       r = MODEM_STREAM.readString();                                        debug2(r);
  200.       if ( r.indexOf("OK") >= 0 ) break;;
  201.     }
  202.   }
  203.  
  204.  
  205.   while (1) {
  206.     MODEM_STREAM.write("AT+CGDCONT?\r\n");                                      debug2("AT APN checked");
  207.     sodaq_wdt_safe_delay(650);
  208.     String r;
  209.     if (MODEM_STREAM.available())   {
  210.       r = MODEM_STREAM.readString();                                        debug2(r);
  211.       if ( r.indexOf("OK") >= 0 ) break;;
  212.     }
  213.   }
  214.  
  215.  
  216.   while (1) {
  217.     MODEM_STREAM.write("AT+CSQ\r\n");                                       debug2("AT signal quality checked");
  218.     sodaq_wdt_safe_delay(650);
  219.     String r;
  220.     if (MODEM_STREAM.available())   {
  221.       r = MODEM_STREAM.readString();                                        debug2(r);
  222.       if ( r.indexOf("OK") >= 0 ) break;;
  223.     }
  224.   }
  225.  
  226.   int i = 0;
  227.   while (1) {
  228.     i = i + 1;
  229.     MODEM_STREAM.write("AT+CGACT=1,1\r\n");                                     debug2("AT GPRS attached");
  230.     sodaq_wdt_safe_delay(650);
  231.     String r;
  232.     if (MODEM_STREAM.available())   {
  233.       r = MODEM_STREAM.readString();                                        debug2(r);
  234.       if ( r.indexOf("OK") >= 0 ) break;;
  235.     }
  236.  
  237.     if (i > 10) break;
  238.   }
  239.  
  240.   debug2("successful in APN connect ....");
  241. }
  242.  
  243. void connectClient() {    // Connect to your client server, for example
  244.   while (!!!GSMclient.connect(server, port)) {
  245.     Serial.print("*");
  246.   }
  247. }
  248.  
  249. /*-----------------------------LOOP-------------------------------*/
  250.  
  251. void loop() {
  252.  
  253. }
  254.  
  255. /*-------------------------GET-UPDATE-----------------------------*/
  256.  
  257. // used to extract header value from headers for ota update
  258. String getHeaderValue(String header, String headerName) {
  259.   return header.substring(strlen(headerName.c_str()));
  260. }
  261.  
  262.  
  263. void GSMOTA() {
  264.  
  265.   const uint32_t clientReadTimeout = 5000;
  266.   SerialUSB.println("\n♦ ♦ UPDATE AVALIBLE ♦ ♦\n");
  267.   connectClient();                                // Connect to your client
  268.   if (GSMclient.connect(server, port)) {
  269.     SerialUSB.println("Fetching bin file at: " + String(fwImageURL));    // tells where its going to get the .bin and the name its looking for
  270.  
  271.     // Get the contents of the bin file
  272.     GSMclient.print(String("GET ") + fwImageURL + " HTTP/1.1\r\n" +
  273.                     "Host: " + String(server) + "\r\n" +
  274.                     "Cache-Control: no-cache\r\n" +
  275.                     "Connection: close\r\n\r\n");
  276.  
  277.  
  278.     unsigned long timeout = millis();
  279.     while (GSMclient.available() == 0) {
  280.       if (millis() - timeout > 5000) {
  281.         SerialUSB.println("Client Timeout !");
  282.         GSMclient.stop();
  283.         return;
  284.       }
  285.     }
  286.     // Once the response is available start reading reply
  287.     while (GSMclient.available()) {
  288.  
  289.       // read line till /n
  290.       String line = GSMclient.readStringUntil('\n');
  291.       // remove space, to check if the line is end of headers
  292.       line.trim();
  293.  
  294.       // if the the line is empty, this is end of headers
  295.       // break the while and feed the remaining client to the Update.writeStream();
  296.       if (!line.length()) {
  297.         // headers ended
  298.         break; // and get the OTA update started
  299.       }
  300.       debug2(line);
  301.       // Check if the HTTP Response is 200 if not break and Exit Update
  302.       if (line.startsWith("HTTP/1.1")) {
  303.         if (line.indexOf("200") < 0) {
  304.           SerialUSB.println("Got a non 200 status code from server. Exiting OTA Update.");
  305.           break;
  306.         }
  307.       }
  308.  
  309.       // extracting headers starting with content length
  310.       if (line.startsWith("Content-Length: ")) {
  311.         contentLength = atol((getHeaderValue(line, "Content-Length: ")).c_str());
  312.         SerialUSB.println("Got " + String(contentLength) + " bytes from server");
  313.       }
  314.  
  315.       // Next, the content type
  316.       if (line.startsWith("Content-Type: ")) {
  317.         String contentType = getHeaderValue(line, "Content-Type: ");
  318.         SerialUSB.println("Got " + contentType + " payload.");
  319.         if (contentType == "application/octet-stream") {
  320.           isValidContentType = true;
  321.         }
  322.       }
  323.     }
  324.   } else {
  325.     SerialUSB.println("Connection to " + String(server) + " failed. Please check your setup");
  326.   }
  327.   // Check what is the contentLength and if content type is `application/octet-stream`
  328.   SerialUSB.println("contentLength : " + String(contentLength) + ", isValidContentType : " + String(isValidContentType));
  329.  
  330.   // check contentLength and content type
  331.   if (contentLength && isValidContentType) {
  332.     uint32_t readLength = 0;
  333.  
  334.     if(!SD.begin(10)) {                                              debug2("SD FAILURE ::: ");
  335.     return;
  336.   }
  337.  
  338.   File file = SD.open("UPDATE.bin", O_CREAT | O_WRITE);
  339.   if (!file) {
  340.     GSMclient.stop();                                              debug2("Could not create bin file. Can't continue with update.");
  341.     return;
  342.   }
  343.  
  344.     uint32_t clientReadStartTime = millis();
  345.     SerialUSB.println(F("Reading response data"));
  346.     clientReadStartTime = millis();
  347.  
  348.     printPercent(readLength, contentLength);
  349.     //      uint32_t readLength = 0;
  350.     while (readLength < contentLength && GSMclient.connected() && millis() - clientReadStartTime < clientReadTimeout) {
  351.       while (GSMclient.available()) {
  352.         uint8_t c = GSMclient.read();
  353.         SerialUSB.print((c));  // Uncomment this to show data
  354.         file.write(c);
  355.         readLength++;
  356.         if (readLength % (contentLength / 13) == 0) {
  357.           printPercent(readLength, contentLength);
  358.         }
  359.         clientReadStartTime = millis();
  360.       }
  361.     }
  362.     printPercent(readLength, contentLength);
  363.    
  364.     file.close();
  365.   GSMclient.stop();
  366.  
  367.  
  368.   #ifdef __AVR__
  369.   wdt_enable(WDTO_15MS);
  370.   while (true);
  371.   #else
  372.   NVIC_SystemReset();
  373.   #endif
  374.   }
  375.   // Check if there is enough to OTA Update
  376.  
  377. }
  378.  
  379.  
  380. void printPercent(uint32_t readLength, uint32_t contentLength) {
  381.   // If we know the total length
  382.   if (contentLength != (uint32_t) - 1) {
  383.     SerialUSB.print("\r ");
  384.     SerialUSB.print((100.0 * readLength) / contentLength);
  385.     SerialUSB.print('%');
  386.   } else {
  387.     SerialUSB.println(readLength);
  388.   }
  389. }
  390.  
  391. void debug2(String s) {
  392.   SerialUSB.println(s);
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement