Koen Kempeneers
Published © GPL3+

IoT@School: An IoT Implementation for Monitoring Air Quality

A classroom, in winter times is ill ventilated leading to poor air quality causing fatigue, headaches and in some instances nausea.

IntermediateShowcase (no instructions)5 hours9,644
IoT@School: An IoT Implementation for Monitoring Air Quality

Things used in this project

Hardware components

IOTOPIA Rapid Development kit
AllThingsTalk IOTOPIA Rapid Development kit
×1
Arduino Ethernet
×1
Arduino Ethernet
×1
Seeed Studio Grove TPH sensor
×1
Seeed Studio Grove TPH sensor
×1
Seeed Studio Grove ESP8266 WiFi module
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Schematic

Code

Arduino software

C/C++
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <Sodaq_TPH.h>

// Local Network Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xDE, 0x3B }; // Must be unique on local network

// ThingSpeak Settings
byte server[]  = { 184, 106, 153, 149 };
String writeAPIKey = "****************";
const long updateThingSpeakInterval = 120 * 1000L;      // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

// Variable Setup
long lastConnectionTime = 0L; 
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()
{
  // Start Serial for debugging on the Serial Monitor
  Serial.begin(9600);
  Wire.begin();
  
  // Start Ethernet on Arduino
  startEthernet();
}

void loop()
{
  // Read value from Analog Input Pin 0
  String humidity = String(tph.readHumidity());
  String pressure = String(tph.readPressure());
  String tempSHT = String(tph.readTemperatureSHT());
  String tempBMP = String(tph.readTemperatureBMP());
  
  // Print Update Response to Serial Monitor
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected)
  {
    Serial.println();
    Serial.println("... disconnected");
    
    client.stop();
  }
  
  // Update ThingSpeak
  if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
    String tmp = "&field1="+humidity+"&field2="+tempSHT+"&field3="+pressure+"&field4="+tempBMP;
    Serial.println(tmp);
    updateThingSpeak(tmp);
  }
  
  // Check if Arduino Ethernet needs to be restarted
  if (failedCounter > 3 ) {startEthernet();}
  
  lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
  if (client.connect(server, 80))
  {         
    client.println("POST /update HTTP/1.1");
    client.println("Host: api.thingspeak.com");
    client.println("Connection: close");
    client.println("X-THINGSPEAKAPIKEY: "+writeAPIKey);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(tsData.length());
    client.println();
    client.println();

    client.print(tsData);
    lastConnectionTime = millis();
    
    if (client.connected()) {
      Serial.println("Connecting to ThingSpeak...");     
      failedCounter = 0;
    } else {
      failedCounter++;
      Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");   
    }   
  } else {
    failedCounter++;
    
    Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");   
    lastConnectionTime = millis(); 
  }
}

void startEthernet()
{
  client.stop();
  Serial.println("Connecting Arduino to network ...");
  while(!Serial);
  
  // Connect to network amd obtain an IP address using DHCP
  if (Ethernet.begin(mac) == 0)  {
    Serial.println("DHCP Failed, reset Arduino to try again");
  } else {
    Serial.print("Arduino connected to network using DHCP, IP: ");
    Serial.println(Ethernet.localIP());
  }  
  while(!Serial);
}

Credits

Koen Kempeneers

Koen Kempeneers

2 projects • 13 followers
I have a masters degree in Electronics Engineering, My days are filled with teaching engineering science and technology to high school students (Age 16 and up)

Comments