Ubidots Community

SIM800A not working

Hello,

I am using SIM800A

I run the below sequence of AT commands it runs till AT+CIICR and then gets stuck .

I was referring to Sim800l 400 bad request

PS- Kindly ignore the comments

gsm_send("AT\n");

udelay(1000);
gsm_send("AT+CPIN?\r\n"); //check network registration

udelay(1000);

gsm_send("AT+CREG?\r\n"); //check network registration

udelay(1000);

gsm_send("AT+CGATT?\r\n"); //check network registration

udelay(1000);
gsm_send("AT+CIPSHUT\r\n"); //check network registration

udelay(1000);

gsm_send("AT+CIPSTATUS\r\n"); //check network registration

udelay(1000);
gsm_send("AT+CIPMUX=0\r\n"); //check network registration

udelay(1000);
gsm_send("AT+CSTT=\"[airtelgprs.com](http://airtelgprs.com/)\"\r\n"); //check network registration

udelay(1000);
gsm_send("AT+CIICR\r\n"); //check network registration

udelay(3000);
gsm_send("AT+CIFSR\r\n"); //check network registration

udelay(2000);
gsm_send("AT+CIPSPRT=0\r\n"); //check network registration

udelay(3000);
gsm_send("AT+CIPSTART=\"TCP\",\"[things.ubidots.com](http://things.ubidots.com/)\",80\r\n");

Also wanted to know if I run the below commands will I get the same results as above
because the below set of AT commands ran without any error and they kind of made the same logic according to me.

gsm_send("AT\n");
udelay(1000);
gsm_send("AT+CREG?\r\n"); //check network registration
udelay(1000);

gsm_send("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n"); //Set GPRS mode
udelay(1000);
gsm_send("AT+SAPBR=3,1,\"APN\",\"[airtelgprs.com](http://airtelgprs.com/)\"\r\n"); //Set APN
udelay(1000);
gsm_send("AT+SAPBR=1,1\r\n"); //Open connection
udelay(1000);
gsm_send("AT+SAPBR=2,1\r\n"); //Query connection, IP address
udelay(1000);
gsm_send("AT+CIPSPRT=0\r\n"); //check network registration

udelay(3000);
gsm_send("AT+CIPSTART=\"TCP\",\"things.ubidots.com\",80\r\n"); //check network registration


udelay(8000);

Thank you.

Dear user, for us and our community to be able to help you, can you please:

1. Confirm which API are you using and manually send data to it

It seems to be our TCP ingestion broker, but please confirm and check you’re sending the expected payload. In your texts, it’s not possible to identify any payload being sent.

Please use a TCP simulation tool to send data from your computer, before jumping in your firmware code. This will help you iron out the concepts behind the protocol being used. For TCP, we recommend netcat. See Sending data using TCP/UDP and netcat.

2. Provide the response that you’re getting from our API

We can’t tell what the issue might be, unless you show it to us :slight_smile: So please paste the response you’re getting from Ubidots.

Lastly, before we or our community can provide any inputs, check out our

Cellular Pre-flight checklist

to make sure you’re device is already connected to the Internet:

1. Is my device connected to the Internet?

This can be as simple as putting your SIM card in a phone and checking if it has data coverage. Then, do the same with your device and try to reach the outside world with a corresponding AT command.

2. Can you make HTTP POST or GET requests?

To check this, simply make a request to an external site (like google.com) and make sure you’re getting an answer.

Kindly check the void main section
I am running alternate AT commands because AT+CIFSR was not running on my GSM module
The payload is built in the AT_CIPSEND send function

#include "uart.h"
    #include "stdlib.h"
    #include <stdlib.h>
    #include "config.h"
    #include "gpio.h"
    #include <string.h>
    #define BUFFER_SIZE 1200
    char TXBuffer[BUFFER_SIZE];
    int temperature=2;
    char send_whole_str[300]={0, };
    //token=BBFF-GPigdeygux8m9S0Frh3QCESHBkx3fe
    //variable=604d00741d8472129a7ab12c
    char post_cmd[]= "POST /api/v1.6/variables/604d00741d8472129a7ab12c/values HTTP/1.1\r\nX-Auth-Token: BBFF-GPigdeygux8m9S0Frh3QCESHBkx3fe\r\nHost: things.ubidots.com\r\nConnection: close\r\nContent-Type:  application/json\r\nContent-Length:14\r\n\n{\"value\": " ;
    char http_request[BUFFER_SIZE];
    char http_page[BUFFER_SIZE];
    char html_page[BUFFER_SIZE];
    int response_ok = 0;
    int connection_id = 0;
    char led_status = 0;
    int send_string(char * str) {
    	char error;
    	response_ok = 0;
    	while (*str != '\0') {
    		uart_putchar(UART_1, *str, &error);
    		/*if (*str == '\n')
    		 uart_putchar(UART_1, '\r', &error);*/
    		str++;
    	}
    	return 0;
    }
    int send_char(char code) {
    	char error;
    	uart_putchar(UART_1, code, &error);
    	return 0;
    }
    void receive_string(char * str) {
    	char error;
    	char data;
    	while (1) {
    		data = uart_getchar(UART_1, &error);
    		*str = data;
    		str++;
    		//Check for OK
    		if (data == 'O') {
    			data = uart_getchar(UART_1, &error);
    			*str = data;
    			str++;
    			if (data == 'K') {
    				response_ok = 1;
    				break;
    			}
    		}
    		//Check for ERROR
    		if (data == 'E') {
    			data = uart_getchar(UART_1, &error);
    			*str = data;
    			str++;
    			if (data == 'R') {
    				data = uart_getchar(UART_1, &error);
    				*str = data;
    				str++;
    				if (data == 'R') {
    					response_ok = 0;
    					break;
    				}
    			}
    		}
    		if (data == 'D') {
    			data = uart_getchar(UART_1, &error);
    			*str = data;
    			str++;
    			if (data == 'O') {
    				data = uart_getchar(UART_1, &error);
    				*str = data;
    				str++;
    				if (data == 'W') {
    					response_ok = 0;
    					break;
    				}
    			}
    		}
    		if (data == '>') {			
    					response_ok = 0;
    					break;			
    		}
    	}
    }
    int gsm_send(char * sstr) {
    	char response[100] = { 0, };	
    	send_string(sstr);
    	receive_string(response);
    	printf("%s\n",response);
    	if (response_ok)
    		return 1;
    	else
    		return 0;
    }
    int gsm_send_data(char * data)
    {
    	char response[100] = { 0, };
    	char send_str[50]={0,};
    	sprintf(send_str,"AT+HTTPDATA=%d,10000\r\n", strlen(data));	
    	send_string(send_str);
    	receive_string(response);
    	printf("%s\n",response);
    	udelay(5000000);
    	send_string(data);
    	receive_string(response);
    	printf("%s\n",response);
    	if (response_ok)
    		return 1;
    	else
    		return 0;
    }
    void AT_CIPSEND() {
    char send_str[300]={0,};
    char str[100] = {0, };
    sprintf(send_whole_str,"%s%d}\r\n",post_cmd,temperature);
    int length = strlen(send_whole_str);
    sprintf(send_str,"AT+CIPSEND\r\n",length);
    send_string(send_str);
    receive_string(str);
    //printf("STR: %s\n",str);
    	if (response_ok)
    		printf("CIPSEND connected\n");
    	else
    		printf("CIPSEND error\n");
    }
    void API_KEY() {
    const char* len;
    char str[100] = {0, };
    char send_str[50]={0,};
    send_string(send_whole_str);
    printf("STR: %s\n",send_whole_str);
    printf("          value sent       \n");
    }
    /**
     @fn main
     @brief transmit and reception through uart
     @details 1 character is transmitted and received through uart
     @param[in] No input parameter.
     @param[Out] No ouput parameter.
     @return Void function.
     */
    void main() {
    	led_status = 0;
    	printf("\n\r *****************************************************************************");
    	printf("\n\r INFO: Connect GSM module [SIM800A] to UART 1 ");	
    	printf("\n\r *****************************************************************************");
    	**printf("\n\r Setting up GSM Modem \n");**
**    	uart_set_baud_rate(UART_1, 9600, 40000000);**
**    	udelay(10000);**
**    	gsm_send("AT\n");**

**    	udelay(1000);**
**    	gsm_send("AT+CIPSHUT\n");**
**    	udelay(1000);**
**    	gsm_send("AT+CREG?\r\n"); //check network registration**
**    	udelay(1000);	**
**    	gsm_send("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n"); //Set GPRS mode**
**    	udelay(1000);**
**    	gsm_send("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r\n"); //Set APN**
**    	udelay(1000);**
**    	gsm_send("AT+SAPBR=1,1\r\n"); //Open connection**
**    	udelay(1000);**
**    	gsm_send("AT+SAPBR=2,1\r\n"); //Query connection, IP address**
**    	udelay(1000);**
**    	gsm_send("AT+CIPSPRT=0\r\n"); //check network registration**
**    	udelay(3000);**
**    	udelay(20000);**
**    	udelay(20000);**
**    	udelay(20000);**
**    	gsm_send("AT+CIPSTART=\"TCP\",\"things.ubidots.com\",80\r\n");**
**    	AT_CIPSEND();**
**            udelay(5000);**
**            udelay(20000);**
**    	API_KEY();**
**    	udelay(5000);**
**    gsm_send("AT+CIPCLOSE\r\n");**
    }

