Thursday, April 13, 2017

Temperature and hygrometer with FlashAir + Arduino + Raspberry Pi (1) : Read values from sensor

Table of contents.
  1. Read values from sensor
  2. Read from SD card with iSDIO
  3. Send values from arduino to Raspbery Pi
  4. Visualize on Raspberry Pi

I made a thermo-hygrometer sensing with arduino.
It sends values via Toshiba FlashAir to Raspberry Pi, and visualizes on a web page.

[ Points ]
  • Visualize sensor data using FlashAir, arduino and Raspberry Pi
  • Measure temperature and humidity using DHT22 sensor.
  • Convert from float to number text on arduino.
  • Support iSDIO WiFi connection and SD card read/write at the same time.
  • Show a bubble chart with Chart.js.


Final image.


The data of multiple rooms are displayed, but the temperature and humidity meter is one.
I only want to compare the rooms, so I measured the data one by one and overlapped the data.

The hardware of the thermo-hygrometer.



Basic Design

Take temperature and humidity from the temperature and humidity sensor with Arduino Pro Mini.
The retrieved data is sent to Raspberry Pi using FlashAir's iSDIO function.
Finally, draw a chart on Raspberry Pi's web page and display it on the iPad.


It is also possible to connect directly to FlashAir from the iPad and omit Raspberry Pi.
But, in that case, Flash Air will always be connected to WiFi and power consumption will be intensified.

With this configuration, FlashAir starts WiFi only when sending data, so arduino works with battery-powered.
If it is battery operated, it can measure even in a place without a receptacle such as in a closet.

Communication sequence is below.


Materials

Temperature / humidity sensor - DHT22

Cheap temperature and humidity sensor.
There are two kinds of sensors available, bare sensor and modularized one.
As the price does not change so much, I recommend a module version.
The module is more manageable with a built-in pull-up resistor.


Arduino Pro Mini


There are several Arduino Pro Mini in every home!
It's so reasonable. It' $5.

When Pro Mini receives 5V power supply via RAW pin, 3.3V comes out from ACC.
Most sensor and SD card are 3.3V, so it is convenient to be connected directly.

FlashAir (W-02 or later)

これもどこのご家庭にもある FlashAir です。

iSDIO is supported by Flash Air W-02 and later.
Capacity does not matter.

SD card DIP conversion module

It is used for mounting SD card on arduino.

Button battery holder and CR 2032 x 2 pieces

This battery holder can be hold two CR2032 battery.

Although a nominal voltage of CR2032 is 3V, when the current value is large, the voltage drops to around 2.5V.

So, two CR2032 makes 5V, and arduino pro mini works by battery.

However, since this time we have not properly implemented power saving, the battery will be consumed quite quickly.

If it moves for a long time, it is more economical to connect the USB smart battery via the serial module.

Circuit diagram

Just connect DHT22 and Flash Air to arduino pro mini.



Measure temperature and humidity with DHT22

It's so easy using Adafruit library.
#include 

#define DHT_PIN 3
dht DHT;

void setup() { }

void loop() {
  int chk = DHT.read22(DHT_PIN);
  if ( chk != 0 ){
    Serial.println(F("Fail to read humidity."));
    return;
  }
  char humid[8];
  char temp[8];
  char param[32];
  sprintf(param,"H=%s&T=%s",
    ID,dtostrf(DHT.humidity,5,2,humid),dtostrf(DHT.temperature,5,2,temp));
  Serial.print("PARAM:");
  Serial.println(param);
}

This code concatenates values to send them as HTTP parameters.
Although normal C language can convert floating numbers to text by sprintf, sprintf of arduino does not support it.

So, I used dtostrf.
The specification of dtostrf is like this.
1st arg.: Source float number
2nd arg.: Number of characters including decimal point.
3rd arg.: Number of decimal places.
4th arg.: Destination char array.
Return value: Pointer to 4th argument.


Next time, I introduce the way to use FlashAir as SD card and WiFi client.

No comments:

Post a Comment