|
发表于 2012-8-31 17:39:32
|
显示全部楼层
但还是不行
代码:- /*
- 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] = {
- 0x74,0x69,0x69,0x2D,0x32,0x06};
- 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
-
-
- // 分配一个MAC IP网关的以太网控制器的DNS assign a MAC IP gateway dns for the ethernet controller.
-
- const uint8_t MyGateway[4] = {
- 222,16,228,15 };
- const uint8_t yeelinkIP[4] = {
- 202,136,60,231};
-
-
- const uint16_t PostingInterval = 15000;// 2个数据点之间的延迟时间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() {
- // 创建一个发表yeelink服务器Create a Post for yeelink server,
- // 发送请求保存会话IDand 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;
- }
复制代码 |
|