Hi there! I can see you’re using HTTP payload, which is fine.

Please confirm YES or NO to these questions:

Pre-flight checklist:

  • Is your device connected to the Internet?
  • Can you make an HTTP GET request to another site?

Then, and only after you checked the above:

  • Have you built an HTTP payload from your computer and sent it successfully to Ubidots?
  • Do you have the response you’re getting from Ubidots?
  1. Yes

  2. Yes I sent a get request to thingspeak and I’m able to send values to it

  3. You mean using a third-party API checking tool?

  4. No I’m not getting any response from ubidots
    It gives me an AT+CIPSEND error that’s all

I modified the code according to the sim800L article Sim800l 400 bad request

#include "uart.h"

#include "stdlib.h"

#include <stdlib.h>

#include "config.h"

#include "gpio.h"

#include <string.h>



#define BUFFER_SIZE 1200



char TXBuffer[BUFFER_SIZE];



char http_request[BUFFER_SIZE];

char http_page[BUFFER_SIZE];

char html_page[BUFFER_SIZE];



int response_ok = 0;

int connection_id = 0;

char led_status = 0;
//X-Auth-Token: BBFF-GPigdeygux8m9S0Frh3QCESHBkx3fe\r\n
//"{\"temperature\":" + String(value1) + "}";
char post_cmd[]= "POST /api/v1.6/variables/604d00741d8472129a7ab12c/values HTTP/1.1\r\nX-Auth-Token: BBFF-GPigdeygux8m9S0Frh3QCESHBkx3fe\r\nHost: things.ubidots.com\r\nConnection: close\r\nContent-Type:  application/json\r\nContent-Length:14\r\n\n{\"value\": " ;

int temperature=2;
char send_whole_str[300]={0, };



int send_string(char * str) {

	char error;

	response_ok = 0;

	while (*str != '\0') {

		uart_putchar(UART_1, *str, &error);

		/*if (*str == '\n')

		 uart_putchar(UART_1, '\r', &error);*/

		str++;

	}

	return 0;

}



int send_char(char code) {

	char error;

	uart_putchar(UART_1, code, &error);

	return 0;

}



