nedaa22
Published

MKR1000& ENV &Blynk

IOT application with MKR1000 and ENV shield just 2 components with them we can get (Temp, Lux, Hum, UV) values from everywhere

IntermediateProtip291
MKR1000& ENV &Blynk

Things used in this project

Story

Read more

Schematics

Just in this way

Code

MKR_1000_with_ENV_Demo.ino

Arduino
// first including the neccessry libs 
#define BLYNK_PRINT Serial
#include <math.h>
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleMKR1000.h>
#include <Arduino_MKRENV.h>

// Auth for connectiong Blynk with your Device 
char auth[] = "*****";// here comes the auth code wich been send to you from Blynk server
char ssid[] = "*****";//your WIFI name
char pass[] = "*****";//your WIFI password


BlynkTimer timer;// this timer is required to send data from shield to blynk app in intervals 


// this function to round float numbers to just 2 decimal points although it is for AVR but it worked :)
char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
  char fmt[20];
  sprintf(fmt, "%%%d.%df", width, prec);
  sprintf(sout, fmt, val);
  return sout;
}


void setup()
{
  
  Serial.begin(9600);
  // this line is very immportant which i dont know why but it didnt work without it i got it from env arduino documentation 
  if (!ENV.begin()) {
   Serial.println("Failed to initialize MKR ENV shield!");
    while (1);
  } 
  



  Blynk.begin(auth, ssid, pass);

  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}


void sendSensor()
{
  float h = ENV.readHumidity();
  float t = ENV.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  float lx = ENV.readIlluminance();
  float p=ENV.readPressure();
  int uva= ENV.readUVA();
  int uvb= ENV.readUVB();
  int uvIndex= ENV.readUVIndex();
  // rounding all vairables to 2 decimal points
  char tt[16];
  dtostrf(t,1,2,tt);
char hh[16];
  dtostrf(h,1,2,hh);
  
  char lxx[16];
  dtostrf(lx,1,2,lxx);
  char pp[16];
  dtostrf(p,1,2,pp);
  
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Error");
    return;
  }
  
  Blynk.virtualWrite(V5, hh);
  Blynk.virtualWrite(V6, tt);
  Blynk.virtualWrite(V7, lxx);
  Blynk.virtualWrite(V8, pp);
  Blynk.virtualWrite(V9,uva);
  Blynk.virtualWrite(V10,uvb);
  Blynk.virtualWrite(V11,uvIndex);
  
}

Credits

nedaa22

nedaa22

0 projects • 0 followers

Comments