- Hands-On Edge Analytics with Azure IoT
- Colin Dow
- 1038字
- 2025-04-04 13:01:51
Connecting a sensor to the ESP-12F microcontroller
The following diagram shows a weather predictor I built using the ESP-12F module. Although it is not an edge analytics application per se, it does show how to connect a microcontroller to an LED and the internet. The weather predictor uses barometric pressure values taken for a web service to determine whether there will be good weather coming:
As you can see in the diagram, an RGB LED is connected to the GPIO ports on the ESP-12F. The type of RGB LED used in this circuit has a common anode. Hence, the RGB LED is connected as follows:
- Red cathode to GPIO12
- Common anode to VCC
- Green cathode to GPIO14
- Blue cathode to GPIO16
The following code written in C is for the weather predictor:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <OpenWeatherMap.h>
const char *ow_key = "XXXXXXXXXXXXXXXXXXX";
const char *ssid = "XXXXX";
const char *pass = "XXXXXXXXX";
OWMconditions owCC;
float press_old=0;
float press_new;
bool high_press_flag = false;
bool initialize = false;
#define GREEN_LED 14
#define BLUE_LED 16
#define RED_LED 12
void setup() {
Serial.begin(9600);
delay(10);
Wi-Fi.softAPdisconnect (true);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
}
void currentConditions(void) {
OWM_conditions *ow_cond = new OWM_conditions;
owCC.updateConditions(ow_cond, ow_key, "ca", "Toronto", "metric");
press_new = ow_cond->pressure.toFloat();
if (press_new > 0)
{
if (press_old == 0)
{
press_old = press_new;
}
if (press_new != press_old)
{
initialize = true;
if (press_new > press_old)
{
high_press_flag = true;
}
else
{
high_press_flag = false;
}
press_old = press_new;
}
}
delete ow_cond;
}
void loop() {
if (Wi-Fi.status() != WL_CONNECTED) { //wifi not connected?
Wi-Fi.begin(ssid, pass);
Serial.println("Connecting to Wi-Fi");
delay(2000);
if (Wi-Fi.waitForConnectResult() == WL_CONNECTED) {
Serial.println("Wi-Fi Connected!");
delay(2000);
return;
}
}
if (Wi-Fi.waitForConnectResult() == WL_CONNECTED) {
currentConditions();
if (initialize == true)
{
if (high_press_flag == true)
{
FlashGreenLED(250, 5);
}
else
{
FlashRedLED(100, 10);
}
}
else
{
FlashBlueLED(500, 5);
}
if (press_new > 0)
{
delay(900000);
}
else
{
delay(10000);
}
return;
}
}
void FlashGreenLED(int delayTime, int numOfFlashes) {
int var = 0;
while(var < numOfFlashes ) {
digitalWrite(BLUE_LED, LOW);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
delay(delayTime);
digitalWrite(GREEN_LED, HIGH);
delay(delayTime);
var++;
}
}
void FlashBlueLED(int delayTime, int numOfFlashes) {
int var = 0;
while(var < numOfFlashes ) {
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW);
digitalWrite(BLUE_LED, LOW);
delay(delayTime);
digitalWrite(BLUE_LED, HIGH);
delay(delayTime);
var++;
}
}
void FlashRedLED(int delayTime, int numOfFlashes) {
int var = 0;
while(var < numOfFlashes ) {
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(RED_LED, LOW);
delay(delayTime);
digitalWrite(RED_LED, HIGH);
delay(delayTime);
var++;
}
}
Our code starts off by including the necessary libraries:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <OpenWeatherMap.h>
We then set up the variables used in our code:
const char *ow_key = "XXXXXXXXXXXXXXXXXXX";
const char *ssid = "XXXXX";
const char *pass = "XXXXXXXXX";
OWMconditions owCC;
float press_old=0;
float press_new;
bool high_press_flag = false;
bool initialize = false;
#define GREEN_LED 14
#define BLUE_LED 16
#define RED_LED 12
The *ow_key constant is obtained when we set up an account with Open Weather. The *ssid and *pass constants are the SSID and password for our Wi-Fi router respectively. We use the owCC variable to call the Open Weather Map web service later in the code. press_old and press_new are float values we use to determine whether the barometric pressure is rising or falling. The high_press_flag is set high in the code once it has been determined that the pressure is rising. The initialize flag is used to indicate whether an initial state has been determined (barometric pressure is rising or falling).
We then set the pin numbers of the ESP-12F to the colors of the RGB LED that they are wired to as shown in the preceding diagram.
Our setup() method sets the baud rate (for using the Arduino serial monitor in troubleshooting) to 9600. A delay of 10 milliseconds is set. Our code then turns off the microcontroller's Wi-Fi AP mode (so you won't see it pop up in a list of local Wi-Fi routers) before the GREEN_LED, BLUE_LED, and RED_LED values are set to OUTPUT:
void setup() {
Serial.begin(9600);
delay(10);
Wi-Fi.softAPdisconnect (true);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
}
The currentConditions() method is where all of the magic happens. It is here where we call the Open Weather Map web service using the owCC.updateConditions() method of the OWM_conditions variable, *ow_cond. We acquired access to the OWM_conditions object when we included OpenWeatherMap.h at the top of our code.
In our code, we are using the Open Weather Map web service to pull out barometric pressure for Toronto, Canada. We assign the press_new variable to the barometric pressure reading we acquire from the web service:
void currentConditions(void) {
OWM_conditions *ow_cond = new OWM_conditions;
owCC.updateConditions(ow_cond, ow_key, "ca", "Toronto", "metric");
press_new = ow_cond->pressure.toFloat();
if (press_new > 0)
{
if (press_old == 0)
{
press_old = press_new;
}
if (press_new != press_old)
{
initialize = true;
if (press_new > press_old)
{
high_press_flag = true;
}
else
{
high_press_flag = false;
}
press_old = press_new;
}
}
delete ow_cond;
}
We then perform a series of logic tests to determine whether the barometric pressure is rising or falling. If this is our first reading, we keep the initialize flag at false (by not setting it to true) as we are not yet ready to determine a rising or falling barometric pressure. It may take a few hours before a rising or falling barometric pressure has been determined.
Our loop() method checks the Wi-Fi connection before proceeding. What follows is a series of logic tests to determine which light to flash:
void loop() {
if (Wi-Fi.status() != WL_CONNECTED) { //wifi not connected?
Wi-Fi.begin(ssid, pass);
Serial.println("Connecting to Wi-Fi");
delay(2000);
if (Wi-Fi.waitForConnectResult() == WL_CONNECTED) {
Serial.println("Wi-Fi Connected!");
delay(2000);
return;
}
}
if (Wi-Fi.waitForConnectResult() == WL_CONNECTED) {
currentConditions();
if (initialize == true)
{
if (high_press_flag == true)
{
FlashGreenLED(250, 5);
}
else
{
FlashRedLED(100, 10);
}
}
else
{
FlashBlueLED(500, 5);
}
if (press_new > 0)
{
delay(900000);
}
else
{
delay(10000);
}
return;
}
}
The FlashGreenLED(), FlashBlueLED(), and FlashRedLED() methods are pretty self-explanatory. As you can tell in the logic, we flash the green LED when the barometric pressure is rising, the red LED when the barometric pressure is falling, and the blue LED before it has been determined whether the barometric pressure is rising or falling. Looking at the code to flash the LED (which we will not repeat), we can see that we end the flashing with a high state. This means that the LED will flash for a short time before remaining either green, red, or blue.
Now that we have a basic understanding of how to connect a component, let's take a look at more real-world edge analytics examples.