Jump to content

Connecting Fibaro HC2 to an ESP8266 - two tutorials


Recommended Posts

Hi, I've had a few questions on how to use Fibaro HC2 to talk to ESP8266 devices.

 

If you're interested, the simple example over at https://github.com/ianbren/Fibaro_Esp8266_101 can help.

 

there are two assumptions

- that you can create a Virtual Device in Fibaro and have used LUA before

- that you've got the Arduino environment to build and ESP8266 (of whatever flavour you want).

 

Basically, the solution has a Virtual Device with one button - press the button in Debug mode and the ESP8266 is called and returns with some JSON.

 

 

If there's interest, I'll expand the examples to include one for ESPEASY and a DHT11 - where you can build wifi-enabled temperture devices.

 

 

Cheers

 

Ian

 

Edited by ianbren
change of title
Link to post
Share on other sites

JFYI:

 

1) ESP8266 Serial ESP-12F Wi-Fi Witty Cloud Development Board + DHT22

Spoiler

 

cb6413a0-561a-d8d4-383a-68451ae9ea84.jpg

 

DHT22-PinOut.png

 

 

 

 

 

2) Code TemperatureHumidity.ino (see attach file TemperatureHumidity.ino)

3) FIBARO VD (see attach file Climate_ESP.vfib 

 

lUfmYTZ2or.png

 

lQb2w50hNa.png

 

 

 

 

 

TemperatureHumidity.ino

 

Climate_ESP.vfib

Edited by 10der
  • Like 1
Link to post
Share on other sites

Hi,

 

On ESP8266 board You have Light sensor ( that thing in upper corner). Are You not using it in the skech ??

Edited by pablo77
Link to post
Share on other sites
1 hour ago, pablo77 said:

Hi,

 

On ESP8266 board You have Light sensor ( that thing in upper corner). Are You not using it in the skech ??

 

MlwMZLd3eQ.png

 

#define LDR_PIN		A0

[...]
  
l = analogRead(LDR_PIN); // read input value and store it

+ "," + String("\"light\":") + String(lightString) +

Perhaps you failed to pay attention to the arduino sketch file.

 

notice:

const char* WIFI_PASS = "id2891Sz500Qp";

 

it's fake WiFi password :) but in any case - thank you and welcome to my home and Ukraine :)

 

 

Link to post
Share on other sites

I like the Code - some suggestions...

(a) - you calculate Dewpoint - but don't return it in the HTML.

(b) the calculation is deferred every 15 seconds (in the main loop) - perhaps pull the constant out to the top to make it clear what's happening.

(c) you might want to consider starting up the DHT/reportreading  before calling the server.begin() - as it stands the HTML strings (humidityString etc) are empty and Fibaro may start polling before the first read.


Oh, and I love the icon model.

Oh, and I found the WITTY useful for the LDR, but getting access to the Digital pins is a pain for anything other than experimenting.

 

Have a look at WEMOS D1 Mini - they're about the same price, the same form-factor but have Shields (only the DHT11 in this case) https://wiki.wemos.cc/products:d1_mini_shields:dht_shield

 

Edited by ianbren
strike through on (c)
Link to post
Share on other sites
20 minutes ago, ianbren said:

I like the Code - some suggestions...

(a) - you calculate Dewpoint - but don't return it in the HTML.

 

(b) the calculation is deferred every 15 seconds (in the main loop) - perhaps pull the constant out to the top to make it clear what's happening.

(c) you might want to consider starting up the DHT/reportreading  before calling the server.begin() - as it stands the HTML strings (humidityString etc) are empty and Fibaro may start polling before the first read.


Oh, and I love the icon model.

Oh, and I found the WITTY useful for the LDR, but getting access to the Digital pins is a pain for anything other than experimenting.

 

Have a look at WEMOS D1 Mini - they're about the same price, the same form-factor but have Shields (only the DHT11 in this case) https://wiki.wemos.cc/products:d1_mini_shields:dht_shield

 

 

 

a) yep! you're right. I was thinking later what it's useless :)

b) yes. :)

