Today I would like to write a very easy tutorial on how to read remotely temperature and humidity using Blynk.
I am using a HTU21D temperature / humidity sensor by Adafruit and an (amazing ) Particle Photon.
Lets start from the wiring layout:
Connect the HTU21Dâs pins to the Photon as follows:
VIN, GND ----------> 3V3, GND (in my case they are located in the + and - tracks)
SDA -----------------> D0
SCL -----------------> D1
(nothing on 3V3 pin!)
Make sure to import the HTU21D and the Blynk libraries in your app.
Here is the code:
// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"
// This #include statement was automatically added by the Particle IDE.
#include "HTU21D/HTU21D.h"
HTU21D htu = HTU21D();
double tempVal, tempFVal, humidVal;
char auth[] = "01234567890123456789";
void setup() {
//Serial.begin(9600);
Blynk.begin(auth);
if (!htu.begin()){
Serial.println("Couldn't find sensor!");
while(1);
}
}
BLYNK_READ(V0){
Blynk.virtualWrite(V0, round(tempVal*100)/100);
}
BLYNK_READ(V1){
Blynk.virtualWrite(V1, round(tempFVal*100)/100);
}
BLYNK_READ(V2){
Blynk.virtualWrite(V2, humidVal);
}
void loop() {
Blynk.run();
//Serial.print("Temp: "); Serial.print(htu.readTemperature()); Serial.print(" C"); Serial.print("\t"); Serial.print(tempFVal); Serial.print(" F");
//Serial.print("\t\tHUM: "); Serial.print(htu.readHumidity()); Serial.println(" %");
//Serial.println("");
//delay(10000);
tempVal = htu.readTemperature();
tempFVal = ((9.0/5.0)*tempVal + 32);
humidVal = htu.readHumidity();
}
Make sure in the char auth[] line to use the token Blynk generates for your project. You can find the token in the Project Settings of the Blynk app you installed in your Android or IOS device. Within the Blynk app it is very easy and intuitive to create widgets. In my case I have two gauge widgets to measure the temperature (Celsius and Fahrenheit) and a label value for the humidity. Here are the three widgets and relative settings:
I am refreshing the reading of temperature and humidity every 10 seconds. Make sure to look at the Blynkâs doc about the code you must include in your app.
Also be sure to create the Blink.virtualWrite() function outside of the void loop and avoid to use the delay() function inside the void loop. If you do not follow this step your app is going to send hundreds of readings per second to the Blynk server and your Photon will start disconnecting from the cloud for a short period of time every couple of minutes (thank you @ScruffR for the heads-upâŚ). Originally I thought the delay() function was enough to avoid communication issues with the Blynk server. Please refer to the Blynk doc.
We are almost there⌠Flash the firmware to your Photon.
Open the Blynk app:
Congratulations you have made your first weather station (or at least a small part of it⌠)! Enjoy monitoring the temperature and humidity of your room from anywhere in the world!!