ESP8266 integration


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:

Lux Sensor:

Ultrasonic distance sensor

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"

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);
uint16_t lux = lightMeter.readLightLevel() * 100;
digitalWrite(sensorvcc, LOW);
SendMessage(message + lux);

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.println(F("CONTENT-TYPE: text"));
  st_client.print(F("CONTENT-LENGTH: "));
  //connection failed;
    Serial.println(F("SendMessage() - Ethernet Connection Failed"));
    Serial.print(F("hubIP = "));
    Serial.print(F(" "));
    Serial.print(F("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 =;



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!


I am using an ESP8266 for my chicken coop as well. A few comments to make things easier in the long run (so that you don’t have to hardcode anything in the ESP8266 code):

  1. Use WiFiManager to allow configuring the WiFi network the sensor should connect to
  2. Use ArduinoJson to parse/construct JSON payloads
  3. Use ESP8266HTTPClient for easier POSTing of messages to the hub
  4. Use ESP8266WebServer to allow configuring the hub’s IP and port from the DTH
  5. Use ESP8266HTTPUpdateServer to allow updating the code on the sensor without having to connect it to a computer again
  6. Use ESP8266SSDP to give your device a nice name on your network

I’m happy to elaborate more if you have questions - my own code is probably too specific to my implementation (and also a little over 800 lines long :wink:) so I’m not posting it…

As for 3D printing cases, you might want to check whether your local library offers access to a 3D printer.


Nice! Yea i’m using most of your suggestions already. But didn’t know about the ability to update the code without connecting it to your pc. Can you explain the updateserver?


I am new to smartthings. I also just found out what an esp8266 and an arduino existed while searching through the smartthings thread. I did buy an arduino starter kit and an esp8266 and have been watching some u-tube vids on them. So I am new to all of this, If you could make a guide for dummies, it would be approciated. I know everyone is busy with their own life and if your too busy, I understand, and thanks for what you have put up so far.


Add this library:

(There are some examples as well). Once you added it correctly you can navigate to a web page on your ESP8266 and upload a compiled binary to upload.


Well what do you want to make?


I need to edit my code to force WiFi reconnect when router reboots.


I don’t think you need to do anything there – the ESP8266 handles that automatically.


Hmm, I will post the code I have in mine when I get to a PC, might be doing something wrong. I just use mine to control an old Onkyo receiver.


I bought an ESP8266 NodeMCU LUA CP2102 ESP-12E, and was wanting to use it to use it to
1). learn the RF frequency of my ceiling fan.
2). be able to broadcast back the learned.
3). integrate it with smartthings and make an app with light on, light off, fan on high, medium, low, and off buttons, so that I can voice command it with my alexa.

If learning the RF frequency and broadcasting back is impossible, then I would like to use and integrate, Frankenstein, the ESP8266 NodeMCU LUA CP2102 ESP-12E, and, I’m guessing, a 6 or 8 gang of relays, to trigger the current buttons on my current remote to control the fan by voice command.

Learning the RF frequency and broadcasting back would be nice, I have seen many other people looking a smart fan control.

I would also like to build a time machine so that I can prevent myself from contracting gonorrhea with the first girl I slept with.


For the RF part you would need to add a separate RF transmitter/receiver (for the learning) to the ESP.

For the time machine – while I understand the reason – the ESP might just not be powerful enough :grin:


When you say powerful enough, do you mean the broadcast signal would be weak? If so, then could I place the esp in the cover next to the actual fan receiver?


Get these:

Then you just have to decode the signal with the receiver. Program the signal so it transmits it.
You could make it in a device handler with multiple buttons for the different speeds and on/off.
Try to look up your fan if someone already make a library for your remote.


Would this work? The one in your link take 30-90 days.


Well i’ve got these aswell but the range of the receiver isn’t as good as the one i send you. Try to find the same one if possible, otherwise get the one you just said and be sure to have everything close by when testing. The receiver is pretty good with a range around the 50 meters.


newegg 8-15 days, 5.99. cant complain about a 50% mark up when shipping is you get 6 times faster.


This is the existing code I have loaded, this should be all the WiFi related ones. I do need to add the others mentioned in the thread so I can updated code remotely.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//#include <IRremoteESP8266at5V.h>
#include <IRremoteESP8266.h>
const char* ssid = "SSID";
const char* password = "wifipass";

MDNSResponder mdns;
ESP8266WebServer server(80);
IRsend irsend(D2);


  // Static IP details...
  // 13=ONKYO, 12=OPTOMA
  IPAddress ip(192, 168, 0, 13);
  IPAddress gateway(192, 168, 0, 1);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress dns(192, 168, 0, 1);
  // Static IP Setup Info Here...
  WiFi.config(ip, dns, gateway, subnet); //If you need Internet Access You should Add DNS also...
  WiFi.begin(ssid, password);

  if (WiFi.status() == 6)

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print("Connected to ");
  Serial.print("IP address: ");

  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");


To update remotely you need to add ESP8266HTTPUpdateServer as well.


I would add wifimanager in here. Then you don’t have any credentials hard coded in your code :wink:


Thank you @ahndee and @johhnwilliam for the tips. I’ll get those added in, the remote update would have come in handy now since one of my esp8266 is up there with the projector mount :smiley:

I am also interested in RF control of a ceiling fan so I’m going to check out the link to the receiver. I’ve captured IR before with the esp8266 so the RF would be another interesting project.