Remote Control of Temperature and Humidity with Blynk


#21

Hello,
I want to thank you guys again for all your help. I have modified the code and it all work well at first glance. The one issue is that it publishes to particle 4 - 5 time in a row even tho im asking it to publish one every 5 min. Ubidots seems to work good, one thing i noticed was that once in a blue it will give me min data verse 5 min data but once in an hour. Here is the code . I think somting is wrong with the time part of the code

#include <HTU21D.h>
#include <Ubidots.h>

#ifndef TOKEN
#define TOKEN "A1E-*****************"  // Put here your Ubidots TOKEN
#endif

Ubidots ubidots(TOKEN, UBI_TCP); // Comment this line to use another protocol.

HTU21D htu = HTU21D();

float tempVal, tempFVal, humidVal;

int Time_old;
char msg[200];

void setup() {
  pinMode(D7, OUTPUT);

  Serial.begin(115200);
  digitalWrite(D7, LOW);
    
  if (!htu.begin()){
    Serial.println("Couldn't find sensor!");
    while(1) Particle.process();  //Stay here forever... never exit. Must reconnect sensor and reset device.
  }
}

void loop() {
  if(Time.minute() % 5 == 0 && Time_old != Time.minute())
    digitalWrite(D7, HIGH);
      
  unsigned long timestamp_seconds = Time.now();

  Serial.println("Time Condition Satisfied!");
  Time_old = Time.minute(); 

  tempVal = htu.readTemperature();
  tempFVal = ((9.0/5.0)*tempVal + 32);
  humidVal = htu.readHumidity();
  Serial.println("HTU Values Aquired!");
  
  snprintf(msg, sizeof(msg) - 1, "TempC:%f,TempF:%f,Humidity:%f", tempVal, tempFVal, humidVal);
  Particle.publish("EnvironmentData", msg, PRIVATE);
  Serial.println("Particle Published!");

  ubidots.add("temperature", tempVal, NULL, timestamp_seconds);  // Change for your variable 
  ubidots.add("Humidity", humidVal, NULL, timestamp_seconds);  // Change for your variable name 
 
  //ubidots.add("Temperature C", tempVal);  // Change for your variable name
  //ubidots.add("Temperature F", tempFVal);
  //ubidots.add("Humidity", humidVal);
    
  bool bufferSent = false;
  bufferSent = ubidots.send();  // Will send data to a device label that matches the device Id

  if(bufferSent){
    // Do something if values were sent properly
    Serial.println("Values sent by the device");
  }
  Time_old = Time.minute();
    
  delay(2000);
  digitalWrite(D7, LOW);   
}

Maybe someone can spot the problem

Thanks again


#22

I think your main issue is that you are missing an opening curly brace after your if() statement in loop().
The only line affected by this if is the immediately following digitalWrite(D7, HIGH) - all other lines are executed all the time.

I’ve reformatted your code and corrected the indentation to make it a bit clearer what the issue is.

You should adopt a consistent indenting scheme to make such issues easier to spot.