/* Följande program rapporterar in Temperatur, luftfuktighet och Ljus till vera lite Skriven av Markus Jakobsson www.automatiserar.se Skapad: 2015-01-02 Senast uppdaterad: 2015-01-13 Använda Pinnar: D10,D11,D12,D13 - Ehternet D2 - Digtal tempgivare. A0 - ljus givare. */ #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 int veraLjusID = 81; // ID på din ljussensor i Vera int veraHumidityID = 82; // ID på din luftfuktighets sensor i Vera int veraTempid = 83; // ID på din Temperaturgivare i Vera // Ethernet byte mac[] = { 0xDE, 0xAD, 0xBE, 0xAF, 0xFE, 0xED }; char server[] = "DittVeraIP"; // Namnet på Controllern. int Serverport = 3480; // porten dit data ska rapporteras på vera IPAddress ip(10,10,10,231); // 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(veraTempid, tempgivaren); httpLjusRegister(veraLjusID, sensorValue); httpFuktRegister(veraHumidityID, humiditygivaren); Serial.print("Avvaktar nu: "); Serial.print(loopDelay); Serial.println(" ms"); delay(loopDelay); } // 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, Serverport)) { 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.print("Host: "); client.print(server); 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, Serverport)) { 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.print("Host: "); client.print(server); 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, Serverport)) { 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.print("Host: "); client.print(server); 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(); } }