void receive_string(char * str) {

	char error;

	char data;



	while (1) {

		data = uart_getchar(UART_1, &error);

		*str = data;

		str++;

		//Check for OK

		if (data == 'O') {

			data = uart_getchar(UART_1, &error);

			*str = data;

			str++;

			if (data == 'K') {

				response_ok = 1;

				break;

			}

		}

		//Check for ERROR

		if (data == 'E') {

			data = uart_getchar(UART_1, &error);

			*str = data;

			str++;

			if (data == 'R') {

				data = uart_getchar(UART_1, &error);

				*str = data;

				str++;

				if (data == 'R') {

					response_ok = 0;

					break;

				}

			}

		}

		if (data == 'D') {

			data = uart_getchar(UART_1, &error);

			*str = data;

			str++;

			if (data == 'O') {

				data = uart_getchar(UART_1, &error);

				*str = data;

				str++;

				if (data == 'W') {

					response_ok = 0;

					break;

				}

			}

		}



		if (data == '>') {			

					response_ok = 0;

					break;			

		}

	}

}



int gsm_send(char * sstr) {

	char response[100] = { 0, };	

	send_string(sstr);

	receive_string(response);

	printf("%s\n",response);



	if (response_ok)

		return 1;

	else

		return 0;

}



int gsm_send_data(char * data)

{

	char response[100] = { 0, };

	char send_str[50]={0,};

	sprintf(send_str,"AT+HTTPDATA=%d,10000\r\n", strlen(data));	



	send_string(send_str);

	receive_string(response);

	printf("%s\n",response);

	udelay(5000000);

	send_string(data);



	receive_string(response);

	printf("%s\n",response);



	if (response_ok)

		return 1;

	else

		return 0;

}
void AT_CIFSR() {

char str[100] = {0, };
printf("enterting cifsr");
send_string("AT+CIFSR\r\n");
//receive_string(str);
//printf(str);
	//printf("STR: %s\n",str);
	/*if (response_ok)
		printf("CIPMUX connected\n");
	else
		printf("CIPMUX=0 error\n");*/
}

void AT_CIPSEND() {
char send_str[300]={0,};

char str[100] = {0, };
sprintf(send_whole_str,"%s%d}\r\n",post_cmd,temperature);
int length = strlen(send_whole_str); 
sprintf(send_str,"AT+CIPSEND\r\n",length);
send_string(send_str);
receive_string(str);
	//printf("STR: %s\n",str);
	if (response_ok)
		printf("CIPSEND connected\n");
	else
		printf("CIPSEND error\n");

}


void API_KEY() {
const char* len;
char str[100] = {0, };
char send_str[50]={0,};

send_string(send_whole_str);
printf("          value sent       \n");

}







/**

 @fn main

 @brief transmit and reception through uart

 @details 1 character is transmitted and received through uart

 @param[in] No input parameter.

 @param[Out] No ouput parameter.

 @return Void function.



 */

void main() {

	led_status = 0;



	printf("\n\r *****************************************************************************");

	printf("\n\r INFO: Connect GSM module [SIM800A] to UART 1 ");	

	printf("\n\r *****************************************************************************");





	printf("\n\r Setting up GSM Modem \n");

	uart_set_baud_rate(UART_1, 9600, 40000000);

	

	udelay(10000);

	gsm_send("AT\n");

	udelay(1000);

	gsm_send("AT+CPIN?\r\n"); //check network registration

	udelay(1000);

	gsm_send("AT+CREG?\r\n"); //check network registration

	udelay(1000);	

	gsm_send("AT+CGATT=1\r\n"); //check network registration

	udelay(1000);

	gsm_send("AT+CIPSHUT\r\n"); //check network registration

	udelay(1000);

	gsm_send("AT+CIPSTATUS\r\n"); //check network registration

	udelay(1000);

	gsm_send("AT+CIPMUX=0\r\n"); //check network registration

	udelay(2000);

	gsm_send("AT+CSTT=\"airtelgprs.com\"\r\n"); //check network registration

	udelay(300000);

	gsm_send("AT+CIICR\r\n"); //check network registration

	udelay(3000);

	//gsm_send("AT+SAPBR=2,1\r\n");

	//udelay(3000);
	AT_CIFSR();
	udelay(20000);
	udelay(20000);
	udelay(20000);
	udelay(20000);
	udelay(20000);
	udelay(20000);
	udelay(20000);
	printf("cifsr done");

	//gsm_send("AT+CIFSR\r\n");//check network 

	//udelay(2000);

	gsm_send("AT+CIPSPRT=0\r\n"); //check network registration

	udelay(3000);

	gsm_send("AT+CIPSTART=\"TCP\",\"things.ubidots.com\",80\r\n");
	udelay(3000);
	AT_CIPSEND();
	udelay(1000);
	API_KEY();



        

	

	

	

	while(1);



}

