Blynk 2.0 configuration for XIAO ESP32 C3

I want to convert my below code to blynk.

#include <MPU6050_tockn.h>
#include <Wire.h>
#include <EEPROM.h>

MPU6050 mpu6050(Wire);

// EEPROM address to store reference angles
int eeAddressX = 0;
int eeAddressY = sizeof(float);
int eeAddressZ = 2 * sizeof(float);

// Variables to store reference angles
float refAngleX = 0.0;
float refAngleY = 0.0;
float refAngleZ = 0.0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Read reference angles from EEPROM
  EEPROM.get(eeAddressX, refAngleX);
  EEPROM.get(eeAddressY, refAngleY);
  EEPROM.get(eeAddressZ, refAngleZ);
}

void loop() {
  mpu6050.update();
  Serial.print("angleX : ");
  Serial.print(mpu6050.getAngleX() - refAngleX);
  Serial.print("\tangleY : ");
  Serial.print(mpu6050.getAngleY() - refAngleY);
  Serial.print("\tangleZ : ");
  Serial.println(mpu6050.getAngleZ() - refAngleZ);
  
  // Check if reset button is pressed
  if (digitalRead(D9) == LOW) {
    delay(50);  // Debounce delay
    if (digitalRead(D9) == LOW) {
      calibrateAngles();  // Reset reference angles to 0.0
    }
  }
}

void saveReferenceAngles() {
  EEPROM.put(eeAddressX, refAngleX);
  EEPROM.put(eeAddressY, refAngleY);
  EEPROM.put(eeAddressZ, refAngleZ);
}

void calibrateAngles() {
  const int readings = 100;
  float sumX = 0.0;
  float sumY = 0.0;
  float sumZ = 0.0;

  for (int i = 0; i < readings; i++) {
    mpu6050.update();
    sumX += mpu6050.getAngleX();
    sumY += mpu6050.getAngleY();
    sumZ += mpu6050.getAngleZ();
    delay(10);
  }

  refAngleX = sumX / readings;
  refAngleY = sumY / readings;
  refAngleZ = sumZ / readings;

  saveReferenceAngles();
}
  1. If I use edgenet, what should be the board configuration for XIAO esp32 C3
  2. If I simply use <BlynkSimpleEsp32.h>, then how should I reconfigure/connect the project in BLYNK 2.0?
  3. Since this sketch uses EEPROM, will it affect BLYNK functioning? i.e. in storage of wifi credentials etc?

Try one of the example sketches for ESP32 in the Sketch Builder, nut I’d recommend using the SSL ones rather than the one that use <BlynkSimpleEsp32.h>.
Once you have the board working then add-in your code to the example sketch. You’ll wnat to clean-up your void loop though…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

If you use Edgent then you’ll need to be careful because Edgent also uses EEPROM. If you search the forum you’ll find a few posts about how to avoid overwriting the Edgent saved settings.

You will probably want to define your own board type in Settings.h if you use Edgent, or modify the Custom board type.

Is there any reason why you’re using the SEEED Studio board for this project?
I use them for a battery p[owered project that requires a small footprint board, but its not a board that I’d normally choose.

Pete.

1 Like

Thanks for the prompt response, SSL sketch worked. Does this sketch use EEPROM?

Without knowing which example you’ve used it’s difficult to say, but it should be very simple for you to work-out.

Pete.

here’s my updated code:
/*************************************************************

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
 (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
//#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
//#define BLYNK_TEMPLATE_NAME         "Device"
//#define BLYNK_AUTH_TOKEN            "YourAuthToken"

#define BLYNK_TEMPLATE_ID "TMPL6NXNsQ1YH"
#define BLYNK_TEMPLATE_NAME "CD"
#define BLYNK_AUTH_TOKEN "GTo8YrNbZk3xkHbZujzHUfIKWqrawYvx"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>

#include <MPU6050_tockn.h>
#include <Wire.h>
#include <EEPROM.h>


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "vivo 1713";
char pass[] = "ablgaozra";



int btnpin = D9;

MPU6050 mpu6050(Wire);

// EEPROM address to store reference angles
int eeAddressX = 0;
int eeAddressY = sizeof(float);
int eeAddressZ = 2 * sizeof(float);

// Variables to store reference angles
float refAngleX = 0.0;
float refAngleY = 0.0;
float refAngleZ = 0.0;

BlynkTimer timer;



void cdi(){
   mpu6050.update();
  Serial.print("angleX : ");
  Serial.print(mpu6050.getAngleX() - refAngleX);
  Serial.print("\tangleY : ");
  Serial.print(mpu6050.getAngleY() - refAngleY);
  Serial.print("\tangleZ : ");
  Serial.println(mpu6050.getAngleZ() - refAngleZ);

  Blynk.virtualWrite(V1,mpu6050.getAngleZ() - refAngleX);
  Blynk.virtualWrite(V2,mpu6050.getAngleZ() - refAngleY);
  Blynk.virtualWrite(V3,mpu6050.getAngleZ() - refAngleZ);
  
  // Check if reset button is pressed
  if (digitalRead(btnpin) == LOW) {
delay(50);  // Debounce delay
if (digitalRead(btnpin) == LOW) {
  calibrateAngles();  // Reset reference angles to 0.0
}
  }

}


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

 Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  // Read reference angles from EEPROM
  EEPROM.get(eeAddressX, refAngleX);
  EEPROM.get(eeAddressY, refAngleY);
  EEPROM.get(eeAddressZ, refAngleZ);


  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
   timer.setInterval(500L, cdi); 
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 443);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8443);
}

void saveReferenceAngles() {
  EEPROM.put(eeAddressX, refAngleX);
  EEPROM.put(eeAddressY, refAngleY);
  EEPROM.put(eeAddressZ, refAngleZ);
}

void calibrateAngles() {
  const int readings = 100;
  float sumX = 0.0;
  float sumY = 0.0;
  float sumZ = 0.0;

  for (int i = 0; i < readings; i++) {
mpu6050.update();
sumX += mpu6050.getAngleX();
sumY += mpu6050.getAngleY();
sumZ += mpu6050.getAngleZ();
delay(10);
  }

  refAngleX = sumX / readings;
  refAngleY = sumY / readings;
  refAngleZ = sumZ / readings;

  saveReferenceAngles();
}

void loop()
{
  Blynk.run();
   timer.run(); 
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

So is your problem now solved?

Pete.

yes, the data is visible on Blynk app but there’s a lag, I’ll try to figure it out.
Thanks again.