Compare commits

...

2 Commits

Author SHA1 Message Date
ffeb1d3a2c Added i2cScan and ICM-42670P whoami 2024-01-03 12:14:29 +00:00
7455236e8a Added i2cScan and ICM-42670P whoami. 2024-01-03 12:09:03 +00:00

View File

@@ -1,3 +1,6 @@
// #define MAG
#define IMU
#include <Arduino.h> #include <Arduino.h>
#include <AsyncElegantOTA.h> #include <AsyncElegantOTA.h>
@@ -15,22 +18,22 @@
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <SPIFFSEditor.h> #include <SPIFFSEditor.h>
#include <Arduino_JSON.h> #include <Arduino_JSON.h>
#ifdef MAG
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <Adafruit_BusIO_Register.h> #include <Adafruit_BusIO_Register.h>
// #include <Adafruit_MMC56x3.h> #include <Adafruit_MMC56x3.h>
#include <ICM42670P.h> #endif
#include <Preferences.h> #include <Preferences.h>
Preferences settings; Preferences settings;
#define LED_PIN 2 #define LED_PIN 8
/* Assign a unique ID to this sensor at the same time */ /* Assign a unique ID to this sensor at the same time */
// Adafruit_MMC5603 mag = Adafruit_MMC5603(12345); #ifdef MAG
Adafruit_MMC5603 mag = Adafruit_MMC5603(0);
/* ICM42670P*/ String magData;
ICM42670P imu(Wire,0); #endif
//********************************************************* //*********************************************************
// Web server variable declarations // Web server variable declarations
@@ -74,6 +77,8 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
void initSPIFFS(); void initSPIFFS();
bool initWiFi(); bool initWiFi();
String getLEDState(); String getLEDState();
bool initIMU();
void i2cScan();
// ---------------------------------------------- // ----------------------------------------------
void portScan(){ void portScan(){
byte error, address; byte error, address;
@@ -123,40 +128,29 @@ void setup() {
while (!Serial) while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens delay(10); // will pause Zero, Leonardo, etc until serial console opens
if (!Wire.begin(4,5)) { if (!Wire.begin(4,5,400000)) {
while(1){ while(1){
Serial.println("WIRE NOT INIT"); Serial.println("WIRE NOT INIT");
delay(500); delay(500);
} }
} }
portScan(); i2cScan();
Serial.println("init IMU");
/* Initialise the sensor */ Serial.println("");
// Serial.println("Adafruit_MMC5603 Magnetometer Calibration"); initIMU();
// Serial.println("");
// if (!mag.begin(MMC56X3_DEFAULT_ADDRESS, &Wire)) { // I2C mode
// /* There was a problem detecting the MMC5603 ... check your connections */
// Serial.println("Ooops, no MMC5603 detected ... Check your wiring!");
// while (1) delay(10);
// }
// Initializing the ICM42670P /* Initialise the mag sensor */
int ret = imu.begin(); #ifdef MAG
if (ret != 0) { if (!mag.begin(MMC56X3_DEFAULT_ADDRESS, &Wire)) { // I2C mode
Serial.print("ICM42670P initialization failed: "); /* There was a problem detecting the MMC5603 ... check your connections */
Serial.println(ret); Serial.println("Ooops, no MMC5603 detected ... Check your wiring!");
while(1); while (1) delay(10);
} }
// Accel ODR = 100 Hz and Full Scale Range = 16G #endif
imu.startAccel(100,16);
// Gyro ODR = 100 Hz and Full Scale Range = 2000 dps
imu.startGyro(100,2000);
// Wait IMU to start
delay(100);
//***************************************** //*****************************************
//Init OTA //Init OTA
AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA
@@ -357,30 +351,30 @@ server.addHandler(new SPIFFSEditor(SPIFFS,http_username,http_password));
server.begin(); server.begin();
} }
#ifdef MAG
String readMAG() {
sensors_event_t event;
mag.getEvent(&event);
magData = "Mag: X";
magData += String(event.magnetic.x);
magData += " Y";
magData += String(event.magnetic.x);
magData += " Z";
magData += String(event.magnetic.x);
magData += "\n";
return magData;
}
#endif
void loop() { void loop() {
inv_imu_sensor_event_t imu_event; #ifdef MAG
Serial.println(readMAG());
// Get last event #endif
imu.getDataFromRegisters(&imu_event); digitalWrite(LED_PIN,HIGH);
delay(500);
// Format data for Serial Plotter digitalWrite(LED_PIN,LOW);
Serial.print("AccelX:"); delay(500);
Serial.println(imu_event.accel[0]);
Serial.print("AccelY:");
Serial.println(imu_event.accel[1]);
Serial.print("AccelZ:");
Serial.println(imu_event.accel[2]);
Serial.print("GyroX:");
Serial.println(imu_event.gyro[0]);
Serial.print("GyroY:");
Serial.println(imu_event.gyro[1]);
Serial.print("GyroZ:");
Serial.println(imu_event.gyro[2]);
Serial.print("Temperature:");
Serial.println(imu_event.temperature);
// Run @ ODR 100Hz
delay(100);
} }
// functions // functions
@@ -558,3 +552,58 @@ String getLEDState(const String& var) {
} }
return String(); return String();
} }
bool initIMU(){
bool isOK=false;
int out = 0;
Wire.beginTransmission(0x68);
Wire.write(0x75);
Wire.endTransmission();
Wire.requestFrom(0x68,1);
if(Wire.available()){
out = Wire.read();
isOK = true;
}
Serial.println(out);
return isOK;
}
void i2cScan(){
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 100; address < 127; address++ )
{
Serial.println(address);
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
}