ESP8266 integration


#1

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:
https://www.aliexpress.com/item/1Pcs-TSL2561-Luminosity-Sensor-Breakout-infrared-Light-Sensor-module-integrating-sensor/32792689415.html?spm=a2g0s.9042311.0.0.vhEYHD

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!


#2

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.


#3

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?


#4

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.


#5

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.


#6

Well what do you want to make?


#7

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


#8

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


#9

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.


#10

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 commands.it 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.


#11

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:


#12

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?


#13

Get these:
https://www.aliexpress.com/item/433MHz-100-Meters-Wireless-Module-Kit-ASK-Transmitter-STX882-ASK-Receiver-SRX882-2Pcs-Copper-Spring-Antenna/32637181317.html?spm=a2g0s.9042311.0.0.9HlYe4

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.


#14

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


#15

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.


#16

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


#17

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);


 
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);

  // 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);
  Serial.println("");

  // TEST... AUTO WIFI RECONNECT
  if (WiFi.status() == 6)
  {
    ESP.reset();
  }

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

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

#18

To update remotely you need to add ESP8266HTTPUpdateServer as well.


#19

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


#20

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.