TA的每日心情 | 开心 2020-2-14 12:16 |
---|
签到天数: 827 天 连续签到: 1 天 [LV.10]以坛为家III
|
arduino+W5100+yelink平台实现温度上传,代码分享
#include <EtherCard.h>
#include <OneWire.h>
//
//18B20
#define ONEWIRE_PIN A0
OneWire ds(ONEWIRE_PIN);
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
char SensorID[] PROGMEM = "XXX"; // replace your sensor ID
// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// remote website name
char website[] PROGMEM = "api.yeelink.net";
////////////////////////////////////////////////////////////
// for yeelink api
char APIKey[] PROGMEM = "XXXXXXXXXXXXXXXXX"; // replace your yeelink api key here
char DeviceID[] PROGMEM = "XXX"; // replace your device ID
// assign a MAC IP gateway dns for the ethernet controller.
const uint16_t PostingInterval = 15000;// delay between 2 datapoints, 30s
// Some global variables
char sensorData[20];
uint8_t dataLength;
uint8_t Ethernet::buffer[300];
uint32_t lastConnectionTime = 0; // last time you connected to the server, in milliseconds
Stash stash;
void setup() {
Serial.begin(57600);
Serial.println("\n[yeelink client]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("My IP: ", ether.myip);
// ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
}
void loop() {
if (!ether.dhcpValid() && !ether.dhcpSetup())
Serial.println("DHCP failed");
ether.packetLoop(ether.packetReceive());
if(millis() - lastConnectionTime > PostingInterval){
get_Send_String();
send_Data();
}
}
void send_Data() {
// Create a Post for yeelink server,
// and send request saving sessionID
Stash::prepare(PSTR("POST /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"
"Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n"
"$S"),
DeviceID,SensorID,APIKey,dataLength,sensorData);
ether.tcpSend();
lastConnectionTime = millis();
}
|
|