[SOLVED] Arduino Temperature Personalization

I have a project and I need help. My project is to read temperature by using temperature sensor and arduino. By using blynk email widget, I am able to send warning notification if the temperature is too high or too low. For example, in my code now I put any thing greater than 90 is too high and anything lower than 32 is too low. How can I control what is too high and too low from blynk app ? I was trying to use the slider widget but I am not successful to do so. Please help me in my code.

    //#define BLYNK_PRINT Serial
    #define BLYNK_MAX_SENDBYTES 128
    #include <BlynkSimpleStream.h>

    int redPin = A2;
    int bluePin = A3;
    int sensorPin = A10;
    unsigned int notified = 0;
    unsigned int X= V1;
    BLYNK_WRITE(V1) 
    { 
      X = param.asInt();
    }

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "";


    void setup()
    {
      // Debug console
     Serial.begin(9600);
     delay(10);
      // Blynk will work through Serial
      // Do not read or write this serial manually in your sketch
      //Serial.begin(9600);
      Blynk.begin(Serial, auth);
     
     pinMode(redPin, OUTPUT);
    //pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);

      // Send e-mail when your hardware gets connected to Blynk Server
      // Just put the recepient's "e-mail address", "Subject" and the "message body"
      Blynk.email("", "Subject", X  );

      // Setting the button
     // pinMode(2, INPUT_PULLUP);
      // Attach pin 2 interrupt to our handler
     // attachInterrupt(digitalPinToInterrupt(2), emailOnButtonPress, CHANGE);
    }
    void sendData()
    {
       int reading = analogRead(sensorPin);  
     
     // converting that reading to voltage, for 3.3v arduino use 3.3
     float voltage = reading * 5.0;
     voltage /= 1024.0; 
     
     // print out the voltage
     Serial.print(voltage); Serial.println(" volts");
     
     // now print out the temperature
     float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                                   //to degrees ((voltage - 500mV) times 100)
     Serial.print(temperatureC); Serial.println(" degrees C");
     
     // now convert to Fahrenheit
     float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
     
     Serial.print(temperatureF); Serial.println(" degrees F");
     
     delay(1000);                                     //waiting 5 seconds
     
    Blynk.virtualWrite(V1, 57);

      if(temperatureF >X && notified == 0)
     {
      notified = 1; 
      Blynk.email("", "Subject", X );

      setColor(255, 0, 0);
      digitalWrite(redPin, HIGH);
      delay(1000);
      //timer.setTimeout(300000L, resetNotified);
     }else if(temperatureF<78){
      notified = 0;
      setColor(0, 0, 255); // blue
     digitalWrite(bluePin, HIGH);
     delay(1000);
     }
    }

    void loop()
    {
      
      int reading = analogRead(sensorPin);  
     
     // converting that reading to voltage, for 3.3v arduino use 3.3
     float voltage = reading * 5.0;
     voltage /= 1024.0; 
     
     // print out the voltage
     Serial.print(voltage); Serial.println(" volts");
     
     // now print out the temperature
     float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                                   //to degrees ((voltage - 500mV) times 100)
     Serial.print(temperatureC); Serial.println(" degrees C");
     
     // now convert to Fahrenheit
     float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
      Serial.print(temperatureF); Serial.println(" degrees F");
      Blynk.run();
     // timer.run();
    }
    void setColor(int red,int green, int blue)
    {
    #ifdef COMMON_ANODE
    red = 255 - red;
    blue = 255 - blue;
    #endif
    analogWrite(redPin, red);
    analogWrite(bluePin, blue);
    }

How do you set those ranges now? Simply change them to a variable and use a slider, or other appropriate widget to determine the value of that variable.

BLYNK_WRITE(Vx) // Slider function to get SomeValue for use in another function.
{
  int SomeValue = param.asInt();
  // do something with this value in your code
}

I also recommend moving much of your code, currently in the main loop, into functions of their own and calling them from the main loop via timer.

And minimise or eliminate delays, they will just cause connection issues.

@Gunner I already have this in my code, but I am not getting the slider function to work. Where should I put it though? Is it correct to assign it at the beginning?

It is a basic Blynk function, it gets called whenever something happens with the widget assigned to the Vx pin, V0, V1, V2, etc)… Place anywhere after setup().

Check out the various examples in the Docs and Sketch Builder (top right of this page) for more info about specific Blynk commands and how they fit in a sketch.

There is at least one problem with your sketch. You are putting too much stuff in the main loop. You have to remember that the loop() part gets executed thousands of times per second. If you want to stay connected to Blynk you have to have just Blynk.run() and timer.run() in the main loop.

From what I can see is, you got the right example (sendData), but you removed everything which would actually make things run smooth. Mainly the timer part, SimpleTimer (which has been replaced by BlynkTimer, but it does the same). I don’t know if the examples are updated yet with BlynkTimer, but I suggest getting the lastest example and start with sending the data to your app every second or so (if that is even needed, usually it can be a longer period, water doesn’t warm up that fast in normal circumstances).

Furthermore, if you want to send data to the hardware to setup the limits you need to create special functions for reading the slider widget. For example a slider attached to V0, you would catch the values you set from the app:

// All this stuff goes outside any other functions b.t.w.

int maxValue = 0;   // Global variable, so you can use it anywhere

