// Set up for GPS and Temperature sensing thing
#include "NMEA.h"
#include "DHT.h"
#define DHTPIN 12 // Temperature pin setup
#define DHTTYPE DHT22
// Create Data Connections to GPS and Temp sensor
NMEA gps(GPRMC);
DHT dht(DHTPIN, DHTTYPE);
//define variables to hold GPS and temperature readings
float latitude;
float longitude;
float temperature;
// Set up for wearable control thing
#define SWITCH_PIN 5
#define LED_PIN 13
// Tracks the previous state of the switch
bool wasSwitchOn = false;
// Set up for Air Pollution Sensing thing
//define three variables to hold each pollutant value (NO2, pm2.5m pm10)
float pollution1;
float pollution2;
float pollution3;
// Storing sensor readings from all things in a single data structure
#define MAX_READINGS 360 // 3 a minute, max 2 hour activity (3*2*60)
// Struct to hold a single set of sensor readings
struct SensorReadings {
float temperature;
float pollution1;
float pollution2;
float pollution3;
float latitude;
float longitude;
int flag;
};
// Array to store all sensor readings
SensorReadings readings[MAX_READINGS];
// Counter to keep track of the current reading so list can be written to
int currentReading = 0;
void setup() {
// GPS and temperature sensor thing
Serial.begin(115200);
Serial1.begin(9600); // Serial1 is connected to the custom GPS chip
dht.begin();
// LED and switch (wearable control thing)
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
// air pollution sensing thing
// set up potentiometers simulating air pollution sensors
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}
void loop() {
while (Serial1.available()) { // Waits for serial port data
if (digitalRead(SWITCH_PIN) == LOW) { // Switch is ON
// Turn on LED to indicate sampling
digitalWrite(LED_PIN, HIGH);
// Receives data from GPS serial port
char serialData = Serial1.read();
// Checks if the GPS sentence is valid then proceeds to read sensors
if (gps.decode(serialData)) {
latitude = gps.gprmc_latitude();
longitude = gps.gprmc_longitude();
// print Latitude
Serial.print("Latitude: ");
Serial.println(latitude, 8);
// print Longitude
Serial.print("Longitude: ");
Serial.println(longitude, 8);
// Read and print temperature
if (!isnan(dht.readTemperature())) {
temperature = dht.readTemperature();
Serial.print(F("Temperature: "));
Serial.print(temperature);
Serial.println(F("°C"));
} else {
Serial.println(F("Failed to read from DHT sensor!"));
}
// Read three potentiometers
pollution1 = analogRead(A0);
Serial.print("PM2.5: ");
Serial.println(pollution1);
pollution2 = analogRead(A1);
Serial.print("PM10: ");
Serial.println(pollution2);
pollution3 = analogRead(A2);
Serial.print("NO2: ");
Serial.println(pollution3);
Serial.println(); // print blank line
// add recorded data to an array to be sent to phone
readings[currentReading].latitude = latitude;
readings[currentReading].longitude = latitude;
readings[currentReading].temperature = temperature;
readings[currentReading].pollution1 = pollution1;
readings[currentReading].pollution2 = pollution2;
readings[currentReading].pollution3 = pollution3;
//indicates vals have been assigned (0 by default)
readings[currentReading].flag = 1;
// Update the counter, wrapping around if reach the maximum
currentReading = (currentReading + 1) % MAX_READINGS;
wasSwitchOn = true;
// wait 20 seconds for next read
delay(2000);
}
} else {
// Turn off LED to indicate stop
digitalWrite(LED_PIN, LOW);
// If the switch was on during the last iteration but is now off, print readings
if (wasSwitchOn){
printReadings();
wasSwitchOn = false;
}
// Loop until the switch is turned on again.
// Short delay to avoid bouncing or rapid switching issues
delay(10);
}
}
}
void printReadings() {
Serial.println(F("All Sensor Readings:"));
for (int i = 0; i < MAX_READINGS; ++i) {
// Only print valid readings
if (readings[i].flag != 0) {
Serial.print(F("Reading "));
Serial.print(i + 1);
Serial.print(F(": Latitude: "));
Serial.print(readings[i].latitude, 8);
Serial.print(F(", Longitude: "));
Serial.print(readings[i].longitude, 8);
Serial.print(F(", Temperature: "));
Serial.print(readings[i].temperature);
Serial.print(F("°C, Pollution1: "));
Serial.print(readings[i].pollution1);
Serial.print(F(", Pollution2: "));
Serial.print(readings[i].pollution2);
Serial.print(F(", Pollution3: "));
Serial.println(readings[i].pollution3);
}
}
}