Back
Featured image of post Wifi light sensor using a Wemos D1 and Domoticz

Wifi light sensor using a Wemos D1 and Domoticz

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

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

Domoticz Setup

Create a new hardware dummy device :

Domoticz wemos hardware device
Domoticz wemos hardware device

And then a light sensor device :

Domoticz wemos sensor device
Domoticz wemos sensor device

Wiring

Can’t be more easy :

Light sensor wiring
Light sensor wiring

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&param=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&param=udevice&idx=166&nvalue=0&svalue="+String(sensorValue);

Built with Hugo
Theme Stack designed by Jimmy