|
发表于 2012-8-31 16:27:42
|
显示全部楼层
static byte mymac[] = {
0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = {
192,168,1,203 };
这两个要不要改啊,自己的IP和mac?
我的代码是:
/*
Yeelink sensor client
by zcb
2012.07.17
*/
#include <EtherCard.h>
#include <OneWire.h>
//
//18B20
#define ONEWIRE_PIN 7
OneWire ds(ONEWIRE_PIN);
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
char SensorID[] PROGMEM = "626"; // replace your sensor ID
const uint8_t MyMac[6] = {
0xf0,0xdf,0xf1,0xc7,0x9f,0x61 };
const uint8_t MyIP[4] = {
222,16,228,228 };
////////////////////////////////////////////////////////////
// for yeelink api
char APIKey[] PROGMEM = "5c137bc47b1fe0d3d7854509d7783c78"; // replace your yeelink api key here
char DeviceID[] PROGMEM = "500"; // replace your device ID
// assign a MAC IP gateway dns for the ethernet controller.
const uint8_t MyGateway[4] = {
10,21,0,1 };
const uint8_t yeelinkIP[4] = {
202,136,60,231};
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(4800);
// start serial port:
// start the Ethernet connection:
ether.begin(sizeof Ethernet::buffer, MyMac);
//
ether.staticSetup(MyIP,MyGateway);
}
void loop() {
Serial.print("yeelink:");
Serial.println(get_Current_Temp());
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.copyIp(ether.hisip, yeelinkIP);
ether.tcpSend();
lastConnectionTime = millis();
}
///////////////////////////////////////////////////////////////////////////
// get data from temperature sensor
// you can replace this code for your sensor
void get_Send_String(){
uint16_t Tc_100 = get_Current_Temp();
uint8_t i,whole, fract;
whole = Tc_100/10 ; // separate off the whole and fractional portions
fract = Tc_100 % 10;
dataLength = sprintf(sensorData,"{\"value\":%d.%d}",whole,fract);
}
uint16_t get_Current_Temp(){ //0.1C
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return 0;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return 1000;
}
if ( addr[0] != 0x28) {
return 1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000);
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
uint16_t tempRead = (data[1] << 8) | data[0]; //using two's compliment
return tempRead*10/16;
}
|
|