|
|
发表于 2016-4-17 11:09:36
|
显示全部楼层
参考下这个吧!- #include <EtherCard.h>
-
- static byte mymac[] = {0x74,0x69,0x69,0x2D,0x30,0x21};
- const char server[] PROGMEM = "api.yeelink.net";
-
- byte Ethernet::buffer[700];
- byte sensorData;
- static uint32_t lastConnectionTime = 0;
- const unsigned long postingInterval = 100;
-
- const char SensorID[] PROGMEM = "171286";
- const char APIKey[] PROGMEM = "2c4265c045794880588ffeea97d70216";// replace your yeelink api key here
- const char DeviceID[] PROGMEM = "153110"; // replace your device ID
-
- Stash stash;
- void setup () {
- InitIP();
- }
-
- void loop() {
-
- ether.packetLoop(ether.packetReceive());
- if(millis() - lastConnectionTime > postingInterval){
- GetData();
- }
- }
-
- void GetData()
- {
- float demo = random(0,500);
- word one = random(0,500);
- String msje;
-
- if (demo < 250){
- msje = "low";
- }
- else{
- msje = "high";
- }
-
- byte sd = stash.create();
- stash.print("demo,");
- stash.println(demo);
- stash.print("one,");
- stash.println(one);
- stash.print("mensaje,");
- stash.println(msje);
- stash.save();
-
- Stash::prepare(PSTR("GET /v1.0/device/$F/sensor/$F/datapoints HTTP/1.1" "\r\n"
- "Host:api.yeelink.net" "\r\n"
- "U-ApiKey: $F" "\r\n"
- "Content-Length:$D" "\r\n"
- "\r\n""$H"),DeviceID,SensorID,APIKey,stash.size(),sd);
-
- sensorData = ether.tcpSend();
-
- const char* reply = ether.tcpReply(sensorData);
- const char* re;
- char t[3];
- if(reply != 0) {
- Serial.println(reply);
- Serial.println(strstr(reply,"{"));
-
- if(strstr(reply,"1}") != NULL)
- { Serial.println("Led state: ON");return;}
-
- if(strstr(reply,"0}") != NULL)
- { Serial.println("Led state: OFF");return;}
-
- }else
- {
- // Serial.println("2");
- }
-
- lastConnectionTime = millis();
- }
-
-
- void InitIP()
- {
- Serial.begin(57600);
- Serial.println("Client Demo");
- Serial.println();
-
- if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
- Serial.println( "Failed to access Ethernet controller");
- else
- Serial.println("Ethernet controller initialized");
- Serial.println();
-
- if (!ether.dhcpSetup())
- Serial.println("Failed to get configuration from DHCP");
- else
- Serial.println("DHCP configuration done");
-
- ether.printIp("IP Address:\t", ether.myip);
- ether.printIp("Netmask:\t", ether.netmask);
- ether.printIp("Gateway:\t", ether.gwip);
- Serial.println();
-
- if (!ether.dnsLookup(server))
- Serial.println("DNS failed");
- else
- Serial.println("DNS resolution done");
- ether.printIp("SRV IP:\t", ether.hisip);
- Serial.println();
- }
复制代码 |
|