User Tools

Site Tools


proiecte:environment-monitoring

This is an old revision of the document!


Environment monitoring using Sparrow and ESP8266

Cocioaba Cristian Gabriel - SRIC

Introduction

To make use of the low power of the IEEE 802.15.4 and the integration of the Wi-Fi, a hybrid network can be used, a heterogeneous WSN composed by both of this types of wireless networks but in which each node can transmit messages to any other node, regardless of its position and network type. In this way, all IEEE 802.15.4 nodes have access to the Internet.

For 802.15.4 network, we used Sparrow v4 board and for Wi-Fi, the ESP8266 module.

Architecture

Hardware connection

To connect the two types of networks, there must be a direct link between the Coordinator from the Wi-Fi network and the Coordinator from the IEEE 802.15.4. This link can be implemented by a serial interface through UART.

Sparrow ESP8266
RX Pin 20 - MOSI GPIO14 TX
TX Pin 12 - MISO GPIO12 RX

Routing

For the purpose of monitoring environment parameters, it is only needed that each node to send data from its own sensors to the Internet or a Sink node (data aggregation node). In this way, sensor data travels in only one direction. For future need of configuring, or sending a specific message to the whole network or only one node, there should be very little to modify in implementation, the process just reversing the transmission direction and implement the handle action to that message.

WSN Protocol for ESP8266 is a lightweight protocol for creating a WSN over Wi-Fi with ESP8266 chips. It is a similar model to the CoAP protocol, but is based on IPv4 and UDP stack. The Coordinator is called a “Head node”, which can also act as a Router and the Device is called simply “Node”. It supports network discovery and auto-arranging nodes into star, tree and mesh networks. It extends the limitations of the Wi-Fi range by creating new access points for other nodes to connect and auto-redirect the messages from and into the subnets.

Serial data

Sensor data

struct sensor_data_t {
  uint32_t node_id;
  sensor_type_t sensor_type;
  sensor_value_t sensor_value;
};

Sensor value struct

union {
  sensor_temperature_t temperature;
  sensor_light_t light;
  sensor_battery_t battery;
} sensor_value_t;

Sensor types

enum sensor_type_t {
  SENSOR_TEMPERATURE = 0x0,
  SENSOR_LIGHT,
  SENSOR_BATTERY
};

Gathering data

Temperature and humidity

struct sensor_temperature_t {
  float temperature;
  float humidity;
};

Light

struct sensor_light_t {
  uint16_t UV;
  uint16_t visible;
  uint16_t IR;
  uint16_t proximity;
};

Battery

typedef float sensor_battery_t;

Usage

Software serial example on Sparrow

#include <SoftwareSerial.h>
#include <SHT2x.h>
#include "sensor_interface.h"
 
SoftwareSerial espSerial(MOSI, MISO); // RX, TX
 
void serial_write_packet(const sensor_packet_t* packet)
{
    espSerial.write((uint8_t*)packet, sizeof(sensor_packet_t));
}
 
sensor_temperature_t sensor_temperature_read()
{
	sensor_temperature_t sensorValue;
	sensorValue.humidity = SHT2x.GetHumidity();
	sensorValue.temperature = SHT2x.GetTemperature();
 
	return sensorValue;
}
 
void setup(){
    espSerial.begin(ESP_SERIAL_BAUND); // default 115200
}
 
void loop(){
    sensor_temperature_t temperature = sensor_temperature_read();
    packet.sensor_type = SENSOR_TEMPERATURE;
    packet.sensor_value.temperature = temperature;
    serial_write_packet(&packet);
    delay(100);
}

ESP8266 solar energy harvesting

For creating a Wireless Sensor Network, all the nodes need to run for a long period of time. And, because they are usually deployed in a remote or hard to access area, they are required to be powered by batteries. Therefore, rises the need to efficiently use that limited battery power and to also recharge it over time, so that the network to be “always-on”.

Nodes power consumption

In a Wireless Sensor Network, nodes are typically equipped with power-constrained batteries, which are often difficult and expensive to be replaced once the nodes are deployed. Therefore, it is a critical consideration on reducing the power consumption in the network design. Energy in WSN nodes is consumed by the CPU, by sensors or actuators, and by radio, with the last consuming the most

Although both protocols operate at 2.4 GHz frequency, 802.15.4 is low-speed and low-rate and ATmega128RFA1 has Ultra Low Power consumption as we can see below.

ATmega128RFA1 power consumption

ESP8266 power consumption when radio is on

In contrast, ESP8266 has more processing power and together with the power required by the bandwidth and data rate of Wi-Fi, it utilizes almost 10 times more energy, as we can see below.

The ESP8266 microcontroller provides 3 configurable sleep modes, which can be configured as required by each application: • Modem-sleep • Light-sleep • Deep-sleep

ESP8266 measured power consumption

The first step for making a sustainable solution for a WSN was to measure the power consumption of the ESP8266 chip and see if the measurements align with the technical specifications.

For initial measurements, we load the ESP microcontroller with a program that starts in modem-sleep for 10 seconds, in which it turns on and off the build in LED, then connects to the Wi-Fi network and sends a HTTP POST request to a server with the value of its VCC voltage and at the end it enters deep-sleep for another 20 seconds.

As we can see in the figure below, the Wi-Fi radio switches on as soon as the ESP wakes up. The boot loader runs for about 0.35 seconds, after which it is deactivated when the “setup” function is run in the program and we force it to be off, thus entering modem-sleep. The next 3 to 10 seconds (in some of our cases) are spent connecting to the Wi-Fi access point and getting an IP. This may vary, depending on the router and DHCP traffic, taking about 3 seconds sometimes.

Next comes about 1.2 seconds of idle time, which, after some research, it is associated with the fact that Espressif SDK persists the connection information to flash memory.

The final power peak is the HTTP request, which varies between 90mA to 200mA.

The average current consumption for the ESP when running this test program in different states can be seen in the table below:

There were also peeks of power randomly occurring during data transmission, which varies from 100mA to even 300 mA. Either there was an error at the measurements, or the ESP needed to send a stronger signal. The maximum length of these peaks was around 0.05 seconds.

Estimating battery life for an ESP node

Optimizing power consumption

Keeping data after Deep-Sleep

When the ESP chip shuts down the CPU and after it wakes up, it runs the program from the userinit. This means that all the values stored in memory in a processing cycle are not available anymore, which is not very good for sensor value comparation and for message queues. For resolving this problem, we used the chip flash memory for storing important information before going to sleep. It can store files as text or binary data using the Arduino FS library implementation of ESP SDK.

void seput() {
  Serial.begin(115200);
  while (!Serial) continue;
  if (!SPIFFS.begin())
  {
    Serial.println("Failed to mount file system");
    return;
  }
  //...
}
 
bool loadDataRaw(const char* filename, Data& data){
  File dataFile = SPIFFS.open(filename, "r");
  if (!dataFile){
    Serial.println("Failed to open data file");
    return false;
  }
 
  size_t size = dataFile.size();
  if (size != sizeof(Data)) {
    Serial.print("Data file size is too large. Found "); Serial.print(size); Serial.print(" expected "); Serial.println(sizeof(Data));
 
    SPIFFS.remove(filename);
    Serial.println("Bad file was removed.");
    return false;
  }
 
  char* buffer = new char[size];
  dataFile.readBytes(buffer, size);
 
  memcpy(&data, buffer, size);
 
  dataFile.close();
 
  return true;
}
 
bool saveDataRaw(const char* filename, Data& data) {
  File dataFile = SPIFFS.open(filename, "w+");
  if (!dataFile) {
    Serial.println("Failed to open data file for writing");
    return false;
  }
 
  size_t size = sizeof(Data);
  uint8_t* buffer = new uint8_t[size];
  memcpy(buffer, &data, size);
 
  const uint8_t* bufferToWrite = buffer;
  size_t sizeWritten = dataFile.write(bufferToWrite, size);
 
  if(sizeWritten != size) {
    Serial.println("Failed to save data file. Not enough bytes were written.");
    if(SPIFFS.exists(filename)) {
      SPIFFS.remove(filename);
    }
    return false;
  }
 
  // Close the file (File's destructor doesn't close the file)
  dataFile.close();
 
  return true;
}

Solar energy harvesting

Results

Resources

proiecte/environment-monitoring.1528376782.txt.gz · Last modified: 2018/06/07 16:06 by cristian.cocioaba