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

View Post

I2C Scanner for ESP8266-01

I2CI am semi new to programming electronics. Straight amateur thats for sure, but I know enough to be dangerous. I have been trying to figure out how to get my MPU6050 sensor to work with the ESP-01. I couldn’t figure it out for the life of me. I have tried all kinds of things. I started this endeavor back when there was no Arduino IDE support for the ESP (a while ago). It was AT commands or LUA. Now that the Arduino IDE can be used it makes things much easier (I initially had issues getting it to work on my Mac). But sometimes there is a catch. I have noticed that most of the Arduino sketches will work but with a small minor tweak here or there. For example most tutorials I have found people are using the ESP8266-12. The model 12 has multiple pins available for TX & RX, the 01 model does not. The 01 model is the cheapest and happens to be what I have.

I found out it was pretty easy to change the sketch to get it working, most of the time. In the case of say I2C you have to specify the pins used for communication. The 01 only has two free pins, GPIO0 and GPIO2. Those can be used as inputs/outputs for a relay or say an LED. Or, you can use them for I2C communication. You need to specify the pins used in the sketch though. I am pretty sure this is beginner stuff, but hey I just figured it out.

Load up the default I2C scanner sketch and where you see Wire.being()  change that to Wire.being(0,2)  and you are now good to go. That should get you going on almost any I2C based sketch for Arduinos. You wont be able to use the pins for input/outputs (as far as I know) but now you have I2C.

ESP8266 and MQTT

ESP8266 and MQTT, Google that and see what happens (you most likely already did and that is how you got here). Shit is everywhere. It’s gobbin up the bandwidth of the intarwebs. And you know what? I couldn’t get any of that shit to work on my standard ESP8266-01. I have a few of them, I tried them all with all kinds of shit. I would get close but never a damn cigar (I can’t smoke it anyways). Until today for months I have been stuck on this. So if you are like me and have not been able to get the damn thing to work, for whatever reason, try this. Hope it helps.

This is the page that saved it all for me, thank these guys (I did not read or follow any guidelines on this page, I merely snagged their code):
https://developer.ibm.com/recipes/tutorials/connect-espressif-esp8266-to-ibm-iot-foundation/

I copied “mainIoTF.lua” and made some minor tweaks to get it working. I removed the orgID and changed the broker to my raspi IP address. Don’t forget to change the topic. I made those tweaks and uploaded it. BAM!! I was getting data send via MQTT to the Pi every 10 seconds.

This is what I was getting on Node-Reds debug window every 10 seconds (the number will change):  {"d": {"data":69}}

This is where it comes from, should be easy enough to modify:
m:publish(topic ,'{"d": {"data":'..t1..'}}', 0, 0,

I came back and edited this post because I hate it when I find a site that says they found the fix, but never posts it.. so heres the code. Like I said, its 99% the same code from the other site I linked/mentioned. But here it is for you anyways.