Just another update. I got a configuration page on the devices working, so they can now connect to a WPA/WPA2 protected SSID. Before getting that working, for whatever reason, I could not get them to connect to anything that wasn’t wide open.
While it would be really nice if the code could be static, and everything from what sensors (and how many) are connected (and in the case of DS18B20’s, their addresses) to the Node name, EmonCMS server and your API key could be set up through a web interface, while I’m sure it’s possible, I’d say I’ve pretty much hit the limit of my (insignificant) abilities when it comes to programming/coding. Truthfully, if even just the node name could be configured through a web interface, that would be great, but really, this is quite workable for me - should I choose to lock my IoT network down with WPA/WPA2, which to be honest, isn’t that critical considering how I have it locked down, I can do so. And if anyone else wants to use the code and can’t lock their network down the way mine is with guest isolation and no internet access, that’s a win for them too.
So unless I have some kind of epiphany and suddenly can figure out those few remaining things that would be nice, this is probably my last and best revision to the code:
//Sketch using ESP8266 ESP-01 modules, reading from DHT22 & DS18B20, posting up to emoncms using the ESP8266 wifi module.
// This file is part of OpenEnergyMonitor project.
#include <ESP8266WiFi.h>
#include "DHT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#define DHTPIN 2 // Pin to which DHT sensor is connected
#define ONE_WIRE_BUS 0 // Pin to which one or more DS18B20 temperature sensors are attached
#define ONE_WIRE_MAX_DEV 15 // The maximum number of devices
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SENSOR_RESOLUTION 11 // How many bits to use for temperature values: 9, 10, 11 or 12
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
DeviceAddress Sensor1 = {0x28, 0x17, 0x6F, 0xD4, 0x05, 0x00, 0x00, 0x84};
//DeviceAddress Sensor2 = {0x28, 0xFF, 0xC2, 0x72, 0x60, 0x14, 0x02, 0x88};
const char* host = "YourServerIPAddress"; //Enter the EmonCMS Server address here
const char* apikey = "****YourAPIKey****"; //Enter your api key here
const char* NodeName = "ESP8266Testa"; // Enter your node name here
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
void setup() {
Serial.begin(9600);//Baud rate
delay(10);
WiFi.mode(WIFI_STA); // Force to station mode because if device was switched off while in access point mode it will start up next time in access point mode.
// Uncomment for testing wifi manager - If used, flash, boot, comment out and flash again
//wifiManager.resetSettings();
//or use this for auto generated name ESP + ChipID
wifiManager.autoConnect();
//Manual Wifi
//WiFi.begin(WIFI_SSID, WIFI_PWD);
Serial.println();
Serial.println();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
DS18B20.begin();
dht.begin();
}
int value = 0;
void loop() {
delay(30000);
++value;
Serial.print("Requesting data...");
// errorCode = DHT.readData();
// switch(errorCode)
{
float DSTemp1;
//float DSTemp2;
float DHT1h;
float DHT1tC;
float DHT1tF;
DS18B20.requestTemperatures();
DSTemp1 = DS18B20.getTempF(Sensor1);
//DSTemp2 = DS18B20.getTempF(Sensor2);
DHT1h = dht.readHumidity(); // Read Humidity
DHT1tC = dht.readTemperature(); // Read temperature as Celsius (the default)
DHT1tF = dht.readTemperature(true); // Read temperature as Farenheit
// Check if any reads failed and exit early (to try again).
if (isnan(DHT1h) || isnan(DHT1tC) || isnan(DHT1tF)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
Serial.print("\n");
Serial.print("Humidity: ");
Serial.print(DHT1h);
Serial.print(" %\n");
Serial.print("Temperature: ");
Serial.print(DHT1tF);
Serial.print(" *F\n ");
Serial.print("DS1 Temperature: ");
Serial.print(DSTemp1),4;
Serial.print(" *F\n ");
//Serial.print("DS2 Temperature: ");
//Serial.print(DSTemp2),4;
//Serial.print(" *F\n ");
// We now create a URI for the request
String url = "/input/post.json?node="+String(NodeName)+"&fulljson={\"Humidity\":"+String(DHT1h)+",\"Temperature\":"+String(DHT1tF)+",\"DS1\":"+String(DSTemp1)+"}&apikey="+String(apikey);
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
}
As previously mentioned, if you want to add this to the GitHub in place of the existing sketch in the ‘Guide’ folder, feel free to do so (and if I may be so bold, it’s probably a good idea considering how broken the sketch that’s in there is).