|
|

楼主 |
发表于 2013-6-22 23:03:39
|
显示全部楼层
本帖最后由 blueleafxp 于 2013-6-22 23:06 编辑
- //#include <OneWire.h>
- #include <EtherCard.h>
- //#include <DallasTemperature.h>
- #define OUT
- // Data wire is plugged into port A5 on the Arduino
- #define ONE_WIRE_BUS A5
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- //OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- //DallasTemperature sensors(&oneWire);
- #include <dht11.h>
- dht11 DHT11;
- #define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3
-
- #define REQUEST_RATE 15000 // milliseconds
- // ethernet interface mac address
- static byte mymac[] = {
- 0x74,0x69,0x69,0x2D,0x30,0x31 };
- // ethernet interface ip address
- static byte myip[] = {
- 192,168,101,139 };
- // gateway ip address
- static byte gwip[] = {
- 192,168,101,1 };
- // remote dns ip address
- static byte dnsip[] = {
- 192,168,101,1 };
- int mode_id=0;
- // remote website name
- char website[] PROGMEM = "api.yeelink.net";
- char urlBuf[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";// 用自己的id
- char apiKey[] PROGMEM = "U-ApiKey: xxxxx";
- char urlBuf1[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";//用自己的key
- byte Ethernet::buffer[400]; // a very small tcp/ip buffer is enough here
- static long timer;
-
- // called when the client request is complete
- static void my_result_cb (byte status, word off, word len) {
- Serial.print("<<< reply ");
- Serial.print(millis() - timer);
- Serial.println(" ms");
- Serial.println((const char*) Ethernet::buffer + off);
- }
-
- void setup () {
- ///sensors.begin();
- Serial.begin(57600);
- Serial.println("\n[getStaticIP]");
-
- if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
- Serial.println( "Failed to access Ethernet controller");
-
- ether.staticSetup(myip, gwip, dnsip);
-
- while (ether.clientWaitingGw())
- ether.packetLoop(ether.packetReceive());
- Serial.println("Gateway found");
-
- timer = - REQUEST_RATE; // start timing out right away
- }
-
- void loop () {
- ether.packetLoop(ether.packetReceive());
-
- if (millis() > timer + REQUEST_RATE) {
- timer = millis();
- Serial.println("\n>>> REQ");
- static char buf[20],buf1[20];
- get_send_string(buf,buf1);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("Server: ", ether.hisip);
- switch(mode_id)
- {
- case 1:
- Serial.println("\n>>> temperature");
- ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
- mode_id--;
- break;
- case 0:
- Serial.println("\n>>> humidity");
- ether.httpPost (urlBuf1, website, apiKey, buf1, my_result_cb);
- mode_id++;
- break;
- default:
- mode_id=0;
- break;
- }
-
- }
- }
-
- void get_send_string(OUT char *p ,OUT char *p1){
-
- int chk = DHT11.read(DHT11PIN);
- // sensors.requestTemperatures();
- int Tc_100 =(float) DHT11.temperature*10;
- int whole, fract;
- whole = Tc_100/10 ; // separate off the whole and fractional portions
- fract = Tc_100 % 10;
- //whole=35;
- //fract=3;
- sprintf(p,"{"value":%d.%d}",whole,fract);
- int Tc_200 =(float) DHT11.humidity*10;
- whole = Tc_200/10 ; // separate off the whole and fractional portions
- fract = Tc_200 % 10;
- //whole=35;
- //fract=3;
- sprintf(p1,"{"value":%d.%d}",whole,fract);
-
- Serial.println(p);
- Serial.println(p1);
- // sprintf(p,"{"value":%d}",Tc_100 );
- }
复制代码 |
|