Разбор ответа от SIM900

jahmal
Offline
Зарегистрирован: 01.06.2016

Задался целью настраивать некоторые параметры контроллера при помощи SIM900. В ответ на команду получаю вот такие строки.

+HTTPREAD:13
50,40,35,30.
OK

В первой строке из полезного только размер ответа, а вот во второй уже значения переменных, которые я хотел бы использовать.

Вопрос. Как считать только вторую строку?

На простотах интернета нашел вот такой парсер, думаю многим будет полезен.

const char EOPmarker = '.'; //This is the end of packet marker
char serialbuf[32]; //This gives the incoming serial some room. Change it if you want a longer incoming.
 
#include <SoftwareSerial.h>
#include <string.h> // we'll need this for subString
#define MAX_STRING_LEN 20 // like 3 lines above, change as needed.
 
SoftwareSerial SoftSer(11, 10); // RX, TX
 
void setup(){
  Serial.begin(9600);
  SoftSer.begin(9600);
}
 
void loop() {
    if (SoftSer.available() > 0) { //makes sure something is ready to be read
      static int bufpos = 0; //starts the buffer back at the first position in the incoming serial.read
      char inchar = SoftSer.read(); //assigns one byte (as serial.read()'s only input one byte at a time
      if (inchar != EOPmarker) { //if the incoming character is not the byte that is the incoming package ender
        serialbuf[bufpos] = inchar; //the buffer position in the array get assigned to the current read
        bufpos++; //once that has happend the buffer advances, doing this over and over again until the end of package marker is read.
      }
      else { //once the end of package marker has been read
        serialbuf[bufpos] = 0; //restart the buff
        bufpos = 0; //restart the position of the buff
 
        int x = atoi(subStr(serialbuf, ",", 1));
        int y = atoi(subStr(serialbuf, ",", 2));
        int z = atoi(subStr(serialbuf, ",", 3));
 
        Serial.print("The first number, x is: ");
        Serial.print(x);
        Serial.print(" - The second number, y is: ");
        Serial.print(y);
        Serial.print(" - The third number, z is: ");
        Serial.print(z);
        Serial.println();
 
      }
    }
}
 
char* subStr (char* input_string, char *separator, int segment_number) {
  char *act, *sub, *ptr;
  static char copy[MAX_STRING_LEN];
  int i;
  strcpy(copy, input_string);
  for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
    sub = strtok_r(act, separator, &ptr);
    if (sub == NULL) break;
  }
 return sub;
}

 

jahmal
Offline
Зарегистрирован: 01.06.2016

отправляю данные при помощи этого набора

void GPRS()
{
        power_on();
		int k = 0;
		k = sprintf(commandbuffer, "&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%d&h=%d&i=%d&j=%d&k=%d", t1, h1, w_t1, distance, w_t2, distance2, int_ph, ppm, light, fan, ram);

		sendATcommand("AT+HTTPINIT", "OK", 2000);
		sendATcommand("AT+HTTPPARA=\"CID\",\"1\"", "OK", 2000);

		// set http param value
		Serial2.print("AT+HTTPPARA=\"URL\",\"gsmserver.esy.es/get.php?i=");
        Serial2.print(commandbuffer);
        Serial2.println("\"");
		delay(2000);
	    toSerial();

	//	sendATcommand("AT+HTTPACTION=0", "OK", 9000);
		Serial2.println("AT+HTTPACTION=0");
		delay(8000);
	    toSerial();

        // read server response
		Serial2.println("AT+HTTPREAD");
		delay(2000);
    	toSerial();
    
        Serial2.println("");
		sendATcommand("AT+HTTPTERM", "OK", 2000);
}