HTTP Response Json

Could someone help me?

I’m trying to change the state of the toggle switch according to the response in the onPoll() function.

Plugin:

plugin.Name = “ModuleSimple”;
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) {
if (device.Id == “1”) {
switch (value) {
case “On”:
http.get(“http://IP/ON”);
break;
case “Off”:
http.get(“http://IP/OFF”);
break;
default:
break;
}
}
else {
throw “Comandos para este dispositivo ainda não foram implementados”;
}
}

function onConnect() {
}

function onDisconnect() {
}

function onPoll() {
var lampadaState = http.get(“http://IP/”);
var response = JSON.parse(lampadaState);
var lampadaStatus = response.lampada;

var lampadaDevice = plugin.Devices["1"];
lampadaDevice.Attributes["Switch"] = lampadaStatus;

}

function onSynchronizeDevices() {
var lampada = new Device();
lampada.Id = “1”;
lampada.DisplayName = “Lâmpada”;
lampada.Capabilities = [“Switch”];
lampada.Attributes = [];
plugin.Devices[lampada.Id] = lampada;
}

Your plugin code looks correct - make sure the JSON response is in the expected format and that the lampada field contains the status of the lamp. You can check this by logging the response. Ensure that the device is updated correctly when the status is changed. You can also check this by logging the status of the device after the update as above.

In HR you can display the log window by clicking on Log at the bottom left while the code is running in the simulator.

function onPoll() {
    var lampadaState = http.get("http://IP/");
    console.log('Lampada State:', lampadaState); // Log the response
    var response = JSON.parse(lampadaState);
    var lampadaStatus = response.lampada;
    console.log('Lampada Status:', lampadaStatus); // Log the status

    var lampadaDevice = plugin.Devices["1"];
    lampadaDevice.Attributes["Switch"] = lampadaStatus;
    console.log('Device Status:', lampadaDevice.Attributes["Switch"]); // Log the device status
}
2 Likes

The device is sending data as expected and the log accurately reflects the current status of the device. I would like to integrate this information directly into the application, perhaps by displaying it on a label or updating the state of the switch, for example.

Currently, when interacting with the device through the application, everything works as expected. However, when interaction occurs outside of the application, the interface does not reflect the current state of the device.

In the log, the status is updated every minute. Is it possible to reduce this time?

Hi Lucas,
the polling interval should be 1 second and not 1 minute (plugin.PollingInterval = 1000;) The value is specified in milliseconds and can be changed.
I will describe later how you can display the status, e.g. in a label or as a switch status.

1 Like

Create a label and use multibinding for “Text”. Use {0} as the string format and test whether the label displays the desired information. You can post your complete Plugin Code to take a look on it.

1 Like

Kalle, unfortunately, it didn’t work. When I do as you showed, the device’s name appears.

ESP8266Lampada.plugin (1.3 KB)


If I understand it correctly, you are controlling a relay with an ESP - correct? So you have an http command for ON and one for OFF. In your onPoll function, you send an http command for the status query that only contains the IP of the ESP (line 36).
If you send this command using only a web browser, what response do you get?

Correct. The same message that appears in the log also appears in the web browser and on the serial monitor (current status of the lamp).

On line 36, esp sends the current state of the relay (as shown in the photo above).

For example, if I turn off the lamp through the app and turn it on using the physical switch, the current state of the lamp “on” will not appear in the app, it will show as “off”. So, I want to retrieve the information about the current state that appears in the log and display it in the application.

Ok, I didn’t realize what “desligado” meant.
You can try adding the Switch attribute to your code at the end:
lampada.Attributes = [“Switch”];

If you then create a label and use multibinding for “Text”, you should be able to select the attribute “Switch” for the switch and apply string format {0}, as shown in my example above.
You can upload your code for the ESP and I can test it on my end as well.

ESP8266Lampada.plugin (1.3 KB)

Esp8266Lampada.txt (2.2 KB)

Hi Lucas,
your code could not work properly with HR because it was missing some important things.
I have customised your code and added a few things.
Above all, the response from the server to the client was missing - if you now send
http://ESP_IP/gpio/1 you get “On” as a response
http://ESP_IP/gpio/0 you get “Off” as a response
http://ESP_IP/gpio/status (the answer from server is On or Off)
http://ESP_IP/gpio/toggle (the switch toggle the state)

ESP_Lampada_NEW_V2.txt (3.1 KB)

please try this code and let me know how it works

Hi Kalle,
In the ESP, the code worked perfectly. In the console.log, the status doesn’t appear, like it did before.

Hi Lucas,
I have solved it.

Here the ESP Code:
ESP_Code_New.txt (1.6 KB)

here the Plugin Code:
ESP_Lampada_NEW_V3.txt (1.8 KB)

Example: You can create a Label an use Binding Light.Status

For example, create a switch and set in the event trigger Light.Switch as binding

You can use as value: On Off Status

Don’t forget to set up your IP and password in the ESP code.
The Plugin has now settings for your ESP_IP and the polling interval (15 seconds are the default value)

Ask if you have questions and have fun

Kalle

1 Like

Hi Kalle,

Firstly, I wanted to thank you for the attention you gave to my project, the codes you provided worked perfectly. Thank you.

You welcome, so I can publish this code in the Plug-in section :wink:

1 Like