/* Följande pryl är tänkt att rapportera in saker till en Vera Lite Skriven av Markus Jakobsson www.automatiskt.nu 2015-01-02 Använda Pinnar: 10,11,12,13 - Ehternet 2 - Digtal tempgivare. A0 - ljus givare. http://$($ip):3480/data_request?id=variableset&DeviceNum=$($deviceId)&serviceId=$($serviceId)&Variable=$($varName)&Value=$varValue http://vera:3480/data_request?id=variableset&DeviceNum=69&serviceId=urn:micasaverde-com:serviceId:LightSensor1&Variable=CurrentLevel&Value=50 urn:micasaverde-com:serviceId:LightSensor1 - CurrentLevel // http://vera:3480/data_request?id=variableset&DeviceNum=68&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature&Value=10.0 http://Vera:3480/data_request?id=variableset&DeviceNum= &serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature= urn:upnp-org:serviceId:TemperatureSensor1 - CurrentTemperature */ #include #include #include // lägger till biblioteket för DTH11 tempgivaren och fuktighetsgivaren.. dht11 DHT11; //deklararar objeket för tempgiivaren. #define DHT11PIN 2 // används för tempgivaren. int sensorPin = A0; // Pinnen som ska nyttjas för att läsa analoga värden, dvs ljus int sensorValue = 0; // Värdet för den analoga enheten ska sparas här. float loopDelay = 300000; // hur många ms loopen ska vänta. float tempgivaren; // sparar värdet för temp float humiditygivaren; // sparar värdet för humidity byte mac[] = { 0xDE, 0xAD, 0xBE, 0xAF, 0xFE, 0xED }; char server[] = "vera.hemma.ispep.se"; // Namnet på Controllern. IPAddress ip(172,20,0,177); // sätter den som statisk adress just nu... EthernetClient client; void setup() { Serial.begin(9600); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: // try to congifure using IP address instead of DHCP: Ethernet.begin(mac, ip); } delay(500); Serial.println("Nu ska Ethernet vara klar..."); Serial.println("kollar vidare med tempgivaren"); Serial.print("LIBRARY VERSION: "); Serial.println(DHT11LIB_VERSION); } void loop() { // Loopen kommer att få gå lite långsamare till att börja med eftersom jag testar just nu... Serial.println("Inne i loopen, ska just kontrollera temp givaren"); int chk = DHT11.read(DHT11PIN); Serial.print("Read sensor: "); switch (chk) { case 0: Serial.println("OK"); break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } Serial.print("Humidity (%): "); Serial.println((float)DHT11.humidity, 2); humiditygivaren = (float)DHT11.humidity, 2; Serial.print("Temperature (oC): "); Serial.println((float)DHT11.temperature, 2); tempgivaren = (float)DHT11.temperature, 2; sensorValue = analogRead(sensorPin); Serial.print("kollade ljus: "); Serial.println(sensorValue); // Hittade följande för att räkna fram Procent http://arduino.fisch.lu/index.php?menu=12&page=&portal=9 float percent = sensorValue / 1024.0 * 100; sensorValue = floor(percent*100) / 100; Serial.print("Ljus i Procent: "); Serial.println(sensorValue); //httpRequest("ljus", sensorValue); // httpRequest("Temp", tempgivaren); httpTempRegister(68, tempgivaren); httpLjusRegister(69, sensorValue); httpFuktRegister(72, humiditygivaren); //httpRequest("Humidity", humiditygivaren); Serial.print("Avvaktar nu: "); Serial.print(loopDelay); Serial.println(" ms"); delay(loopDelay); } // ----- Följande används för att översätta temperaturen // delta max = 0.6544 wrt dewPoint() // 5x faster than dewPoint() // reference: http://en.wikipedia.org/wiki/Dew_point double dewPointFast(double celsius, double humidity) { double a = 17.271; double b = 237.7; double temp = (a * celsius) / (b + celsius) + log(humidity/100); double Td = (b * temp) / (a - temp); return Td; } // Givarid = ditt id i vera MittValue = vad den ska uppdatera // Denna ska uppdatera enbart Temperatur ! void httpTempRegister(int GivarID, int MittValue) { // if there's a successful connection: if (client.connect(server, 3480)) { Serial.println("Kopplar upp..."); // send the HTTP PUT request: client.print("GET /data_request?id=variableset&DeviceNum="); client.print(GivarID); client.print("&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature&Value="); client.print(MittValue); client.println(" HTTP/1.1"); client.println("Host: vera.hemma.ispep.se"); client.println("Connection: close"); client.println(); Serial.println("skickade Temp data till Vera, kopplar ner"); client.stop(); // note the time that the connection was made: } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println("disconnecting."); client.stop(); } } // Givarid = ditt id i vera MittValue = vad den ska uppdatera // Denna ska uppdatera enbart Temperatur ! void httpLjusRegister(int GivarID, int MittValue) { // if there's a successful connection: if (client.connect(server, 3480)) { Serial.println("Kopplar upp..."); // send the HTTP PUT request: client.print("GET /data_request?id=variableset&DeviceNum="); client.print(GivarID); client.print("&serviceId=urn:micasaverde-com:serviceId:LightSensor1&Variable=CurrentLevel&Value="); client.print(MittValue); client.println(" HTTP/1.1"); client.println("Host: vera.hemma.ispep.se"); client.println("Connection: close"); client.println(); Serial.println("skickade Ljus data till Vera, kopplar ner"); client.stop(); // note the time that the connection was made: } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println("disconnecting."); client.stop(); } } // Givarid = ditt id i vera = mittValue = vad den ska uppdatera. void httpFuktRegister(int GivarID, int MittValue) { // if there's a successful connection: if (client.connect(server, 3480)) { Serial.println("Kopplar upp..."); // send the HTTP PUT request: client.print("GET /data_request?id=variableset&DeviceNum="); client.print(GivarID); client.print("&serviceId=urn:micasaverde-com:serviceId:HumiditySensor1&Variable=CurrentLevel&Value="); client.print(MittValue); client.println(" HTTP/1.1"); client.println("Host: vera.hemma.ispep.se"); client.println("Connection: close"); client.println(); Serial.println("skickade Ljus data till Vera, kopplar ner"); client.stop(); // note the time that the connection was made: } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println("disconnecting."); client.stop(); } }