codes
Scheduling Arduino Code (run every x seconds)
May 8, 2016
micro
Getting Started with Offline C++ on the BBC Micro:Bit
May 21, 2016
Show all

Spark Core / Particle – Room Temp/Humidity Monitoring & Mobile App

particle

We have had our hands on the Spark Core, now known as the Particle Photon for quite some time. We have played around with it here and there but never really used it in a final project.

Now that the weather is starting to get hotter, we thought a useful use for this little WiFi connected device would be to monitor temperatures in our server room.

We are going to be using the Blynk mobile app for recording the data and some nice visuals, if you haven’t heard of Blynk before:

Blynk is a Platform with iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet. It’s a digital dashboard where you can build a graphic interface for your project by simply dragging and dropping widgets.

That’s all there is to it really, it supports a ton of microcontrollers and it’s very quick to get up and running. We used a DHT-22 temperature sensor to read the temp and humidity of the room. Here’s what we did:

  1. Assemble the Spark Core and our DHT-22 Temperature Sensor (Pin 1 = VCC, Pin 2 = DATA, Pin 3 = NC, Pin 4 = GND)
  2. Connect our Spark Core upto our Phone using the official app. This let us join the Spark to the WiFi network and connect to the internet.
  3. Download the Blynk Mobile App (for Android and iPhone)
  4. Create an application in the Blynk app, ours is called ‘ServerRoom’ and add some widgets.
    1. We added a History Graph, Value Displays and Email / Push Notifications.
    2. Generate an auth token (or press email to have it sent to you) and save this for the Spark code.
  5. Login to the Particle Builder App at https://build.particle.io
  6. We first included the Blynk Library and our Adafruit DHT Sensor library.

The Code

#include "blynk/blynk.h"
#include "Adafruit_DHT/Adafruit_DHT.h"
#define BLYNK_PRINT Serial
#include "blynk/blynk.h"
#include <math.h>
#define DHTPIN 2    // what pin we're connected to
#define DHTTYPE DHT22		// DHT 22 (AM2302)3

char auth[] = "enter your blynk auth code here";
#define publish_delay 60000
unsigned int lastPublish = 0;

float minTemp = 0;
float maxTemp = 0;

DHT dht(DHTPIN, DHTTYPE);
WidgetLCD lcd(V5);

void setup() {
    dht.begin();
    delay(2000);
    Blynk.begin(auth);
}

void loop() {
    Blynk.run();
    
    unsigned long now = millis();
    if ((now - lastPublish) < publish_delay) {
        return;
    }
    
    float h = dht.getHumidity();
    float t = dht.getTempCelcius();
	float f = dht.getTempFarenheit();

	if (minTemp == 0 || t < minTemp) {
	    minTemp = t;
	}
	
	if (maxTemp == 0 || t > maxTemp) {
	    maxTemp = t;
	}
	
	Blynk.virtualWrite(1, round(t));
	Blynk.virtualWrite(3, round(h));
	Blynk.virtualWrite(4, round(t));
	Blynk.virtualWrite(5, round(minTemp));
	Blynk.virtualWrite(6, round(maxTemp));
	

	if (t > 30) {
	    Blynk.notify("Server Room is getting hot!");
	    Blynk.email("it@company", "Server Room - Too Hot", "The server room is currently too hot. Check the Aircon.");
	} 
	
	if (t > 40) {
	    Blynk.notify("Something is wrong. Server room is too hot.!");
	    Blynk.email("it@company.com", "Server Room - Too Hot", "The server room is currently too hot. Check the Aircon.");
	}
    
    lastPublish = now;
}

IMG_0606The code above is extremely simple, we firstly include our required libraries. We then initialize the Blynk and DHT objects ready to be used in the main program loop. We added a variable with our Blynk authentication token that we got earlier.

In the loop() function we create a basic timer by checking how long ago we last ran, this is so the application only queries the sensor every 60 seconds (in our case) it then writes the values to the Blynk virtual pins.

We added a couple of very simple checks, if (t > 30) etc. to test out the notifications. These can be configured to whatever temperature you would like just by changing the values. You can assign the required virtual pins on the Blynk app in the settings.

You can see a screenshot of our Blynk application on the left hand side. The Spark Core prototype photo can be seen below (in action!)

IMG_0603

You can find out more about Blynk by clicking here. and Spark Core/Particle by clicking here.

Leave a Reply

Your email address will not be published. Required fields are marked *