There are plenty of tutroials out there on how to do this. The sort of unique thing for mine is I wanted the data put in to an ElasticSearch instance I run. I'm all about sensors and storing info, and ElasticSearch has been great for that. So since I already have an ELK setup running for things like my Geiger Counter, Nest Thermostat, and Weather Underground data, I figured having my own little temp/humidity modules adding data would be a great spot.
The problem with most tutorials out there is that they focused on creating a webpage you go to, or posting data to some website that offers IOT services. So my version 1 of this included just making the ESP have a basic webpage with the available temperature and humidity information on it. It would just constantly loop checking the sensor and providing the connection. That wasn't ideal as that involves me to visit the webpage either manually or with a script. So I used a script for a while and it worked fine, but I still wanted them to be standalone objects where I didn't need to have static IP's for them or modify the script everytime I added or removed one.
So version 2 I sat down and finally figured out how to have the ESP module post data to a Logstash instance and get it imported in to Elasticsearch. Turns out it was pretty easy to do.
First the Hardware. This is the working version and it's compact. Not the prettiest but it works fine. Here's what's inside it and some rough prices.
- ESP01 module - $2
- DHT22 sensor module - $3
- TP4056 LiPo Charger - $1.20
- 100 mAh LiPo battery - $4
- Micro USB prt on the TP4056 and a Male USB port added for power as well $0.20
- On/Off Switch $0.02
- A Diode to drop the voltage just a little to the ESP $0.01
So about $11 per module. Cheaper if you just use a 3.3 volt regulator on a USB port to power it. But I wanted a battery to make it portable (put it where you want to measure something like a car or garage).
You can plug it in to Micro USB....
Or plug it in to one of the readily available USB plugs and let it be.
Here's a basic chart of the data the module POSTs to ELK. The temp is high because it's sitting on my computer tower running in battery only mode right now. I'm trying to see the run time on it, and I'll hopefully update the next sentance once I get a time.
And the data that's being collected each POST and stored.
And here's the code. The few parts you'd want to change are probably the Node name of the module, and your Logstash instance IP/Port.
#include
#include
const char* ssid = "SSID Home";
const char* password = "P@ssword";
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
pinMode(LED_BUILTIN, OUTPUT);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
}
dht.begin();
}
void loop() {
// Read humidity
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
WiFiClient client;
String PostData = "{\"node\": \"LivingRoom\",\"humidity\": ";
PostData += (float)h, 2;
PostData += ",";
PostData += "\"temp_c\": ";
PostData += (float)t, 2;
PostData += ",";
PostData += "\"temp_f\": ";
PostData += (float)f, 2;
PostData += "}";
const uint16_t port = 8080;
const char* host = "192.168.1.18";
if (client.connect(host, port)) {
digitalWrite(LED_BUILTIN, LOW);
client.println("POST /posts HTTP/1.1");
client.println("Host: 192.168.1.18");
client.println("Cache-Control: no-cache");
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
client.stop();
digitalWrite(LED_BUILTIN, HIGH);
}
delay(60000);
}
No comments:
Post a Comment