Skip to content
Permalink
Browse files
Enable deep sleep, cleanup
  • Loading branch information
Oliver Bell committed Oct 8, 2018
1 parent c3c1408 commit 39cf7805d38848222e260e83eb2b59823ee927bc
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 20 deletions.
@@ -12,9 +12,9 @@
platform = espressif8266
board = nodemcuv2
framework = arduino
lib_ldf_mode=2
lib_deps =
562
BME280
PubSubClient
ArduinoJson
ArduinoJson
NTPClient
@@ -9,6 +9,8 @@
#include "PubSubClient.h"
#include "Ethernet.h"
#include "ArduinoJson.h"
#include "NTPClient.h"
#include <WiFiUdp.h>

const char *ssid = WIFI_SSID;
const char *password = WIFI_PASSWORD;
@@ -21,16 +23,21 @@ bool sensorAvailable = true;

WiFiClient net;

IPAddress server(192, 168, 0, 114);
WiFiUDP ntpUDP;

IPAddress server(192, 168, 0, 103);
PubSubClient mqClient(server, 1883, net);

BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,

NTPClient ntpClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);


// Initialize the OLED display using Wire library
// This also initialising wire on pins D0 and D1
SSD1306Wire display(0x3c, D3, D5);
bool displayInitialised = false;

/* Display some text on the screen */
void displayText(const String text) {
@@ -85,13 +92,25 @@ void pollSensor() {
mqClient.publish(clientName.c_str(), dataBuffer);
}

void setup() {
Serial.begin(9600);
delay(100);
void generateClientName() {
clientName += "sensor-";
clientName += SENSOR_ID;
}

void initialiseDisplay() {
// Initialising the UI will init the display too.
if (displayInitialised) {
return;
}
display.init();
display.flipScreenVertically();
displayInitialised = true;
}

void ensureWifi() {
if (WiFi.status() == WL_CONNECTED) {
return;
}

// Open wifi connection wifi settings in config
WiFi.begin(ssid, password);
@@ -111,17 +130,18 @@ void setup() {

Serial.println("Wifi connected");
displayText("Wifi connected");
delay(1000);
delay(300);
}

void ensureMqtt() {
if (mqClient.connected()) {
return;
}

// Open a connection to the MQTT server
String m_conn_str = "Connecting to MQTT broker: " + server.toString();
displayText(m_conn_str);


// Create out client name
clientName += "sensor-";
clientName += SENSOR_ID;

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
@@ -134,19 +154,33 @@ void setup() {
}

displayText("Connected MQTT broker");
delay(200);
}

void syncTime() {
displayText("Syncing clock");
ntpClient.begin();

// Display the time for a few seconds
for (int i = 0; i < 5; i++) {
ntpClient.update();
displayText(ntpClient.getFormattedTime());
delay(500);
}
}

void setupSensor() {
displayText("Finding BME280 sensor");
delay(200);
// Initialse a connection to the BME sensor
while(!bme.begin()) {

while (!bme.begin()) {
Serial.println("Could not find BME280 sensor!");
displayText("Could not find BME280 sensor! Trying again");
delay(1000);
}
String sensorMessage;

// Print what our sensor has
switch(bme.chipModel()) {
switch (bme.chipModel()) {
case BME280::ChipModel_BME280:
sensorMessage = "Found BME280 sensor! Success.";
break;
@@ -163,12 +197,53 @@ void setup() {
delay(500);
}

/* Poll the sensor values every 100 milliseconds */
void loop() {
// Process sensor data
/**
* Called on power up, and deepsleep powerup
*
* Setup the display, wifi, sync clock, connect
* to the MQTT broker, make a reading
* and send that reading to the MQTT broker
*/
void setup() {
Serial.begin(9600);
delay(100);

initialiseDisplay();
// The display could be off from the last deep sleep
display.displayOn();

// Only need to generate the client name
// if it is not already in ram
if (clientName.length() == 0) {
generateClientName();
}

ensureWifi();

syncTime();

// Initialse a connection to the BME sensor
setupSensor();

ensureMqtt();

// Get the sensor data and
// send it to the broker
if (sensorAvailable) {
pollSensor();
}

delay(100);
delay(1000);

displayText("Sleeping");
delay(500);

// Turn off the display before deep sleep
display.displayOff();
// 60 seconds
ESP.deepSleep(60e6);
}

// Loop as unused as we use the setup as a loop
// after deep sleep
void loop() {}
@@ -0,0 +1,79 @@
SD card read/write

This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <Wire.h>
#include <SPI.h>
#include <SD.h>

File myFile;

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}


Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}

void loop()
{
// nothing happens after setup
}

0 comments on commit 39cf780

Please sign in to comment.