Commit 61bc8ef0 authored by norbert.dajnowski's avatar norbert.dajnowski

Arduino Weather Station Code

parent 9d5fe06a
#include <SoftwareSerial.h>
#include <Wire.h> //I2C needed for sensors
#include <MQUnifiedsensor.h>
#include "SparkFunMPL3115A2.h" //Pressure sensor
#include "SparkFun_Si7021_Breakout_Library.h" //Humidity sensor
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define pin A0 //Analog input 0
#define type "MQ-135"
#define ADC_Bit_Resolution 10
#define RatioMQ135CleanAir 3.6//RS / R0 = 3.6 ppm
MPL3115A2 myPressure;
Weather myHumidity;
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte STAT_BLUE = 7;
const byte STAT_GREEN = 8;
const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
MQUnifiedsensor MQ135(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
long lastSecond;
char data[26];
const int ledWarning = 8; //use digital I/O pin 8
const int ledGood = 9; //use digital I/O pin 9
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
myPressure.begin(); // Get sensor online
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
pinMode(ledGood,OUTPUT);
pinMode(ledWarning,OUTPUT);
//Set math model to calculate the PPM concentration and the value of constants
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
MQ135.init();
Serial.print("Calibrating please wait.");
float calcR0 = 0;
for(int i = 1; i<=10; i ++)
{
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
Serial.print(".");
}
MQ135.setR0(calcR0/10);
Serial.println(" done!.");
if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
digitalWrite(ledGood,HIGH);
myHumidity.begin();
lastSecond = millis();
Serial.println("Weather Shield online!");
}
void loop()
{
float humidity = myHumidity.getRH();//Check Humidity Sensor
MQ135.update();
if (humidity == 998) //Humidty sensor failed to respond
{
Serial.println("I2C communication to sensors is not working. Check solder connections.");
//Try re-initializing the I2C comm and the sensors
myPressure.begin();
myPressure.setModeBarometer();
myPressure.setOversampleRate(7);
myPressure.enableEventFlags();
myHumidity.begin();
}
else
{
//Check Humidity Sensor
String dataH = "Humidity: " + String(humidity) + "%";
dataH.toCharArray(data,26);
BTSerial.write(data);
Serial.print(data);
//Check Temperature Sensor
float temp_h = (myHumidity.getTempF() - 32) * 5/9;
String dataT = "Temp: " + String(temp_h) + "C";
dataT.toCharArray(data,26);
BTSerial.write(data);
Serial.print(data);
//Check Pressure Sensor
float pressure = myPressure.readPressure();
String dataP = "Pressure: " + String(pressure) + "Pa";
dataP.toCharArray(data,26);
BTSerial.write(data);
Serial.print(data);
//Check light sensor
float light_lvl = get_light_level();
String dataL = ("Light: " + String(light_lvl) + "V");
dataL.toCharArray(data,26);
BTSerial.write(data);
Serial.print(data);
delay(100);
//Check CO Sensor
float CO = get_CO();
String dataCO = ("CO1: " + String(CO) + "PPM");
dataCO.toCharArray(data,26);
Serial.write(data);
delay(100);
//Check Alcohol Sensor
float Alcohol = get_Alcohol();
String dataAlc = ("Alcohol: " + String(Alcohol) + "PPM");
dataAlc.toCharArray(data,26);
Serial.write(data);
delay(100);
}
checkLevels(CO);
delay(10000);
}
void checkLevels(float CO)
{
if (CO > 10){
digitalWrite(ledGood,LOW); //Green LED off
digitalWrite(ledWarning,HIGH); //Red LED on
}else{
digitalWrite(ledGood,HIGH); //Green LED on
digitalWrite(ledWarning,LOW); //Red LED off
}
}
float get_CO()
{
MQ135.setA(605.18); MQ135.setB(-3.937);
return(MQ135.readSensor());
}
float get_Alcohol()
{
MQ135.setA(77.255); MQ135.setB(-3.18);
return(MQ135.readSensor());
}
float get_CO2()
{
MQ135.setA(110.47); MQ135.setB(-2.862);
return(MQ135.readSensor());
}
float get_light_level()
{
float operatingVoltage = analogRead(REFERENCE_3V3);
float lightSensor = analogRead(LIGHT);
operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V
lightSensor = operatingVoltage * lightSensor;
return (lightSensor);
}
float get_battery_level()
{
float operatingVoltage = analogRead(REFERENCE_3V3);
float rawVoltage = analogRead(BATT);
operatingVoltage = 3.30 / operatingVoltage; //The reference voltage is 3.3V
rawVoltage = operatingVoltage * rawVoltage; //Convert the 0 to 1023 int to actual voltage on BATT pin
rawVoltage *= 4.90; //(3.9k+1k)/1k - multiple BATT voltage by the voltage divider to get actual system voltage
return (rawVoltage);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment