Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:16796fbfa94d, committed 2019-07-06
- Comitter:
- Sergey_K
- Date:
- Sat Jul 06 07:22:50 2019 +0000
- Parent:
- 1:441cdd24333e
- Commit message:
- sd card and gsm
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 441cdd24333e -r 16796fbfa94d main.cpp
--- a/main.cpp Sat Jul 06 06:54:10 2019 +0000
+++ b/main.cpp Sat Jul 06 07:22:50 2019 +0000
@@ -6,76 +6,82 @@
#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;
+EventQueue sdEvent;
+//Инициализация потоков
Thread eventThread;
Thread gsmTask;
-EventQueue sdEvent;
Thread sdEventThread;
Thread sdLogWrite;
+//Флаги
+int timeCounter = 0;
int c;
-int stopThread = 1;
+bool pressedButton = false;
+uint8_t counter=0;
+int answerCounter = 0;
+int readTimeStart=0;
+//Буфферы
string timeBuffer;
-uint8_t counter=0;
+uint8_t request[50] = "Hello!I am SIM800,I send you message from stm32";
uint8_t *receiveMsg[50];
char answer[300];
-bool pressedButton = false;
-int i;
-int answerCounter = 0;
+char sslBuffer[1402];
+//Прерывания
InterruptIn button(BUTTON1);
+//Настройка пинов
DigitalOut myLed(LED1);
Serial sim800(GSMRX,GSMTX); //Serial communication of the sim800 and pc.
Serial pc(SERIAL_TX,SERIAL_RX);
+//Инициализации SD карты
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);
+ 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);
}
+//Отправка команд в модуль СИМ800
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);
+ myLed=!myLed;
+ message_t *message = mpool.alloc();
+ message->lcdData = request;
+ queue.put(message);
}
-
+//Запись сертификата с SD карты
void sdReadSsl()
{
sd.init();
@@ -93,16 +99,16 @@
}
-//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);
+ message_t *message = (message_t*)evt.value.p;
+ *receiveMsg = (uint8_t*)message->lcdData;
+ printf("lcdData: %s\n\r" , *receiveMsg);
if(timeCounter == 0)
{
timeCounter = 1;
@@ -110,9 +116,6 @@
}
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");
@@ -125,9 +128,8 @@
wait(3);
answerCounter=1;
sendAt("AT+CCLK?\r\n");
- wait(1);
+ wait(1.5);
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");
@@ -135,10 +137,8 @@
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);
@@ -149,14 +149,13 @@
sendAt("AT+HTTPTERM");
wait(1.5);
sendAt("AT+SAPBR=0,1");
- printf("Time: %s\n\r",timeBuffer);
+ pc.printf("Time: %s\n\r",timeBuffer);
mpool.free(message);
}
}
}
-
-
+//Запись логов
void sdWriteLog()
{
sd.init();
@@ -169,7 +168,7 @@
fs.unmount();
sd.deinit();
}
-
+//Прерывание на нажатие кнопки
void buttonPressed()
{
// sdEvent.call(&sdReadSsl);
@@ -178,33 +177,39 @@
pressedButton=true;
//sdReadSsl();
}
+
+//Прерывание на отжим кнопки
void buttonReleased()
{
myLed=!myLed;
pressedButton=false;
}
+//Задача для записи логов
void sdWork()
{
while(1)
{
- sdEvent.call(&sdWriteLog);
- wait(5);
+ sdEvent.call(&sdWriteLog);
+ wait(15);
}
- }
+}
//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)
{
@@ -217,17 +222,16 @@
}
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;
-
- }
+ if(sim800.readable())
+ {
+ if(readTimeStart == 1)
+ {
+ if(readTimeStart == 1) timeBuffer += char(c);
+ if(char(c) =='+') readTimeStart = 0;
+ }
+ if(char(c) =='"') readTimeStart = 1;
+ c = sim800.getc();
+ }
}
}
}
\ No newline at end of file