Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1.3. AT Command Responses
- //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>”.
- //Responses will be of the format:
- //<CR><LF>+CMD1:<parameters><CR><LF> <CR><LF>OK<CR><LF>
- //Or
- //<CR><LF><parameters><CR><LF> <CR><LF>OK<CR><LF>
- //
- //
- // Sample form Serial Monitor
- //
- // OK
- //
- // If there is any error, response:
- // ERROR
- // Or
- // +CME ERROR:<err>
- //
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(10, 11);
- char chrIMEI[16];
- char serialdata[256]; //Array to store the chars before parsing, size???
- int readline(int readch, char *buffer, int len) {
- static int pos = 0;
- int rpos;
- if (readch > 0) {
- switch (readch) {
- case '\r': // Ignore CR
- break;
- case '\n': // Return on new-line
- rpos = pos;
- pos = 0; // Reset position index ready for next time
- return rpos;
- default:
- if (pos < len - 1) {
- buffer[pos++] = readch;
- buffer[pos] = 0;
- }
- break;
- }
- }
- return 0;
- }
- void setSocket() {
- mySerial.write("AT+NSOCR=DGRAM,17,7000,1\r\n");
- }
- void setup() {
- Serial.begin(9600);
- while (!Serial) {
- }
- Serial.println("Testing up- and downlink");
- mySerial.begin(9600);
- //setSocket(); // Only on power cycle, not after reflashing..?
- }
- void loop() {
- // Open socket first after power cycle: AT+NSOCR=DGRAM,17,7000,1\r\n
- // Send "Hello World", 11=length
- // AT+NSOST=0,999.99.999.999,99999,11,48656c6c6f20576f726c64
- if (mySerial.available()) {
- if (readline(mySerial.read(), serialdata, 512)) {
- // Command: AT+CGSN=1
- // Response: +CGSN:999999999999999
- if (strncmp(serialdata, "+CGSN:", 6) == 0) {
- for (int i = 0; i < 16; i++) {
- chrIMEI[i] = serialdata[6 + i];
- }
- chrIMEI[15] = '\0';
- }
- // +NSONMI Indicator of Arrived Socket Messages (Response Only)
- // Message: "Hello"
- // Response: +NSONMI:0,5 (<socket>,<length>)
- // Command to download: AT+NSORF=0,5 <socket>,<req_length>
- // Response <socket>,<ip_addr>,<port>,<length>,<data>,<remaining_ length>
- // Example: 0,999.99.999.999,99999,5,48656C6C6F,0
- if (strncmp(serialdata, "+NSONMI:", 8) == 0) {
- char *csocket = strtok(serialdata + 8, ":");
- char *cbytes = strtok(NULL, ",");
- // Check the last slicing worked
- if (cbytes != NULL) {
- int socket = atoi(csocket);
- int bytes = atoi(cbytes);
- }
- // Proceed and download messages here?
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement