Advertisement
Marijn78

Up and down serial test

Apr 17th, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //1.3. AT Command Responses
  2. //When the AT command processor has finished processing a line, it will output "OK" or "ERROR" or “+CME ERROR:<err>” indicating that it is ready to accept a new command. Solicited informational responses are sent before the final "OK" or "ERROR" or “+CME ERROR:<err>”.  
  3. //Responses will be of the format:
  4. //<CR><LF>+CMD1:<parameters><CR><LF> <CR><LF>OK<CR><LF>
  5. //Or
  6. //<CR><LF><parameters><CR><LF> <CR><LF>OK<CR><LF>
  7. //
  8. //
  9. // Sample form Serial Monitor
  10. //
  11. // OK
  12. //
  13. // If there is any error, response:
  14. // ERROR
  15. // Or
  16. // +CME ERROR:<err>
  17. //
  18.  
  19. #include <SoftwareSerial.h>
  20. SoftwareSerial mySerial(10, 11);
  21.  
  22. char chrIMEI[16];
  23. char serialdata[256]; //Array to store the chars before parsing, size???
  24.  
  25. int readline(int readch, char *buffer, int len) {
  26.   static int pos = 0;
  27.   int rpos;
  28.   if (readch > 0) {
  29.     switch (readch) {
  30.       case '\r': // Ignore CR
  31.         break;
  32.       case '\n': // Return on new-line
  33.         rpos = pos;
  34.         pos = 0;  // Reset position index ready for next time
  35.         return rpos;
  36.       default:
  37.         if (pos < len - 1) {
  38.           buffer[pos++] = readch;
  39.           buffer[pos] = 0;
  40.         }
  41.         break;
  42.     }
  43.   }
  44.   return 0;
  45. }
  46.  
  47. void setSocket() {
  48.   mySerial.write("AT+NSOCR=DGRAM,17,7000,1\r\n");
  49. }
  50.  
  51. void setup() {
  52.   Serial.begin(9600);
  53.   while (!Serial) {
  54.   }
  55.   Serial.println("Testing up- and downlink");
  56.   mySerial.begin(9600);
  57.   //setSocket(); // Only on power cycle, not after reflashing..?
  58. }
  59.  
  60. void loop() {
  61.   // Open socket first after power cycle: AT+NSOCR=DGRAM,17,7000,1\r\n
  62.   // Send "Hello World", 11=length
  63.   // AT+NSOST=0,999.99.999.999,99999,11,48656c6c6f20576f726c64
  64.    
  65.   if (mySerial.available()) {
  66.     if (readline(mySerial.read(), serialdata, 512)) {
  67.       // Command: AT+CGSN=1
  68.       // Response: +CGSN:999999999999999
  69.       if (strncmp(serialdata, "+CGSN:", 6) == 0) {
  70.         for (int i = 0; i < 16; i++) {
  71.           chrIMEI[i] = serialdata[6 + i];
  72.         }
  73.         chrIMEI[15] = '\0';
  74.       }
  75.      
  76.       // +NSONMI  Indicator of Arrived Socket Messages (Response Only)
  77.       // Message: "Hello"
  78.       // Response: +NSONMI:0,5 (<socket>,<length>)
  79.       // Command to download: AT+NSORF=0,5 <socket>,<req_length>
  80.       // Response <socket>,<ip_addr>,<port>,<length>,<data>,<remaining_ length>
  81.       // Example: 0,999.99.999.999,99999,5,48656C6C6F,0
  82.       if (strncmp(serialdata, "+NSONMI:", 8) == 0) {
  83.         char *csocket = strtok(serialdata + 8, ":");
  84.         char *cbytes = strtok(NULL, ",");
  85.         // Check the last slicing worked
  86.         if (cbytes != NULL) {
  87.           int socket = atoi(csocket);
  88.           int bytes = atoi(cbytes);
  89.         }
  90.         // Proceed and download messages here?
  91.       }
  92.     }
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement