Hi, I want to upload data to Thingspeak using the SIM908 module, but I don't get any answer in my Thingspeak channel.
I'm new with this and if you could help me it would be great. Here is the code:
int8_t answer;
int onModulePin= 2;
char aux_str[50];
float pressure= 100;
String humidity="1";
String str;
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
sendATcommand2("AT+CPIN=3765", "OK", "ERROR", 2000);
delay(3000);
while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000)==0);
}
void loop(){
humidity = "70";
pressure = 100;
str="GET http://api.thingspeak.com/update?api_key=TPGL5UMZGNYO857K&field1=" + String(pressure);
bidali();
delay (20000);
}
void bidali (){
char message_TCP [300];
str.toCharArray(message_TCP, 300); // converting TCP_message to char inorder for it to pass through AT command
if(sendATcommand2("AT+CIPMUX=0","OK","ERROR",1000)==1)
{
while(sendATcommand2("AT+CIPSTATUS","INITIAL","",500)==0);
delay(5000);
if(sendATcommand2("AT+CSTT=\"gprs-service.com\",\"\",\"\"","OK","ERROR",30000)==1)
{
while(sendATcommand2("AT+CIPSTATUS","START","",500)==0);
delay(5000);
if(sendATcommand2("AT+CIICR","OK","ERROR",30000)==1)
{
while(sendATcommand2("AT+CIPSTATUS","GPRSACT","",500)==0);
delay(5000);
if(sendATcommand2("AT+CIFSR",".","ERROR",10000)==1)
{
while(sendATcommand2("AT+CIPSTATUS","IP STATUS","",500)==0);
delay(5000);
Serial.println("Openning TCP");
if(sendATcommand2("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"","CONNECT OK", "CONNECT FAIL", 30000)==1)
{
Serial.println("Konektatua");
sprintf(aux_str,"AT+CIPSEND=%d", strlen(message_TCP));
if(sendATcommand2(aux_str,">","ERROR",10000)==1)
{
sendATcommand2(message_TCP,"","ERROR",20000);
Serial.println("Mensaje mandado!");
}
sendATcommand2("AT+CIPCLOSE","CLOSE OK","ERROR",10000);
}
else
{
Serial.println("Errorea konexioa irekitzen");
}
}
else
{
Serial.println("Errorea IP helbidea lortzen");
}
}
else
{
Serial.println("Errorea hari gabeko konexioa irekitzen");
}
}
else
{
Serial.println("Errorea APN-a konfigurazioarekin");
}
}
else
{
Serial.println("Errorea konexioa ezartzen");
}
sendATcommand2("AT+CIPSHUT","OK","ERROR",10000);
delay (4000);
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand2("AT", "OK", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand2("AT", "OK", "OK", 2000);
}
}
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout)
{
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
else if(strstr(response, expected_answer2) !=NULL)
{
answer=2;
}
}
}
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
What can be wrong? Thank you!