c) arduino does not start loop thread while setup() void not done :) + server.handleClient(); last command in loop - all requests from HC2 going to /dev/null :)

 

Quote

Oh, and I found the WITTY useful for the LDR, but getting access to the Digital pins is a pain for anything other than experimenting.

 

but this sandwich pretty good! (IMHO)

 

Quote

Have a look at WEMOS D1 Mini - they're about the same price, the same form-factor but have Shields (only the DHT11 in this case) https://wiki.wemos.cc/products:d1_mini_shields:dht_shield

 

yes, I know. 

see also:

WeMos D1 mini DHT11 shield
WeMos D1 mini DHT22 shield
WeMos D1 mini OLED 0.66 shield (for example link here)
WeMos D1 mini TB6612FN (motor driver) shield
WeMos D1 mini button shield
WeMos D1 mini relay 5v shield
WeMos D1 mini LED ws2812 controller shield


:)

Link to post
Share on other sites
16 minutes ago, 10der said:

arduino does not start loop thread while setup() void

- good point - and the variables are populated before that.  So scrub (c)!!

Link to post
Share on other sites

as Another ideas...

 

Arduino sketch

Spoiler


#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* WIFI_SSID = "HOME";
const char* WIFI_PASS = "not-real-passowrd";

extern "C" {
#include "user_interface.h"
}

void connect() {
  unsigned long connectionStart = millis();
  while (WiFi.status() != WL_CONNECTED) {
    WiFi.mode(WIFI_STA);
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      if (millis() - connectionStart > 30000) {
        ESP.restart();
        break;
      }
    }
    if (WiFi.status() == WL_CONNECTED) {
    }
  }
}

void setup() {
  wifi_station_set_hostname("esp-test");
}

void doCall(String param) {

  String url = "http://192.168.1.36:80/api/scenes/65/action/start";

  HTTPClient http;
  http.begin(url);
  http.addHeader("Content-Type", "application/json");
  http.setAuthorization("not-real-login", "not-real-passowrd");
  int httpCode = http.POST(param);
  if (httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    // file found at server
    if (httpCode == HTTP_CODE_OK) {
      String payload = http.getString();
    }
  }
  else {
    // http.errorToString(httpCode).c_str());
  }
  http.end();
}

void checkCall()
{
  static const unsigned long REFRESH_INTERVAL = 1000 * 60 * 1; // 1 min
  static unsigned long lastRefreshTime = 0;

  if (millis() - lastRefreshTime >= REFRESH_INTERVAL)
  {
    lastRefreshTime += REFRESH_INTERVAL;
    doCall("{\"args\":[\"ABCDE\", 12345]}");
  }
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    connect();
  }

  checkCall();
  delay(1 * 1000);
}


 

 

universal fibaro scene :)

 

Spoiler
--[[
%% properties
%% events
%% globals
--]]

local args = fibaro:args()
if (args ~= nil) then
  for key,value in pairs(args) do
    fibaro:debug(value)
  end
else
  	fibaro:debug("Nothing todo!")
end

 

 

 

here are results:

 

RQ5JoyMXtK.png

 

 

so, now ESP poll FIBARO if events occurred

ESP call fibaro scene and pass some parameters.

pay attention: we can use one single scene for many external callers - a simple pass to the scene for example Id or action etc

 

 

 

btw, in the scene we can pass a table as well:

for example

 

{
  "args": [
    "actions",
    [
      {
        "@id": 1,
        "deviceId": 1,
        "typeOfDevice": "1",
        "state": "1",
        "assigned": true
      },
      {
        "@id": 2,
        "deviceId": 3,
        "typeOfDevice": "3",
        "state": "Excelent",
        "assigned": true
      },
      {
        "@id": 3,
        "deviceId": 4,
        "typeOfDevice": "Sauna",
        "state": "Excelent",
        "assigned": true
      }
    ]
  ]
}

fibaro result:

[DEBUG] 02:14:52: actions
[DEBUG] 02:14:52: table: 0xa133818

 

  • Like 1
Link to post
Share on other sites

