Air Quality Monitoring Node

A gadget is designed to measure the air quality index and uploads it on a web server where graphical trends in AQI value can be seen

AdvancedShowcase (no instructions)2 days1,660
Air Quality Monitoring Node

Things used in this project

Story

Read more

Code

Air quality monitoring

Arduino
Code updates the data on ubidots server
#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
int sensorValue;
#endif
#include <WiFi.h>
char ssid[] = "R2P";
// your network password
char password[] = "8700217023";



char* TOKEN = "BBFF-pffKMwicvNd0O3ZSlt5CDai5pZ2m7C"; // Put here your TOKEN

char* DEVICE_LABEL = "aqim"; // Your Device label

/* Put here your variable's labels*/
char const * VARIABLE_LABEL_1 = "aqi";

/* HTTP Settings */
char const * HTTPSERVER = "industrial.api.ubidots.com";
const int HTTPPORT = 80;
char const * USER_AGENT = "Arduino_Ethernet";
char const * VERSION = "1.0";

WiFiClient clientUbi;

/********************************
 * Auxiliar Functions
 *******************************/

void SendToUbidots(String payload) {

  int contentLength =payload.length();

  /* Connecting the client */
  clientUbi.connect(HTTPSERVER, HTTPPORT);

  if (clientUbi.connected( )) {
	/* Builds the request POST - Please reference this link to know all the request's structures https://ubidots.com/docs/api/ */
	clientUbi.print(F("POST /api/v1.6/devices/"));
	clientUbi.print(DEVICE_LABEL);
	clientUbi.print(F(" HTTP/1.1\r\n"));
	clientUbi.print(F("Host: "));
	clientUbi.print(HTTPSERVER);
	clientUbi.print(F("\r\n"));
	clientUbi.print(F("User-Agent: "));
	clientUbi.print(USER_AGENT);
	clientUbi.print(F("/"));
	clientUbi.print(VERSION);
	clientUbi.print(F("\r\n"));
	clientUbi.print(F("X-Auth-Token: "));
	clientUbi.print(TOKEN);
	clientUbi.print(F("\r\n"));
	clientUbi.print(F("Connection: close\r\n"));
	clientUbi.print(F("Content-Type: application/json\r\n"));
	clientUbi.print(F("Content-Length: "));
	clientUbi.print(contentLength);
	clientUbi.print(F("\r\n\r\n"));
	clientUbi.print(payload);
	clientUbi.print(F("\r\n"));

	Serial.println(F("Making request to Ubidots:\n"));
	Serial.print("POST /api/v1.6/devices/");
	Serial.print(DEVICE_LABEL);
	Serial.print(" HTTP/1.1\r\n");
	Serial.print("Host: ");
	Serial.print(HTTPSERVER);
	Serial.print("\r\n");
	Serial.print("User-Agent: ");
	Serial.print(USER_AGENT);
	Serial.print("/");
	Serial.print(VERSION);
	Serial.print("\r\n");
	Serial.print("X-Auth-Token: ");
	Serial.print(TOKEN);
	Serial.print("\r\n");
	Serial.print("Connection: close\r\n");
	Serial.print("Content-Type: application/json\r\n");
	Serial.print("Content-Length: ");
	Serial.print(contentLength);
	Serial.print("\r\n\r\n");
	Serial.print(payload);
	Serial.print("\r\n");
  } else {
	Serial.println("Connection Failed ubidots - Try Again");
  }

  /* Reach timeout when the server is unavailable */
  int timeout = 0;
  while (!clientUbi.available() && timeout < 5000) {
	timeout++;
	delay(1);
	if (timeout >= 5000) {
  	Serial.println(F("Error, max timeout reached"));
  	break;
	}
  }

  /* Reads the response from the server */
  Serial.println(F("\nUbidots' Server response:\n"));
  while (clientUbi.available()) {
	char c = clientUbi.read();
	Serial.print(c); // Uncomment this line to visualize the response on the Serial Monitor
  }

  /* Disconnecting the client */
  clientUbi.stop();
}

void setup() {
  Serial.begin(9600);
  //Initialize serial and wait for port to open:
  Serial.begin(115200);

  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid);
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
	// print dots while we wait to connect
	Serial.print(".");
	delay(300);
  }
 
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
 
  while (WiFi.localIP() == INADDR_NONE) {
	// print dots while we wait for an ip addresss
	Serial.print(".");
	delay(300);
  }

  Serial.println("\nIP Address obtained");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  }


void loop() {
sensorValue = analogRead(6);   	// read analog input pin 0
Serial.print("AirQua=");
Serial.print(sensorValue, DEC);           	// prints the value read
Serial.println(" PPM");
  // if there are incoming bytes available
  // from the server, read them and print them:
   char payload[200];
  char str_val_1[30];

  /*---- Simulates the values of the sensors -----*/
 // float sensor_value_1 = random(0, 1000)*1.0;

 
  delay(100);
String s="";
s=s+"{\"aqi\":{\"value\":";
s=s+sensorValue;
s=s+"}}";

SendToUbidots(s);

delay(100);
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Credits

Dr. Umesh Dutta

Dr. Umesh Dutta

38 projects • 53 followers
Working as Director of Innovation Centre at Manav Rachna, India. I am into development for the last 12 years.
Texas Instruments University Program

Texas Instruments University Program

91 projects • 119 followers
TI helps students discover what's possible to engineer their future.
Energia

Energia

34 projects • 26 followers
Founder of @energiaproject
Prince

Prince

1 project • 1 follower

Comments