So @bangali asked me to make a how-to guide for how i do my sensors and actuators.
I make my own sensors because they are really cheap and it’s fun to make them.
The only thing missing is cases for the sensors, i’m still saving up for a 3D printer to make them.
In this how-to guide im going to show how to add ANY sensor thats compatible with an ESP8266 (I’m using NodeMCU’s). Actuators are handled a bit differently so i might make a other how to for that.
So first step is getting the hardware:
-
NodeMCU or other wifi enabled ESP8266, I use this one:
https://www.aliexpress.com/item/5sets-D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/32643174041.html?spm=a2g0s.9042311.0.0.8sz9Xd -
Choose any sensor or actuators you want to interface to SmartThings. Here are some examples:
Light Sensor
https://www.aliexpress.com/item/Free-Shipping-Photosensitive-Sensor-Module-Light-Detection-Module-for-Arduino/32509595988.html?spm=a2g0s.9042311.0.0.8nxp26
Ultrasonic distance sensor
https://www.aliexpress.com/item/Free-shipping-1pcs-Ultrasonic-Module-HC-SR04-Distance-Measuring-Transducer-Sensor-for-Arduino-Samples-Best-prices/32640823431.html?spm=a2g0s.9042311.0.0.8nxp26
And there are so many more sensors and actuators available. I can’t really think of any examples that don’t work.
Interfacing to SmartThings
So now you have your hardware that you want to use. Now you need to interface it to SmartThings.
I’m now assuming people know how to program a arduino, if people want info about that let me know.
Because there are alot of ways to do this i’m going to show you my way.
First add the libraries for your ESP8266 in my case for the NodeMCU:
Add this URL, go to File -> Preferences -> "Additional BOards Manager URLs"
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Now go to Tools -> Boards -> Boards Manager -> Search “ESP8266” and install it
Select the correct board to compile to.
Most sensors have a arduino library, find it and add it to your code. (If you need an example let me know)
Now you can make a simple code to process your info from your sensor. Here you can read the sensor and process how ever you would want. Here is an example for my Lux sensor:
void loop() {
digitalWrite(sensorvcc, HIGH);
delay(500);
uint16_t lux = lightMeter.readLightLevel() * 100;
delay(500);
digitalWrite(sensorvcc, LOW);
SendMessage(message + lux);
delay(100000);
}
To lower power usage i switch off the sensor after it’s been used. So i first turn on the sensor, read the value and send the value to SmartThings. Here is the code to send the message to SmartThings, you will need to know your SmartThings hub ip adress and port.
//*******************************************************************************
/// Send Message out over Ethernet to the Hub
//*******************************************************************************
void SendMessage(String message)
{
if (st_client.connect(hubIp, hubPort))
{
st_client.println(F(“POST / HTTP/1.1”));
st_client.print(F(“HOST: “));
st_client.print(hubIp);
st_client.print(F(”:”));
st_client.println(hubPort);
Serial.println(message);
st_client.println(F("CONTENT-TYPE: text"));
st_client.print(F("CONTENT-LENGTH: "));
st_client.println(message.length());
st_client.println();
st_client.println(message);
}
else
{
//connection failed;
Serial.print(F(""));
Serial.println(F("SendMessage() - Ethernet Connection Failed"));
Serial.print(F("hubIP = "));
Serial.print(hubIp);
Serial.print(F(" "));
Serial.print(F("hubPort = "));
Serial.println(hubPort);
}
// read any data returned from the POST
while (st_client.connected() && !st_client.available()) delay(1); //waits for data
while (st_client.connected() || st_client.available()) //connected or data available
{
char c = st_client.read();
}
delay(1);
st_client.stop();
}
Now you need to make a device type to pick up this message and process it as you want. If enough people want to know more about this all i will add more info but i’m new to this kind of thing. I’m pretty sure i’m forgetting alot and i have no idea how much experience people have. Please leave feedback and questions if you have any!