Added i2cScan and ICM-42670P whoami.

This commit is contained in:
2024-01-03 12:09:03 +00:00
parent e840da2134
commit 7455236e8a

View File

@@ -1,3 +1,6 @@
// #define MAG
#define IMU
#include <Arduino.h>
#include <AsyncElegantOTA.h>
@@ -15,18 +18,22 @@
#include <ESPAsyncWebServer.h>
#include <SPIFFSEditor.h>
#include <Arduino_JSON.h>
#ifdef MAG
#include <Adafruit_Sensor.h>
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_MMC56x3.h>
#endif
#include <Preferences.h>
Preferences settings;
#define LED_PIN 2
#define LED_PIN 8
/* 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);
String magData;
#endif
//*********************************************************
// Web server variable declarations
@@ -70,6 +77,8 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
void initSPIFFS();
bool initWiFi();
String getLEDState();
bool initIMU();
void i2cScan();
// ----------------------------------------------
void setup() {
@@ -80,25 +89,29 @@ void setup() {
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
if (!Wire.begin(4,5)) {
while(1){
Serial.println("WIRE NOT INIT");
delay(500);
}
}
Serial.println("Adafruit_MMC5603 Magnetometer Calibration");
Serial.println("");
if (!Wire.begin(4,5,400000)) {
while(1){
Serial.println("WIRE NOT INIT");
delay(500);
}
}
i2cScan();
Serial.println("init IMU");
Serial.println("");
initIMU();
/* Initialise the sensor */
/* Initialise the mag sensor */
#ifdef MAG
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);
}
#endif
//*****************************************
//Init OTA
AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA
@@ -300,7 +313,29 @@ server.addHandler(new SPIFFSEditor(SPIFFS,http_username,http_password));
}
#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() {
#ifdef MAG
Serial.println(readMAG());
#endif
digitalWrite(LED_PIN,HIGH);
delay(500);
digitalWrite(LED_PIN,LOW);
delay(500);
}
// functions
@@ -478,3 +513,58 @@ String getLEDState(const String& var) {
}
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");
}