ESp arduino + domoticz + read light switch status (on / off)

  • HI,

    I use esp with relay to switch light.

    I integrated my arduino code with domoticz.

    Everything works great.

    I have only one problem.

    After esp restart I want to read actual (just before restart ) status of my light device (was it on or off)

    To save epprom I don't want to save device status on esp side.

    I know that I can read this status by sending http request : /json.htm?type=devices&rid=3 and parse response.

    My question is :
    Is there other simple way to read this status ?

  • Admin

    @marcin.kasinski All you need to do is ask the gateway for the light status in your setup() method. The gateway will respond to request() by sending a V_STATUS message for your light to your ESP node.

    NOTE: Currently there is a bug in the MYS 2.0 release that invokes the sketch setup() before presentation() and you can't send messages to the gateway until presentation() completes. You will need to upgrade to the development branch (next beta) in order for this to work (i.e. ensure that setup() is call after presentation().

    void setup()  
      //Synchronize relay state with gateway - this will result in receive() being called.
      request( CHILD_ID, V_STATUS );

  • Hi.

    Thank you for your reply.

    I placed
    request(3, V_STATUS);

    in setup () and loop()

    This code does nothing.

    No log messages in domoticz.
    No debug messages on esp side.

    Another thing:
    After ESP restarts it isn't visible in domoticz automically.

    To connect it I have to log domoticz console and :

    Setup - >Hardware -> Click my device

    Then just click update

    Just after that I can see on esp side:

    0;255;3;0;9;Client 0 connected
    0;255;3;0;9;Client 0: 0;0;3;0;2;

    Another way is to wait 30 second for domoticz reconnect.

    I said that becouse I see that when esp invokes setup() function device is nor connected to device.

    Generally speaking I can not see any messages after request(3, V_STATUS);

    No receive() execution.

    Didn't I miss something ?

  • Admin

    @marcin.kasinski said:

    I placed
    request(3, V_STATUS);

    in setup () and loop()

    This code does nothing.

    No log messages in domoticz.
    No debug messages on esp side.

    Did you upgrade to the development version of MySensors?

    Also, please post your entire sketch...

    Another thing:
    After ESP restarts it isn't visible in domoticz automically.

    We will have to figure this problem out first. Maybe the sketch will include a hint.

  • @blacey

    Hi Thank you for hour help.

    I do really need to solve it.

    I prepared sample as simple as possible:

    on ESP:

    0;255;3;0;9;Client 0: 0;0;3;0;18;PING
    0;255;3;0;9;Client 0: 0;0;3;0;18;PING
    0;255;3;0;9;Client 0: 0;0;3;0;18;PING

    On domoticz

    2016-11-23 08:50:12.675 MySensors: Gateway Version: 2.0.1-beta

    After restarting ESP (deploy code again)

    On ESP:

    áNɆŔü0;255;3;0;9;MCO:BGN:INIT GW,CP=R-NGE--,VER=2.0.1-beta
    f 0, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 3
    connected with MK, channel 11
    dhcp client start...
    0;255;3;0;9;MCO:REG:NOT NEEDED
    Setup request sent [0]
    0;255;3;0;9;MCO:BGN:INIT OK,ID=0,PAR=0,DIS=0,REG=1
    pm open,type:2 0

    On domoticz :

    no new messages
    Then silent

    Then After Setup - >Hardware -> Click my device - > Click update

    ON esp:

    0;255;3;0;9;Client 0 connected
    0;255;3;0;9;Client 0: 0;0;3;0;2;
    0;255;3;0;9;Client 0: 0;0;3;0;2;Get Version
    0;255;3;0;9;Client 0: 0;0;3;0;18;PING
    0;255;3;0;9;Client 0: 0;0;3;0;18;PING

    On domoticz:

    2016-11-23 08:55:47.768 MySensors: TCP/IP Worker stopped...
    2016-11-23 08:55:48.782 MySensors: trying to connect to:
    2016-11-23 08:55:49.783 MySensors: connected to:
    2016-11-23 08:55:49.784 MySensors: Gateway Ready...
    2016-11-23 08:55:49.788 MySensors: Node: 0, Sketch Name: Relay
    2016-11-23 08:55:49.790 MySensors: Node: 0, Sketch Version: 1.0
    2016-11-23 08:55:50.793 MySensors: Gateway Version: 2.0.1-beta
    2016-11-23 08:55:50.794 MySensors: Gateway Version: 2.0.1-beta 

    My sketch:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 115200
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_GATEWAY_ESP8266
    #define MY_ESP8266_SSID "xxx"
    #define MY_ESP8266_PASSWORD "xxx"
    // Enable UDP communication
    //#define MY_USE_UDP
    // Set the hostname for the WiFi Client. This is the hostname
    // it will pass to the DHCP server if not static.
    #define MY_ESP8266_HOSTNAME "sensor-gateway"
    // The port to keep open on node server mode
    #define MY_PORT 5003
    // How many clients should be able to connect to this gateway (default 1)
    // Enable inclusion mode
    // Enable Inclusion mode button on gateway
    // Set inclusion mode duration (in seconds)
    // Digital pin used for inclusion mode button
    // Set blinking period
    // #define MY_DEFAULT_LED_BLINK_PERIOD 300
    // Flash leds on rx/tx/err
    // Led pins used if blinking feature is enabled above
    #define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    #if defined(MY_USE_UDP)
      #include <WiFiUdp.h>
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    void setup() {
    bool ret=request(1, V_STATUS); 
    Serial.printf("Setup request sent [%d]\n",ret);
    void presentation() {
      // Present locally attached sensors here
        sendSketchInfo("Relay", "1.0");
        present(1, S_BINARY);
    void loop() {
      // Send locally attached sensors data here
    void receive(const MyMessage &message) {
    int cmd = mGetCommand(message);
           Serial.printf("RECEIVE sensor [%d] type [%d] command [%d]\n",message.type, cmd);

    Edited to use code blocks for readability.

  • Admin

    @marcin.kasinski Hmmm, now I'm confused. Are you using ESP for your Gateway or Node or both? Do I understand correctly that you have not attached either an NRF24 or RFM69 radio to your node (all #defines for radio are commented out)? The MySensors gateway and nodes only communicate with each over NRF24 or RFM69 radios (i.e. not WiFi).

  • @blacey

    You are right.

    I don't use eighter NRF24 or RFM69.

    I have domoticz server and esp with relay connected only.

    All I want to do is switch on or off my relay connected to esp pin from domoticz.

    It works for me.

    I can see that domoticz sends comands to my ESP.

    I can see that receive function on esp side is executed every time I click on domoticz side.

    My only problem is "esp restart issue".

    I would like to ask domoticz for actual status (is it on or off) in esp setup() function to set my relay connected to esp module.

    Now you know my requirements.

    1. If you think my code works but I do it in not a proper way please correct me.
    2. If you know how to ask domoticz for device actual status please help me.

Log in to reply

Suggested Topics