#include <Wire.h>
#define I2C_Freq 100000
#define SDA_0 9
#define SCL_0 10
#define BMP180_ADDRESS 0x77
#define SCREEN_ADDRESS 0x3C
#define SCREEN2_ADDRESS 0x3D
#define AHT10_ADDRESS 0x38
#define DS1307_ADDRESS 0x68
#define MPU6050_ADDRESS 0x69
bool sensor_bmp85_present = false;
bool sensor_aht10_present = false;
bool display_1306_present = false;
bool display2_1306_present = false;
bool sensor_ds1307_present = false;
bool sensor_mpu6050_present = false;
TwoWire I2C_0 = TwoWire(0);
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 1
#define DATA_PIN 8
// Define the array of leds
CRGB leds[NUM_LEDS];
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-C3!");
I2C_0.begin(SDA_0 , SCL_0 , I2C_Freq);
scani2c();
if (sensor_mpu6050_present) {
// Initialize the MPU-6050 and test if it is connected.
I2C_0.beginTransmission( MPU6050_ADDRESS);
I2C_0.write( 0x6B); // PWR_MGMT_1 register
I2C_0.write( 0); // set to zero (wakes up the MPU-6050)
auto error = I2C_0.endTransmission();
if ( error != 0)
{
Serial.println(F( "Error, MPU-6050 init failed"));
leds[0] = CRGB(254, 0, 0);
FastLED.show();
// for(;;); // halt the sketch if error encountered
}
} else {
Serial.println("MPU-6050 not found! Skip it...");
}
if (sensor_ds1307_present) {
if (! rtc.begin(&I2C_0)) {
Serial.println("Failed ds1307 clock initialization");
Serial.println("TODO: and what we gone do now?!");
// for (;;); // Don't proceed, loop forever
} else {
Serial.println("ds1307 init...");
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void checki2c(uint8_t adresok) {
switch (adresok) {
case BMP180_ADDRESS:
sensor_bmp85_present = true;
Serial.printf("I2C BMP180 sensor is found at 0x%02X\n", adresok);
break;
case SCREEN_ADDRESS:
display_1306_present = true;
Serial.printf("I2C Display 1306 is found at 0x%02X\n", adresok);
break;
case SCREEN2_ADDRESS:
display2_1306_present = true;
Serial.printf("I2C Display 1306 is found at 0x%02X\n", adresok);
break;
case AHT10_ADDRESS:
sensor_aht10_present = true;
Serial.printf("I2C AHT10 sensor is found at 0x%02X\n", adresok);
break;
case MPU6050_ADDRESS:
sensor_mpu6050_present = true;
Serial.printf("I2C MPU6050 sensor is found at 0x%02X\n", adresok);
break;
case DS1307_ADDRESS:
sensor_ds1307_present = true;
Serial.printf("I2C DS1307 clock is found at 0x%02X\n", adresok);
break;
case 4:
break;
case 5:
break;
default:
Serial.printf("I2C device found at address 0x%02X\n", adresok);
break;
}
}
void scani2c() {
byte error, address;
int nDevices = 0;
//clean flags before scan
sensor_bmp85_present = false;
sensor_aht10_present = false;
display_1306_present = false;
sensor_mpu6050_present = false;
Serial.println("Scanning for I2C devices...");
for (address = 0x01; address < 0x7f; address++) {
// Serial.print(">");
I2C_0.beginTransmission(address);
error = I2C_0.endTransmission();
if (error == 0) {
Serial.printf("I2C device found at address 0x%02X\n", address);
checki2c(address);
nDevices++;
} else if (error != 2) {
Serial.printf("Error %d at address 0x%02X\n", error, address);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found");
}
}