123

Dependencies:  

main.cpp

Committer:
Sergey_K
Date:
2019-07-06
Revision:
1:441cdd24333e
Parent:
0:f7e73515c3fc
Child:
2:16796fbfa94d

File content as of revision 1:441cdd24333e:

//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 "SDBlockDevice.h"
#include "FATFileSystem.h"
#define GSMRX PA_9
#define GSMTX PA_10 
#define SAMPLE_FLAG1 (1UL << 0)
#define SAMPLE_FLAG2 (1UL << 9)
#define BUFFER_SIZE 50

int timeCounter = 0;
typedef struct {
uint8_t *lcdData;
char logMessage;      
} message_t;
uint8_t request[50] = "Hello!I am SIM800,I send you message from stm32";
Queue<message_t, 32> queue;
MemoryPool<message_t, 100> mpool;
EventQueue evq;
Thread eventThread;
Thread gsmTask;
EventQueue sdEvent;
Thread sdEventThread;
Thread sdLogWrite;
int c;
int stopThread = 1;
string timeBuffer;
uint8_t counter=0;
uint8_t *receiveMsg[50];
char answer[300];
bool pressedButton = false;
int i;
int answerCounter = 0;
InterruptIn button(BUTTON1);
DigitalOut myLed(LED1);
Serial sim800(GSMRX,GSMTX);                                   //Serial communication of the sim800 and pc.
Serial pc(SERIAL_TX,SERIAL_RX);
SDBlockDevice sd(D11, D12, D13, D4);
FATFileSystem fs("sd");
char sslBuffer[1402];
int a;

void simTime()
{   
        sim800.printf("AT\r\n");
        wait(1);
        sim800.printf("AT+CLTS=1\r\n");
        wait(1);
        sim800.printf("AT+CNTPCID=1\r\n");
        wait(1);
        sim800.printf("AT+CNTP=ntp1.vniiftri.ru,7\r\n");
        wait(1);
        sim800.printf("AT+CNTP\r\n");
        wait(1);
        sim800.printf("AT+CCLK?\r\n");
        wait(1); 
        sim800.printf("AT&W\r\n");
        wait(1); 
}
void sendAt(const char *AT)
{
  sim800.printf("%s\r\n",AT);
  wait(1);
}
//LCD DISPLAY WORK++++++++++++++++++++++++++++++++++
void lcdStartWork() {
        if (stopThread==0) wait(3);
        myLed=!myLed;       
        message_t *message = mpool.alloc();
        request[47]=counter;
        message->lcdData = request; 
        queue.put(message);      
}

void sdReadSsl()
{
    sd.init();
    fs.mount(&sd);
    FILE *fp = fopen("/sd/ssl.txt", "r");   
    if (fp == NULL) {
        printf("Fail open txt file\r\n");
    }
    fread(sslBuffer, sizeof(sslBuffer), 1, fp );
    fclose(fp);
    printf("File successfully read!\r\n");
    printf(" Your data:\n%s\n",sslBuffer);
    fs.unmount();
    sd.deinit();
    
}

//GSM MODULE WORK_________________________________
void gsmStartWork() {
    while(1)
    {
        osEvent evt = queue.get();
        if (evt.status == osEventMessage) 
        {
        message_t *message = (message_t*)evt.value.p;
        *receiveMsg = (uint8_t*)message->lcdData; 
        printf("lcdData: %s\n\r"   , *receiveMsg);   
        if(timeCounter == 0)
        {
            timeCounter = 1;
            simTime();
        }
        
        sendAt("AT\r\n");
        sendAt("AT+CCLK?\r\n");


      /*  sim800.printf("AT+FSDEL=C:\\USER\\SSL1.CRT\n\r");
        wait(0.5);
        sim800.printf("AT+FSCREATE=C:\\USER\\SSL1.CRT\n\r");
        wait(1.5);
        sim800.printf("AT+FSWRITE=C:\\USER\\SSL1.CRT,0,1402,10\n\r");
        wait(0.2);
        sim800.printf("%s\n\r",sslBuffer);
        wait(1);*/
        sim800.printf("AT+SSLSETCERT=C:\\USER\\SSL1.CRT\n\r");
        wait(3);
        answerCounter=1;
        sendAt("AT+CCLK?\r\n");
        wait(1);
        answerCounter=0;
        a=0;
        sendAt("AT+SAPBR=3,1,Contype,GPRS\r\n");     
        sendAt("AT+SAPBR=3,1,APN,internet.mts.ru\r\n");
        sendAt("AT+SAPBR=3,1,USER,mts\r\n");
        sendAt("AT+SAPBR=3,1,PWD,mts");
        sendAt("AT+SAPBR=1,1");
        sendAt("AT+HTTPINIT");
        sendAt("AT+HTTPSSL=1");
        //sendAt("AT+HTTPPARA =REDIR,1");
        sendAt("AT+HTTPPARA=URL,157.230.245.76:1880/message");
        sendAt("AT+HTTPPARA=CONTENT,application/octet-stream");
      
        sendAt("AT+HTTPDATA=51,10000");
        sim800.write(*receiveMsg,51,NULL,SERIAL_EVENT_TX_COMPLETE);
        wait(0.5);
        sendAt("AT+HTTPACTION=1");
        wait(1.5);
        sendAt("AT+HTTPTERM");
        wait(1.5);
        sendAt("AT+HTTPTERM");
        wait(1.5);
        sendAt("AT+SAPBR=0,1");
        printf("Time: %s\n\r",timeBuffer);   
        mpool.free(message);  
            }
        }
}



void sdWriteLog()
{
    sd.init();
    fs.mount(&sd);
    printf(" Log write start!\n\n");
    FILE *fp2 = fopen("/sd/log.txt", "a+");
    fprintf(fp2, "Hello World!%s\r\n",timeBuffer);
    fclose(fp2);
    printf(" Log write DONE!\n\n");
    fs.unmount();
    sd.deinit();
}

void buttonPressed()
{   
   // sdEvent.call(&sdReadSsl);
    counter++;
    evq.call(&lcdStartWork);
    pressedButton=true;
    //sdReadSsl();
}
void buttonReleased()
{
    myLed=!myLed;
    pressedButton=false; 
}
void sdWork()
{
    while(1)
    {
    sdEvent.call(&sdWriteLog);
    wait(5);
    }
    }
//MAIN-----------------------------------------------    
int main()
{
    pc.baud(9600);
    sim800.baud(9600);
    pc.printf("*******GPRS TEST*******\r\n");
    sdReadSsl();
    button.rise(&buttonReleased);
    button.fall(&buttonPressed);
    gsmTask.start(gsmStartWork);
    sdLogWrite.start(sdWork);
    eventThread.start(callback(&evq, &EventQueue::dispatch_forever));
    sdEventThread.start(callback(&sdEvent,&EventQueue::dispatch_forever));
    
    while(1) {
        if(answerCounter==0)
        {
            if(pc.readable()) { 
            sim800.putc(pc.getc()); 
            } 
            if(sim800.readable()) { 
            pc.putc(sim800.getc()); 
            } 
        }
        else
        {
             if(sim800.readable()) { 
             if(a == 1)
             {
             
             if(a == 1) timeBuffer += char(c);
             if(char(c) =='+') a = 0;
             }
             c = sim800.getc(); 
             if(char(c) =='"') a = 1;
             
        } 
        }
    }
}