Skip to content

Commit 58093a4

Browse files
committed
add SIM800 HTTP Example
1 parent 6cd8ce1 commit 58093a4

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include "M5Stack.h"
2+
#include "TFTTerminal.h"
3+
TFT_eSprite TerminalBuff = TFT_eSprite(&M5.Lcd);
4+
TFTTerminal terminal(&TerminalBuff);
5+
6+
String waitRevice()
7+
{
8+
String recvStr;
9+
do
10+
{
11+
recvStr = Serial2.readStringUntil('\n');
12+
} while (recvStr.length() == 0);
13+
Serial.println(recvStr);
14+
terminal.println(recvStr);
15+
return recvStr;
16+
}
17+
18+
void sendATCMD(String cmdStr)
19+
{
20+
Serial2.print(cmdStr);
21+
delay(100);
22+
}
23+
24+
int sendATCMDAndRevice(String cmdStr)
25+
{
26+
delay(1000);
27+
Serial2.print(cmdStr);
28+
delay(100);
29+
waitRevice();
30+
String recvStr = waitRevice();
31+
delay(100);
32+
if (recvStr.indexOf("OK") != -1)
33+
{
34+
return 0;
35+
}
36+
else
37+
{
38+
return -1;
39+
}
40+
}
41+
42+
void GET() {
43+
terminal.println("GET Request");
44+
sendATCMD("AT?\r\n");
45+
delay(100);
46+
Serial2.flush();
47+
sendATCMDAndRevice("AT+CGATT?\r\n");
48+
sendATCMDAndRevice("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");
49+
sendATCMDAndRevice("AT+SAPBR=3,1,\"APN\",\"CMNET\"\r\n");
50+
sendATCMDAndRevice("AT+SAPBR=0,1\r\n");
51+
sendATCMDAndRevice("AT+SAPBR=1,1\r\n");
52+
sendATCMDAndRevice("AT+HTTPINIT\r\n");
53+
sendATCMDAndRevice("AT+HTTPPARA=\"URL\",\"http://api.m5stack.com/v1\"\r\n");
54+
sendATCMDAndRevice("AT+HTTPACTION=0\r\n");
55+
// while(!Serial2.available());
56+
delay(5000);
57+
sendATCMDAndRevice("AT+HTTPREAD\r\n");
58+
while(Serial2.available()){
59+
String recvStr;
60+
recvStr = Serial2.readStringUntil('\n');
61+
Serial.println(recvStr);
62+
terminal.println(recvStr);
63+
}
64+
sendATCMDAndRevice("AT+HTTPTERM\r\n");
65+
}
66+
67+
// void POST() {
68+
// terminal.println("POST Request");
69+
// sendATCMD("AT?\r\n");
70+
// delay(100);
71+
// Serial2.flush();
72+
// sendATCMDAndRevice("AT+CGATT?\r\n");
73+
// sendATCMDAndRevice("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");
74+
// sendATCMDAndRevice("AT+SAPBR=3,1,\"APN\",\"CMNET\"\r\n");
75+
// sendATCMDAndRevice("AT+SAPBR=0,1\r\n");
76+
// sendATCMDAndRevice("AT+SAPBR=1,1\r\n");
77+
// sendATCMDAndRevice("AT+HTTPINIT\r\n");
78+
// sendATCMDAndRevice("AT+HTTPPARA=\"CID\",1\r\n");
79+
// sendATCMDAndRevice("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\"\r\n");
80+
// sendATCMDAndRevice("AT+HTTPPARA=\"URL\",\"https://getman.cn/api/request\"\r\n");
81+
// sendATCMD("AT+HTTPDATA=10,10000\r\n");
82+
// sendATCMD("M5STACK,GO\r\n");
83+
// sendATCMDAndRevice("AT+HTTPACTION=1\r\n");
84+
// delay(5000);
85+
// sendATCMDAndRevice("AT+HTTPREAD\r\n");
86+
// while(Serial2.available()){
87+
// String recvStr;
88+
// recvStr = Serial2.readStringUntil('\n');
89+
// Serial.println(recvStr);
90+
// terminal.println(recvStr);
91+
// }
92+
// sendATCMDAndRevice("AT+HTTPTERM\r\n");
93+
// }
94+
95+
void setup()
96+
{
97+
M5.begin();
98+
Serial2.begin(115200, SERIAL_8N1, 16, 17);
99+
Serial2.flush();
100+
delay(1000);
101+
M5.Lcd.fillRect(0, 0, 320, 240, TFT_BLACK);
102+
M5.Lcd.fillRect(0, 0, 320, 40, TFT_WHITE);
103+
M5.Lcd.setTextColor(TFT_BLACK);
104+
M5.Lcd.setTextDatum(TC_DATUM);
105+
M5.Lcd.drawString("SIM800L HTTP GET/POST", 160, 10, 4);
106+
M5.Lcd.setTextDatum(TL_DATUM);
107+
M5.Lcd.setTextColor(TFT_WHITE);
108+
TerminalBuff.createSprite(240,200);
109+
terminal.setGeometry(20,55,300,200);
110+
terminal.setFontsize(1);
111+
terminal.println("Press Btn A GET Request");
112+
terminal.println("Press Btn B POST Request");
113+
};
114+
115+
116+
void loop()
117+
{
118+
M5.update();
119+
if(M5.BtnA.wasPressed()) GET();
120+
if(M5.BtnC.wasPressed()) {
121+
String recvStr;
122+
recvStr = Serial2.readStringUntil('\n');
123+
Serial.println(recvStr);
124+
terminal.println(recvStr);
125+
};
126+
delay(10);
127+
}
128+
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include "TFTTerminal.h"
2+
3+
TFTTerminal::TFTTerminal(TFT_eSprite *dis_buff_ptr)
4+
{
5+
_dis_buff_ptr = dis_buff_ptr;
6+
memset(discharbuff, 0, 55 * 60);
7+
}
8+
9+
TFTTerminal::~TFTTerminal()
10+
{
11+
12+
}
13+
14+
15+
void TFTTerminal::setcolor( uint16_t color, uint16_t bk_color )
16+
{
17+
_color = color;
18+
_bkcolor = bk_color;
19+
}
20+
21+
void TFTTerminal::setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h )
22+
{
23+
_win_x_pos = x;
24+
_win_y_pos = y;
25+
_win_w = w;
26+
_win_h = h;
27+
28+
_line_x_limit = _win_w / _font_x_size;
29+
_line_y_limit = _win_h / _font_y_size;
30+
}
31+
32+
void TFTTerminal::setFontsize(uint8_t size)
33+
{
34+
size = ( size == 0 ) ? 1 : size;
35+
_font_x_size = 5 * size ;
36+
_font_y_size = 6 * size ;
37+
_fontSize = size;
38+
39+
_line_x_limit = _win_w / _font_x_size;
40+
_line_y_limit = _win_h / _font_y_size;
41+
}
42+
43+
size_t TFTTerminal::write(uint8_t chardata)
44+
{
45+
46+
bool flush_page_flag = false;
47+
uint8_t dis_y_pos = 0;
48+
49+
if ((chardata == '\r') || (chardata == '\n'))
50+
{
51+
xpos = 0;
52+
ypos++;
53+
ypos = ypos % 60;
54+
memset(discharbuff[ypos % 60], 0, 55);
55+
return 1;
56+
}
57+
else if(xpos >= _line_x_limit)
58+
{
59+
xpos = 0;
60+
ypos++;
61+
ypos = ypos % 60;
62+
memset(discharbuff[ypos % 60], 0, 55);
63+
}
64+
65+
discharbuff[ypos][xpos] = chardata;
66+
xpos++;
67+
68+
if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
69+
{
70+
dispos = ypos - _line_y_limit;
71+
flush_page_flag = true;
72+
}
73+
else if ((dispos <= ypos) && ((ypos - dispos) <= _line_y_limit))
74+
{
75+
dis_y_pos = ypos - dispos;
76+
flush_page_flag = false;
77+
}
78+
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
79+
{
80+
dispos = 60 - ( _line_y_limit - ypos );
81+
flush_page_flag = true;
82+
}
83+
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
84+
{
85+
dis_y_pos = 60 - ( dispos - ypos );
86+
flush_page_flag = false;
87+
}
88+
89+
dispos = dispos % 60;
90+
91+
_dis_buff_ptr->setTextColor(_color);
92+
_dis_buff_ptr->setTextSize(_fontSize);
93+
94+
if( flush_page_flag )
95+
{
96+
_dis_buff_ptr->fillSprite(_bkcolor);
97+
98+
for (size_t i = 0; i < _line_y_limit; i++)
99+
{
100+
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
101+
}
102+
}
103+
else
104+
{
105+
_dis_buff_ptr->drawChar(chardata, ( xpos - 1 ) * _font_x_size, dis_y_pos * _font_y_size );
106+
}
107+
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);
108+
109+
return 1;
110+
111+
}
112+
113+
size_t TFTTerminal::write(const uint8_t *buffer, size_t size)
114+
{
115+
116+
while ((size != 0) && (*buffer != '\0'))
117+
{
118+
if ((*buffer == '\r') || (*buffer == '\n'))
119+
{
120+
xpos = 0;
121+
ypos++;
122+
ypos = ypos % 60;
123+
memset(discharbuff[ypos % 60], 0, 55);
124+
buffer++;
125+
size--;
126+
continue;
127+
}
128+
else if(xpos >= _line_x_limit)
129+
{
130+
xpos = 0;
131+
ypos++;
132+
ypos = ypos % 60;
133+
memset(discharbuff[ypos % 60], 0, 55);
134+
}
135+
discharbuff[ypos][xpos] = *buffer;
136+
xpos++;
137+
buffer++;
138+
size--;
139+
}
140+
141+
if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
142+
{
143+
dispos = ypos - _line_y_limit;
144+
}
145+
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
146+
{
147+
dispos = 60- ( _line_y_limit - ypos );
148+
}
149+
150+
dispos = dispos % 60;
151+
152+
_dis_buff_ptr->setTextColor(_color);
153+
_dis_buff_ptr->setTextSize(_fontSize);
154+
_dis_buff_ptr->fillSprite(_bkcolor);
155+
//_dis_buff_ptr->fillRect(_win_x_pos, _win_y_pos, _win_w, _win_h, _bkcolor);
156+
for (size_t i = 0; i < _line_y_limit; i++)
157+
{
158+
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
159+
}
160+
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);
161+
return 1;
162+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _TFTTERMINAL_H_
2+
#define _TFTTERMINAL_H_
3+
4+
#include <M5Stack.h>
5+
#include <Print.h>
6+
7+
class TFTTerminal : public Print
8+
{
9+
private:
10+
TFT_eSprite *disptr;
11+
char discharbuff[60][55];
12+
uint32_t xpos = 0,ypos = 0, dispos = 0;
13+
TFT_eSprite* _dis_buff_ptr = NULL;
14+
uint16_t _bkcolor = TFT_BLACK;
15+
uint16_t _color = TFT_GREEN;
16+
17+
uint16_t _win_x_pos = 0,_win_y_pos = 0,_win_w = 320,_win_h = 240;
18+
uint16_t _font_x_size = 6,_font_y_size = 8;
19+
uint8_t _fontSize = 0;
20+
uint16_t _line_x_limit = 53,_line_y_limit = 30;
21+
22+
public:
23+
TFTTerminal(TFT_eSprite *dis_buff_ptr);
24+
~TFTTerminal();
25+
26+
void setcolor( uint16_t color, uint16_t bk_color );
27+
void setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h );
28+
void setFontsize(uint8_t size);
29+
30+
size_t write(uint8_t) ;
31+
size_t write(const uint8_t *buffer, size_t size);
32+
};
33+
34+
35+
#endif

0 commit comments

Comments
 (0)