MPU6050 on an ESP8266-01 via MQTT

Update 3/16/17: I added code at the end of the post for dual MPU6050s
Update 2/2/18: Added images of possible wiring examples for ESP-01 and NodeMCU

Another milestone for me. I’m on a fucking roll! I found an Arduino sketch that takes an MPU6050 on an ESP8266-01  and spits it out data via MQTT. I have been looking for this and trying to accomplish this for months. My end goal is a clothes washer and dryer monitor for the family. There are sketches out there for laundry monitors but they don’t have what I need, mainly multiple users…I digress. So I found the script and it works, but some of it was it Italian which made it confusing for me and it did clumped all the sensor data into just two categories; accelerometer or gyroscope. I need more detailed information so I hacked it up a bit and it works!!

I commented out some stuff that was not needed (it all works for me YMMV), and I split all of the sensor data up into their own MQTT topic. So you get topics for AcX, AcY, AcZ and GyX, GyY, GyZ. This allows me to just use the sensor that works for the washer or dryer. Made it really easy once I plotted the data to a line graph.

Code below:
Originally taken from here

Arduino IDE code for the ESP8266, same as above but modified for dual (2) MPU6050s. It will spit out the MPU sensor data to MQTT topics: AcX, AcY, AcZ, GyX, GyY, GyZ. And the same for MPU #2. AcX2, AcY2, etc…

Code snaked from https://github.com/jrowberg/i2cdevlib/issues/63#issuecomment-38204650

 

Example wiring of a single MPU and ESP-01 (image borrowed from myrobotlab)

Esp8266 Mpu6050

Example possible wiring of a single MPU and NodeMCU. Keep in mind that you may use different pins for the SDA/SCL on a NodeMCU because well, it has more pins. Just specify which ones you are using in the code before you flash (see code line #87 or #127 “Wire.begin(0,2)” ). (Image borrowed from FilipeFlop)

nodemcu mpu6050

8 Comments

  1. Ivanny Shintia
    July 8, 2017 / 8:52 am

    Hi, my name is Ivanny. There are several questions I want to ask you, I also make my final project using esp8266 and mpu6050 accel gyro. I have no idea about mqtt server that you mention above.
    1. how can I make my mqtt server?
    2. how’s the final result? is that a table or graph?
    thank you so much if you want to help me about this….

    • justin
      Author
      July 15, 2017 / 8:00 pm

      I run my mqtt server off a raspberry pi and another on an old PC running Ubuntu. It doesn’t take much to run one. I use mosquitto, but there are others.

      I use the mqtt server along with Node-Red. It takes the incoming messages from my sensors and then I can do what I want with the data. So I can graph it or use a table or send SMS messages… etc. Sky is the limit really.

  2. Lior G.
    February 2, 2018 / 1:33 am

    Hey m8 well done!

    Quick question, can u provide how u connected the MPU? which wire goes where?

    • justin
      Author
      February 2, 2018 / 1:52 pm

      It all depends on which ESP module you are using. The ESP only has two pins available so you must use those (0 &2). If you look at the first example at line #87, #127 in the dual MPU example it shows “Wire.begin(0,2);”. That specifies which pins are to be used for SDA/SCL. If you are using a NodeMCU you have many choices, just specify what they are on that line.

      I updated the post with some example images, hope that helps!

  3. Michael
    February 27, 2018 / 2:27 am

    Hi, thank you for this…Can i get my esp-01 to mpu6050 connected to my arduino for power source?
    how do i do it?

    • justin
      Author
      February 27, 2018 / 1:24 pm

      The 3.3v rail on the Arduino can only put out about 200mA of current. The ESP-01 can draw up to 435mA of current – so be careful. If you are using an Arduino Uno you can use the 5v rail, you will need to step down the voltage to 3.3v for the ESP though. Double check your MPU6050, some use 3.3v and some (like the GY-521) prefer 5v.

  4. Paul
    March 26, 2018 / 5:35 pm

    This also looks like what I am looking for as well. As I am pretty new at this stuff I would like to ask a question or two. I am using Arduino-1,8.5 on windows;trying to flash a nodemcu. Which libraries do I need to import/load to get this to work? I try to compile the sketch, and it always stops saying it’s missing some such library which I thought I had. I know I need pubsub. What else do I need? Sorry if this is rather basic but as I said I am rather new at this.

    Thanks.

    • justin
      Author
      March 26, 2018 / 8:13 pm

      Hey there, lines 1-6 say which libraries we’re including (the others are commented out, unused).

      include Wire.h
      include PubSubClient.h
      include ESP8266WiFi.h

      If you include those and it still fails try to read the output message carefully, as it should tell you why it failed and point you in the direction needed.

Leave a Reply

%d bloggers like this: