Room Monitor

From Electronic Sweet Peas
Jump to: navigation, search

Overview

Now available for preorder in our new web shop

This example was created to give you an overview on how a few node.IT boards can be used to monitor and upload data from the environment sensors on the EnvironOne module. This example also makes use of the BatOne module to connect a 2000mAh Li Ion battery that drives the the whole node.IT stack.

The node.IT stack

Hardware

These are the node.IT boards form top to bottom used in this example.

  1. EnvironOne
  2. BatOne
  3. ESP210

Comment: In this example we are using the prototype BatOne board which requires an external connection between GPIO16 and the RST# pin. This connection is used in deep sleep mode to wake the ESP210 up. The production boards of BatOne have a solder jumper on the board that creates this connection.

The following video gives you an idea on how easy it is to build the stack

Libraries

In this example I have used the following node.IT libraries (https://github.com/node-it)

If you want to you can also use the BatOne library to monitor and control the charge cycle

Source Code

And this is the source to go with it.

/*
 * ----------------------------------------------------------------------------
 *            _____ _           _                   _
 *           | ____| | ___  ___| |_ _ __ ___  _ __ (_) ___
 *           |  _| | |/ _ \/ __| __| '__/ _ \| '_ \| |/ __|
 *           | |___| |  __/ (__| |_| | | (_) | | | | | (__
 *           |_____|_|\___|\___|\__|_|  \___/|_| |_|_|\___|
 *            ____                   _   ____
 *           / ___|_      _____  ___| |_|  _ \ ___  __ _ ___
 *           \___ \ \ /\ / / _ \/ _ \ __| |_) / _ \/ _` / __|
 *            ___) \ V  V /  __/  __/ |_|  __/  __/ (_| \__ \
 *           |____/ \_/\_/ \___|\___|\__|_|   \___|\__,_|___/
 *
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <pontus@sweetpeas.se> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return - Pontus Oldberg
 * ----------------------------------------------------------------------------
 */
 
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <BME280_MOD-1022.h>
#include <Opt3001.h>
 
#define USE_USCI_B1 
 
#define LED_PIN          5
#define LED_ON           1
#define LED_OFF          0
#define setLed(x)        digitalWrite(LED_PIN,x)
 
#define ONE_SEC          1000000
 
#define SDA_PIN          2
#define SCL_PIN          14
 
char ssid[] = "your ssid";  //  your network SSID (name)
char pass[] = "your pass phrase";       // your network password
 
const char* host = "api.thingspeak.com";
const int httpPort = 80;
String writeAPIKey = "your write api key";    // Write API Key for a ThingSpeak Channel
 
// Default is that we sleep for this amount of time before waking up again.
uint32_t  deepSleepTime = ONE_SEC * 60 * 5;
 
// The OPT3001 instance
Opt3001 opt3001;
 
// The client instance
WiFiClient client;
 
void setup() {
  float temperature;
  float humidity;
  float pressure;
  uint32_t light;
 
  pinMode(LED_PIN, OUTPUT);
  setLed(LED_OFF);
 
  Wire.begin(SDA_PIN, SCL_PIN);
 
  // Debug out put on the USB Serial port
  Serial.begin(115200);
 
  Serial.println();
  Serial.println();
 
  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
 
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  // Initialize optical sensor
  opt3001.begin();
 
  // Make sure we found the BME280
  Serial.print("ChipID = 0x");
  Serial.println(BME280.readChipId(), HEX);
 
  // And do the same for the OPT3001  
  Serial.print("Manufacturer ID = 0x"); 
  Serial.println(opt3001.readManufacturerId(), HEX);
 
  // need to read the NVM compensation parameters
  BME280.readCompensationParams();
 
  // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything
  BME280.writeOversamplingPressure(os1x);  // 1x over sampling (ie, just one sample)
  BME280.writeOversamplingTemperature(os1x);
  BME280.writeOversamplingHumidity(os1x);
 
  // example of a forced sample.  After taking the measurement the chip goes back to sleep
  BME280.writeMode(smForced);
  delay(50);
  while (BME280.isMeasuring()) {
    Serial.println("Measuring...");
    delay(50);
  }
  Serial.println("Done!");
 
  // read out the data - must do this before calling the getxxxxx routines
  BME280.readMeasurements();
 
  temperature = BME280.getTemperature();
  Serial.print("Temp = ");
  Serial.println(temperature);  // must get temp first
 
  humidity = BME280.getHumidity();
  Serial.print("Humidity = ");
  Serial.println(humidity);
 
  pressure = BME280.getPressure();
  Serial.print("Pressure = ");
  Serial.println(pressure);
 
  // Read the result from the opt3001
  light = opt3001.readResult();
  Serial.print("LUX = ");
  Serial.println(light);
 
  // Convert readings to string and add it to the thingspeak argument
  String startTime = String(micros(), DEC);
  String strTemperature = String(temperature, 2);
  String strHumidity = String(humidity, 2);
  String strPressure = String(pressure, 2);
  String strLight = String(light);
  String tsData = "field1=" + startTime + "&field2=" + strTemperature +
                  "&field3=" + strHumidity + "&field4=" + strPressure + 
                  "&field5=" + strLight;
 
  if (client.connect(host, httpPort))
  {
    Serial.println(F("WiFi client connected to ThingSpeak !"));
 
    // Indicate that we are doing the transfer
    setLed(LED_ON); 
    client.println(F("POST /update HTTP/1.1"));
    client.println(F("Host: api.thingspeak.com"));
    client.println(F("Connection: close"));
    client.println("X-THINGSPEAKAPIKEY: " + writeAPIKey);
    client.println(F("Content-Type: application/x-www-form-urlencoded"));
    client.print("Content-Length: ");
    client.println(tsData.length());
    client.println();
    client.println(tsData);   
    client.stop();
 
    setLed(LED_OFF);
 
    Serial.println(F("Sent Data to ThingSpeak..."));
  }
  else
  {
    Serial.println(F("Connection to ThingSpeak Failed, trying again in 15 seconds."));
    deepSleepTime = ONE_SEC * 15;
  }
 
  setLed(LED_OFF);
  delay(100);
 
  // Now we need to adjust the deepSleepTime with the time that it has taken to
  // connect to the WLAN and the time it took to send all data and to actually get
  // to this point.
  uint32_t cTime = micros();
  // Need to make sure that the time elapsed is not bigger than the repeat time
  if (cTime < deepSleepTime) {
    deepSleepTime -= cTime;
  } else {
    deepSleepTime = ONE_SEC * 10;
  }
  Serial.print(F("Going down for deep sleep to wake again in "));
  Serial.print(deepSleepTime);
  Serial.println(F(" microseconds."));
 
  // Now sleep for a while
  ESP.deepSleep(deepSleepTime, WAKE_RF_DEFAULT);
}
 
void loop() {
  // We will never get here.....
}