ESP Home Sensor (HCDVBD0045)

Post Reply
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

ESP Home Sensor (HCDVBD0045)

Post by admin » Thu May 11, 2023 3:07 pm

Image




ESP8266 Home Sensor module is a wireless ESP2866 (ESP-07) based microcontroller development board which includes a DHT22 temperature and humidity sensor, an LDR light level sensor and a PIR motion detector.

Its array of sensors makes it great for monitoring the environmental conditions and occupancy of a room wirelessly. Being ESP8266 based it is compatible with the Arduino IDE (via board manager) and we have provided a simple library with example sketches to interface to the on-board sensors making developing your own sketches as straightforward as possible.

Alternatively the module is compatible with existing Arduino/ESP/MQTT libraries and also off-the-shelf firmware such as Tasmota or ESPHome allowing it to be integrated with home automation software like Home Assistant.



Features:

Image



Specifications:

Product code:								HCDVBD0045
Supply voltage:								5V (or 3.3V via reg bypass mod - see below) via + & - solder pads or 5V & GND serial header pins
Current consumption (deep sleep):				2mA (inc module LED)
Current consumption (modem sleep):			21.5mA
Current consumption (connected to WiFi network):	215mA peek / ~72mA idle
Interfaces:								+ & - power solder pads, serial programming interface (5V tolerant), Program mode button
ESP8266 type: 								ESP-07 
ESP8266 clock: 								80MHz
ESP8266 Flash: 								8Mbit
ESP8266 Transmit power: 						802.11b: 16±2 dBm (11Mbps)
										802.11g: 14±2 dBm (54Mbps)
										802.11n: 13±2 dBm (HT20, MCS7)
Temperature sensor range:					-40 to 80oC
Temperature sensor resolution:					0.1oC
Humidity sensor range:						0 to 10 %RH
Humidity sensor resolution:					0.1 %RH
LDR sensor range:							0V = 100%, 1V = Dark
PIR Trigger on time:							~4 seconds
PIR Sensor angle:							100 degree cone
Sensor range:								~3 to 5 metres
Dimensions (LxWxH): 						68.8mm x 22.4mm x 18mm



Dimensions:

Image




Sensor pinouts:

Image



Arduino IDE setup guide:

The module is compatible with the Arduino IDE via the board manager feature. The latest version of the Arduino IDE can be downloaded from the official Arduino website here:

https://www.arduino.cc/en/software

Once installed you will need to add board support for ESP8266 devices. To do this just follow steps 1 & 2 in our blog post here:

https://blog.hobbycomponents.com/?p=937



Arduino IDE board settings:

Board: Generic ESP8266 Module
Port: The COM port of your USB interface module.
Crystal Frequency: 26MHz
Flash Frequency: 40MHz
Flash mode: DIO
Reset Method: dtr
Upload Speed: 115200
Flash Size: 1MB (FS64KB OTA:~470KB)
CPU Frequency: 80MHz

You can leave any additional settings to their defaults.




Programming the ESP Home Sensor

To interface this module to your PC you will need an additional USB to serial interface adapter. The serial interface is compatible with both 3.3V and 5V TTL adapters:

https://hobbycomponents.com/usb-interfa ... al-adaptor
https://hobbycomponents.com/usb-interfa ... rt-adapter
https://hobbycomponents.com/usb-interfa ... le-adaptor


Image

Connect the USB to serial adapter to the module's serial interface as shown above.

To put the module into programming mode hold down the program button (see image above) whilst plugging the USB adapter into your computer.

After uploading your sketch the module can be put back into run mode by removing and re-inserting the USB adapter or by cycling the module's power.



Example Sketches:

Basic example

Basic sensor example:

This sketch uses the HCHomeSensor library to read the sensors and output their states to the serial monitor
  1. #include "ESPHomeSensor.h"
  2.  
  3. // Create an instance of the ESHomeSensor library
  4. ESPHS ESPHS;
  5.  
  6. void setup()
  7. {
  8.   Serial.begin(115200);
  9.  
  10.   // Initialise the library
  11.   ESPHS.init(EN_PIR_INTERRUPT);
  12. }
  13.  
  14.  
  15. void loop()
  16. {
  17.   // Perform a read of the DHT22 (temp & hum)
  18.   if(ESPHS.DHTRead())
  19.   {
  20.     Serial.print((float)ESPHS.DHTTemp() / 10);
  21.     Serial.print(" : ");
  22.     Serial.println((float)ESPHS.DHTHum() / 10);
  23.   }
  24.  
  25.   // Read the LDR level
  26.   Serial.println(ESPHS.LDRRead());
  27.  
  28.   // Read the current PIR state and how many times it has been triggered
  29.   Serial.println(ESPHS.PIRRead());
  30.   Serial.println(ESPHS.PIRTrigs());
  31.  
  32.   delay(2000);
  33. }

MQTT Example

MQTT sensor example:

This sketch demonstrates how to remotely monitor the sensors via an MQTT broker
  1. /* ESP8266 Home Sensor MQTT Example
  2.  
  3.    The sketch will connect to your WiFi network and send the state
  4.    of the sensors to an MQTT broker. To use this sketch change the
  5.    ssid and password settings to the ones for your WiFi network and
  6.    the MQTT_SERVER to the IP address of your MQTT broker.
  7.    
  8.    This sketch requires the MQTT PubSubClient library to be installed.
  9.    This can be installed via the Arduino IDEs library manager or can be
  10.    downloaded from here:
  11.  
  12.    https://github.com/knolleary/pubsubclient/archive/master.zip
  13.  
  14.    This sketch will also need access to an MQTT server (broker) such as
  15.    mosquitto (https://mosquitto.org/)
  16. */
  17.  
  18. #include <PubSubClient.h>
  19. #include <ESP8266WiFi.h>
  20. #include "ESPHomeSensor.h"
  21.  
  22. // Network settings. EDIT THESE TO MATCH YOUR NETWORK
  23. const char* ssid = "Your WiFi SSID";    // Your router's SSID
  24. const char* password = "Your WiFi password";    // Your router's WiFi password
  25. #define MQTT_SERVER "xxx.xxx.xxx.xxx"    // IP address of your MQTT broker
  26.  
  27. // Function prototypes
  28. void callback(char* topic, byte* payload, unsigned int length);
  29. void reconnect(void);
  30.  
  31. // Create an instance of the WiFiClient, PubSubClient, & ESHomeSensor libraries
  32. WiFiClient wifiClient;
  33. ESPHS ESPHS;
  34. PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
  35.  
  36. // Define some useful variables
  37. unsigned long lastTransmit = millis();
  38. char RxBuffer[10];
  39. boolean pirLastState = 0;
  40.  
  41.  
  42. void setup()
  43. {
  44.   // Start the serial for debugging
  45.   Serial.begin(115200);
  46.   delay(100);
  47.  
  48.   // Initialise the home sensor library
  49.   ESPHS.init(EN_PIR_INTERRUPT);
  50.  
  51.   // Start the WiFi subsystem
  52.   WiFi.begin(ssid, password);
  53.  
  54.   // Connect to the WIFI network and then connect to the MQTT server
  55.   reconnect();
  56.  
  57.   // Wait a bit before starting the main loop
  58.   delay(2000);
  59. }
  60.  
  61. void loop()
  62. {
  63.   // If we lose connection try to reconnect
  64.   if (!client.connected() && WiFi.status() == 3)
  65.     reconnect();
  66.  
  67.   // Keep the MQTT connection alive
  68.   client.loop();
  69.  
  70.   // Check if the PIR has been triggered
  71.   if(ESPHS.PIRRead())
  72.   {
  73.     if(!pirLastState)
  74.     {
  75.       client.publish("HCSens/Trigs", "Triggered!");
  76.       pirLastState = true;
  77.     }
  78.   }else
  79.     pirLastState = false;
  80.  
  81.  
  82.   // Send the sensor values once every 10 seconds
  83.   if(millis() >= (lastTransmit + 10000))
  84.   {
  85.     lastTransmit = millis();
  86.  
  87.     // Send the current temperature and humidity
  88.     if(ESPHS.DHTRead())
  89.     {
  90.       dtostrf(ESPHS.fDHTTemp(), 1, 1, RxBuffer);
  91.       client.publish("HCSens/Temp", RxBuffer);
  92.       dtostrf(ESPHS.fDHTHum(), 1, 1, RxBuffer);
  93.       client.publish("HCSens/Hum", RxBuffer);
  94.     }
  95.    
  96.     // Send the LDR level
  97.     dtostrf(ESPHS.LDRRead(), 1, 0, RxBuffer);
  98.     client.publish("HCSens/LDR", RxBuffer);
  99.  
  100.     // Send the amount of times the PIR has been triggered
  101.     dtostrf(ESPHS.PIRTrigs(), 1, 0, RxBuffer);
  102.     client.publish("HCSens/Trigs", RxBuffer);
  103.   }
  104.  
  105.   // Small delay to allow ESP8266 WiFi functions to run in the background
  106.   delay(10);
  107. }
  108.  
  109.  
  110. // MQTT callback function
  111. void callback(char* topic, byte* payload, unsigned int length)
  112. {
  113.   // Convert the received topic to a string to make it easier to work with
  114.   String topicStr = topic;
  115.  
  116.   // Print out some debugging info
  117.   Serial.print("Topic: ");
  118.   Serial.println(topicStr);
  119. }
  120.  
  121.  
  122. // Function to reconnect to the WiFi network if the connection is lost
  123. void reconnect()
  124. {
  125.   // Check if connection is lost
  126.   if(WiFi.status() != WL_CONNECTED)
  127.   {
  128.     Serial.print("Connecting to ");
  129.     Serial.println(ssid);
  130.  
  131.     // Wait for the connection to be established
  132.     while (WiFi.status() != WL_CONNECTED)
  133.     {
  134.       delay(500);
  135.       Serial.print(".");
  136.     }
  137.  
  138.     // Print out some connection info
  139.     Serial.println();
  140.     Serial.println("WiFi connected to: ");  
  141.     Serial.println(WiFi.localIP());
  142.   }
  143.  
  144.   // If the connection is established then attempt to connect tot he MQTT broker
  145.   if(WiFi.status() == WL_CONNECTED)
  146.   {
  147.  
  148.     // Keep looping until we connect to the broker
  149.     while (!client.connected())
  150.     {
  151.       Serial.print("Connection to the MQTT broker...");
  152.  
  153.       // Generate client name based on MAC address
  154.       String clientName;
  155.       clientName += "esp8266-";
  156.       uint8_t mac[6];
  157.       WiFi.macAddress(mac);
  158.       clientName += macToStr(mac);
  159.  
  160.       // If connected to the broker then subscribe to the /r, /g, /b, /a topics
  161.       if (client.connect((char*) clientName.c_str()))
  162.       {
  163.         Serial.print("MQTT Connected");
  164.  
  165.         // Subscribe to topics here
  166.         // client.subscribe("exampletopic);
  167.       }
  168.  
  169.       // If a connection couldn't be established the print out the abort message
  170.       else{Serial.println("\tFailed."); abort();}
  171.     }
  172.   }
  173. }
  174.  
  175.  
  176. // Get the MAC address
  177. String macToStr(const uint8_t* mac)
  178. {
  179.   String result;
  180.  
  181.   for (int i = 0; i < 6; ++i) {
  182.     result += String(mac[i], 16);
  183.  
  184.     if (i < 5){
  185.       result += ':';
  186.     }
  187.   }
  188.  
  189.   return result;
  190. }
  191.  

