Marijn78

Untitled

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