Need guidence in implementing ESP8266+MPU6050

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Need guidence in implementing ESP8266+MPU6050

Post by bukaida » Sun Jul 19, 2020 9:44 am

I need to collect the 6DOF accelerometer+Gyroscope data from ESP8266+MPU6050 combo, via a python server which will display the value of Ax,Ay,Az and Gx,Gy,Gz along with UNIX timestamp and also stores them in a file as json with the format like

Code: Select all

{"aX": "-1.532289", "aY": "9.739613", "aZ": "0.5746084","gX": "9.865169", "gY": "-3.9590178", "gZ": "-0.7498371" }
{"aX": "-1.8291701", "aY": "9.701305", "aZ": "0.58418524", "gX": "3.1838295", "gY": "2.5143213", "gZ": "-4.4101415"}
with some kind of noise pre-processing on the device is required. An UDP server will be ideal as acknowledgement is not required, speed is more important. Is it possible to control number of data/second in WiFi ? The transfer speed should atleast be 40 records/second ( I have no idea about the default transfer speed of ESP8266+MPU6050 combo).
I am trying with https://github.com/micropython-IMU/micropython-mpu9x50 and https://github.com/micropython-IMU/micr ... -fusion both by Peter. However, not sure if the code of mpu9x50 will work on MPU6050. Giving it a try.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need guidence in implementing ESP8266+MPU6050

Post by pythoncoder » Sun Jul 19, 2020 10:19 am

If you read the docs you will see that both modules have been tested with MPU6050. Naturally sensor fusion is limited when the device has no magnetometer: it can deduce yaw and pitch, but not heading.
Peter Hinch
Index to my micropython libraries.

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need guidence in implementing ESP8266+MPU6050

Post by bukaida » Sun Jul 19, 2020 7:11 pm

I have downloaded the fusion based code from https://github.com/micropython-IMU/micropython-fusion. I have few doubts before trying to implement it on ESP8266+MPU6050 as the readme file seems to be written for a bit advanced level users. In it's present form--

1. will it work over WiFi ?

2. For 6DOF MPU, which one should I use? Synchronous version or Asynchronous version?

3. Which one is the program to be run on Desktop python prompt and which one on ESP? This project https://github.com/peterhinch/micropython-iot has a much easier instruction for implementation. There is no such implementation instruction for noobs like me for the current one.

4. will the file capture.py under remote folder be run as client?

Please help.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need guidence in implementing ESP8266+MPU6050

Post by pythoncoder » Mon Jul 20, 2020 8:34 am

Your client (the ESP8266) is physically connected to the MPU6050. The simplest approach is where it performs the fusion and sends the results to the server using the IOT interface. I recommend the asynchronous version as the IOT code is asynchronous. I suggest you adapt and run the test program fusiontest_as6.py which is for the MPU6050. It will need to be adapted for the ESP8266: in this line instead of the string 'X' you will need to pass an initialised I2C instance.

There is a question mark over how well this will work on ESP8266 as the fusion code is maths intensive. One option may be to use frozen bytecode to reduce RAM use. Another option is to perform the fusion on the server, this is discussed in this doc. In essence you send the IMU data plus a precision timestamp to the server (over the IOT interface) and the server runs the fusion code. A final option is to use an ESP32 or (even better) a Pyboard D.

I'm afraid this isn't an easy beginner project. Asynchronous programming, sensor fusion, client/server operation and frozen bytecode all present rather steep learning curves. You will need patience, determination and a willingness to experiment and to read documentation. Good luck ;)
Peter Hinch
Index to my micropython libraries.

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need guidence in implementing ESP8266+MPU6050

Post by bukaida » Mon Jul 20, 2020 9:38 am

Ok , fusion on ESP seems to be complicated. At present it will be sufficient for me to accept 3 axis accelerometer and gyroscope data in json format at the python server running on desktop, connected to ESP via WiFi. Any suggestion for this?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need guidence in implementing ESP8266+MPU6050

Post by pythoncoder » Tue Jul 21, 2020 7:11 am

Taking as the starting point the c_app.py demo you'll need to add the following imports. Ensure your I2C is instantiated with the correct pins and change the writer code as below. This will send [[accel_x, accel_y, accel_z],[gyro_x, gyro_y, gyro_z]] every five seconds.

You'll need to adapt the server code to accept this JSON data and to do with it whatever you intend.

Code: Select all

import ujson
from machine import I2C
from imu import MPU6050

i2c = I2C(<your args>)  # Adapt for your physical connection
imu = MPU6050(i2c)

# User coro returns data and determines update rate.
# For 6DOF sensors two 3-tuples (x, y, z) for accel and gyro
    async def writer(self):
        self.verbose and print('Started writer')
        while True:
            gc.collect()
            data = (imu.accel.xyz, imu.gyro.xyz)
            print('Sent', data, 'to server app\n')
            await self.cl.write(ujson.dumps(data))
            await asyncio.sleep(5)
Peter Hinch
Index to my micropython libraries.

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need guidence in implementing ESP8266+MPU6050

Post by bukaida » Thu Jul 23, 2020 7:35 am

Will this run on ESP or Desktop or both?

Post Reply