/* OpenJumper WebServer Example [url] http://www.openjumper.com/ethernet/ [/url] 顯示室內照度+開關燈控制 通過手機、平板、計算機等設備訪問 Arduino Server,就看到當前室內光線照度 在A0引腳連接光敏模塊,用於採集室內光線;在2號引腳連接LED模塊。 奈何col 2014.12.25 ng344 2018.05.09 */ #include <SPI.h> #include <Ethernet.h> #include "DHT.h" #define DHTPIN 2 // what digital pin we're connected to #define DHTTYPE DHT21 // DHT 22 (AM2302), AM2321 DHT dht(DHTPIN, DHTTYPE); byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte gateway[] = { 203, 68, 102, 254 }; // internet access via router byte subnet[] = { 255, 255, 255, 0 }; // 24bit Subnet Mask byte dns1[] = {163,26,1,1}; IPAddress ip(203,68,102,217); EthernetServer server(80); EthernetClient client; // ThingSpeak Settings char thingSpeakAddress[] = "api.thingspeak.com"; String APIKey = "YCIX1HI3P48BGM6X"; // enter your channel's Write API Key const int updateTSInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak // ifttt char iftttAddress[] = "maker.ifttt.com"; String tttKey = "dZIsDaOk8yzMG8vaATmoRx"; const int updateiftttInterval = 30 * 1000; // 20 second interval at which to update ifttt // Variable Setup long tslastCT = 0; long tttlastConnectionTime = 0; void setup() { Serial.begin(9600); //初始化Ethernet通信 Ethernet.begin(mac, ip, dns1, gateway, subnet); server.begin(); dht.begin(); Serial.print( "Server is at " ); Serial.println(Ethernet.localIP()); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // 監聽連入的客戶端 client = server.available(); if (client) { Serial.println( "new client" ); // boolean currentLineIsBlank = false ; while (client.connected()) { if (client.available()) { SendHTML(h,t); break ; } } delay(1); client.stop(); Serial.println( "client disonnected" ); } // Update ThingSpeak if ((millis() - tslastCT) > updateTSInterval) { Serial.println( "Update ThingSpeak" ); updateThingSpeak("GET /update?api_key="+APIKey+"&field1="+String(t)+"&field2="+String(h)); } /* // Update ifttt if ((millis() - tttlastConnectionTime > updateiftttInterval)) { Serial.println( "Update ifttt" ); updateifttt("GET /trigger/sensortnic/with/key/"+tttKey+"?value1="+String(t)+"&value2="+String(h)+" HTTP/1.1\r\n"); } */ } // 用於輸出HTML文本的函數 void SendHTML(float h, float t) { client.println( "HTTP/1.1 200 OK" ); client.println( "Content-Type: text/html" ); client.println( "Connection: close" ); client.println(); //client.println( "<!DOCTYPE HTML>" ); //client.println( "<html><head><meta charset=\"UTF-8\"><title>Arduino Web Server</title>" ); //client.println( "</head><body>" ); client.println("<prtg>"); client.println("<result><channel>Humidity</channel>"); client.print("<float>1</float><unit>%</unit><DecimalMode>2</DecimalMode><value>"); client.print(h); client.println("</value></result>"); client.println("<result><channel>Temperature</channel>"); client.print("<float>1</float><unit>degree</unit><DecimalMode>2</DecimalMode><value>"); client.print(t); client.println("</value></result>"); client.println("</prtg>"); //client.println( "</body></html>" ); } void updateThingSpeak(String tsData) { if ( !client.connect("api.thingspeak.com", 80)) { Serial.println( "connection failed" ); return; } else { client.print(tsData); Serial.println(tsData); client.print("Host: api.thingspeak.com\r\n"); client.print("Connection: close\r\n\r\n"); client.stop(); tslastCT = millis(); } } void updateifttt(String tttData) { if ( !client.connect("maker.ifttt.com", 80)) { Serial.println( "connection failed" ); return; } else { Serial.println(tttData); client.print(tttData + " HTTP/1.1\r\n" + "Host: maker.ifttt.com\r\n" + "Connection: close\r\n\r\n"); delay(100); client.stop(); tttlastConnectionTime = millis(); } }