Page 1 of 1

ESP8266 to Domoticz with arduino IDE

Posted: Wednesday 10 August 2016 12:24
by pippin88
I've managed to cobble together working code to have an ESP8266 send temperature and humidity data to Domoticz directly.

Background: I have an ESP-201 running ESPEasy, and it works great. It is a very good solution. I also looked at MySensors, but did not care for the gateway required - I just thing it is unnecessary for what I need. I have five Oak by Digistump. They are based on ESP8266 but I couldn't immediately work out how to get ESPEasy on to them. I figured it should not be hard to send a temperature reading to Domoticz with simple code.

Hardware:
Board: Oak by Digistump
Sensor: si7021 bought on Aliexpress. The ones I got have a voltage regulator on them to make them 5V tolerant. Not good if batteries required. This can be removed, or you can buy boards without.

Procedure:

Add virtual hardware
To be able to communicate with devices like the ESP, we have to add virtual hardware to Domoticz. This step has to be done only once in your setup.

From the main Domoticz Menu, Select Setup -> Hardware
Image

And add following hardware to you system:
Image

Create a virtual sensor
The new hardware should be in the list and there should be a button "Create Virtual Sensors"
Image

Click the button "Create Virtual Sensors" and choose Sensor Type "Temp+Hum"

From the main Domoticz Menu, Select Setup -> Devices
Note down the IDX number of your virtual sensor - you'll need to add it to the arduino code.

Sending data from arduino/ESP to Domoticz
This uses HTTP GET. Formatting must be correct.
This wiki page has a list of all urls that Domoticz uses.

Temperature/humidity

domoticzIP:PORT/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
IDX = id of your device (This number can be found in the devices tab in the column "IDX")
TEMP = Temperature
HUM = Humidity
HUM_STAT = Humidity status

HUM_STAT can be one of:
0=Normal
1=Comfortable
2=Dry
3=Wet

Arduino code

Code: Select all

#include <Wire.h>
#include <SparkFun_Si7021_Breakout_Library.h> //https://github.com/sparkfun/Si7021_Breakout/blob/master/Libraries/Arduino/Si7021/src/SparkFun_Si7021_Breakout_Library.h
#include <ESP8266WiFi.h>

//int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiClient client;

// domoticz
const char * domoticz_server = "192.168.1.10"; //Domoticz port
int port = 8080; //Domoticz port
int idx = 5; //IDX for virtual sensor, found in Setup -> Devices

float humidity = 0;
float temp = 0;

//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather sensor;

void setup()
{
 //Initialize the I2C sensors and ping them
    sensor.begin();
}

void loop()
{
    //Get readings from all sensors
    getWeather();
    printInfo();
    delay(60000); // Wait 60 seconds

}

void getWeather()
{
  // Measure Relative Humidity from the HTU21D or Si7021
  humidity = sensor.getRH();

  // Measure Temperature from the HTU21D or Si7021
  temp = sensor.getTemp();
  // Temperature is measured every time RH is requested.
  // It is faster, therefore, to read it from previous RH
  // measurement with getTemp() instead with readTemp()
}

void printInfo()
{
// Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT

if (client.connect(domoticz_server,port)) {
  
    client.print("GET /json.htm?type=command&param=udevice&idx=");
    client.print(idx);
    client.print("&nvalue=0&svalue=");
    client.print(temp);
    client.print(";");
    client.print(humidity);
    client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.print(domoticz_server);
    client.print(":");
    client.println(port);
    client.println("User-Agent: Arduino-ethernet");
    client.println("Connection: close");
    client.println();

    client.stop();
    }
}
I hope that helps someone. It took me a few hours of fiddling to get going. I'm no coder, I just copy, paste, and adapt code from others. This is not the cleanest or most concise code. There is probably better code possible, particularly for the client.print part - I did it this way to make it easily understandable.

Re: ESP8266 to Domoticz with arduino IDE

Posted: Wednesday 16 November 2016 5:50
by trigx300
I've been struggling to modify this code to include the authentication process.

I had it working then I added a password protected username and now it stopped working.

Anyone have any sample code for this or any suggestions on how to do this.

Re: ESP8266 to Domoticz with arduino IDE

Posted: Wednesday 16 November 2016 6:40
by qwerk
If you are using espeasy, then there is no need to change code.
EspEasy is capable of transmitting data to domoticz, with and without authentication.
Or am I missing something?

Re: ESP8266 to Domoticz with arduino IDE

Posted: Friday 18 November 2016 6:33
by Jimd
CAnt get it to compile:
sketch_nov16b.ino.cpp.o:(.text._Z10getWeatherv+0x8): undefined reference to `Weather::getRH()'
and many more.

Re: ESP8266 to Domoticz with arduino IDE

Posted: Friday 18 November 2016 9:02
by blackdog65
I have an esp8266-12 with a dht22 in my kids bathroom. It operates the lights, extractor and 2 x 12v door locks. It uses the standard espeasy code with no mods or changes. The only snag was working out which pins would work properly for each job.

Re: ESP8266 to Domoticz with arduino IDE

Posted: Wednesday 23 November 2016 11:48
by pippin88
ESP Easy works great, this was just a solution if you don't want to or can't use ESP Easy.

Jimd: I don't know why you are having that problem.
Do you have all the libraries? (#include <Wire.h>
#include <SparkFun_Si7021_Breakout_Library.h> //https://github.com/sparkfun/Si7021_Brea ... _Library.h
#include <ESP8266WiFi.h>)
Please post your code.

Re: ESP8266 to Domoticz with arduino IDE

Posted: Wednesday 23 November 2016 19:21
by Poppius
Hi. Any luck on developing code on Arduino IDE, that can first connect to a password protected router, and then pass for example temp data to Domoticz?

Re: ESP8266 to Domoticz with arduino IDE

Posted: Thursday 24 November 2016 6:00
by Jimd
I finally got it to compile. Trying to go with espeasy but very frustrated. the website just is not clear to me.

Re: ESP8266 to Domoticz with arduino IDE

Posted: Wednesday 30 November 2016 17:16
by Poppius
Jimd wrote:I finally got it to compile. Trying to go with espeasy but very frustrated. the website just is not clear to me.
I can't understant what you mean. Making code for ESP8266 with Arduino IDE doesen't have anything to do with EspEasy if I understant this thing correctly?