It took me about a week to get this going. There are still a few kinks that need to be ironed out, or worked around. But I wanted to get this out there before I forgot about it. I could not find a sketch for using an Arduino or ESP with an OLED and MQTT. Not one that didn’t just display data from a connected sensor., that is not what I wanted. I wanted to be able to send messages to the OLED screen via MQTT. I spent quite a while looking for it and I couldn’t find anything. About a week or two ago I finally broke the wall that was keeping me from sending and receiving MQTT message on an ESP. So I took that code and the working code from the Adafruit OLED sketch and made a baby.
I was able to produce a sketch that will simply display any text received via MQTT. Perfect. I plan on using this cobbled together with Node-Red. That way I can have one screen and send multiple sensor readings to it with minimal coding and parts. There are a few things I still need to figure out. For example I can’t get the screen to clear. Perhaps thats what the OLED reset pin was for? But my OLED only has four pins; SDA, SCL, Vcc and Ground. No reset. What to do? I think that is actually the only thing I need to work out. The Adafruit code will wrap your text to new lines, so make a note of that. I have gotten around clearing the screen by injecting spaces via MQTT. Its a little more effort in Node-Red for now until I get that down. But it works. It doesn’t matter how you get there as long as you get there right?
I Program the ESP via the Arduino IDE, if you don’t know how to set that up take a stroll down Google lane. Tons of help there on that topic. As it stands in the code below, once powered up the OLED should flash the Adafruit logo until a message is received then it will display the message until a new one is received. Simple. The default topic is “inTopic”, and the default OLED font size is 1. I have tried a font size of 2 but no higher. I also stumbled upon this library here. Much smaller and designed just for text. May try that in the future, but if it ain’t broke don’t fix it.
(1) ESP-01 (mine is a standard ESP-01 from eBay)
(1) OLED I2C Screen (I used a 0.96″ 4pin OLED also off eBay)
IDE Library Requirements:
This was the tutorial that helped me get what I have going. I couldn’t get the OLED to work quite right until I came across this post. I used it as the base for what I have. http://randomnerdtutorials.com/guide-for-oled-display-with-arduino/
This page helped but not until I specified the SDA, SCL pins for the ESP. I couldn’t get the display to work properly and do what I wanted. http://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/
And the code (below): [See the comments for updated code] View Post
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.
I 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.
Doesn’t work. Fucking go figure. There’s always something to cause a problem. If you have been following the last few posts I’ve made you would see that I have been working on ESPs all weekend. I finally got a new TTL adapter that works with the ESPs and have been playing with ESPlorer and LUA code. I decided to switch over to the Arduino IDE since there seems to be more support and tutorials for it. Well it won’t connect.
That is what I am greeted with. Well after about an hour of trying different shit I found this tidbit on the Github for Arduino issues: This is because esptool-ck doesn’t work with PL2303 based converters on OS X. Fucking great. Thats what I just bought, so I could flash the chips again. So I fired up Virtual Box and Windows 7, downloaded/installed all the needed Arduino shit and drivers and blah blah. Opened a blank sketch in the IDE, selected the ESP board and hit upload. BAM! Shit worked. Took daaays, but it worked. Fucking Apple. Looks like I’m stuck to a Windows machine no matter what I do. Sad face.
Update: So after flashing via Windows in Arduino IDE and it working I went back to macOS to try again. I changed “ck” to “nodemcu”, which didn’t work at all. So I changed it back, thats when it started working. I still had to do the reset-flash-dance. Ground GPIO02 and reset, once booted release GPIO02 and set it high, or let it float. That should put you in bootloader mode so you can flash via the Arduino IDE. I also noticed that I cannot switch back and form between the IDE and ESPlorer. They don’t play nice together. If I want to switch I have to reflash the ESP firmware. That sucks. Picky ass modules.
I finally did it! I was able to flash my ESP8266s again. It only took months and a bunch of scratching my head – then banging it against a wall. I cannot even really remember where I left off with my programs, but I do recall I stopped because I wasn’t able to flash my chips anymore.
That was a big problem obviously, and I do not know why it started as all was well in the beginning. I originally had ordered a few ESP8266-01s and some Arduino Mini Pros. I figured they would work well together. To go with the Mini Pros and ESPs I nabbed a USB FTDI programmer and it worked fine when I initially got it. I was able to send AT commands to the ESPs and program my Mini Pros no problem. Then I started playing with flashing new firmware on the ESPs to try NodeMCU, and I think thats where everything started to go downhill. The FTDI and ESPs would stop communicating half way through flashing. Eventually I couldn’t flash or upload anything anymore. It just wouldn’t go. Fast forward a months later and I ran into a few posts that mentioned the FTDI chips are unstable with the ESPs and won’t work. Hmmm. They all recommended using a USB TTL adapter instead.
So thats what I finally did, purchased a USB TTL adapter off Amazon for $6. I got the Armorview PL203HX adapter. USB plug on one end and four breadboard connectors on the other for PW, GND, RX and TX. Simple. As soon as I got it I plugged it in and… nothing. A
ls/dev/tty.* yielded nothing. WTF. I probably need a driver. Yup, the PL2303 driver was needed in my case. Installed that and plugged the adapter back in and now I see it
tty.usbserial . Nice and generic. First thing I tried was flashing the AT firmware to an ESP. And it worked!!
I can now flash firmware to the ESPs again, and the Arduinos if I wanted to but the FTDI still works fine with those. For those that want to know; I use a breadboard power supply for powering the ESPs and Arduinos. I only use the USB adapters for communication. You can and I have done it without it, but it is just way more stable to use the extra power supply. The adapters don’t have the juice, especially for the ESPs. Now I can get back to tinkering again.