ESP8266——OneNet的GET指令及C语言代码

GET指令的HTTP数据报文

1
2
3
4

GET /devices/你的设备号/datastreams/你的数据流名称 HTTP/1.1\r\n
Host: api.heclouds.com\r\n
api-key: 你的apikey\r\n\r\n

我更改了ESP8266的波特率,改成了4800的;

1
AT+UART=4800,8,1,0,0\r\n //更改esp8266的波特率
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 #include "reg52.h"

typedef unsigned int uint;
typedef unsigned char uchar;

#define DATASIZE 110

sbit LED = P2^0;

uchar datalen = 0;
uchar value;
uchar cnt = 0;
uchar recbuffer[DATASIZE];



void Delay600ms() //@12.000MHz
{
unsigned char i, j, k;

i = 5;
j = 144;
k = 71;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}




void UsartInit(void) {
SCON = 0x50;
TMOD = 0x20;
PCON = 0x80;
TH1 = 0xF3;
TL1 = 0xF3;
ES = 1;
EA = 1;
TR1 = 1;
}
void SendUart(uchar value) {
ES = 0;
SBUF = value;
while(!TI);
TI = 0;
ES = 1;
}

void SendString(uchar *str) {
while(*str != '\0') {
SendUart(*str);
str++;
}


}
void wificonnection() {
SendString("AT\r\n");
//SendString("AT+CWMODE=1\r\n");
//SendString("AT+RST");
SendString("AT+CWJAP=\"****\",\"******\"\r\n");
Delay600ms();
Delay600ms();

}

void TCPconnection() {
SendString("AT+CIPMUX=0\r\n");
Delay600ms();
Delay600ms();
SendString("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n");
Delay600ms();
Delay600ms();
SendString("AT+CIPMODE=1\r\n");
Delay600ms();
Delay600ms();
SendString("AT+CIPSEND\r\n");
Delay600ms();
Delay600ms();
}

void check() {
SendString("GET /devices/******/datastreams/*** HTTP/1.1\r\n");
SendString("Host: api.heclouds.com\r\n");
SendString("api-key: ********************\r\n\r\n");
Delay600ms();
}
void main() {
UsartInit();
wificonnection();
TCPconnection();
while(1) {
check();
Delay600ms();
if(value == '1') LED = 0;
else if(value == '0') LED = 1;
}
}

void Usart() interrupt 4 {
uchar rec;
if(RI != 0) {
rec = SBUF;
RI = 0;
}
if(rec == '{') cnt = 1;
if(cnt == 1) {
if(rec != '}') {
recbuffer[datalen++] = rec;
}
else {
cnt = 0;
value = recbuffer[datalen - 1];
//SendString(recbuffer);
//SendString("\r\n");
//SendUart(value);
//SendString("\r\nok\r\n");
datalen = 0;
}
}
}
------ 本文结束 ------
0%