Get Adobe Flash player
FacebookTwitterGoogle+
English Arabic French German Italian Portuguese Russian Spanish

Did you know?

Electrocution is one of the top five causes of workplace deaths.
 

Help us stay online:

small donate

Hardware usedIn this article we will present project and complete C code in which we will send data to internet socket via AT commands and Siemens GPRS modem ES75. We used ATmega8. Project will teach you basic data sending and receiving commands from socket connection.

 

 

After you read Introduction to GPRS modems, understand how these modems are controlled and read AT commands GPRS connection tutorial using Siemens ES75 GPRS modem to learn GPRS AT commands you are ready to send some data via GPRS. We made simple state machine with one state for each command sending. Recognizing answers from modem is explained with more details at AVR string parsing – UART command recognition example.
To send data via GPRS you have to have opened socket that will receive data on server side. For this we used tool explained in article Simple but very useful UDP/SOCKET terminal development tool. In order to make this tool work properly with connections from internet you must have public IP on your server PC or configure your router to forward port that GPRS connection is configured to use to your PC with UDP/SOCKET terminal development tool running.
We configured UDP/SOCKET terminal development tool as server for socket connection on port 50099. ATmega8 microcontroller goes through state machine states configuring all GPRS parameters as explained in AT commands GPRS connection tutorial using Siemens ES75 GPRS modem. After connecting to server side, Atmega8 sends constant array of 6 bytes. This can be easily adjusted to your need to send for example readings from ADC or digital pins. After that code goes to SOCKET_READ state and read if some data are received from GPRS. If data are received, they are parsed to check if STAND BY string is received. If not, data continues to be sent. If STAND BY is received via GPRS, microcontroller disconnects from GPRS, goes to STAND_BY state which does nothing and turns complete PORTD to Vcc which in my case can be seen by turning on LEDS that are connected to this port.
Complete code is displayed below with key lines highlighted and you can download complete project and source code from this direct link:

/*****************************************************
Read more at www.Electronics-Base.com
You can get support about this code at forum 
http://www.electronics-base.com/forum/
Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 7.372800 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/
#include <mega8.h>
// Standard Input/Output functions
#include <stdio.h>
//must be included to use delay functions
#include <delay.h>
//must be included to use itoa ftoa functions
#include <stdlib.h >
//must be included with work with strings for example strstrf()
#include <string.h>
const char at[] = "at\r";
const char at_sics_conType[] = "at^sics=0,conType,GPRS0\r";
const char at_sics_inactTO[] = "AT^SICS=0,inactTO,\"0\"\r";
const char at_sics_passwd[] = "AT^SICS=0,passwd,\"gprs\"\r";
const char at_sics_apn[] = "AT^SICS=0,apn,\"internet\"\r";
const char at_sics_user[] = "at^sics=0,user,\"telenor\"\r";
const char at_siss_srvType[] = "at^siss=1,srvType,socket\r";
const char at_siss_conId[] = "at^siss=1,conId,0\r";
const char at_siss_addres[] = "at^siss=1,address,\"socktcp://147.91.175.168:50099\"\r";
    
const char at_siso[] = "at^siso=1\r";
const char at_sisw[] = "at^sisw=1,6\r";
const char at_sisr[] = "at^sisr=1,100\r";
const char at_sisc[] = "at^sisc=1\r";  
const char at_ans_OK[] = "OK"; 
const char at_ans_DATA_WRITTEN[] = "^SISW: 1, 1";   
const char at_ans_DATA_WRITE_READY[] = "^SISW: 1";
const char at_ans_DATA_READ[] = "^SISR: 1";    
const char at_ans_GO_STAND_BY[] = "STAND BY";      
//state machine variables
unsigned char state;
#define START               0
#define SOCKET_INIT_CONTYPE 1
#define SOCKET_INIT_INACTO  2
#define SOCKET_INIT_PASSWD  3
#define SOCKET_INIT_APN     4
#define SOCKET_INIT_USER    5
#define SOCKET_INIT_SRVTYPE 6
#define SOCKET_INIT_CONID   7
#define SOCKET_INIT_ADDRESS 8
#define SOCKET_OPEN         9
#define SOCKET_WRITE        10
#define DATA_WRITE          11
#define SOCKET_READ         12
#define SOCKET_CLOSE        13
#define STAND_BY            14
#define ANSWER_WAIT_LONG    8000
#define ANSWER_WAIT_SHORT   3000
#ifndef RXB8
#define RXB8 1
#endif
#ifndef TXB8
#define TXB8 0
#endif
#ifndef UPE
#define UPE 2
#endif
#ifndef DOR
#define DOR 3
#endif
#ifndef FE
#define FE 4
#endif
#ifndef UDRE
#define UDRE 5
#endif
#ifndef RXC
#define RXC 7
#endif
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 100
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index]=data;
   if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      };
   };
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
void clear_rx_buffer(void)
{
unsigned int temp;
for(temp=0;temp<RX_BUFFER_SIZE;temp++)
    {     
    rx_buffer[temp]=0;
    }
rx_wr_index=0;
rx_counter=0;
rx_buffer_overflow=0;
rx_rd_index=0;
}
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 19200
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x17;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
      {     
      switch (state) 
        {
        case START:
            putsf(at); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_CONTYPE;
            else
                state=START;
            break;
        case SOCKET_INIT_CONTYPE:
            putsf(at_sics_conType); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_INACTO;
            else
                state=SOCKET_CLOSE;
            break;
          
        case SOCKET_INIT_INACTO:
            putsf(at_sics_inactTO); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_PASSWD;
            else
                state=START;
            break;
             
        case SOCKET_INIT_PASSWD:
            putsf(at_sics_passwd); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_APN;
            else
                state=START;
            break;  
            
        case SOCKET_INIT_APN:
            putsf(at_sics_apn); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_USER;
            else
                state=START;
            break;
        case SOCKET_INIT_USER:
            putsf(at_sics_user); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_SRVTYPE;
            else
                state=START;
            break;
        case SOCKET_INIT_SRVTYPE:
            putsf(at_siss_srvType); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_CONID;
            else
                state=START;
            break;
        case SOCKET_INIT_CONID:
            putsf(at_siss_conId); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_INIT_ADDRESS;
            else
                state=START;
            break;
        case SOCKET_INIT_ADDRESS:
            putsf(at_siss_addres); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_OPEN;
            else
                state=START;
            break;
         case SOCKET_OPEN:
            putsf(at_siso); 
            delay_ms(ANSWER_WAIT_LONG); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_WRITE;
            else
                state=START;
            break;
        case SOCKET_WRITE:
            putsf(at_sisw); 
            delay_ms(ANSWER_WAIT_LONG); 
            if(strstrf(rx_buffer,at_ans_DATA_WRITE_READY))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=DATA_WRITE;
            else
                state=START;
            break;     
            
        case DATA_WRITE:
            puts("123456"); 
            delay_ms(ANSWER_WAIT_LONG);  
            if(strstrf(rx_buffer,at_ans_DATA_WRITTEN))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_READ;
            else
                state=START;
            break;            
                       
        case SOCKET_READ:
            putsf(at_sisr); 
            delay_ms(ANSWER_WAIT_LONG);  
            if(strstrf(rx_buffer,at_ans_DATA_READ)) 
                {
                //searches the string str1 for the first occurrence of the string str2.
                //If there is a match, returns a pointer to the character in str1 where str2 begins.
                //If there is no match, returns a NULL pointer. 
                //puts(rx_buffer);
                if(strstrf(rx_buffer,at_ans_GO_STAND_BY))
                    {
                    clear_rx_buffer();
                    state=STAND_BY; 
                    break; 
                    } 
                else
                    state=SOCKET_WRITE;   
                }
            else
                state=START;
            break;             
        case SOCKET_CLOSE:
            putsf(at_sisc); 
            delay_ms(ANSWER_WAIT_SHORT); 
            if(strstrf(rx_buffer,at_ans_OK))
            //searches the string str1 for the first occurrence of the string str2.
            //If there is a match, returns a pointer to the character in str1 where str2 begins.
            //If there is no match, returns a NULL pointer.   
                state=SOCKET_OPEN;
            else
                state=START;
            break;            
        case STAND_BY:
            //puts("finito");   
            PORTD=0xFF;
            delay_ms(ANSWER_WAIT_SHORT); 
            break;   
            
        }; 
      clear_rx_buffer();  
      };
}

Video showing this code in action is displayed below.



UDP/SOCKET terminal development tool is controlled on remote server via Team viewer. We have connected com port terminal - development tool to sniff all the data coming from Modem to Microcontroller. TX pin of MAX232 is not connected so we don’t disturb Atmega8-GPRS modem communication. RX pin of MAX232 is connected to RX pin of Atmega8. Complete functionality of this project is achieved with microcontroller and GPRS modem alone and terminal is here only to read what is happening.
Hardware used is shown on picture below.

Hardware used