Setting initial state of onn/off -button in gateway

Hi,
I am a new WebThings user and also new to programming in general.

My setup is an ESP8266 with a relay connected to GPIO 4 and a pushbutton switch connected to GPIO5. I like to control the relay with the button in the gateway, and with the physical switch.

I have copied, pasted and experimented, and now the code works as I want it to. But I am stuck on one thing. I am using the following code in the main loop to update the status of the on/off-button in the gateway:

Blockquote
ThingPropertyValue stateValue;
stateValue.boolean = relayState;
relayOn.setValue(stateValue);
Blockquote

But using the same code in setup, I can not get the gateway button state to update on initial startup off the ESP8266.

I have also tried this version of the code:

Blockquote
ThingPropertyValue initialOn = {.boolean = false};
relayOn.setValue(initialOn);
(void)relayOn.changedValueOrNull();
Blockquote

If the ESP8266 is powered off when the gateway button displays “On” the button does not update to display “Off” when the ESP is powered back up.

Here is the complete code I am using (I am not sure if it is best practice to paste the entire code here, so please tell me if I should do it differently):

Blockquote
//Controlling a relay from WebThings Gateway and from a pyhsical pushbutton switch
#include <Arduino.h>
#include “Thing.h”
#include “WebThingAdapter.h”
//Function forward declaration
bool debounce(bool sState);
//Hardcode your wifi credentials here (and keep it private)
const char *ssid = “X”;
const char *password = “X”;
//Hostname used by mDNS
const String mDNSHostname = “ESP8266RelayX”;
//Using built in LED for booting an wifi-connecting information
#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 13; // manually configure LED pin
#endif
//Defining output pin for relay and switch
const int relayPin = 4;
const int switchPin = 5;
//Handle to connection between Thing and Gateway
WebThingAdapter *adapter;
//@type members: Capabilities supported by your Thing
//See schemas for compability: https://webthings.io/schemas/
const char *outputTypes[] = {“OnOffSwitch”, nullptr};
//Description of your Thing
//ThingDevice device(id, title, types)
//id: unique identifier for Thing (part of URL: http:///things/)
//description: string that shows up in Gateway for your Thing
//types: array of types
ThingDevice relay(“RelayX”, “On/Off button for RelayX”, outputTypes);
//Define one or more properties supported by your Thing
//ThingProperty property(id, description, type, atType)
//id: unique identifier for property
//description: user-readable description of property
//type: NO_STATE, BOOLEAN, NUMBER or STRING
//atType: property @type (https://webthings.io/schemas/)
ThingProperty relayOn(“relayX-on”, “”, BOOLEAN, “OnOffProperty”);
bool on = false;
bool lastOn = false;
bool lastSwitch = true;
bool switchState = true;
bool relayState = false;
void setup(void)
{
//Pin definitions and initialisations
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
digitalWrite(ledPin, HIGH); //Active low led
digitalWrite(relayPin, LOW);
//Connection and serial debugging
Serial.begin(115200);
Serial.println("");
Serial.print(“Connecting to “”);
Serial.print(ssid);
Serial.println(”"");
#if defined(ESP8266) || defined(ESP32)
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
bool blink = true;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, blink ? LOW : HIGH);
blink = !blink;
}
digitalWrite(ledPin, HIGH);
//Show connection details
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
//Setup for Things
adapter = new WebThingAdapter(“w25”, WiFi.localIP());
relay.addProperty(&relayOn);
adapter->addDevice(&relay);
adapter->begin();
Serial.println(“HTTP server started”);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.print(”/things/”);
Serial.println(relay.id);
Serial.println(“Version-027”);
//Setting initial gateway button state
//This code is not working
ThingPropertyValue stateValue;
stateValue.boolean = relayState;
relayOn.setValue(stateValue);
}
void loop(void)
{
adapter->update();
//Get gateway button state
on = relayOn.getValue().boolean;
//Check if physical switch has been pressed
if (debounce(switchState) != lastSwitch)
{
Serial.print("switchState = ");
Serial.println(switchState);
//Check if switch has gone from true to false and change gateway button state
if (!switchState)
{
on = !on;
}
}
//Check if gateway button state has been changed (by gateway button or physicalswitch)
if (on != lastOn)
{
Serial.print("on = ");
Serial.println(on);
if (on)
{
relayState = true;
}
else
{
relayState = false;
}
digitalWrite(relayPin, relayState);
delay(2000);
//Update button state in gateway
ThingPropertyValue stateValue;
stateValue.boolean = relayState;
relayOn.setValue(stateValue);
}
lastOn = on;
lastSwitch = switchState;
}
bool debounce(bool sState)
{
switchState = digitalRead(switchPin);
if (sState != switchState)
{
delay(100);
switchState = digitalRead(switchPin);
}
return switchState;
}
Blockquote

Hope someone can help me with this, best regards
Robert Friberg

Edit: I had to remove empty lines in the code to get the code be displayed inside the Blockquote. This makes the code harder to read, sorry for that.