Hello @chirag_chinvar ,

I hope all is well on your side.

I’ve reviewed your code and it seems that you are sending the data to the wrong host, that is, you are sending data to things.ubidots.com. That was for our now closed plaform Ubidots for Education.

The correct host is industrial.api.ubidots.com

Also, I’m not quite clear on what is the payload you are sending. I can see that there is a payload, but it is commented.

Please view our API Docs for more information on how Ubidots expects the JSON payload.

Best regards,
Sebastián

Hey
No that’s not the pay
load
I had copied that line from the article just as reference
My payload is the post_cmd[]
This load gets built in the function AT_CIPSEND()
Once the AT_CIPSEND runs the API_KEY() function sends the payload to ubidots
I will change the end point as suggested
I had a few doubts-

  • I wanted to know if the content length is correct in the payload
  • What’s the command to send data
    Is it AT+CIPSEND = LENGTH or is it only AT+CIPSEND?
  • And once I execute AT+CIPSEND I can just send the payload right?
  • What is user agent?
    Can you please trace the program from main?
    And help us if you spot any mistake

Thanks a lot.

Hello @chirag_chinvar ,

I think that there is a confusion here. The line containing post_cmd[] isn’t a payload, it is the complete request made to Ubidots. The payload should look something like this {"temperature": 24}, for example.

To answer your questions:

The content length varies depending on the size of the payload, so it has to be calculated once you know what you will be sending to Ubidots.

The command to send the data is AT+CIPSEND = {length of the complete request}, so again, before sending the request, you must calculate the whole length for the request before actually sending it.

No, after using AT+CIPSEND, you must send the complete request, not only the payload.

The User Agent is a string containing Alphanumeric characters that helps the server identify which device is sending the data. We recommend the User Agent to be a random string, as this will avoid having problems with other devices (for example if to devices have the same User Agent)

I don’t understand when you say “Can you please trace the program from main?”. Could you please rephrase or explain this question?

Finally, I’d strongly recommend that you follow exactly the post that you said you took as a reference, as this example, as said by the user, already works. All that you’d have to do is change the info regarding your application (Ubidots Token, Device and Variable Labels, and the payload to be sent)

If you’re not too familiar with how requests are expected by our API, then please view its Documentation

Best regards,
Sebastián

The below code worked properly
If anybody is using a custom controller and don’t have access to the ubidots library just follow the AT commands in the same order and it works like a gem!

Thanks a lot @Sebastian for your quick replies, really grateful!

#include "uart.h"
#include "stdlib.h"
#include <stdlib.h>
#include "config.h"
#include "gpio.h"
#include <string.h>
#define BUFFER_SIZE 1200
char TXBuffer[BUFFER_SIZE];
char data[]="{\"value\":4}";

