Wifi light sensor using a Wemos D1 and Domoticz


Update a domoticz sensor using a Wemos D1 and a seed light sensor.

1. Domoticz Setup

Create a new hardware dummy device :

And then a light sensor device :

2. Wiring

Can’t be more easy :

3. Code

#include           //https://github.com/esp8266/Arduino

//needed for library
#include 
#include 
#include  //https://github.com/tzapu/WiFiManager
#include 
#include 

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);

Leave a Reply

Your email address will not be published. Required fields are marked *