Please could someone tell me how to use the virtual write function from an analog pin?
the reading I want to send to a virtual pin is from an MQ7 Gas Sensor. The reason I want to do this is because the analog pin is not available in the Blynk app on my ESP32 DevKit 1.
I have tried the following code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "2V85xWYJCdG7zCBuNatn9Z32SOu5THjB";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Home";
char pass[] = "H@1l3yB3@r21";
#define DHTPIN 4 // What digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
BLYNK_WRITE(V2)
{
int val;
val=analogRead(4);//Read Gas value from analog 0
Serial.println(val,DEC);//Print the value to serial port
delay(100);
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}```
@StonerStuart 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:
```
I think you should use the Blynk.virtualWrite(V2, val); BLYNK_WRITE is for device receiving from Server values. But you probably should declare the gas sensor in the sketch as well.
When i define this MQ7 sensor, is there a set list of sensors i must go through that have settings in them? or do i create the MQ7 by defining it? in other words does the code go look for settings for the defined items or does each definition require me to make the settings? i dont know if im asking this question right!
You don’t need to declare the sensor if you’re just taking an analog pin reading then doing the rest of the calculation (converting the analog reading into a PPM value and applying a calibration factor) yourself.
If you use an MQ7 library to do the heavy lifting for you then you’ll need to install the library in the IDE, include the library in your sketch, then declare the sensor object in whatever way is required by the library that you use. Take a look at the documentation and code examples for the library if you go down that route.
BTW, trying to read a DHT11 sensor once every second (1000ms) isn’t going to work well.
Increasing this to every 5 seconds will give much better results.
How can i use deep sleep? i want this to work for at least one day, you will all be shocked to see how many batteries this ESP32 is running on and running flat overnight, charged in the day by solar. it is 10x 18650 batteries.
I only want a raw reading from the sensor, a 0-1023 number. its attached to analog pin A4, can anyone see where this is wrong? it compiles but there is no reading in the app on pin V4.
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include "MQ7.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "2V85xWYJCdG7zCBuNatn9Z32SOu5THjB";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Home";
char pass[] = "H@1l3yB3@r21";
#define DHTPIN 4 // What digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
#define A_PIN 2
#define VOLTAGE 5
// init MQ7 device
MQ7 mq7(A_PIN, VOLTAGE);
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
int Gas = mq7.readPpm();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
Blynk.virtualWrite(V2, Gas);
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
dht.begin();
mq7.calibrate();
// Setup a function to be called every second
timer.setInterval(5000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}
I see nothing connected to V4 in you sketch. Is it correct? Try to redefine Virtual pin either in your app or in your sketch to match values of your data stream.
Get it working normally first, then look at Deep Sleep later.
Are you wanting the gas levels that you are monitoring with the MQ7 sensor to be uploaded just for information, or is this an alert system as well? If you want alerts then long periods of deep sleep aren’t going to be compatible with that approach.
Also, I do t know what sort of warm-uptime the MQ7 has, have you looked into this?
You seem to have mixed the two options I provided earlier - using a library or taking a raw reading - you are doing both!
This comment makes no sense, especially when using Blynk Legacy.
What do you mean? When I has wrote something to V0 at MCU side, I should read it also at V0 at application side. AFAIK it is impossible to write sensor data at V2 and read it at V4.
I.e.
sensor → MCU pin (say, Analog pin A4) → any virtual pin (say V8) → internet → the same virtual pin at app (V8) → terminal (or other widget)
The MCU is taking readings from sensors. These are temperature, humidity, and a voltage reading from the gas sensor.
These are being sent to the app, via the server, on pins V5, V6 and V2.
No additional code is needed on the MCU, all that is needed is suitable widgets attached to V5, V6 and V2 in the app. As @StonerStuart hasn’t shared any screenshots of his app setup, we have to assume that he’s set it up correctly.
Datastreams are a Blynk IoT concept, and don’t apply to this situation, so only confuse the issue.
Sorry Pete, English isn’t my native language. Probably I’m wrong with terms, but I want to say the same - if gas sensor tied to V2, it should be read on V2 at other side. Not on V4, as @StonerStuart mentioned - in this case no information from gas sensor will be printed at application’s display.