char http_request[BUFFER_SIZE];
char http_page[BUFFER_SIZE];
char html_page[BUFFER_SIZE];
int response_ok = 0;
int connection_id = 0;
char led_status = 0;
int send_string(char * str) {
	char error;
	response_ok = 0;
	while (*str != '\0') {
		uart_putchar(UART_1, *str, &error);
		/*if (*str == '\n')
		 uart_putchar(UART_1, '\r', &error);*/
		str++;
	}
	return 0;
}
int send_char(char code) {
	char error;
	uart_putchar(UART_1, code, &error);
	return 0;
}
void receive_string(char * str) {
	char error;
	char data;
	while (1) {
		data = uart_getchar(UART_1, &error);
		*str = data;
		str++;
		//Check for OK
		if (data == 'O') {
			data = uart_getchar(UART_1, &error);
			*str = data;
			str++;
			if (data == 'K') {
				response_ok = 1;
				break;
			}
		}
		//Check for ERROR
		if (data == 'E') {
			data = uart_getchar(UART_1, &error);
			*str = data;
			str++;
			if (data == 'R') {
				data = uart_getchar(UART_1, &error);
				*str = data;
				str++;
				if (data == 'R') {
					response_ok = 0;
					break;
				}
			}
		}
		if (data == 'D') {
			data = uart_getchar(UART_1, &error);
			*str = data;
			str++;
			if (data == 'O') {
				data = uart_getchar(UART_1, &error);
				*str = data;
				str++;
				if (data == 'W') {
					response_ok = 0;
					break;
				}
			}
		}
		if (data == '>') {			
					response_ok = 0;
					break;			
		}
	}
}
int gsm_send(char * sstr) {
	char response[100] = { 0, };	
	send_string(sstr);
	receive_string(response);
	printf("%s\n",response);
	if (response_ok)
		return 1;
	else
		return 0;
}
void AT_auth(){
char auth[]="AT+HTTPPARA=\"URL\",\"http://industrial.api.ubidots.com/api/v1.6/devices/testing/demoo/values?token=<replace with your token>\"\r\n";
send_string(auth);
}
int gsm_send_data()
{
	char response[100] = { 0, };
	char send_str[100]={0,};
	int len=strlen(data)+1;
	sprintf(send_str,"AT+HTTPDATA=%d,10000\r\n", len);	
	send_string(send_str);
	receive_string(response);
	printf("%s\n",response);
	//udelay(5000000);
	udelay(1000);
	send_string(data);
	receive_string(response);
	printf("%s\n",response);
	if (response_ok)
		return 1;
	else
		return 0;
}
/**
 @fn main
 @brief transmit and reception through uart
 @details 1 character is transmitted and received through uart
 @param[in] No input parameter.
 @param[Out] No ouput parameter.
 @return Void function.
 */
void main() {
	led_status = 0;
	printf("\n\r *****************************************************************************");
	printf("\n\r INFO: Connect GSM module [SIM800A] to UART 1 ");	
	printf("\n\r *****************************************************************************");
	printf("\n\r Setting up GSM Modem \n");
	uart_set_baud_rate(UART_1, 9600, 40000000);
	udelay(10000);
	gsm_send("AT\n");
	udelay(1000);
	gsm_send("AT+CREG?\r\n"); //check network registration
	udelay(1000);	
	gsm_send("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n"); //Set GPRS mode
	udelay(1000);
	gsm_send("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r\n"); //Set APN
	udelay(1000);
	gsm_send("AT+SAPBR=1,1\r\n"); //Open connection
	udelay(1000);
	gsm_send("AT+SAPBR=2,1\r\n"); //Query connection, IP address
	udelay(1000);
	gsm_send("AT+HTTPINIT\r\n"); //HTTP connection init
	udelay(1000);
	gsm_send("AT+HTTPPARA=\"CID\",1\r\n"); //Set parameters, set identifier
	udelay(1000);
	AT_auth();
	udelay(1000);
	udelay(1000);
	udelay(1000);

	gsm_send("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n"); //set content type
	udelay(1000);
	udelay(1000);
	gsm_send_data(); 
	//gsm_send("AT+HTTPDATA=14,10000\r\n");
	//set datalength, time, passing data
	gsm_send("AT+HTTPACTION=1\r\n"); //Send POST request
	udelay(1000);
        gsm_send("AT+HTTPREAD\r\n"); //Read server response
	udelay(1000);
	printf("sent data");
	gsm_send("AT+HTTPTERM\r\n"); //End HTTP connection
	udelay(1000);
	while(1);
}
1 Like