@949BFN, @vseven
Hendre,
I have created a new Child Device Handler called “Child Presence Sensor” which is now available in the ST_Anything GitHub repository. You will also need to update the Parent Ethernet Device Handler as well.
In order for this Child Device to appear, the ESP8266 device’s sketch will need to send updates to a device named “presence1” (or “presence2”, “presence3”, etc…) This name is what the Parent DTH uses to determine which type of child device to create. Below is an example sketch that demonstrates this naming convention.
The new “Child Presence Sensor” device also exposes a custom attribute called “level” which is the RAW numeric value sent from ST_Anything to SmartThings. This means you can use this value in an app like webCoRE if you’d like.
In this sketch, which is a continuation of the previous example we created together, you will see that three ST_Anything devices are being created. You can remove any that aren’t needed for your project. You should be able to use one NodeMCU ESP8266 for both garage stalls. Just define unique pin assignments.
//Polling Sensors
static st::PS_Ultrasonic sensor1(F("ultrasonic1"), 60, 0, PIN_ULTRASONIC_T, PIN_ULTRASONIC_E);
static st::PS_Ultrasonic sensor2(F("presence1"), 60, 5, PIN_ULTRASONIC_T, PIN_ULTRASONIC_E);
static st::PS_Voltage sensor3(F("presence2"), 15, 1, PIN_ANALOG, 0, 1023, 0, 100);
The first one should look very familiar to you. Nothing has changed, and it will still cause an “Child Ultrasonic Sensor” to be created and updated, just like before. This is because of the first argument, the name, which is “ultrasonic1” (i.e. the first ultrasonic child device).
The second one is exactly the same as the first, BUT the name is now “presence1”, which will cause a new Child Presence Sensor child device to be created and updated. This new child MUST be configured by you in the child’s settings page. The settings are the “Presence Trigger Value” (numeric) and the “Invert Logic” (boolean). With these two settings, you can determine what value will change the presence from “present” to “not present”, and if the logic should check for presence above or below the trigger value.
The third one demonstrates the creation of a second “Child Presence Device”, but this one is driven by an analog input’s voltage reading, not the ultrasonic distance measurement sensor you’ve been using. This is what Allen was describing above as an option. It uses an st::PS_Voltage device instead of the st::PS_Ultrasonic device. Since both devices simply transmit a numeric value, the Child Device Handler doesn’t care who sends the data, as long as they call themselves “presence1” (or “presence2”, “presence3”, etc…) Hopefully this is making sense.
So, at this point, I think you have everything you need to create your Ultrasonic Distance based Garage Occupancy Sensors.
@vseven Allen - FYI - the new “Child Presence Sensor” will accept numeric values, or the strings “present” and “not_present” as values. If numeric, it does the comparison against the optional settings for the child device. If non-numeric, it simply uses the string values to update the status.
If anyone finds any issues/bugs, please let me know so we can get them addressed ASAP.
//******************************************************************************************
// File: ST_Anything_UltrasonicESP8266WiFi.ino
// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
// Summary: This Arduino Sketch, along with the ST_Anything library and the revised SmartThings
// library, demonstrates the ability of one ESP8266 to
// implement a multi input/output custom device for integration into SmartThings.
// The ST_Anything library takes care of all of the work to schedule device updates
// as well as all communications with the ESP8266's WiFi.
//
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2018-02-25 Dan Ogorchock Original Creation
//
//******************************************************************************************
//******************************************************************************************
// SmartThings Library for ESP8266WiFi
//******************************************************************************************
#include <SmartThingsESP8266WiFi.h>
//******************************************************************************************
// ST_Anything Library
//******************************************************************************************
#include <Constants.h> //Constants.h is designed to be modified by the end user to adjust behavior of the ST_Anything library
#include <Device.h> //Generic Device Class, inherited by Sensor and Executor classes
#include <Sensor.h> //Generic Sensor Class, typically provides data to ST Cloud (e.g. Temperature, Motion, etc...)
#include <Executor.h> //Generic Executor Class, typically receives data from ST Cloud (e.g. Switch)
#include <InterruptSensor.h> //Generic Interrupt "Sensor" Class, waits for change of state on digital input
#include <PollingSensor.h> //Generic Polling "Sensor" Class, polls Arduino pins periodically
#include <Everything.h> //Master Brain of ST_Anything library that ties everything together and performs ST Shield communications
#include <PS_Illuminance.h> //Implements a Polling Sensor (PS) to measure light levels via a photo resistor
#include <PS_TemperatureHumidity.h> //Implements a Polling Sensor (PS) to measure Temperature and Humidity via DHT library
#include <PS_DS18B20_Temperature.h> //Implements a Polling Sesnor (PS) to measure Temperature via DS18B20 libraries
#include <PS_Water.h> //Implements a Polling Sensor (PS) to measure presence of water (i.e. leak detector)
#include <IS_Motion.h> //Implements an Interrupt Sensor (IS) to detect motion via a PIR sensor
#include <IS_Contact.h> //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_Smoke.h> //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_DoorControl.h> //Implements an Interrupt Sensor (IS) and Executor to monitor the status of a digital input pin and control a digital output pin
#include <IS_Button.h> //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin for button presses
#include <EX_Switch.h> //Implements an Executor (EX) via a digital output to a relay
#include <EX_Alarm.h> //Implements Executor (EX)as an Alarm Siren capability via a digital output to a relay
#include <S_TimedRelay.h> //Implements a Sensor to control a digital output pin with timing capabilities
#include <PS_Ultrasonic.h> //Ultrasonic Distance Measurement Sensor, being used to monitor water level in cylindrical tank
#include <PS_Voltage.h> //Implements a Polling Sensor (PS) to measure voltage on an analog input pin
//*************************************************************************************************
//NodeMCU v1.0 ESP8266-12e Pin Definitions (makes it much easier as these match the board markings)
//*************************************************************************************************
//#define LED_BUILTIN 16
//#define BUILTIN_LED 16
//
//#define D0 16 //no internal pullup resistor
//#define D1 5
//#define D2 4
//#define D3 0 //must not be pulled low during power on/reset, toggles value during boot
//#define D4 2 //must not be pulled low during power on/reset, toggles value during boot
//#define D5 14
//#define D6 12
//#define D7 13
//#define D8 15 //must not be pulled high during power on/reset
//******************************************************************************************
//Define which Arduino Pins will be used for each device
//******************************************************************************************
#define PIN_ULTRASONIC_T D1 //digital output to trigger ultrasonic
#define PIN_ULTRASONIC_E D2 //digital input to read the echo
#define PIN_ANALOG A0 //Analog pin to read a voltage
//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid = "yourSSIDhere"; // <---You must edit this line!
String str_password = "yourPASSWORDhere"; // <---You must edit this line!
IPAddress ip(192, 168, 1, 228); //Device IP Address // <---You must edit this line!
IPAddress gateway(192, 168, 1, 1); //Router gateway // <---You must edit this line!
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line!
IPAddress dnsserver(192, 168, 1, 1); //DNS server // <---You must edit this line!
const unsigned int serverPort = 8090; // port to run the http server on
// Smartthings Hub Information
IPAddress hubIp(192, 168, 1, 149); // smartthings hub ip // <---You must edit this line!
const unsigned int hubPort = 39500; // smartthings hub port
//******************************************************************************************
//st::Everything::callOnMsgSend() optional callback routine. This is a sniffer to monitor
// data being sent to ST. This allows a user to act on data changes locally within the
// Arduino sktech.
//******************************************************************************************
void callback(const String &msg)
{
// Serial.print(F("ST_Anything Callback: Sniffed data = "));
// Serial.println(msg);
//TODO: Add local logic here to take action when a device's value/state is changed
//Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
//st::receiveSmartString("Put your command here!"); //use same strings that the Device Handler would send
}
//******************************************************************************************
//Arduino Setup() routine
//******************************************************************************************
void setup()
{
//******************************************************************************************
//Declare each Device that is attached to the Arduino
// Notes: - For each device, there is typically a corresponding "tile" defined in your
// SmartThings Device Hanlder Groovy code, except when using new COMPOSITE Device Handler
// - For details on each device's constructor arguments below, please refer to the
// corresponding header (.h) and program (.cpp) files.
// - The name assigned to each device (1st argument below) must match the Groovy
// Device Handler names. (Note: "temphumid" below is the exception to this rule
// as the DHT sensors produce both "temperature" and "humidity". Data from that
// particular sensor is sent to the ST Hub in two separate updates, one for
// "temperature" and one for "humidity")
// - The new Composite Device Handler is comprised of a Parent DH and various Child
// DH's. The names used below MUST not be changed for the Automatic Creation of
// child devices to work properly. Simply increment the number by +1 for each duplicate
// device (e.g. contact1, contact2, contact3, etc...) You can rename the Child Devices
// to match your specific use case in the ST Phone Application.
//******************************************************************************************
//Polling Sensors
static st::PS_Ultrasonic sensor1(F("ultrasonic1"), 60, 0, PIN_ULTRASONIC_T, PIN_ULTRASONIC_E);
static st::PS_Ultrasonic sensor2(F("presence1"), 60, 5, PIN_ULTRASONIC_T, PIN_ULTRASONIC_E);
static st::PS_Voltage sensor3(F("presence2"), 15, 1, PIN_ANALOG, 0, 1023, 0, 100);
//Interrupt Sensors
//Special sensors/executors (uses portions of both polling and executor classes)
//Executors
//*****************************************************************************
// Configure debug print output from each main class
// -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
// to prevent communication conflicts with the ST Shield communications
//*****************************************************************************
st::Everything::debug=true;
st::Executor::debug=true;
st::Device::debug=true;
st::PollingSensor::debug=true;
st::InterruptSensor::debug=true;
//*****************************************************************************
//Initialize the "Everything" Class
//*****************************************************************************
//Initialize the optional local callback routine (safe to comment out if not desired)
st::Everything::callOnMsgSend = callback;
//Create the SmartThings ESP8266WiFi Communications Object
//STATIC IP Assignment - Recommended
st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, st::receiveSmartString);
//DHCP IP Assigment - Must set your router's DHCP server to provice a static IP address for this device's MAC address
//st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, serverPort, hubIp, hubPort, st::receiveSmartString);
//Run the Everything class' init() routine which establishes WiFi communications with SmartThings Hub
st::Everything::init();
//*****************************************************************************
//Add each sensor to the "Everything" Class
//*****************************************************************************
st::Everything::addSensor(&sensor1);
st::Everything::addSensor(&sensor2);
st::Everything::addSensor(&sensor3);
//*****************************************************************************
//Add each executor to the "Everything" Class
//*****************************************************************************
//*****************************************************************************
//Initialize each of the devices which were added to the Everything Class
//*****************************************************************************
st::Everything::initDevices();
}
//******************************************************************************************
//Arduino Loop() routine
//******************************************************************************************
void loop()
{
//*****************************************************************************
//Execute the Everything run method which takes care of "Everything"
//*****************************************************************************
st::Everything::run();
}