123

Dependencies:  

main.cpp

Committer:
Sergey_K
Date:
2019-07-02
Revision:
0:f7e73515c3fc
Child:
1:441cdd24333e

File content as of revision 0:f7e73515c3fc:

//It is the code of the GPRS communication by the use of the Sim800 module.
//This code has work is "to upload data on data.sparkfun.com in every 5 seconds.
//To check the uploaded data the URL is "data.sparkfun.com/streams/roYVdJgWrXUpxVpDYJpA".
 
#include "mbed.h"
#include <string> 
#include "gsmWork.h"
#include "lcdWork.h"
#define GSMRX PA_9
#define GSMTX PA_10 
#define SAMPLE_FLAG1 (1UL << 0)
#define SAMPLE_FLAG2 (1UL << 9)



typedef struct {
uint8_t *lcdData;      
} mail_t;

Mail<mail_t, 32> mail_box;


uint8_t request[50] = "Hello!I am SIM800,I send you request from stm32";
uint8_t counter=0x00;
uint8_t receiveMsg[50];
char answer[300];
//(const char*) receiveMsg[10];
bool pressedButton = false;
int i;
InterruptIn button(USER_BUTTON);

DigitalOut myLed(LED1);


Serial sim800(GSMRX,GSMTX);                                   //Serial communication of the sim800 and pc.
Serial pc(SERIAL_TX,SERIAL_RX);

 
Thread gsmTask;
Thread lcdTask;

void buttonPressed()
{
     counter++;
    request[49]=counter;
   pressedButton=true;


}
void buttonReleased()
{
    myLed=!myLed;
    pressedButton=false;
  
}

void simAnswer()
{
 /*if (answer.indexOf("RDY") > -1)
  {
    
   
   
  }
else if (answer.indexOf("OK") > -1)
  {
    
  }
else if (answer.indexOf("ERROR") > -1)
  {
   
  }
else if(answer!="")
  {

  }*/
  
}



    
void sendAt(const char *AT,int ATdelay)
{
  sim800.printf("%s\r\n",AT);
}

void startGPRS()
{
  
}
//LCD DISPLAY WORK++++++++++++++++++++++++++++++++++
void lcdStartWork() {
  
    while(1)
    {
           if(pressedButton==true)  {
        
        
       
        myLed=!myLed;
       
          mail_t *mail = mail_box.alloc();
          mail->lcdData = request; 
          mail_box.put(mail);
        wait(1);
        pressedButton=false;
         
    
    }
wait(0.3);
    }
     
}
//GSM MODULE WORK_________________________________
void gsmStartWork() {
    
       while(1)
    {
        osEvent evt = mail_box.get();;
        if (evt.status == osEventMail) {
              mail_t *mail = (mail_t*)evt.value.p;
            uint8_t*receiveMsg = mail->lcdData;
            printf("lcdData: %s\n\r"   , *receiveMsg);
            mail_box.free(mail);
            sim800.printf("AT\r\n");
wait(1);
  if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sim800.printf("AT+SAPBR=3,1,Contype,GPRS\r\n");
  
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
            
  sim800.printf("AT+SAPBR=3,1,APN,internet.mts.ru\r\n");
wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sim800.printf("AT+SAPBR=3,1,USER,mts\r\n");
 wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+SAPBR=3,1,PWD,mts",0);
wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+SAPBR=1,1",0);
 wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+HTTPINIT",0);
  wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+HTTPPARA=CID,1",0);
wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
    sendAt("AT+HTTPPARA=URL,157.230.245.76:1880/message",0);
   wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+HTTPPARA=CONTENT,application/octet-stream",0);
  wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+HTTPDATA=51,10000",0);
  wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sim800.write(*receiveMsg,51,NULL,SERIAL_EVENT_TX_COMPLETE);
 wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+HTTPACTION=1",0);
 wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+HTTPTERM",0);
  wait(1);
if(sim800.readable()) {
            pc.putc(sim800.getc());
            }
  sendAt("AT+SAPBR=0,1",0);
             
    }
    }

}
//MAIN-----------------------------------------------    
int main()
{
      pc.baud(9300);
    sim800.baud(9600);
    pc.printf("*******GPRS TEST*******\r\n");

     button.rise(&buttonReleased);
     button.fall(&buttonPressed);
     
    gsmTask.start(gsmStartWork);
    lcdTask.start(lcdStartWork);
   
  /*  pc.printf("*******GPRS TEST*******\r\n");
 
   
       
        sim800.printf("AT+CGATT?\r\n");                     //To check the GPRS is attached or not.
            sim800.scanf("%s%s",x,y);
            
        
        sim800.printf("AT+CIPSHUT\r\n");                    //To reset the IP session if any.
            sim800.scanf("%s%s",x,y);
            pc.printf("%s   %s\r\n",x,y);
  
        sim800.printf("AT+SAPBR=3,1,Contype,GPRS\r\n");     //To Set the connection type to GPRS.
            sim800.scanf("%s",x);
            pc.printf("%s\r\n",x);
        
        sim800.printf("AT+SAPBR=3,1,APN,www\r\n");          //To Set the APN to to "www" , It might be different for you, and depends on the network.
            sim800.scanf("%s",x);
            pc.printf("%s\r\n",x);
        
        sim800.printf("AT+SAPBR =1,1\r\n");                 //To Enable the GPRS.
            sim800.scanf("%s",x);
            pc.printf("%s\r\n",x);
        
        sim800.printf("AT+SAPBR =2,1\r\n");                 //To get the IP address.
            sim800.scanf("%s%s%s",x,y,z);
            pc.printf("%s   %s  %s\r\n",x,y,z); 
        
        
        sim800.printf("AT+HTTPINIT\r\n");                   //To enable the HTTP mode.
            sim800.scanf("%s",x);
            pc.printf("%s\r\n",x);
        
        sim800.printf("AT+HTTPPARA=CID,1\r\n");             //To sets up HTTP parameters for the HTTP call.
            sim800.scanf("%s",x);
            pc.printf("%s\r\n",x);
            
        
        float lng,lat = 0;
        while(lat == 0)
        {                            
            sim800.printf("AT+CIPGSMLOC=1,1\r\n");          //To check the Location of GSM in Co-Ordinates(Longitude,Latitude).
            sim800.scanf("%s%s%s",x,y,z);
            sscanf(y,"0,%f,%f",&lng,&lat);                  //To sperate out the value of longitude and latitude from the string 'y'.       
        }
        pc.printf("%0.6f   %0.6f",lng,lat);
        */
        /*
        while(1)
        {
        
            sim800.printf("AT+HTTPPARA=URL,data.sparkfun.com/input/roYVdJgWrXUpxVpDYJpA?private_key=jkgpNMYr81cgMGgbypgr&co_lat=%0.6f&co_long=%0.6f&ldr=%0.2f&pot=%0.2f\r\n",lat,lng,potval,ldrval);
                                                            //The above command is for "To Set the url to the address of the webpage you want to access".
                sim800.scanf("%s",x);
                pc.printf("%s\r\n",x);
            
            sim800.printf("AT+HTTPACTION=0\r\n");           //To Start the HTTP GET session, by giving this command.
                sim800.scanf("%s%s%s",x,y,z);
                pc.printf("%s   %s  %s\r\n",x,y,z);
        
            sim800.printf("AT+HTTPREAD\r\n");               //To read the received data.
                sim800.scanf("%s%s%s%s%s",x,y,z,l,m);
                pc.printf("%s   %s  %s  %s  %s\r\n",x,y,z,l,m);
            
            wait(5);
        }*/
         while(1) {
        if(pc.readable()) {
            sim800.putc(pc.getc());
        }
        if(sim800.readable()) {
            pc.putc(sim800.getc());
        }
    }
}