// ESP8266 Plugin for HomeRemote V 1.3 // Value to send: On / Off / Status // Author: Kalle //The following values can be used in HomeRemote: On Off Status (case sensetive) plugin.Name = "ESP_Switch_Test"; plugin.OnChangeRequest = onChangeRequest; plugin.OnConnect = onConnect; plugin.OnDisconnect = onDisconnect; plugin.OnPoll = onPoll; plugin.OnSynchronizeDevices = onSynchronizeDevices; plugin.DefaultSettings = { "IpAddress": "ESP_IP", "PollingInterval": "15000" }; var http = new HTTPClient(); function onChangeRequest(ESPtest, attribute, value) { var ipAddress = plugin.Settings["IpAddress"]; var command = value == "On" ? "1" : "0"; var url = "http://" + ipAddress + "/gpio/" + command; console.log("Sent request: " + url); http.get(url); onPoll(); // Update the status immediately after sending a change request } function onConnect() { plugin.PollingInterval = parseInt(plugin.Settings["PollingInterval"], 10); } function onDisconnect() { // Here you can add code to respond to disconnection events } function onPoll() { var ipAddress = plugin.Settings["IpAddress"]; var url = "http://" + ipAddress + "/gpio/status"; console.log("Sent request: " + url); var response = http.get(url); if(response.status == 200) { for (var id in plugin.Devices) { var ESPtest = plugin.Devices[id]; ESPtest.Status = response.data == "1" ? "On" : "Off"; } } } function onSynchronizeDevices() { var ESPtest = new Device(); ESPtest.Id = 1; ESPtest.DisplayName = "Light"; //the device which is created in the Plugin as Switch ESPtest.Capabilities = ["Switch"]; ESPtest.Attributes = ["Status"]; plugin.Devices[ESPtest.Id] = ESPtest; }