I've seen this before - it does split the logic between the ESP and the Fibaro - but this approach pushes the FB-specific actions into the Scene.

 

 

Link to post
Share on other sites
  • 6 months later...
On 20-9-2017 at 3:52 AM, ianbren said:

201 is also posted now at https://github.com/ianbren/Fibaro_ESP8266_201

 

Shows how a Fibaro HC2 can poll an ESP8266 and receive JSON data back - then post it to a label.

 

This is a work in progress and if there's interest I'll move on to ESPEASY - to monitor temperature.

 

 

 

Hi,

 

First of, thanks for the info. It was realy helpfull thusfar. Perhaps you can help me a littlebit further.

 

I'm building a touchscreen based on a Nextion display and a ESP8266 Nodemcu chip. I want to make a wall controller for my Homecenter 2. I can already send data from mij ESP8266 to HC2 to start a scene or switch a device. And i can send the GET command from my HC2 to the ESP in order to start a void there. (With your code) I was wondering, is there a way that i can send the status or brightness value from my HC2 to a int in the ESP? I'm trying to figure it out for a while now and can't seem to fin a good soulution.

 

Greetings,

 

Damien

 

P.S. Sorry for my English, not my native language.

 

Link to post
Share on other sites

well, the simplest way would be to pass it as a parameter.  If you have the ESP8266 libraries installed, there's an example called "advanced web server".  It shows how you can pass page requests to the ESP and have it break them down.  Look for 

 

 server.on ( "/inline", []() {
  server.send ( 200, "text/plain", "this works as well" );
 } );

 

If you want to pass a parameter something like this for "i" and "s" --- http://someUrl/myPage?i=5&s=20 --- then it get's a little more complicated.

 

 

use a line like 

 

 

 server.on ( "/myPage", handleMyPageHere)

 

then have a function

void handleMyPageHere(){

int myVar1, myVar2;

  if (server.hasArg("i")) myVar1 = (server.arg("i").toInt());

  if (server.hasArg("s")) myVar2 = (server.arg("s").toInt());

// do something interesting with your variables.

}

 

Obviously scope of the variables is important.

 

Maybe the return string is the response that Fibaro is waiting for.

 

Have fun!

Link to post
Share on other sites
  • 3 weeks later...
  • 3 months later...

I want use esp8266 for 2 relays for my garden watering. With Raspberry Pi and Domoticz all working. But i want use these relays with Fibaro.  It is possible and how ?

Link to post
Share on other sites

insippo, I had some success using EasyESP and have a scene calling the EasyESP REST API.

 

- Check the EasyESP docs for API, you will have to set up a task for this.

- Here in the forum you find numerous examples how a Fibaro scene makes a call to a REST API.

 

Cheers,

 

jayrock

Link to post
Share on other sites

A little off topic perhaps...

I have some d1 mini's flashed with Tasmota and it exposes a web interface... very easy to get it to interact (status/ control) with the hc2 with a vd.

 

Only downside is that it is over http (not https) . It also exposes an mtqq interface which is more secure  but with no HC2 support I have not played with that part... ....yet

Link to post
Share on other sites
1 hour ago, AutoFrank said:

A little off topic perhaps...

I have some d1 mini's flashed with Tasmota and it exposes a web interface... very easy to get it to interact (status/ control) with the hc2 with a vd.

 

Only downside is that it is over http (not https) . It also exposes an mtqq interface which is more secure  but with no HC2 support I have not played with that part... ....yet

 

btw:

about tasmota

 

fDLJCL0Sgs.png

function updateStatus()
    local deviceID = fibaro:getSelfId();
    local ipaddress = fibaro:getValue(deviceID, "IPAddress");
    local port = fibaro:getValue(deviceID, "TCPPort");
    
    local rPi = Net.FHttp(ipaddress, port);
    local resp, status, result = rPi:GET("/cm?cmnd=status");
    
    local stat = "ERR";
  	local icon = 6
    
    if ((result == 0) and (status == "200")) then      
        local status = json.decode(resp)["Status"]["Power"]
        if (status == 1) then icon = 5 else icon = 1006 end
    	if status == 1 then stat = "ON" else stat = "OFF" end
    else
        --fibaro:debug("Result = "..result)
        --fibaro:debug("Status = "..status)
    end

  	fibaro:call(deviceID, "setProperty", "currentIcon", icon);
    fibaro:call(deviceID, "setProperty", "ui.".."lblStatus"..".value", stat);  
