forked from jacoblai/Coolpy5SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARDUINO_HTTP_GET_传感器示例.ino
109 lines (96 loc) · 2.21 KB
/
ARDUINO_HTTP_GET_传感器示例.ino
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
/*
Power By Coolpy
Site:http://icoolpy.com
This example code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[6];
//服务器域名或IP地址
char server[] = "192.168.1.105";
//服务器端口号
int port = 6543;
//UserKey用户密钥(必改项)
char ukey[] = "51dd40af-135f-4733-89ff-a07615282812";
//Hub ID(必改项)
char hub[]="5";
//Node ID(必改项)
char node[]="8";
//数据结点Key值(必改项)
char dpkey[] = "2016-07-24T18:57:37.1834777Z";
EthernetClient client;
void setup() {
Serial.begin(9600);
mac[0]=random(1, 254);
mac[1]=random(1, 254);
mac[2]=random(1, 254);
mac[3]=random(1, 254);
mac[4]=random(1, 254);
mac[5]=random(1, 254);
if (Ethernet.begin(mac) == 0) {
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(1000);
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
// Make a HTTP request:
client.print("GET /api/hub/");
client.print(hub);
client.print("/node/");
client.print(node);
client.print("/datapoint/");
client.print(dpkey);
client.println(" HTTP/1.1");
client.print("Host:");
client.println(server);
client.println("Accept:");
client.print("U-ApiKey:");
client.println(ukey);
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
String inStr="";
boolean isOnData=false;
int packer = 0;
void loop()
{
if (client.available()) {
char c = client.read();
if(c == '{'){
if(!isOnData){
inStr = "";
isOnData = true;
}else{
packer++;
}
inStr += c;
}else if(c == '}'){
inStr += c;
if(packer>0){
packer--;
}else{
isOnData = false;
//完成操作显示所有数据
Serial.println("");
Serial.println(inStr);
}
}else if(isOnData){
inStr += c;
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
}