ESP32 + I2C Temp+Hum sensor - Blynk

"Hello
I am really giving up on this soon. I hate coding, i just like the hardware :slight_smile:

I am trying to get the I2c function active on my ESP32 but NOTHING seems to work. I have checked several libraries and sketches but none of them explains how to read temp + humidity and then sending this to a virtual pin in blynk.

The project all-in-all should be something like this:

Read temp + hum on SHT21 (SDA-PIN21, SCL-PIN22)
Read motionsensor

4xrelay card
1 = 12vdc Fans, activated either by button (done) or if temp>22C
2 = 12vdc Fans toilet, activated by button if crisis occurs :slightly_smiling_face:
= DONE
3 = 12vdc LED strip inside ceilning, activated by button = DONE
4 = 12vdc LED strip outside, activated by button = DONE

Next step:
If Motionsensor = activated, then start a videorecording and save to SD Card for approx 5 minutes.

Now my code is really f***ed up since i have tried so many things, so please help me sort this out :slight_smile:

Hardware:
ESP32 Devkit 1
SHT21 sensor
Smartphone = Huawei P30 Pro, ā€œlatest versionā€
Blynk Server
Blynk library version = I have no idea.
Camera = not bought yet
SD Card reader

Wifidetails and blynk auth is replaced with *****

=========================================

#define SEALEVELPRESSURE_HPA (1013.25)
#define BLYNK_PRINT Serial

#include <SimpleTimer.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include "SHT21.h"

SHT21 SHT21;




void sendSensor()
{
    float h = SHT21_H.readHumidity();
    float t = SHT21_H.readTemperature();
  
    Blynk.virtualWrite(V5, h);
    Blynk.virtualWrite(V6, t);

}






void setup()
{
  SHT21.begin();
  Serial.begin(9600);


// You should get Auth Token in the Blynk App.

char auth[] = "*********";//Enter your Auth token here

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";    //SSID
char pass[] = "*****";//Password



  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);

  
}

void loop()
{
  Blynk.run(); // Initiates Blynk

}

The code in your sendSensor function is never being called.
The normal procedure would be to initialise a Blynk Timer, and set-up that timer in your void loop so that it calls the sendSensor function every few seconds (the frequency is defined in milliseconds, so 5000 ms = 5 seconds).
You then add an additional line into your void loop to run the timer.
Looking through examples like this:
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FPushData
Will give you the syntax for these commands.

Iā€™d strongly recommend that you initialise a serial output and use serial print commands to output the values from your sensors to your PC. You should also include the line:

#define BLYNK_PRINT Serial

at the start of your sketch so that you get information from the Blynk library about the connection process.

If youā€™re not getting data from your I2C sensor then you may have it connected incorrectly or it may be on a different address to the one expected by your library. Running an I2C scanner sketch is a good way to ensure that your device is connected correctly and to determine which bus address itā€™s using.

Pete.

Hi Pete, Thanks,

But i still have no clue what to do, below is the Blynk example code, with timer, but it looks like the timer is not doing anything, so how should my data be included in the ā€œuptimeā€?

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 myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

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);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

This declares the ā€˜timerā€™ object, so that it can be referenced later.

This declares an instance of the ā€˜timerā€™ object and defines what it does and when it does it. In this case it will call the myTimerEvent function once every second (1000 milliseconds).

Contrary to what the comment next to this says, it doesnā€™t initialise the timer, it ensures that the timer library code checks if any action is needed every time the void loop executes. If it discovered that one of the timer instances is due to go off and run a function then it willensure that this happens.

In your original code, you would need to add a line to your void setup like this:

timer.setInterval(5000L, readSensor);

if you wanted to take a humidity and temperature reading and send this data to Blynk every 5 seconds.

You would also need to add-in the other lines that declared the timer object and runs it in the void loop.

Pete.

1 Like

Hi Pete
Thanks again, but still no luckā€¦

When i added the "'timer.setIntercal(1000L, readSensor);
I got this: Blynk_I2C:36:26: error: ā€˜readSensorā€™ was not declared in this scope

  • timer.setInterval(1000L, readSensor);*

You would also need to add-in the other lines that declared the timer object and runs it in the void loop.

This is what i mean, i have no idea what to add or how to find the right information, i have followed alot of guides, but nothing works getting the data to a virtualpin, or, maybe i can do it to a normal digitalpin, then i can read from that in Blynk?

Thatā€™s probably because I said:

Iā€™ve explained as much as Iā€™m going to about how to declare and use a timer. Iā€™d suggest that you go back and re-read what Iā€™ve written. All the information you need is there, you just need to read it carefully.

Pete.

Wow, thanks alot, really helpful being rude.

I have only been using examples that should be ā€œcomplete for idiotsā€. But shurely i am to stupid for this.

If anyone else likes to have a go, and have a kindergarten-education, please help.

Original code, now with the timer function that does not function, and hasnt functioned:


#define SEALEVELPRESSURE_HPA (1013.25)
#define BLYNK_PRINT Serial

#include <SimpleTimer.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include "SHT21.h"

SHT21 SHT21;


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 myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V4, millis() / 1000);
}

void sendSensor()
{
    float h = SHT21.readHumidity();
    float t = SHT21.readTemperature();
  
    Blynk.virtualWrite(V5, h);
    Blynk.virtualWrite(V6, t);

}






void setup()
{
  SHT21.begin();
  Serial.begin(9600);


// You should get Auth Token in the Blynk App.

char auth[] = "*********";//Enter your Auth token here

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";    //SSID
char pass[] = "*****";//Password



  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);

  
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Init timer

}

Itā€™s not rude at all.
Iā€™ve volunteered my time and knowledge to you to help you get started with this and youā€™ve not taken the time and effort to read what Iā€™ve written and implement it in your code.

If you canā€™t compare the two sketches that youā€™ve posted and read m,y detailed explanation about how the timer works in the second sketch, then transfer the relevant lines (all of which Iā€™ve quoted separately) into your first sketch then youā€™re not yet ready for basic C++ programming.

There are many online C++ tutorials in a variety of different formats. Iā€™m sure that if you found one that suits your personal learning style and spent three or four hours following the tutorial then youā€™d be in a much better place to write code that compliments your hardware skills.

BTW, you used commas instead of triple backticks at the beginning and end of your code in your latest post. Iā€™ve corrected this for you, but it helps other people if you remember to add these correctly, and you may find that your code is removed by one of the other moderators if you donā€™t remember to follow the community rules.

Good luck!

Pete.

3 Likes

Ohh, pick meā€¦ pick meā€¦ I gots crayons :stuck_out_tongue:

3 Likes