ESP8266 to Domoticz with arduino IDE

Moderator: leecollings

Post Reply
pippin88
Posts: 2
Joined: Wednesday 10 August 2016 11:49
Target OS: Linux
Domoticz version:
Contact:

ESP8266 to Domoticz with arduino IDE

Post 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.
trigx300
Posts: 1
Joined: Wednesday 16 November 2016 5:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post 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.
qwerk
Posts: 222
Joined: Tuesday 22 July 2014 7:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post 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?
Jimd
Posts: 3
Joined: Thursday 17 November 2016 19:46
Target OS: Windows
Domoticz version:
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post by Jimd »

CAnt get it to compile:
sketch_nov16b.ino.cpp.o:(.text._Z10getWeatherv+0x8): undefined reference to `Weather::getRH()'
and many more.
User avatar
blackdog65
Posts: 311
Joined: Tuesday 17 June 2014 18:25
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Norfolk, UK
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post 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.
CubieTruck Master
RasPi slaves
Aeon Labs Z-Stick, multi sensor
Fibaro Dimmers, relays, Universal sensors
EQ3 MAX!
TKB Sockets
RFXCOM
LightwaveRF sockets, switches, relays, doorbell
MySensors
ESPEasy ESP8266-12E
pippin88
Posts: 2
Joined: Wednesday 10 August 2016 11:49
Target OS: Linux
Domoticz version:
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post 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.
Poppius
Posts: 3
Joined: Wednesday 23 November 2016 19:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post 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?
Jimd
Posts: 3
Joined: Thursday 17 November 2016 19:46
Target OS: Windows
Domoticz version:
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post by Jimd »

I finally got it to compile. Trying to go with espeasy but very frustrated. the website just is not clear to me.
Poppius
Posts: 3
Joined: Wednesday 23 November 2016 19:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: ESP8266 to Domoticz with arduino IDE

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest