Hardware components | ||||||
| × | 1 | ||||
![]() |
| × | 1 | |||
| × | 1 | ||||
Software apps and online services | ||||||
![]() |
| |||||
| ||||||
I have been playing around with Arduino and various sensors for some time now and have made several using the Uno, Pro Mini and some home manufactured boards over the past couple of years. The big problem with them was they were not easy to use to send data to sites like ThingSpeak.
Enter the Geekcreit Diot NodeMcu ESP8266!
The NodeMcu is a development board with an ESP8266 sitting on top, this means it's relatively cheap and easy to develop IoT projects.
The NodeMcu can be programmed using Arduino IDE so if you have any previous experience of Arduino IDE you'll feel at home with the NodeMcu.
You can purchase them from various suppliers on eBay. I prefer to get mine from banggood.com they can take a while to get to the UK but are worth the wait in my opinion. This is a work in progress but you can check out my public ThingSpeak page at https://thingspeak.com/channels/85382
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DHT.h>
#define DHTPIN 2 // SO CONNECT THE DHT11/22 SENSOR TO PIN D4 OF THE NODEMCU
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#define SLEEP_DELAY_IN_SECONDS 60*30 // every 30 mins
/*
Connections to BMP180
====================
Connect SCL to D1
Connect SDA to D2
Connect VDD to 3.3V DC
Connect GROUND to common ground
Connections to DH11
====================
Connect Signal pin to D4
Connect middle pin, usually V+ to 3v3 DC pin on NodeMcu
Connect - pin to GROUND on NodeMcu
*/
//declare some variables
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
char *ssid = "your networks name here"; // your network SSID (name)
char *pass = "your network password here"; // your network password
int status = WL_IDLE_STATUS;
WiFiClient client;
float num = 0;
ESP8266WiFiMulti WiFiMulti;
DHT dht(DHTPIN, DHT11,15);
unsigned long myChannelNumber = your thingspeak channel number; // my thing speak account channel number
const char * myWriteAPIKey = "your thingspeak apikey here";// my thing speak apikey
void setup() {
Serial.begin(115200); // so we can out put to the screen
delay(10);
// We start by connecting to a WiFi network
WiFiMulti.addAP(ssid, pass);
Serial.println();
Serial.println();
Serial.print("Waiting for WiFi to connect...");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
// give some feed back that we are connected to router
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
// connect to thingspeak.com
if (ThingSpeak.begin(client)) {
//feed back that we have connected to thingspeak
Serial.println("connected to ThingSpeak");
dht.begin();
if(!bmp.begin())
{
/* There was a problem detecting the BMP085 ... check your connections */
Serial.print("Ooops, no BMP180 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on the BMP180 sensor */
displaySensorDetails();
}
//or we have not!
else Serial.println("not connected to ThingSpeak");
}
// set up complete
void loop() {
float pressure = fetchPressure(); /*Get the current pressure from the BMP085 */
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" mBar");
/* First we get the current temperature from the BMP085 */
float temperature = fetchTemperature();
//bmp.getTemperature(&temperature);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
float humidity = fetchHumidity(); //get humidity from DH11 sensor
Serial.print("Humidity: ");
Serial.println(humidity);
float dewPoint = fetchDewPoint(temperature, humidity); // calculate dew point fromm temp and humidity data
Serial.print("Dew Point: ");
Serial.println(dewPoint);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
ThingSpeak.setField(1,temperature);
ThingSpeak.setField(2,pressure);
ThingSpeak.setField(3,humidity);
ThingSpeak.setField(4,dewPoint);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
WiFi.mode(WIFI_OFF);
//delay(1000*60*30); // Update ThingSpeak 15 min.
Serial.println("------------------------------------");
Serial.print("Entering deep sleep mode for ");
Serial.print(SLEEP_DELAY_IN_SECONDS);
Serial.println(" seconds...");
Serial.println("------------------------------------");
ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
//ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
}
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
bmp.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" mPa");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" mPa");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" mPa");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
float fetchHumidity()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
} else
{
return h;
}
}
float fetchPressure()
{
/* Get a new sensor event */
sensors_event_t event;
bmp.getEvent(&event);
if (event.pressure)
{
float pressure = event.pressure;
if (isnan(pressure)) // if its not a number then display error
{
Serial.println("Failed to read pressure from BMP180 sensor!");
}
else
return pressure; // return value to calling function
}
else
{
Serial.println("BMP180 Sensor error");
}
}
float fetchTemperature()
{
float temperature;
bmp.getTemperature(&temperature);
if (isnan(temperature)) // if its not a number then display error
{
Serial.println("Failed to read temperature from BMP180 sensor!");
}
else
{
return temperature; // return value to calling function
}
}
float fetchDewPoint(float temperatureC, float humidity)
{
//Calculate dew point
//Specify the constants for water vapor (b) and barometric (c) pressure.
float b = 17.62;
float c = 243.5;
//Calculate the intermediate value 'gamma'
float gamma = log(humidity/100) + b*temperatureC / (c+temperatureC);
//Calculate dew point in Celsius
float dewPoint = c*gamma / (b-gamma);
if (isnan(dewPoint)) // if its not a number then display error
{
Serial.println("Failed to caculate dew point!");
}
else
{
return dewPoint; // return value to calling function
}
}









Comments