I am brand new to Blynk. I am hoping to connect an ESP32 with a Thermistor to Blynk so I can watch temperatures when away from the office. I am having a hard time using either virtual or analog pins and having them register. The thermistor does need to do some calculations to be accurate so I am trying to incorperate those but cannot seem to get the value to transfer to Blynk. I appreciate any help anyone can give on this project. It works when hardwired so I’m wondering if the lack of Blynk experience on my side could be the issue.
#define BLYNK_TEMPLATE_ID "TMPL25YFiL2sE"
#define BLYNK_TEMPLATE_NAME "Temp Sensor"
#define BLYNK_AUTH_TOKEN "n2L_JULkdujCJr9N_zq8YwfqX2asryRc"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <SimpleTimer.h>
#include <WiFiClient.h>
//#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp32.h>
int sensorVal;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = ""; // type your wifi name
char pass[] = ""; // type your wifi password
#define ThermistorPin 23
#define AmbientThermistorPin 32
BlynkTimer timer;
void sensorDataSend()
{
int Vo;
int Vi;
float R1 = 10000;
float logR2, R2, T, Tc, Tf, Tca, Tfa;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
Vo = analogRead(ThermistorPin);
R2 = R1 * (4095.0 / (float)Vo - 1.0);// for Arduino use 1023 for ESP32 use 4095
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Tc = T - 273.15;
Tf = (Tc * 9.0)/ 5.0 + 32.0;
Serial.print("WoodTemperature: ");
Serial.print(Tf);
Serial.print(" F; ");
Serial.print(Tc);
Serial.println(" C");
Vi = analogRead(AmbientThermistorPin);
R2 = R1 * (4095.0 / (float)Vi - 1.0);// for Arduino use 1023 for ESP32 use 4095
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Tca = T - 273.15;
Tfa = (Tc * 9.0)/ 5.0 + 32.0;
Serial.print("AmbientTemperature: ");
Serial.print(Tfa);
Serial.print(" F; ");
Serial.print(Tca);
Serial.println(" C");
//Blynk.virtualWrite(V0, sensorValue); // sending sensor value to Blynk app
BLYNK_WRITE(V0)
}
//thermistorPin = param.asInt();
void setup() {
Serial.begin(115200);
timer.setInterval(1000L, sensorDataSend); //timer will run every sec
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run(); // run Blynk magic
timer.run(); // run timer every second
}
@Rand Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
Pete.
Thanks for the guidance. Hopefully things look good now.
Okay, you have a number of issues with your code…
Pin 23 is not an analog input on the ESP32.
You need too choose one of the pins attached to ADC1, which are these…
- ADC1_CH0 (GPIO 36)
- ADC1_CH1 (GPIO 37)
- ADC1_CH2 (GPIO 38)
- ADC1_CH3 (GPIO 39)
- ADC1_CH4 (GPIO 32)
- ADC1_CH5 (GPIO 33)
- ADC1_CH6 (GPIO 34)
- ADC1_CH7 (GPIO 35)
and you obviously cant use GPIO32 as this is already in use…
Your SensorDataSend()
function takes readings and performs calculations on the results to give four values, which are assigned to variables called Tf
& Tc
and Tfa
and Tca` giving the two temp[eratures in Celcius and Farenheit.
But, instead of sending these values (or maybe just two of them depending if you prefer Celsius or Fahrenheit) to Blynk, you’re actually sending a variable called sensorValue
which was never defined and would have given you a compilation error.
You do declare a global variable called sendorVal
at the top of your code…
but this is never used for anything, so probably needs to be deleted.
So, you should have a couple of lines of code at the end of SensorDataSend()
that look like this…
Blynk.virtualWrite(V0, Tc);
Blynk.virtualWrite(V1, Tc);
This assumes that you have set-up virtual datastreams V0 and V1 with min-max values that suit your expected temperature value ranges and attached widgets to these datastreams to display the values.
You also need to remove this line of code…
and this comment…
You obviously don’t understand how the BLYNK_WRITE()
function works in Blynk, and why you don’t use it to write data to Blynk (despite its name).
BLYNK_WRITE(vPin)
is triggered when a datastream value changes on the Blynk server and is usually used to get your sketch to respond to a widget such as the Switch widget being toggled in the app.
As this is something that you aren’t trying to do in your sketch at the moment that piece of code needs removing.
Pete.
Firstly and fore mostly, thank you for taking the time to look at my code and give feedback. I know you get a bunch of daily requests so taking the time out to help address my issues is appreciated.
Seeing as I am looking for similar examples, and more or less cutting and pasting them together to get a code that works for me, is there a place anywhere online that teaches incrementing classes on how to use blynk to the fullest? Id really like to get good at this platform but would like to learn from a progressive curriculum.
There are a number of independents who have YouTube channels covering Blynk stuff, but my advice would be to setter clear of them for a variety of reasons.
Some are based on the old Legacy version of Blynk and therefore aren’t relevant now, others don’t always follow good coding practice.
The best sources of info are the Blynk examples that are installed with the library (but steer clear of the Edgent examples at this stage as they are extremely complex), the Blynk documentation, and this forum.
It’s also worth adding that using thermistors isn’t the mainstream method of measuring temperature, so unless you halve a very specific reason to use this approach then you’d be better choosing something that’s digital rather than analog (but avoid the DHT range of sensors, they are awful).
Pete.
Yes I have noticed that the DHT sensors lack the accuracy and durability of the thermistor I am currently using and due to me incorporating it into a system that is hard to get to, I opted for longevity. Had I known the complexity it would add, I may have rethought my process.
The problem with Analog devices is that they are analog, and therefore prone to inaccuracy due to cable runs, interference, calibration drift etc
Digital sensors overcome these issues to a large extent, so I always digital wherever I can.
I tend to use SHT30 temperature and humidity sensors, as they come in a variety of physical enclosures. I use the waterproof version in my outdoor weather station, and you can get versions designed for measuring soil temperature and humidity.
A bit of research and planning before you choose your hardware can make your project much more reliable.
Pete.
Thanks again Pete for your expertise and advice. Due to your suggestions, I have been able to get my code working on Blynk. I will be looking into the Blynk examples you have suggested earlier as this project has really enticed my interest in the union between Arduino and Blynk. This is very cool!!! I am thirsty to learn more so thank you once again for all of your help. You really make something this complicated seem feasible and possible for hobbyist like myself.
One last question for you on your Saturday. Why is it so many people seem to select the ESP8266 over the ESP32? It seems the majority of the code here is directed towards the ESP8266.
The ESP8266 has been around longer.
There are very few differences in terms of code, so its very easy to convert an ESP8266 sketch to an ESP32 one.
Pete.
Makes sense.
Thanks Again.