BLYNK_WRITE(V0)   // Catch value from Widget attached to V0, wether it be slider, button, whatever
{
   maxValue == param.asInt();
}

This basically grabs the value you set from the App and you can work with that in your sketch

1 Like

Since you’re using Blynk to set your values, you’ll likely want to also view your data on the same device you’re using to set the high and low points. When I migrated my existing project into Blynk, I’ve found the Terminal Widget to be most valuable. It works just like the Serial monitor, but on the device the app launches from.

//Above setup
WidgetTerminal terminal(Vx);   //Virtual pin number

//Inside your function(s)
{
terminal.print("Generic Text Prints: ");
terminal.println(variableName);           //prints value in variableName
terminal.flush();         
}

The same data that was previously printed to the Serial monitor on the PC is now printed to the Terminal monitor on the smartphone.

@Lichtsignaal thank for your answer. I have tried to simplfy my code. I will posted when I get home. This is the code I tried but still didn’t work. I keep getting the value of X ( The tempreture the user wantS to set up) =0 when I recive an email notfication.

 int X = 0 ; 
    BLYNK_WRITE(V1) 
        { 
          X = param.asInt();
        }
1 Like

@myggle Thank you so much myggle. This is what I was going to do for the next step .I want kind of a history for the tempreture during the day. Does it also store the old values? or it only show you the current?

@Lichtsignaal Also, the reson I didn’t include the simple timer, because I get errors whenever I include it in my code.

Well, if you get errors, please show us. We may be able to help with that too :wink:

As for the BLYNK_WRITE part, you have to do something with the value you got. Show us the whole code and we can answer you better.

@Lichtsignaal Thanks again! I ll post the code when I go home tonight.

1 Like

I didn’t yet create a history graph for my project, but I’m certain the readings can be logged to a widget that way.

Blynk has built-in data logging using the History widget :slight_smile:

@Lichtsignaal Here is my code. Bare with me, I never used c or c++ before and this is my first experience. I want to change the value of X ( the temperature setting) based on user preference.

//#define BLYNK_PRINT Serial
#define BLYNK_MAX_SENDBYTES 128
#include <BlynkSimpleStream.h>
#include <SimpleTimer.h>


int redPin = A2;
int bluePin = A3;
int sensorPin = A10;
unsigned int notified = 0;
int X=0 ; 

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " ";
BlynkTimer timer;


 BLYNK_WRITE(V1) // Slider function to get  X Value for use in another function.
{
  int X =  param.asInt();
  
}

void setup()
{
  // Debug console
 Serial.begin(9600);
 delay(10);
  //Serial.begin(9600);
  Blynk.begin(Serial, auth);
 
 pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email(" ", "Subject", X  );
 
}

void sendData()
{
   int reading = analogRead(sensorPin);  
 
 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 5.0;
 voltage /= 1024.0; 
 
 // print out the voltage
 Serial.print(voltage); Serial.println(" volts");
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((voltage - 500mV) times 100)
 Serial.print(temperatureC); Serial.println(" degrees C");
 
 // now convert to Fahrenheit
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
 
 Serial.print(temperatureF); Serial.println(" degrees F");
 
 delay(1000);                                     //waiting 5 seconds
 
  Blynk.virtualWrite(V1,70);

  if(temperatureF > X && notified == 0)
 {
  notified = 1; 
  Blynk.email(" ", "Subject", X );
  setColor(255, 0, 0);
  digitalWrite(redPin, HIGH);
  delay(1000);
  timer.setTimeout(300000L, resetNotified);
 }else if(temperatureF<78){
  notified = 0;
  setColor(0, 0, 255); // blue
 digitalWrite(bluePin, HIGH);
 delay(1000);
 }
}

void loop()
{
  Blynk.run();
  timer.run();
}
void setColor(int red,int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(bluePin, blue);
}

@Lichtsignaal that’s what I want to use next.

@Lichtsignaal has mentioned there is a history widget, I will do that next.

Two remarks here, remove the delay from the sendData function. Second, your timer will never work because you don’t set a timer interval:

int BlynkTimer = timer.setInterval(5000, sendData);

This will effectively run the sendData() function every 5 seconds. You can put this line in setup() part.

@Lichtsignaal Im trying to change my library from simple timer to Blynk timer. My coonection is through USB Serial. The blynktimer library for USB Serial is
include <BlynkSimpleSerialBLE.h>
Im guessing this is for BLE use. Do you know how should I use for USB Serial?
Also I still have problem for the slider, the value of X i am getting in the email is always 0.

See my post above yours, you need to set a timer to run periodically, that’s how you do it.

The library for USB Serial connection for Blynk is in the Blynk Example Browser, top of this page, Sketch Builder. Select Arduino and see the Serial USB or SoftSerial USB example :slight_smile:

@Lichtsignaal From the timer example for USB Serial, it looks like there is no need for a timer library. However, when I run the code, it say that Blynk timer does not name a type and it refer to this line of code:
BlynkTimer timer;

Also, for this line of code. I realized I have to change it from 70 in the earlier code to X. But I I have to call for X under the Send data Function because X is not declared in that scope. Can you please tell me how to call for it?

 Blynk.virtualWrite(V1,X);

  if(temperatureF > X && notified == 0)
 {