Сергей Купавцев
/
123
123
Diff: main.cpp
- Revision:
- 1:441cdd24333e
- Parent:
- 0:f7e73515c3fc
- Child:
- 2:16796fbfa94d
--- a/main.cpp Tue Jul 02 07:47:25 2019 +0000 +++ b/main.cpp Sat Jul 06 06:54:10 2019 +0000 @@ -4,287 +4,230 @@ #include "mbed.h" #include <string> -#include "gsmWork.h" -#include "lcdWork.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.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; +#define BUFFER_SIZE 50 -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]; +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]; -//(const char*) receiveMsg[10]; bool pressedButton = false; int i; -InterruptIn button(USER_BUTTON); - +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; - -Thread gsmTask; -Thread lcdTask; - -void buttonPressed() +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) { - counter++; - request[49]=counter; - pressedButton=true; - - + 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 buttonReleased() + +void sdReadSsl() { - myLed=!myLed; - pressedButton=false; - + 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(); + } -void simAnswer() -{ - /*if (answer.indexOf("RDY") > -1) - { - - - - } -else if (answer.indexOf("OK") > -1) - { - - } -else if (answer.indexOf("ERROR") > -1) - { - - } -else if(answer!="") - { +//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 sendAt(const char *AT,int ATdelay) +void sdWriteLog() { - sim800.printf("%s\r\n",AT); + 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 startGPRS() -{ - +void buttonPressed() +{ + // sdEvent.call(&sdReadSsl); + counter++; + evq.call(&lcdStartWork); + pressedButton=true; + //sdReadSsl(); } -//LCD DISPLAY WORK++++++++++++++++++++++++++++++++++ -void lcdStartWork() { - +void buttonReleased() +{ + myLed=!myLed; + pressedButton=false; +} +void sdWork() +{ 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); - + sdEvent.call(&sdWriteLog); + wait(5); } } - -} //MAIN----------------------------------------------- int main() { - pc.baud(9300); + pc.baud(9600); sim800.baud(9600); pc.printf("*******GPRS TEST*******\r\n"); - - button.rise(&buttonReleased); - button.fall(&buttonPressed); - + sdReadSsl(); + 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'. + 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()); + } } - pc.printf("%0.6f %0.6f",lng,lat); - */ - /* - while(1) + else { - - 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()); + 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; + + } } } } \ No newline at end of file