Hello. Can I add in the code for my ESP8266 notification from another Switch / device?
For now I use only gpio14.
What do I need to add to my code to upload it?
You have to do the below part again for the new pin/switch/sensor and modify the variables like PIRSignalPin, DOMOTICZ_SWITCH_IDX to PIRSignal2Pin, DOMOTICZ_SWITCH_IDX2 etc.
Serial.println("Sending On signal to Domoticz.");
//Build API command
api = DOMOTICZ_SRV;
api += "/json.htm?type=command¶m=switchlight&idx=";
api += DOMOTICZ_SWITCH_IDX;
if (digitalRead(PIRSignalPin)==HIGH) {
api += "&switchcmd=On";
} else {
DEBUGPRINTLN0("Sending 'Off' signal to Domoticz.");
api += "&switchcmd=Off";
}
http.begin(api); //Specify request destination
httpCode = http.GET(); //Send the request and set return code
Serial.print("Return code: ");
Serial.println(httpCode);
if (httpCode > 0) { //Check the returning code
response = http.getString(); //Get the request response
Serial.println("Response:");
Serial.println(response);
}
http.end(); //Close connection
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Hello! Thanks first for the help.
I tried to do what you say.
I added a few lines of code. I upload it to see if it is correct because I receive various notifications.
/* PIR Advanced motion Detection with deep-sleep mode */
#define DEBUGPRINTLN0(x) Serial.println(x)
ADC_MODE(ADC_VCC); //vcc read
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// WiFi credentials & Domoticz API IDX.
const char* WIFI_SSID = "mywifiname";
const char* WIFI_PASS = "1231231321";
const char* DOMOTICZ_SRV = "http://192.168.11.20:8080";
const char* DOMOTICZ_SWITCH_IDX = "1";
const char* DOMOTICZ_SWITCH_IDX2 = "7";
const char* DOMOTICZ_VOLT_IDX = "2";
const char* DOMOTICZ_VOLT_IDX2 = "8";
const int PIRSignalPin = 14; //Using GPIO14 for PIR Data
const int PIRSignalPin2 = 4; //Using GPIO4 for PIR Data
void setup() {
HTTPClient http; // HTTP object
String api; // Holds full API string for Domoticz
int httpCode; // Return HTTP code
String response; // Response from HTTP GET request
float vdd; // Internal voltage
String s_vdd; // Internal voltage (formatted as string)
Serial.begin(115200);
Serial.setTimeout(2000);
while (!Serial) { } // Wait for serial to initialize.
Serial.println("");
Serial.println("-------------------------------------");
Serial.println("PIR Motion Detection Firmware!");
Serial.println("-------------------------------------");
// Connect to Wifi.
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
// Check to see if
if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.println("");
Serial.println("Failed to connect to WiFi. Please verify credentials!");
delay(10000);
} else {
delay(500);
Serial.print(".");
}
}
Serial.println("");
Serial.print("WiFi connected with IP address: ");
Serial.println(WiFi.localIP());
// Send post request to Domoticz
Serial.println("");
Serial.println("Sending On signal to Domoticz.");
//Build API command
api = DOMOTICZ_SRV;
api += "/json.htm?type=command¶m=switchlight&idx=";
api += DOMOTICZ_SWITCH_IDX;
if (digitalRead(PIRSignalPin)==HIGH) {
api += "&switchcmd=On";
} else {
DEBUGPRINTLN0("Sending 'Off' signal to Domoticz.");
api += "&switchcmd=Off";
}
http.begin(api); //Specify request destination
httpCode = http.GET(); //Send the request and set return code
Serial.print("Return code: ");
Serial.println(httpCode);
if (httpCode > 0) { //Check the returning code
response = http.getString(); //Get the request response
Serial.println("Response:");
Serial.println(response);
}
http.end(); //Close connection
Serial.println("Sending On signal to Domoticz.");
//Build API command
api = DOMOTICZ_SRV;
api += "/json.htm?type=command¶m=switchlight&idx=";
api += DOMOTICZ_SWITCH_IDX2;
if (digitalRead(PIRSignalPin2)==HIGH) {
api += "&switchcmd=On";
} else {
DEBUGPRINTLN0("Sending 'Off' signal to Domoticz.");
api += "&switchcmd=Off";
}
http.begin(api); //Specify request destination
httpCode = http.GET(); //Send the request and set return code
Serial.print("Return code: ");
Serial.println(httpCode);
if (httpCode > 0) { //Check the returning code
response = http.getString(); //Get the request response
Serial.println("Response:");
Serial.println(response);
}
http.end(); //Close connection
delay(200);
Serial.print("Sending voltage to Domoticz: ");
vdd = ESP.getVcc() / 1024.0f;
s_vdd = String(vdd, 3);
Serial.println(s_vdd);
api = DOMOTICZ_SRV;
api += "/json.htm?type=command¶m=udevice&idx=";
api += DOMOTICZ_VOLT_IDX;
api += "&nvalue=0&svalue=";
api += s_vdd;
http.begin(api); //Specify request destination
httpCode = http.GET(); //Send the request and set return code
Serial.print("Return code: ");
Serial.println(httpCode);
if (httpCode > 0) { //Check the returning code
response = http.getString(); //Get the request response
Serial.println("Response:");
Serial.println(response);
}
http.end(); //Close connection
Serial.println("");
Serial.println("Going into deep sleep!");
ESP.deepSleep(0); // forever
}
void loop() {
// Never reached!
}
Now when I put a motion sensor that is numbered "101" and it starts sending a notification it should come "101 on" and "101 off". he is on GPIO4.
now from this sensor I see a notification from a sensor that is on GPIO14. I would like it to come only from GPIO4 named 101.
I do not understand your question about notifications on the switches.
Are the 2 switches in Domoticz being updated by the ESP8266 correctly now?
How do you send those notifications? It is not related to the ESP8266 device program but is normally managed by Domoticz Notification system or some Domoticz scripts.