Introduction
Purpose
The aim of this project is to use PMS7003 sensor to measure air quality and transmit data to ThingSpeak IoT cloud platform via WiFi.
This project is based on our previous project named "ESP8266-based air quality monitoring system using PMS7003 sensor". It adds WiFi functionality to send measured data to the IoT cloud. Especially, ThingSpeak offers a free account, which is good enough for prototyping IoT device easily.
Features
This project is based on our previous project named "ESP8266-based air quality monitoring system using PMS7003 sensor". It adds WiFi functionality to send measured data to the IoT cloud. Especially, ThingSpeak offers a free account, which is good enough for prototyping IoT device easily.
Prerequisites
- ESP8266 package for Arduino IDE
- ThingSpeak free account
Hardware
-Wemos D1 mini : US$1.77 on Aliexpress
-PMS7003 air quality sensor : US$16.80 on Aliexpress
Step 1. Setup hardware
PMS7003 sensor comes with a small breakout board, which has TX, RX, VCC, GND pins. Connect TX of PMS7003 to D5 of Wemos, RX to D6, VCC to 5V, and GND to G. Finally, connect micro usb to Wemos for uploading firmware, and check serial monitor and serial plotter in Android IDE to make sure the sensor works correctly.
Step 2. Create ThingSpeak free account and channel
In this project, we will create a channel with 3 fields. Each field stores value for PM1.0, PM2.5, and PM10.0, respectively.
Step 3. Upload sketch to Wemos D1 mini
In the following sketch, three values need to be modified with your own.
- WIFI_SSID : Name of WiFi router
- WIFI_PASS : Password of WiFi router
- TS_KEY : Value of 'Write API Key' of the channel
For WiFi feature, two functions are added to the sketch of our previous project.
- connectWifi() : Connect to WiFi router
- uploadCloud() : Upload measured data to ThingSpeak cloud using HTTP GET request
After uploading firmware, Wemos restarts itself automatically. Once Wemos D1 mini has restarted, serial monitor shows air quality data from PMS7003 every 5 minutes. In the following serial console output, it shows a HTTP GET request and response from ThingSpeak.
On ThingSpeak website, we can check the data which is sent from device. In the menu, go 'Channels/My channels', then, click 'Private' submenu of the channel. By default, it will show you line graph of each individual field. By clicking 'Add Widgets' menu, you can create a gauge widget as well. For example, in the following screenshot, the bottom right gauge is added manually to make it easier to read for users.
Source codes at github
- ThingSpeak free account
Requirements
-Wemos D1 mini : US$1.77 on Aliexpress
-PMS7003 air quality sensor : US$16.80 on Aliexpress
Instructions
PMS7003 sensor comes with a small breakout board, which has TX, RX, VCC, GND pins. Connect TX of PMS7003 to D5 of Wemos, RX to D6, VCC to 5V, and GND to G. Finally, connect micro usb to Wemos for uploading firmware, and check serial monitor and serial plotter in Android IDE to make sure the sensor works correctly.
Step 2. Create ThingSpeak free account and channel
In this project, we will create a channel with 3 fields. Each field stores value for PM1.0, PM2.5, and PM10.0, respectively.
After creating a channel, click 'API Keys' menu to find out 'Write API Key' value, which we will use in our sketch.
Step 3. Upload sketch to Wemos D1 mini
In the following sketch, three values need to be modified with your own.
- WIFI_SSID : Name of WiFi router
- WIFI_PASS : Password of WiFi router
- TS_KEY : Value of 'Write API Key' of the channel
For WiFi feature, two functions are added to the sketch of our previous project.
- connectWifi() : Connect to WiFi router
- uploadCloud() : Upload measured data to ThingSpeak cloud using HTTP GET request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Air quality monitoring system using PMS7003 sensor with WIFI | |
* | |
* Hardware : Wemos D1 mini, PMS7003 | |
* Software : Arduino IDE | |
* | |
* January 2020. Brian Kim | |
*/ | |
#include <SoftwareSerial.h> | |
#include <ESP8266WiFi.h> | |
/** | |
* WiFi credentials | |
*/ | |
#define WIFI_SSID "your-ssid" | |
#define WIFI_PASS "your-password" | |
/** | |
* Thingspeak credentials | |
*/ | |
#define TS_URL "api.thingspeak.com" | |
#define TS_KEY "your-ts-channel-key" | |
/** | |
* PMS7003 sensor pin map and packet header | |
*/ | |
#define PMS7003_TX D5 // GPIO12 | |
#define PMS7003_RX D6 // GPIO14 | |
#define PMS7003_PREAMBLE_1 0x42 // From PMS7003 datasheet | |
#define PMS7003_PREAMBLE_2 0x4D | |
#define PMS7003_DATA_LENGTH 31 | |
/** | |
* Wemos serial RX - TX PMS7003 | |
* TX - RX | |
*/ | |
SoftwareSerial _serial(PMS7003_TX, PMS7003_RX); // RX, TX | |
int _pm1, _pm25, _pm10; | |
void connectWifi() { | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void uploadCloud(int pm1, int pm25, int pm10) { | |
WiFiClient client; | |
char datetime_str[25]; | |
// Format : https://api.thingspeak.com/update.json?api_key=<write_api_key>&field1=123 | |
String getStr = "GET /update.json?api_key=" + String(TS_KEY) | |
+ "&field1=" + String(pm1) | |
+ "&field2=" + String(pm25) | |
+ "&field3=" + String(pm10) | |
+ " HTTP/1.0"; | |
Serial.println( getStr ); | |
if (client.connect(TS_URL, 80)) { | |
client.println( getStr ); | |
client.println(); | |
} else { | |
Serial.println ("ERROR Connection failed"); | |
} | |
// Wait for response from thingspeak | |
unsigned long timeout = millis(); | |
while (client.available() == 0) { | |
if (millis() - timeout > 5000) { | |
Serial.println("ERROR Client timeout"); | |
client.stop(); | |
delay(60000); | |
return; | |
} | |
} | |
Serial.println("Response from thingspeak :"); | |
while (client.available()) { | |
char ch = static_cast<char>(client.read()); | |
Serial.print(ch); | |
} | |
} | |
void readSensor() { | |
int checksum = 0; | |
unsigned char pms[32] = {0,}; | |
/** | |
* Search preamble for Packet | |
* Solve trouble caused by delay function | |
*/ | |
while( _serial.available() && | |
_serial.read() != PMS7003_PREAMBLE_1 && | |
_serial.peek() != PMS7003_PREAMBLE_2 ) { | |
} | |
if( _serial.available() >= PMS7003_DATA_LENGTH ){ | |
pms[0] = PMS7003_PREAMBLE_1; | |
checksum += pms[0]; | |
for(int j=1; j<32 ; j++){ | |
pms[j] = _serial.read(); | |
if(j < 30) | |
checksum += pms[j]; | |
} | |
_serial.flush(); | |
if( pms[30] != (unsigned char)(checksum>>8) | |
|| pms[31]!= (unsigned char)(checksum) ){ | |
Serial.println("Checksum error"); | |
return; | |
} | |
if( pms[0]!=0x42 || pms[1]!=0x4d ) { | |
Serial.println("Packet error"); | |
return; | |
} | |
_pm1 = makeWord(pms[10],pms[11]); | |
_pm25 = makeWord(pms[12],pms[13]); | |
_pm10 = makeWord(pms[14],pms[15]); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); // For debugging | |
_serial.begin(9600); // For communicating with PMS7003 sensor | |
Serial.printf("\nAir quality monitoring system using PMS7003 sensor with WIFI\n"); | |
connectWifi(); // Connect to WiFi | |
} | |
void loop() | |
{ | |
readSensor(); | |
Serial.printf("PM1.0:%d PM2.5:%d PM10.0:%d\n", _pm1, _pm25, _pm10); | |
uploadCloud(_pm1, _pm25, _pm10); // Upload to thingspeak cloud | |
delay(300000); // Wait 5 minutes | |
} |
Results
On ThingSpeak website, we can check the data which is sent from device. In the menu, go 'Channels/My channels', then, click 'Private' submenu of the channel. By default, it will show you line graph of each individual field. By clicking 'Add Widgets' menu, you can create a gauge widget as well. For example, in the following screenshot, the bottom right gauge is added manually to make it easier to read for users.
References
- IoT Laboratory: ESP8266-based air quality monitoring system using PMS7003 sensor
- IoT Laboratory: ESP8266-based WiFi MQTT air quality monitoring system using PMS7003 sensor
- IoT Laboratory: ESP8266-based WiFi MQTT air quality monitoring system using PMS7003 sensor
- PMS7003 sensor : http://www.plantower.com/en/content/?110.html
No comments:
Post a Comment