/* Copyright (C) 2012 Nathanaël Restori Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "BMP085.h" #include "Chronodot.h" #include "DHT.h" #include "TSL2561.h" #include "WebServer.h" #define TIMEZONE 7200 // 2 hours #define DHTPIN 2 #define DHTTYPE DHT22 #define PREFIX "" #define NTP_PACKET_SIZE 48 static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; static IPAddress ip = IPAddress(192, 168, 1, 210); static IPAddress timeserver = IPAddress(213, 239, 239, 164); P(indexHtml) = "Station Météo

Station Météo

" "<" "tr>
TempsT [°C]P [Pa]A" "lt [m]Hr [%]Lx [lux]
00000<" "/td>0

par Nathanaël Restori
"; // no-cost stream operator as described at // http://sundial.org/arduino/?page_id=119 template inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } BMP085 bmp; Chronodot chronodot; DHT dht(DHTPIN, DHTTYPE); TSL2561 tsl(TSL2561_ADDR_FLOAT); WebServer webserver(PREFIX, 80); byte packetBuffer[ NTP_PACKET_SIZE]; EthernetUDP Udp; void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { server.httpSuccess(); server.printP(indexHtml); } void sensorsJsonCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { float temperature = bmp.readTemperature(); int32_t pressure = bmp.readPressure(); float altitude = bmp.readAltitude(); float humidity = dht.readHumidity(); uint32_t lum = tsl.getFullLuminosity(); uint16_t lightIr = lum >> 16; uint16_t lightFull = lum & 0xFFFF; DateTime time = chronodot.now(); server.httpSuccess("application/json"); server << "{ \"sensors\": [" << "{\"data\":\"" << time.hour() << ":" << time.minute() << ":" << time.second() << " " << time.day() << "/" << time.month() << "/" << time.year() << "\"}," << "{\"data\":" << temperature << "}," << "{\"data\":" << pressure << "}," << "{\"data\":" << altitude << "}," << "{\"data\":" << humidity << "}," << "{\"data\":" << tsl.calculateLux(lightFull, lightIr) << "}" << "] }"; } // Taken from http://arduino.cc/en/Tutorial/UdpNtpClient . void sendNTPpacket() { // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[1] = 0; // Stratum, or type of clock packetBuffer[2] = 6; // Polling Interval packetBuffer[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(timeserver, 123); //NTP requests are to port 123 Udp.write(packetBuffer,NTP_PACKET_SIZE); Udp.endPacket(); Serial.println("Sending ntp packet"); delay(1000); if ( Udp.parsePacket() ) { // We've received a packet, read the data from it Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet // into the buffer // the timestamp starts at byte 40 of the received packet and is // four bytes, or two words, long. First, esxtract the two // words: unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900): unsigned long secsSince1900 = highWord << 16 | lowWord; // Unix time starts on Jan 1 1970. In seconds, that's // 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: unsigned long epoch = secsSince1900 - seventyYears + TIMEZONE; Serial.println("Adjusting time"); chronodot.adjust(DateTime(epoch)); return; } Serial.println("No reponse"); } void setup() { Serial.begin(9600); //----- Ethernet/WebServer Serial.println("INIT Ethernet"); Ethernet.begin(mac, ip); webserver.begin(); webserver.setDefaultCommand(&defaultCmd); webserver.addCommand("sensors.json", &sensorsJsonCmd); //----- Sensors Serial.println("INIT Sensors"); bmp.begin(); dht.begin(); tsl.begin(); chronodot.begin(); //tsl.setGain(TSL2561_GAIN_0X); // bright situtations tsl.setGain(TSL2561_GAIN_16X); // dim situations tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // bright light //tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium light //tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // dim light Udp.begin(8888); sendNTPpacket(); } void loop() { webserver.processConnection(); delay(500); }