Types of analog sensors:
Analog sensors are components that vary their resistivity depending on a real variable, meaning the sensor pin voltage will flow between 0V and Vcc when it is exposed to higher or lower temperatures, light, humidity, etc. This analogic signal will be converted by the microcontroller in a numeric value and stored in a register. Some analog sensors are the LDR (vary according to the luminosity they receive), the thermistor ky-013, and some water or humidity level sensor.
The variable resistors must be wired up with a pull-up or pull-down resistor, with which we could measure the lecture.
There are also linear measurement devices, which allow balanced analog reads more precise than the ones with the variable resistors, there are the thermistor LMP35, the hall-effect amperemeter ACS712, or the new Texas Instrument LMP91200 pH device.
All those sensors can be read with the ESP8266 with the same code, and a similar wire up.
Analog read with ESP8266:
Be carefull! not every ESP-X format can read analog values. In fact, the ESP8266 chip supports it, but depending on the board layout, the analog pin is not connected to any terminal pin. The formats 07, 08, 12 and 12E, are the only one that have the necessary analog pin. We are going to take as an example the reading of a LDR photo-sensor:
- So the first step is to wire a pull-down resistor, so if the LDR is disconnected we have an expected output. Take a look at this documentation to learn more. It has only a resistor from GND to one sensor pin (the pin we connect to the analog input).
-
We can read then the analog pin by using the well-known arduino functions (
analogRead
). -
So now we can build a simple sketch to allow reading analog values from the internet by making a simple thinger sketch like the following:
#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>
#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
// LDR resource
thing["LUX"] >> outputValue(analogRead(A0));
}
void loop() {
thing.handle();
}
- Enjoy thinger interface! In the device API, you can see the raw data that is being send by your device.
or you can create a real time dashboard with a time series chart, like this that is showing the luminosity of last ten days: