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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Arduino UNO R3 or R4 |
|
- |
|
- |
Wiring
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.
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.
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”.
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”.
Code
Open the
Lesson_48_Iot_Weather_Monitor.inofile under the path ofuniversal-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.
You need to enter the
mySSIDandmyPWDof the WiFi you are using.String mySSID = "your_ssid"; // WiFi SSID String myPWD = "your_password"; // WiFi Password
You also need to modify the
myAPIwith your ThingSpeak Channel API key.String myAPI = "xxxxxxxxxxxx"; // API Key
Here you can find your unique API KEY that you must keep private.
After selecting the correct board and port, click the Upload button.
Open the Serial monitor(set baudrate to 9600) and wait for a prompt such as a successful connection to appear.
Code Analysis
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
bleSerialobject for Bluetooth communication.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.
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 asOUTPUT. We also start serial communication for both the Bluetooth module and the default serial (connected to the computer) at a baud rate of 9600.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 thetoggleLights()function.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.