Marijn78

Untitled

Feb 25th, 2021 (edited)
91
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. SoftwareSerial mySerial(10, 11);
  5.  
  6. char chrIMEI[16];
  7. char serialdata[256]; //Array to store the chars before parsing, size???
  8.  
  9. char serial_command_buffer_[32];
  10. SerialCommands serial_commands_(&Serial, serial_command_buffer_, sizeof(serial_command_buffer_), "\r\n", " ");
  11.  
  12. int readline(int readch, char *buffer, int len) {
  13.   static int pos = 0;
  14.   int rpos;
  15.   if (readch > 0) {
  16.     switch (readch) {
  17.       case '\r': // Ignore CR
  18.         break;
  19.       case '\n': // Return on new-line
  20.         rpos = pos;
  21.         pos = 0;  // Reset position index ready for next time
  22.         return rpos;
  23.       default:
  24.         if (pos < len - 1) {
  25.           buffer[pos++] = readch;
  26.           buffer[pos] = 0;
  27.         }
  28.         break;
  29.     }
  30.   }
  31.   return 0;
  32. }
  33.  
  34. //This is the default handler, and gets called when no other command matches.
  35. void cmd_unrecognized(SerialCommands* sender, const char* cmd) {
  36.   sender->GetSerial()->print("Unrecognized command [");
  37.   sender->GetSerial()->print(cmd);
  38.   sender->GetSerial()->println("]");
  39. }
  40.  
  41. //called for IMEI command
  42. void cmd_imei(SerialCommands* sender) {
  43.   sender->GetSerial()->println("command: IMEI");
  44.   mySerial.write("AT+CGSN=1\r\n");
  45. }
  46.  
  47. //called for SETSOCKET command
  48. void cmd_setsocket(SerialCommands* sender) {
  49.   sender->GetSerial()->println("setting socket");
  50.   mySerial.write("AT+NSOCR=DGRAM,17,7000,1\r\n");
  51. }
  52.  
  53. //called for DOWNLOAD command
  54. void cmd_download(SerialCommands* sender) {
  55.   sender->GetSerial()->println("downloading");
  56.   mySerial.write("AT+NSORF=0,5\r"); //ALLEEN /R??????????? //0,172.27.131.100,15683,5,48656C6C6F,0
  57. }
  58.  
  59. void sendMessage(String payload) {
  60.   payload.concat("0");
  61.   char buf[512];
  62.   int payloadLen = payload.length();
  63.   payload.toCharArray(buf, payloadLen);
  64.   String myString = "AT+NSOST=0,172.27.131.100,15683,";
  65.   myString.concat(String(payloadLen - 1));
  66.   myString.concat(",");
  67.   for (int i = 0; i<payloadLen - 1; i++) {
  68.     myString.concat(String(buf[i], HEX));  
  69.   }
  70.   myString.concat("\r\n");
  71.   myString.toCharArray(buf, 512);
  72.   mySerial.write(buf);
  73. }
  74.  
  75. //called for ALIVE command
  76. void cmd_alive(SerialCommands* sender) {
  77.   String payload = "i am alive";
  78.   sendMessage(payload);
  79.   sender->GetSerial()->println("I am alive");
  80. }
  81.  
  82. //Note: Commands are case sensitive
  83. SerialCommand cmd_imei_("IMEI", cmd_imei);
  84. SerialCommand cmd_setsocket_("SETSOCKET", cmd_setsocket);
  85. SerialCommand cmd_download_("DOWNLOAD", cmd_download);
  86. SerialCommand cmd_alive_("ALIVE", cmd_alive);
  87.  
  88. void setup()  {
  89.   Serial.begin(9600);
  90.   serial_commands_.SetDefaultHandler(cmd_unrecognized);
  91.   serial_commands_.AddCommand(&cmd_imei_);
  92.   serial_commands_.AddCommand(&cmd_setsocket_);
  93.   serial_commands_.AddCommand(&cmd_download_);
  94.   serial_commands_.AddCommand(&cmd_alive_);
  95.   Serial.println("Testing up- and downlink");
  96.   mySerial.begin(9600);
  97. }
  98.  
  99. void loop() {
  100.   serial_commands_.ReadSerial();
  101.   if (mySerial.available()) {
  102.     if (readline(mySerial.read(), serialdata, 512)) {
  103.       Serial.println("response: " + String(serialdata));
  104.       //Serial.println(String(serialdata));
  105.       if (strncmp(serialdata, "+CGSN:", 6) == 0) {
  106.         for (int i = 0; i < 16; i++) {
  107.           chrIMEI[i] = serialdata[6 + i];
  108.         }
  109.         chrIMEI[15] = '\0';
  110.       }
  111.  
  112.       // +NSONMI  Indicator of Arrived Socket Messages (Response Only)
  113.       // Response: +NSONMI:0,5 (<socket>,<length>)
  114.       // Command to download: AT+NSORF=0,5 <socket>,<req_length>
  115.       // Response <socket>,<ip_addr>,<port>,<length>,<data>,<remaining_ length>
  116.       // Example: 0,999.99.999.999,99999,5,48656C6C6F,0
  117.       if (strncmp(serialdata, "+NSONMI:", 8) == 0) {
  118.         Serial.println("Message available for download");
  119.         char *csocket = strtok(serialdata + 8, ":");
  120.         char *cbytes = strtok(NULL, ",");
  121.         // Check the last slicing worked
  122.         if (cbytes != NULL) {
  123.           int socket = atoi(csocket);
  124.           int bytes = atoi(cbytes);
  125.         }
  126.         // Proceed and download messages here?
  127.         mySerial.write("AT+NSORF=0,5\r");
  128.       }
  129.     }
  130.   }
  131. }
RAW Paste Data