Page 1 of 1

code for two sensors / switches on one ESP8266 device

Posted: Wednesday 11 May 2022 5:54
by nalivikam
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?


This is the code I use.

Code: Select all

/* PIR Advanced motion Detection with deep-sleep mode */

ADC_MODE(ADC_VCC); //vcc read

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// WiFi credentials & Domoticz API IDX.
const char* WIFI_SSID = "<YOUR_WIFI_SSID>";
const char* WIFI_PASS = "<YOUR_WIFI_PASS>";
const char* DOMOTICZ_SRV = "http://192.168.123.100:8080";
const char* DOMOTICZ_SWITCH_IDX = "45";
const char* DOMOTICZ_VOLT_IDX = "22";
const int PIRSignalPin = 14; //Using GPIO14 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&param=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

  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&param=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!
}


I take it from this topic for which I thank the author!
viewtopic.php?t=30792
viewtopic.php?f=51&t=19572

Thanks in advance!

Re: code for two sensors / switches on one ESP8266 device

Posted: Wednesday 11 May 2022 8:46
by waltervl
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.

Code: Select all

  Serial.println("Sending On signal to Domoticz.");
  //Build API command
  api = DOMOTICZ_SRV;
  api += "/json.htm?type=command&param=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

Re: code for two sensors / switches on one ESP8266 device

Posted: Sunday 15 May 2022 7:59
by nalivikam
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.

Code: Select all

/* 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&param=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&param=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&param=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'm uploading a picture
Help me please!

Re: code for two sensors / switches on one ESP8266 device

Posted: Monday 16 May 2022 16:34
by waltervl
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.

Edit: Seems to be this topic: https://domoticz.com/forum/viewtopic.php?f=59&t=38446 for the notifications. Better have the discussion about notifications there.