end

updateStatus()

 

 

function updateStatus()
    local deviceID = fibaro:getSelfId();
    local ipaddress = fibaro:getValue(deviceID, "IPAddress");
    local port = fibaro:getValue(deviceID, "TCPPort");
    
    local rPi = Net.FHttp(ipaddress, port);
    local resp, status, result = rPi:GET("/cm?cmnd=status");
    
    local stat = "ERR";
  	local icon = 6
    
    if ((result == 0) and (status == "200")) then      
        local status = json.decode(resp)["Status"]["Power"]
        if (status == 1) then icon = 5 else icon = 1006 end
    	if status == 1 then stat = "ON" else stat = "OFF" end
    else
        --fibaro:debug("Result = "..result)
        --fibaro:debug("Status = "..status)
    end

  	fibaro:call(deviceID, "setProperty", "currentIcon", icon);
    fibaro:call(deviceID, "setProperty", "ui.".."lblStatus"..".value", stat);  
end

function setState(state)

    local deviceID = fibaro:getSelfId();
    local ipaddress = fibaro:getValue(deviceID, "IPAddress");
    local port = fibaro:getValue(deviceID, "TCPPort");
    
    local rPi = Net.FHttp(ipaddress, port);
    local resp, status, result = rPi:GET("/cm?cmnd=Power1%20"..state);
    if ((result == 0) and (status == "200")) then
      
    else
        --fibaro:debug("Result = "..result)
        --fibaro:debug("Status = "..status)
    end
end

setState("ON")
updateStatus()
function updateStatus()
    local deviceID = fibaro:getSelfId();
    local ipaddress = fibaro:getValue(deviceID, "IPAddress");
    local port = fibaro:getValue(deviceID, "TCPPort");
    
    local rPi = Net.FHttp(ipaddress, port);
    local resp, status, result = rPi:GET("/cm?cmnd=status");
    
    local stat = "ERR";
  	local icon = 6
    
    if ((result == 0) and (status == "200")) then      
        local status = json.decode(resp)["Status"]["Power"]
        if (status == 1) then icon = 5 else icon = 1006 end
    	if status == 1 then stat = "ON" else stat = "OFF" end
    else
        --fibaro:debug("Result = "..result)
        --fibaro:debug("Status = "..status)
    end

  	fibaro:call(deviceID, "setProperty", "currentIcon", icon);
    fibaro:call(deviceID, "setProperty", "ui.".."lblStatus"..".value", stat);  
end

function setState(state)

    local deviceID = fibaro:getSelfId();
    local ipaddress = fibaro:getValue(deviceID, "IPAddress");
    local port = fibaro:getValue(deviceID, "TCPPort");
    
    local rPi = Net.FHttp(ipaddress, port);
    local resp, status, result = rPi:GET("/cm?cmnd=Power1%20"..state);
    if ((result == 0) and (status == "200")) then
      
    else
        --fibaro:debug("Result = "..result)
        --fibaro:debug("Status = "..status)
    end
end

setState("OFF")
updateStatus()

 

Edited by 10der
Link to post
Share on other sites

Hi there,

 

I am trying to connect Weatherman (https://www.stall.biz/project/weatherman-die-perfekte-wetterstation-fuer-die-hausautomation).

Nice weatherstation....

Controller is based on ESP8266. Communication with browser is fine showing data.

Try to get json with VD based on  https://github.com/ianbren/Fibaro_Esp8266_101 .

Got Status=200 and Error=0 but response (should content json) seems to be "empty", using Port 80.

Any idea?

 

Best regards

Michael

 

 

Link to post
Share on other sites

@miwa ["Content-Type"] = "application/json",

in headers

also see: 

Accept: application/json

["Accept"] = 'application/json',

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...