Last active
February 23, 2023 02:18
-
-
Save Tech500/ad921c260f0661ef15851b2fcaf12312 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ESP32 MPU6050 Accelerometer Gyroscope Sensor utility | |
Developed by William Lucid with assist from OpenAI's ChatGPT 02/22/2023 | |
Collects 40 "passes" of collecting MPU6050 data; then FTP server becomes available for reading data from LittleFS | |
log file. FTP works well with Fillza. | |
*/ | |
// Based on Adafruit MPU6050 library example, "Basic_demo.ino" for accelerometer readings. | |
#include <WiFi.h> | |
#include <LittleFS.h> | |
#include <FTPServer.h> | |
#include <Adafruit_MPU6050.h> | |
#include <Adafruit_Sensor.h> | |
#include <Wire.h> | |
const char * ssid = "SSID"; | |
const char * password = "PASSWORD"; | |
//Server settings | |
#define ipaddress {10,0,0,23} | |
#define subnet {255,255,255,0} | |
#define gateway {10,0,0,1} | |
#define dns {10,0,0,1} | |
Adafruit_MPU6050 mpu; | |
// tell the FtpServer to use LittleFS | |
FTPServer ftpSrv(LittleFS); | |
int flag = 0; | |
int count= 0; | |
void setup(void) { | |
Serial.begin(115200); | |
while (!Serial) | |
delay(10); // will pause Zero, Leonardo, etc until serial console opens | |
Wire.begin(21, 22); | |
WiFi.begin(ssid, password); | |
bool fsok = LittleFS.begin(); | |
Serial.printf_P(PSTR("FS init: %s\n"), fsok ? PSTR("ok") : PSTR("fail!")); | |
wifi_Start(); | |
// Setup the ftp server with username and password | |
// ports are defined in FTPCommon.h, default is | |
// 21 for the control connection | |
// 50009 for the data connection (passive mode by default) | |
ftpSrv.begin(F("ftp"), F("ftp")); //username, password for ftp. set ports in ESP8266FtpServer.h (default 21, 50009 for PASV) | |
Serial.println("Adafruit MPU6050 test!"); | |
// Try to initialize! | |
if (!mpu.begin()) { | |
Serial.println("Failed to find MPU6050 chip"); | |
while (1) { | |
delay(10); | |
} | |
} | |
Serial.println("MPU6050 Found!"); | |
LittleFS.begin(true); | |
} | |
void loop() { | |
if(flag == 0){ | |
for(int x = 0;x < 40;x++){ | |
count = count + 1; | |
Serial.println(count); | |
logtoFile(); | |
} | |
flag = 1; | |
} | |
if(count == 40){ | |
Serial.println("FTP is available\n"); | |
Serial.println("\n\r"); | |
} | |
count = 0; | |
// this is all you need | |
// make sure to call handleFTP() frequently | |
ftpSrv.handleFTP(); | |
} | |
void logtoFile(){ | |
// Open a log file for appended writing. | |
File log = LittleFS.open("/MPU6050log.txt", FILE_APPEND); | |
if (!log) | |
{ | |
Serial.println("file '/MPU6050log.txt' open failed"); | |
} | |
if(flag == 1){ | |
mpu.setAccelerometerRange(MPU6050_RANGE_2_G); | |
log.print("Accelerometer range set to: "); | |
switch (mpu.getAccelerometerRange()) { | |
case MPU6050_RANGE_2_G: | |
log.print("+-2G"); | |
break; | |
case MPU6050_RANGE_4_G: | |
log.print("+-4G"); | |
break; | |
case MPU6050_RANGE_8_G: | |
log.print("+-8G"); | |
break; | |
case MPU6050_RANGE_16_G: | |
log.print("+-16G"); | |
break; | |
} | |
mpu.setGyroRange(MPU6050_RANGE_250_DEG); | |
log.print(" Gyro range set to: "); | |
switch (mpu.getGyroRange()) { | |
case MPU6050_RANGE_250_DEG: | |
log.print("+- 250 deg/s"); | |
break; | |
case MPU6050_RANGE_500_DEG: | |
log.print("+- 500 deg/s"); | |
break; | |
case MPU6050_RANGE_1000_DEG: | |
log.print("+- 1000 deg/s"); | |
break; | |
case MPU6050_RANGE_2000_DEG: | |
log.print("+- 2000 deg/s"); | |
break; | |
} | |
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); | |
log.print(" Filter bandwidth set to: "); | |
switch (mpu.getFilterBandwidth()) { | |
case MPU6050_BAND_260_HZ: | |
log.print("260 Hz"); | |
break; | |
case MPU6050_BAND_184_HZ: | |
log.print("184 Hz"); | |
break; | |
case MPU6050_BAND_94_HZ: | |
log.print("94 Hz"); | |
break; | |
case MPU6050_BAND_44_HZ: | |
log.print("44 Hz"); | |
break; | |
case MPU6050_BAND_21_HZ: | |
log.print("21 Hz"); | |
break; | |
case MPU6050_BAND_10_HZ: | |
log.print("10 Hz"); | |
break; | |
case MPU6050_BAND_5_HZ: | |
log.print("5 Hz"); | |
break; | |
} | |
log.println(""); | |
flag = 2; | |
} else{ | |
exit; | |
} | |
/* Get new sensor events with the readings */ | |
sensors_event_t a, g, temp; | |
mpu.getEvent(&a, &g, &temp); | |
// Attempting to create RGB values to use with WLED python code | |
// this part is not full working in the way I need; likely due | |
// mapping such alarge range of input min. and max values. | |
int red = map((a.acceleration.x), -32768, 32767, 0, 255); | |
int green = map((a.acceleration.y), -32768, 32767, 0, 255); | |
int blue = map((a.acceleration.z), -32768, 32767, 0, 255); | |
/* Log values */ | |
log.print(a.acceleration.x); | |
log.print(" x,\t "); | |
log.print(a.acceleration.y); | |
log.print(" y, \t"); | |
log.print(a.acceleration.z); | |
log.print(" z, \t"); | |
log.print(red); | |
log.print(" r, \t"); | |
log.print(green); | |
log.print(" g, \t "); | |
log.print(blue); | |
log.print(" b, \t"); | |
log.print(g.gyro.x); | |
log.print(" x, \t"); | |
log.print(g.gyro.y); | |
log.print(" y, \t "); | |
log.print(g.gyro.z); | |
log.print(" z, \t "); | |
log.println(""); | |
log.close(); | |
delay(1000); | |
/* Print out the values */ | |
Serial.print("Acceleration X:\t"); | |
Serial.print(a.acceleration.x); | |
Serial.print(", Y: \t"); | |
Serial.print(a.acceleration.y); | |
Serial.print(", Z: \t"); | |
Serial.print(a.acceleration.z); | |
/* Print out the values */ | |
Serial.print(" Red X: \t"); | |
Serial.print(red); | |
Serial.print(" Green Y: \t"); | |
Serial.print(green); | |
Serial.print(" Blue Z: \t"); | |
Serial.print(blue); | |
Serial.print("\nRotation X: \t"); | |
Serial.print(g.gyro.x); | |
Serial.print(", Y: \t"); | |
Serial.print(g.gyro.y); | |
Serial.print(", Z: \t"); | |
Serial.print(g.gyro.z); | |
Serial.print("\tTemperature: \t"); | |
Serial.print(temp.temperature); | |
Serial.println(" degC"); | |
Serial.println(""); | |
delay(1000); | |
} | |
void wifi_Start() | |
{ | |
//WiFi.mode(WIFI_OFF); | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
delay(100); | |
//WiFi.persistent(true); | |
Serial.println(); | |
Serial.print("MAC: "); | |
Serial.println(WiFi.macAddress()); | |
// We start by connecting to a WiFi network | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
//setting the addresses | |
IPAddress ip; | |
IPAddress gateway; | |
IPAddress subnet; | |
IPAddress dns; | |
WiFi.config(ip, gateway, subnet, dns); | |
Serial.println("network..."); | |
if(WiFi.status() != WL_CONNECTED) // Wait for the Wi-Fi to connect | |
{ | |
WiFi.waitForConnectResult(); | |
} | |
while(WiFi.status() == WL_NO_SSID_AVAIL) | |
{ | |
delay(10 * 1000); | |
Serial.println("No SSID availible"); | |
} | |
Serial.printf("\nConnection result: %d\n", WiFi.waitForConnectResult()); | |
Serial.print("IP Address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Signal strength: "); | |
Serial.println(WiFi.RSSI()); | |
Serial.println("WiFi Connected"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment