Blynk doesn't send any data when using Adafruit PCA9685 servo controller library

Hi, I’m building a quadruped robot with temperature sensor (DHT11) for my final project. My Blynk code works perfectly until I put on Wire.h and Adafruit PCA9685 libraries. Then the code only shows that the esp-01 module is connected to wifi but didn’t print any values (I set them so that they also print the values on serial monitor). May I know if Blynk is compatible with those libraries? If not, then what should I do?

Can you add your code?
It will be useful to provide advice…

#define BLYNK_PRINT Serial
#include <Wire.h> //if I remove this line, then the code works
#include <ESP8266wifi.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <Adafruit_PWMServoDriver.h> //if I remove this line, then the code works
#include <SoftwareSerial.h>

//Blynk project and wifi settings
char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";

//Blynk app led widgets
WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
WidgetLED led4(V4);
WidgetLED led5(V9);
WidgetLCD lcd(V10);

//ESP serial settings
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

//PCA9685 setup
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

//Pins
#define DHTPIN A0          // What digital pin we're connected to
#define LFIRE A1 // Left IR Fire Sensor
#define FFIRE A2 // Front IR Fire Sensor
#define RFIRE A3 // Right IR Fire Sensor
#define TPING 4 // Trigger pin for Front Sensor
#define BTRIG 7 // ECHO pin for Front Sensor
#define BECHO 8
#define BATT A6 // Battery level reading pin

//DHT Parameters
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);

//Blynk internal timer
BlynkTimer timer;

//Spray limit is set to 8
int SLimit=8;

//Spray count reset after every startup
int SCount=0;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  //Fire sensor readings
  float LFireval = analogRead(LFIRE); //Left Fire
  float FFireval = analogRead(FFIRE); //Front Fire
  float RFireval = analogRead(RFIRE); //Right Fire
  if (LFireval < 500){
    led1.on();
  }
  else {
    led1.off();
  }
  if (FFireval < 500){
    led2.on();
  }
  else {
    led2.off();
  }
  if (RFireval < 500){
    led3.on();
  }
  else {
    led3.off();
  }
  
  long Bdur;
  int Bdist;
  
  long Tdur;
  int Tdist; 
  boolean Gest; 

  pinMode(BTRIG, OUTPUT);
  digitalWrite(BTRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(BTRIG, HIGH);
  delayMicroseconds(5);
  digitalWrite(BTRIG, LOW);
  pinMode(BECHO, INPUT);
  Bdur = pulseIn(BECHO, HIGH);
  Bdist = 0.034*Bdur/2; //speed of sound is 0.034 cm/microseconds  

  if (Bdist < 10){
    led4.on(); //Indicate that obstacle is detected
  }
  else{
    led4.off();
  }

  pinMode(TPING, OUTPUT);
  digitalWrite(TPING, LOW);
  delayMicroseconds(2);
  digitalWrite(TPING, HIGH);
  delayMicroseconds(5);
  digitalWrite(TPING, LOW);
  pinMode(TPING, INPUT);
  Tdur = pulseIn(TPING, HIGH);
  Tdist = 0.034*Tdur/2; //speed of sound is 0.034 cm/microseconds  
  if (Tdist < 10 && Tdist!=0){
    led5.on(); //Indicate that gesture is detected
    Gest = true;
  }
  else{
    led5.off();
    Gest = false;
  }
  
  Serial.print("Left Fire: ");
  Serial.print(LFireval);
  Serial.print("  Front Fire: ");
  Serial.print(FFireval);
  Serial.print("  Right Fire: ");
  Serial.println(RFireval);
  Serial.print("Bottom Distance: ");
  Serial.print(Bdist);
  Serial.print("  Gesture: ");
  Serial.println(Gest);  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println("°C");
    Serial.println(" ");
  }
  //write to Blynk app
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  timer.setInterval(100L, disp2);
}
void disp(){
  lcd.clear();
  lcd.print(0,0,"CoFiBot V2");
  lcd.print(0,1,"PET");
}
void disp2(){
  lcd.print(0,0,"CoFiBot V2");
  lcd.print(0,1,"RUNNING");
}
void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
  dht.begin();
  // Setup a function to be called every second
}

void loop()
{
  Blynk.run();
  sendSensor();
  timer.run();
}

The void loop() tries to run as fast as possible… hundreds/thousands of times a second. And calling a function sendSensor(); from the void loop like this will also attempt to run that function’s contents as many times/second, including commands like this…

Aside from running the Blynk Write commands (and read from the sensor) waaaay too fast… it is also trying to set that timer multiple times before it can even trigger the first time.

Basically that entire function should only be called by another timer, only as fast as needed but not so fast as to cause Blynk disconnections and other issues.

Thanks for the input. I’ve tried setting up the sendSensor(); to be called using time.setInterval command on void setup(), but my device still disconnects right after connecting to my blynk app.

As for the compatibility with both Wire.h and PCA9685 servo controller, do you have any idea about it? Or my code simply doesn’t work because of the disconnections?

I don’t know. The wire.h library (of which I believe there are many of same name?) shouldn’t affect Blynk (I have used many i2c devices) but I have not used that servo controller.

I would recommend taking Blynk out of the equation (use serial prints to monitor what happens) and test that way first.

Perhaps you have another i2c device that is conflicting? Or as I mentioned, I think there are many wire.h libraries, and not all created equal… but I might be thinking of another common library???

I just tried to run the non-blynk sketch and it works. Do you think this snippet is still too fast for blynk?

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  disp2();

which is run using this

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
  dht.begin();
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

Or should I add a delay on the sendSensor() part?

Then, assuming your test did everything the same, except use Blynk, I am out of ideas… I have never used that expander board, but I am sure a forum search for it will show if others have, and successfully or not.

Blynk recommends no more the 10 Blynk Writes per second… so all is fine there.

However, a DHT11 is pathetically slow and probably shouldn’t be called more than once every 10 seconds, and even that fast for testing only… If your temperatures change that fast, then you are using the wrong tools :stuck_out_tongue:

Blynk recommends no more the 10 Blynk Writes per second… so all is fine there.

Tried to put 1000L on the timer.setInterval, still no luck though

However, I noticed that my esp-01 can’t seem to ‘hold’ the connection when I included those 2 libraries (wire and pca9685). Could it be a power issue? I’m using a 7.4v battery pack and an LM 2596 regulator set to 3.3V.

I’ve got a DHT22 and a PCA9685 like yours,if you are Ok, I can do some tests and come back to you this afternoon… can you share also the QR of that project?

Sure, appreciate that.

However, I can’t share the QR since I don’t have enough Blynk energy

Oh, I’m sorry, my fault, it’s not sharing, it’s just the QR of your project, the clone.

That’s fine, I don’t know if that feature existed lol

Here’s the qr

1 Like

Any update on the test?