Marijn78

Untitled

Feb 27th, 2021
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include <ArduinoJson.h>
  4. SoftwareSerial mySerial(10, 11);
  5.  
  6. char chrIMEI[16];
  7. char serialdata[256]; //Array to store the chars before parsing, size???
  8. const int kLedPin = 13;
  9.  
  10. bool socketIsSet = false;
  11. bool messagesInQueue = false;
  12. unsigned int interval = 1000 * 30 * 1;
  13. unsigned long time_alive_message;
  14.  
  15. void sendMessage(String payload) {
  16.   payload.concat("0");
  17.   char buf[256];
  18.   int payloadLen = payload.length();
  19.   payload.toCharArray(buf, payloadLen);
  20.   String myString = "AT+NSOST=0,172.27.131.100,15683,";
  21.   myString.concat(String(payloadLen - 1));
  22.   myString.concat(",");
  23.   for (int i = 0; i < payloadLen - 1; i++) {
  24.     myString.concat(String(buf[i], HEX));
  25.   }
  26.   myString.concat("\r\n");
  27.   myString.toCharArray(buf, 256);
  28.   mySerial.write(buf);
  29. }
  30.  
  31. int readline(int readch, char *buffer, int len) {
  32.   static int pos = 0;
  33.   int rpos;
  34.   if (readch > 0) {
  35.     switch (readch) {
  36.       case '\r': // Ignore CR
  37.         break;
  38.       case '\n': // Return on new-line
  39.         rpos = pos;
  40.         pos = 0;  // Reset position index ready for next time
  41.         return rpos;
  42.       default:
  43.         if (pos < len - 1) {
  44.           buffer[pos++] = readch;
  45.           buffer[pos] = 0;
  46.         }
  47.         break;
  48.     }
  49.   }
  50.   return 0;
  51. }
  52.  
  53. void setup()  {
  54.   //Serial.begin(9600);
  55.   //Configure the LED for output and sets the intial state to off
  56.   pinMode(kLedPin, OUTPUT);
  57.   digitalWrite(kLedPin, LOW);
  58.   //Serial.println("Testing up- and downlink");
  59.   mySerial.begin(9600);
  60.   mySerial.write("AT+NSOCR=DGRAM,17,7000,1\r\n");
  61.   mySerial.flush();
  62. }
  63.  
  64. void loop() {
  65.   if (!socketIsSet) {
  66.     //Serial.println("Setting SOCKET");
  67.     mySerial.write("AT+NSOCR=DGRAM,17,7000,1\r\n");
  68.     mySerial.flush();
  69.     socketIsSet = true;  
  70.   }
  71.  
  72.   unsigned long time = millis();
  73.   if (time_alive_message + interval < millis()) {
  74.     //Serial.println("Sending ALIVE message");
  75.     String payload = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.75608,2.302038]}";
  76.     sendMessage(payload);
  77.     mySerial.flush();
  78.     time_alive_message = millis();
  79.   }
  80.  
  81.   if (mySerial.available()) {
  82.     if (readline(mySerial.read(), serialdata, 512)) {
  83.       //Serial.println("rtn: " + String(serialdata));
  84.       if (strncmp(serialdata, "OK", 2) == 0) {
  85.         //don't do anything
  86.       } else if (strncmp(serialdata, "+CGSN:", 6) == 0) {
  87.         for (int i = 0; i < 16; i++) {
  88.           chrIMEI[i] = serialdata[6 + i];
  89.         }
  90.         chrIMEI[15] = '\0';
  91.       } else if (strncmp(serialdata, "+NSONMI:", 8) == 0) {
  92.         //Serial.println("Message available for download");
  93.         messagesInQueue = true;
  94.         char nsorf[10] = "AT+NSORF=";
  95.         char *csocket = strtok(serialdata + 8, ":");
  96.         //Serial.println("char *csocket: " + String(csocket)); //0,5
  97.         char buf[256] = "";
  98.         strcat(buf, nsorf);
  99.         strcat(buf, csocket);
  100.         char cr[2] = "\r";
  101.         strcat(buf, cr);
  102.         mySerial.write(buf);
  103.       } else {
  104.         //then it's a comma separated string like //0,172.27.131.100,15683,5,48656C6C6F,0
  105.         if (messagesInQueue) {
  106.           char *soc = strtok(serialdata, ",");
  107.           unsigned int l1 = strlen(soc);
  108.           char *ip = strtok(serialdata + l1 + 1, ",");
  109.           unsigned int l2 = strlen(ip);
  110.           char *port = strtok(serialdata + l1 + 1 + l2 + 1, ",");
  111.           unsigned int l3 = strlen(port);
  112.           char *length = strtok(serialdata + l1 + 1 + l2 + 1 + l3 + 1, ",");
  113.           unsigned int l4 = strlen(length);
  114.           char *data = strtok(serialdata + l1 + 1 + l2 + 1 + l3 + 1 + l4 + 1, ",");
  115.           unsigned int l5 = strlen(data);
  116.           char *remaining_length = strtok(serialdata + l1 + 1 + l2 + 1 + l3 + 1 + l4 + 1 + l5 + 1, ",");
  117.           unsigned int l6 = strlen(remaining_length);
  118.           messagesInQueue = false;
  119.           //Hex to ASCII A (the network always convertes to hex)
  120.           char output[256];
  121.           for (size_t i = 0; i < l5; i++) {
  122.             char temp[3] = {0};  // temp array for hex data
  123.             strncpy(temp, &data[i * 2], 2);  // copy to temp array, two characters only
  124.             output[i] = (char)strtol(temp, NULL, 16);  // convert hex (base 16) from temp to character in output
  125.           }
  126.           //Hex to ASCII B (necessary when orginal message is in hex as well as recommended)
  127.           char output2[128];
  128.           for (size_t i = 0; i < l5; i++) {
  129.             char temp2[3] = {0};  // temp array for hex data
  130.             strncpy(temp2, &output[i * 2], 2);  // copy to temp array, two characters only
  131.             output2[i] = (char)strtol(temp2, NULL, 16);  // convert hex (base 16) from temp to character in output
  132.           }
  133.           //Serial.println("Message: " + String(output2));
  134.           //Deserialize JSON
  135.           StaticJsonDocument<16> doc;
  136.           DeserializationError error = deserializeJson(doc, output2, 128);
  137.           if (error) {
  138.             //Serial.print(F("deserializeJson() failed: "));
  139.             //Serial.println(error.f_str());
  140.             return;
  141.           }
  142.           //Process message..
  143.           const char* command = doc["command"]; // "led_on"
  144.           if (strcmp(command, "led_on") == 0) {
  145.             digitalWrite(kLedPin, HIGH);
  146.             //Serial.println("led is ON");
  147.           } else if (strcmp(command, "led_off") == 0) {
  148.             digitalWrite(kLedPin, LOW);
  149.             //Serial.println("led is OFF");  
  150.           }
  151.         }
  152.       }
  153.     }
  154.   }
  155. }
RAW Paste Data