Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Created February 23, 2016 19:00
Show Gist options
  • Save germanviscuso/11fea8de6140cc32fe62 to your computer and use it in GitHub Desktop.
Save germanviscuso/11fea8de6140cc32fe62 to your computer and use it in GitHub Desktop.
/*
* ------------ [GP_019] - AQM with particle --------------
*
* Explanation: This is the basic code to manage and read CO, O3, SO2, NO2
* gas sensors and a prticle sensor. This gases are commonly meassured in
* air quality monitors. The concentration and the enviromental variables
* will be stored in a frame. Cycle time: 5 minutes.
*
* Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 0.1
* Design: David Gascón
* Implementation: Alejandro Gállego
*/
#include <WaspOPC_N2.h>
#include <WaspSensorGas_Pro.h>
#include <WaspFrame.h>
//---******---
#include <Wasp3G.h>
//---******---
// Each object will be used by each gas sensor
Gas CO(SOCKET_A);
Gas O3(SOCKET_B);
Gas SO2(SOCKET_C);
Gas NO2(SOCKET_F);
float conc_CO; // Stores the concentration level of CO in ppm
float conc_O3; // Stores the concentration level of O3 in ppm
float conc_SO2; // Stores the concentration level of SO2 in ppm
float conc_NO2; // Stores the concentration level of NO2 in ppm
float temperature; // Stores the temperature in ºC
float humidity; // Stores the realitve humidity in %RH
float pressure; // Stores the pressure in Pa
int status;
int measure;
char node_ID[] = "AQM_OPCN2";
//---******---
int8_t answer;
char aux_str[200];
char aux_str2[20];
int d1;
float f2;
int d2;
//---******---
void setup()
{
USB.println(node_ID);
// Set the Waspmote ID
frame.setID(node_ID);
//---******---
// sets operator parameters
_3G.set_APN("gprs-service.com");
// And shows them
_3G.show_APN();
//---******---
}
void loop()
{
///////////////////////////////////////////
// 1a. Turn on particle sensor
///////////////////////////////////////////
// Power on the OPC_N2 sensor.
// If the gases PRO board is off, turn it on automatically.
status = OPC_N2.ON();
if (status == 1)
{
USB.println(F("Particle sensor started"));
}
else
{
USB.println(F("Error starting the particle sensor"));
}
///////////////////////////////////////////
// 1b. Read the particle sensor
///////////////////////////////////////////
if (status == 1)
{
// Power the fan and the laser and perform a measure of 8 seconds
measure = OPC_N2.getPM(8000);
if (measure == 1)
{
USB.println(F("Measure performed"));
USB.print(F("PM 1: "));
USB.print(OPC_N2._PM1);
USB.println(F(" ug/m3"));
USB.print(F("PM 2.5: "));
USB.print(OPC_N2._PM2_5);
USB.println(F(" ug/m3"));
USB.print(F("PM 10: "));
USB.print(OPC_N2._PM10);
USB.println(F(" ug/m3"));
}
else
{
USB.print(F("Error performing the measure. Error code:"));
USB.println(measure, DEC);
}
}
///////////////////////////////////////////
// 1c. Turn off the particle sensor
///////////////////////////////////////////
// Power off the OPC_N2 sensor. If there aren't other sensors powered,
// turn off the board automatically
OPC_N2.OFF();
delay(15000);
///////////////////////////////////////////
// 2a. Turn on gas sensors
///////////////////////////////////////////
// Power on the sensors.
// If the gases PRO board is off, turn it on automatically.
CO.ON();
O3.ON();
SO2.ON();
NO2.ON();
// The sensor needs time to warm up and get a response from gas
// To reduce the battery consumption, use deepSleep instead delay
// After 2 minutes, Waspmote wakes up thanks to the RTC Alarm
PWR.deepSleep("00:00:02:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_ON);
///////////////////////////////////////////
// 2b. Read gas sensors
///////////////////////////////////////////
// Read the sensors and compensate with the temperature internally
conc_CO = CO.getConc();
conc_O3 = O3.getConc();
conc_SO2 = SO2.getConc();
conc_NO2 = NO2.getConc();
// Read enviromental variables
// In this case, CO objet has been used.
// O3, SO2 or NO2 objects could be used with the same result
temperature = CO.getTemp();
humidity = CO.getHumidity();
pressure = CO.getPressure();
// And print the values via USB
USB.println(F("***************************************"));
USB.print(F("CO concentration: "));
USB.print(conc_CO);
USB.println(F(" ppm"));
USB.print(F("O3 concentration: "));
USB.print(conc_O3);
USB.println(F(" ppm"));
USB.print(F("SO2 concentration: "));
USB.print(conc_SO2);
USB.println(F(" ppm"));
USB.print(F("NO2 concentration: "));
USB.print(conc_NO2);
USB.println(F(" ppm"));
USB.print(F("Temperature: "));
USB.print(temperature);
USB.println(F(" Celsius degrees"));
USB.print(F("RH: "));
USB.print(humidity);
USB.println(F(" %"));
USB.print(F("Pressure: "));
USB.print(pressure);
USB.println(F(" Pa"));
///////////////////////////////////////////
// 2c. Turn off the gas sensors
///////////////////////////////////////////
// Power off the sensors sensor. If there aren't more gas sensors powered,
// turn off the board automatically
CO.OFF();
O3.OFF();
SO2.OFF();
NO2.OFF();
///////////////////////////////////////////
// 4. Create ASCII frame
///////////////////////////////////////////
// Maybe it will be necessary to increase the MAX_FRAME constant
// Create new frame (ASCII)
frame.createFrame(ASCII);
// Add CO concentration
/* frame.addSensor(SENSOR_GP_CO, conc_CO);
// Add O3 concentration
frame.addSensor(SENSOR_GP_O3, conc_O3);
// Add SO2 concentration
frame.addSensor(SENSOR_GP_SO2, conc_SO2);
// Add NO2 concentration
frame.addSensor(SENSOR_GP_NO2, conc_NO2);
// Add temperature
frame.addSensor(SENSOR_GP_TC, temperature);
// Add humidity
frame.addSensor(SENSOR_GP_HUM, humidity);
// Add pressure
frame.addSensor(SENSOR_GP_PRES, pressure);
// Add PM 1
frame.addSensor(SENSOR_OPC_PM1,OPC_N2._PM1);
// Add PM 2.5
frame.addSensor(SENSOR_OPC_PM2_5,OPC_N2._PM2_5);
// Add PM 10
frame.addSensor(SENSOR_OPC_PM10,OPC_N2._PM10);
// Add PM 10
frame.addSensor(SENSOR_BAT,PWR.getBatteryLevel());
// Show the frame
frame.showFrame();*/
//---******---
// 2. activates the 3G module:
answer = _3G.ON();
if ((answer == 1) || (answer == -3))
{
USB.println(F("3G module ready..."));
// 4. Waits for connection to the network
answer = _3G.check(180);
if (answer == 1)
{
USB.println(F("3G module connected to the network..."));
USB.print(F("Getting URL with GET method..."));
d1 = temperature;
f2 = temperature - d1;
d2 = f2 * 100;
sprintf(aux_str, "GET /test-get-post.php?temperature=%d.%02d HTTP/1.1\r\nHost: pruebas.libelium.com\r\nContent-Length: 0\r\n\r\n", d1, d2);
// 5. gets URL from the solicited URL
answer = _3G.readURL("pruebas.libelium.com", 80, aux_str);
// Checks the answer
if ( answer == 1)
{
USB.println(F("Done"));
USB.println(_3G.buffer_3G);
}
else if (answer < -14)
{
USB.print(F("Failed. Error code: "));
USB.println(answer, DEC);
USB.print(F("CME error code: "));
USB.println(_3G.CME_CMS_code, DEC);
}
else
{
USB.print(F("Failed. Error code: "));
USB.println(answer, DEC);
}
}
else
{
USB.println(F("3G module cannot connect to the network..."));
}
}
else
{
// Problem with the communication with the 3G module
USB.println(F("3G module not started"));
}
// 7. Powers off the 3G module
_3G.OFF();
//---******---
///////////////////////////////////////////
// 5. Sleep
///////////////////////////////////////////
// Go to deepsleep
// After 3 minutes, Waspmote wakes up thanks to the RTC Alarm
PWR.deepSleep("00:00:02:37", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment