Last active
December 10, 2019 10:59
-
-
Save Electronza/ff71943d06a7c305952dddbd7b3e657b to your computer and use it in GitHub Desktop.
ESP8266 code for the diy air quality monitoring station
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
/******************************************************************* | |
____ __ ____ ___ ____ ____ __ __ _ ____ __ | |
( __)( ) ( __)/ __)(_ _)( _ \ / \ ( ( \(__ ) / _\ | |
) _) / (_/\ ) _)( (__ )( ) /( O )/ / / _/ / \ | |
(____)\____/(____)\___) (__) (__\_) \__/ \_)__)(____)\_/\_/ | |
Project name: DIY Air Quality Monitoring Station | |
Project page: https://electronza.com/diy-air-quality-monitoring-station/ | |
Description : Air quality station with Arduino Due & ESP8266 | |
--- ESP8266 code --- | |
********************************************************************/ | |
#include <ESP8266WiFi.h> | |
#include "ThingSpeak.h" | |
char ssid[] = "XXXX"; // your network SSID (name) | |
char pass[] = "XXXX"; // your network password | |
/* | |
***************************************************************************************** | |
**** Visit https://www.thingspeak.com to sign up for a free account and create | |
**** a channel. The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/ | |
**** has more information. You need to change this to your channel, and your write API key | |
**** IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR WRITE API KEY!! | |
*****************************************************************************************/ | |
unsigned long myChannelNumber = XXXXXX; | |
const char * myWriteAPIKey = "XXXXXX"; | |
String dataString = ""; | |
boolean dataStringComplete = 0; | |
char inChar; | |
float temperature; | |
float humidity; | |
float pressure; | |
int H2S; | |
int RAW_H2S; | |
int RAW_Ozone; | |
WiFiClient client; | |
void setup() { | |
// Configure serial port | |
Serial.begin(9600); | |
delay(10); | |
//Serial.println("Starting..."); | |
// reserve 80 bytes for the dataString | |
dataString.reserve(100); | |
flush_serial(); | |
//Serial.println(); | |
//Serial.print("Connecting to "); | |
//Serial.println(ssid); | |
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, | |
would try to act as both a client and an access-point and could cause | |
network-issues with your other WiFi-devices on your WiFi-network. */ | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
//Serial.print("."); | |
} | |
//Serial.println(""); | |
//Serial.println("WiFi connected"); | |
//Serial.println("IP address: "); | |
//Serial.println(WiFi.localIP()); | |
ThingSpeak.begin(client); | |
} | |
void loop() { | |
// Read the input on each pin, convert the reading, and set each field to be sent to ThingSpeak. | |
// On Uno,Mega,Yun: 0 - 1023 maps to 0 - 5 volts | |
// On ESP8266: 0 - 1023 maps to 0 - 1 volts | |
// On MKR1000,Due: 0 - 4095 maps to 0 - 3.3 volts | |
//float pinVoltage = analogRead(A0) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS); | |
// Read data from serial interface | |
// Wait for some data in the serial buffer | |
// send data only when you receive data: | |
while (Serial.available() < 1) { | |
delay(10); | |
yield(); | |
} | |
Serial_Data_Read(); | |
Parse_data(); | |
//Temperature | |
ThingSpeak.setField(1,temperature); | |
// Humidity | |
ThingSpeak.setField(2,humidity); | |
// Pressure | |
ThingSpeak.setField(3,pressure); | |
ThingSpeak.setField(4,H2S); | |
// RAW H2S | |
ThingSpeak.setField(5,RAW_H2S); | |
//RAW Ozone | |
ThingSpeak.setField(6,RAW_Ozone); | |
// Write the fields that you've set all at once. | |
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); | |
//delay(20000); // ThingSpeak will only accept updates every 15 seconds. | |
} | |
void flush_serial(){ | |
// Do we have data in the serial buffer? | |
// If so, flush it | |
if (Serial.available() > 0){ | |
while(1){ | |
inChar = (char)Serial.read(); | |
delay(10); | |
if (inChar == '\n') break; | |
} | |
} | |
} | |
/* ******************************************************************************** | |
* This function triggers one measurement and receives the data from the sensor | |
**********************************************************************************/ | |
void Serial_Data_Read(){ | |
// First, we do some initialization | |
// dataStringComplete is set as "false", as we don't have any valid data received | |
dataStringComplete = 0; | |
// Clear the data string | |
dataString = ""; | |
dataString = Serial.readStringUntil('\n'); | |
} | |
/* ******************************************************************************** | |
* This function takes the received string and upodates sensor data | |
**********************************************************************************/ | |
void Parse_data(){ | |
// Parses the received dataString | |
// Data string is comma separated | |
int idx1 = dataString.indexOf(','); | |
String S_temp = dataString.substring(0, idx1); | |
temperature = S_temp.toFloat() / 100; | |
int idx2 = dataString.indexOf(',', idx1 + 1); | |
String S_humi = dataString.substring(idx1 + 1, idx2); | |
humidity = S_humi.toFloat() / 100; | |
int idx3 = dataString.indexOf(',', idx2 + 1); | |
String S_pres = dataString.substring(idx2 + 1, idx3); | |
pressure = S_pres.toFloat() / 100; | |
int idx4 = dataString.indexOf(',', idx3 + 1); | |
String S_H2S = dataString.substring(idx3 + 1, idx4); | |
H2S = S_H2S.toInt(); | |
int idx5 = dataString.indexOf(',', idx4 + 1); | |
String S_H2Sraw = dataString.substring(idx4 + 1, idx5); | |
RAW_H2S = S_H2Sraw.toInt(); | |
int idx6 = dataString.indexOf('\r'); | |
String S_O3 = dataString.substring(idx5 + 1, idx6); | |
RAW_Ozone = S_O3.toInt(); | |
//Serial.println (temperature); | |
//Serial.println (humidity); | |
//Serial.println (pressure); | |
//Serial.println (H2S); | |
//Serial.println (RAW_H2S); | |
//Serial.println (RAW_Ozone); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment