Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 48: Weather Monitor with ThingSpeak

This project collects temperature and pressure data using an Atmospheric Pressure Sensor. The collected data is then transmitted to the ThingSpeak cloud platform via an ESP8266 module and Wi-Fi network at regular time intervals.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

Breadboard

BUY

ESP8266 Module

-

Temperature, Humidity & Pressure Sensor (BMP280)

-

Wiring

../_images/Lesson_48_Iot_weather_monitor_uno_bb.png

Configure ThingSpeak

ThingSpeak ™ is an IoT analytics platform service that allows you to aggregate, visualize and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by your devices to ThingSpeak. With the ability to execute MATLAB® code in ThingSpeak you can perform online analysis and processing of the data as it comes in. ThingSpeak is often used for prototyping and proof of concept IoT systems that require analytics.

../_images/signup_tsp_ml.png

1) Creating ThingSpeak Account

The first thing you need to do is to create an account with ThingSpeak. Since the collaboration with MATLAB, you can use your MathWorks credentials to login to ThingSpeak .

If you do not have one, you need to create an account with MathWorks and login to ThingSpeak Application.

../_images/05-thingspeak_signup_shadow.png

2) Creating the channel

After logging in, create a new channel to store the data by going to “Channels” > “My Channels” and clicking on “New Channel”.

../_images/05-thingspeak_channel_1_shadow.png

For this project, we need to create a channel called “Weather Monitor” with two fields: Field 1 for “Temperature” and Field 2 for “Atmospheric Pressure”.

../_images/05-thingspeak_channel_2_shadow.png

Code

  1. Open the Lesson_48_Iot_Weather_Monitor.ino file under the path of universal-maker-sensor-kit\arduino_uno\Lesson_48_Iot_Weather_Monitor, or copy this code into Arduino IDE.

    Note

    To install the library, use the Arduino Library Manager and search for “Adafruit BMP280” and install it.

  2. You need to enter the mySSID and myPWD of the WiFi you are using.

    String mySSID = "your_ssid";     // WiFi SSID
    String myPWD = "your_password";  // WiFi Password
    
  3. You also need to modify the myAPI with your ThingSpeak Channel API key.

    String myAPI = "xxxxxxxxxxxx";  // API Key
    
    ../_images/05-thingspeak_api_shadow.png

    Here you can find your unique API KEY that you must keep private.

  4. After selecting the correct board and port, click the Upload button.

  5. Open the Serial monitor(set baudrate to 9600) and wait for a prompt such as a successful connection to appear.

    ../_images/05-ready_1_shadow.png ../_images/05-ready_2_shadow.png

Code Analysis

  1. Initialization and Bluetooth setup

    // Set up Bluetooth module communication
    #include <SoftwareSerial.h>
    const int bluetoothTx = 3;
    const int bluetoothRx = 4;
    SoftwareSerial bleSerial(bluetoothTx, bluetoothRx);
    

    We begin by including the SoftwareSerial library to help us with Bluetooth communication. The Bluetooth module’s TX and RX pins are then defined and associated with pins 3 and 4 on the Arduino. Finally, we initialize the bleSerial object for Bluetooth communication.

  2. LED Pin Definitions

    // Pin numbers for each LED
    const int rledPin = 10;  //red
    const int yledPin = 11;  //yellow
    const int gledPin = 12;  //green
    

    Here, we’re defining which Arduino pins our LEDs are connected to. The red LED is on pin 10, yellow on 11, and green on 12.

  3. setup() Function

    void setup() {
       pinMode(rledPin, OUTPUT);
       pinMode(yledPin, OUTPUT);
       pinMode(gledPin, OUTPUT);
    
       Serial.begin(9600);
       bleSerial.begin(9600);
    }
    

    In the setup() function, we set the LED pins as OUTPUT. We also start serial communication for both the Bluetooth module and the default serial (connected to the computer) at a baud rate of 9600.

  4. Main loop() for Bluetooth Communication

    void loop() {
       while (bleSerial.available() > 0) {
          character = bleSerial.read();
          Serial.println(character);
    
          if (character == 'R') {
             toggleLights(rledPin);
          } else if (character == 'Y') {
             toggleLights(yledPin);
          } else if (character == 'G') {
             toggleLights(gledPin);
          }
       }
    }
    

    Inside our main loop(), we continuously check if data is available from the Bluetooth module. If we receive data, we read the character and display it in the serial monitor. Depending on the character received (R, Y, or G), we toggle the respective LED using the toggleLights() function.

  5. Toggle Lights Function

    void toggleLights(int targetLight) {
       digitalWrite(rledPin, LOW);
       digitalWrite(yledPin, LOW);
       digitalWrite(gledPin, LOW);
    
       digitalWrite(targetLight, HIGH);
    }
    

    This function, toggleLights(), turns off all the LEDs first. After ensuring they are all off, it turns on the specified target LED. This ensures that only one LED is on at a time.