Webserver Example

Simple webserver example:

This sketch uses the HCHomeSensor library to read the sensors and display the result to a web page.
  1. /* ESP8266 Home Sensor Web Server Example
  2.  
  3.    To use this sketch change the ssid and password settings to
  4.    the ones for your WiFi network. Once connected to your router
  5.    the server's IP address will be shown in the serial monitor.
  6.    Type this IP address into a browser and a page will display
  7.    the current sensor readings every 5 seconds.
  8. */
  9.  
  10. #include <ESP8266WiFi.h>
  11. #include "ESPHomeSensor.h"
  12.  
  13. // Replace with your network credentials
  14. const char* ssid = "Your WiFi SSID";
  15. const char* password = "Your WiFi password";
  16.  
  17. // Create an instance of the webserver on port 80
  18. WiFiServer server(80);
  19.  
  20. // Create an instance of the ESHomeSensor library
  21. ESPHS ESPHS;
  22.  
  23. // Variables to store temperature and humidity;
  24. String dhtTemp;
  25. String dhtHum;
  26.  
  27.  
  28. void setup()
  29. {
  30.   Serial.begin(115200);
  31.  
  32.   // Initialise the home sensor library
  33.   ESPHS.init(EN_PIR_INTERRUPT);
  34.  
  35.   // Connect to WiFi
  36.   WiFi.begin(ssid, password);
  37.  
  38.   // Wait for connection
  39.   Serial.println();
  40.   Serial.print("Connecting to ");
  41.   Serial.print(ssid);
  42.   while (WiFi.status() != WL_CONNECTED)
  43.   {
  44.     delay(500);
  45.     Serial.print(".");
  46.   }
  47.  
  48.   // Wifi connected, print the IP address
  49.   Serial.println("Connected");
  50.   Serial.print("IP address: ");
  51.   Serial.println(WiFi.localIP());
  52.  
  53.   // Start the webserver
  54.   server.begin();
  55. }
  56.  
  57. void loop()
  58. {
  59.   // Check for a new client connection
  60.   WiFiClient client = server.available();
  61.  
  62.   if (client)
  63.   {
  64.     // Get the current temperature and humidity
  65.     if(ESPHS.DHTRead())
  66.     {
  67.       dhtTemp = String(ESPHS.fDHTTemp(), 1);
  68.       dhtHum = String(ESPHS.fDHTHum(), 1);
  69.     }
  70.  
  71.     // Read the LDR and PIR states
  72.     String ldrVal = String(ESPHS.LDRRead());
  73.     String pirState = String(ESPHS.PIRRead());
  74.     String pirTrigs = String(ESPHS.PIRTrigs());
  75.    
  76.     // Display the HTML web page
  77.     client.println("<!DOCTYPE html><html>");
  78.     client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta http-equiv=\"refresh\" content=\"5\">");
  79.  
  80.     client.println("<body><h1>ESP8266 Home Sensor</h1>");
  81.     client.println("<p>Temp (oC): " + dhtTemp + "</p>");
  82.     client.println("<p>Hum (RH%): " + dhtHum + "</p>");
  83.     client.println("<p>LDR Value: " + ldrVal + "</p>");
  84.     client.println("<p>PIR State: " + pirState + "</p>");
  85.     client.println("<p>PIR Triggers: " + pirTrigs + "</p>");
  86.     client.println("</body></html>");
  87.     client.println();
  88.   }
  89. }



Accurate temperature readings.

When the ESP07 modules WiFi is enabled its PA and LNA consume a relatively large amount of current which in turn causes the ESP07 to generate heat. This will in turn affect the temperature sensed by the DHT22.

To help mitigate any temperature errors it is recommended that you turn off the ESP’s WiFI when not needed. This can be achieved by putting the module into either modem sleep, or deep sleep mode using the command WiFi.mode(WIFI_OFF) or ESP.deepSleep() respectively.




3D Printer Case:

ImageImage

For customers with a 3D printer we have uploaded a custom case to thingyverse which can be printed out on a standard 3D printer. You can find the STL files here:

https://www.thingiverse.com/thing:6027205




FAQ:

I want to use the ESPHome home firmware, what should I select for the device type?

If using with ESPhome use the ESP8266 platform and for board type select ESP07.





Downloads:

ESP Home Sensor Library: viewtopic.php?f=58&t=3059

Schematic: https://hobbycomponents.com/downloads/E ... ematic.pdf





Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.

Post Reply

Return to “Expressive (ESP8266)”