Update a domoticz sensor using a Wemos D1 and a seed light sensor.
Domoticz Setup
Create a new hardware dummy device :
And then a light sensor device :
Wiring
Can’t be more easy :
Code
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h>
#include <WiFiClient.h>
int sensorPin = A0;
int sensorValue = 0;
SimpleTimer updateLightTimer;
WiFiClient client;
//Const
const char* host = "192.168.1.4";
const int httpPort = 80;
void setup() {
Serial.begin(9600);
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
pinMode(sensorPin, INPUT);
updateLightTimer.setInterval(100000, updateLight);
updateLight();
}
void loop() {
updateLightTimer.run();
}
//Update the sensor value
void updateLight() {
sensorValue = ((analogRead(sensorPin) * 100) / 1024);
Serial.println("Update light value : "+sensorValue);
String path = "/json.htm?type=command¶m=udevice&idx=166&nvalue=0&svalue="+String(sensorValue);
while (!ServerConnect()) {
delay(5000);
}
if (client.connected())
{
SendRequest(path);
}
}
//Connect to server
bool ServerConnect() {
if (!client.connect(host, httpPort)) {
return false;
}
else {
return true;
}
}
//Send request
void SendRequest(String path) {
String line;
client.print(String("GET ") + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n");
}
Note : Update the IDX value according to your sensor.
String path = "/json.htm?type=command¶m=udevice&idx=166&nvalue=0&svalue="+String(sensorValue);