My new OLED screen has finally turned up, so I’ve been playing with ways of adding a the small display to my wireless temperature sensor.
For now I’ve only been working on using the display to show the current temperature, but soon I hope to add a rotary encoder that will allow me to choose the current desired temperature (in theory in each room, but that’s a way off for now).
The display is one of those cheap 128 x 64 0.96″ ones that cost about £3 each from China. They are small and cheap enough to place into each wall-mounted temperature sensor, but big enough for what I want to display.
I’ve obtained the I2C version which allows me to use the same pins as the temperature sensing IC, which will – hopefully – help simplify designing the final PCB.
To control the display, I’m using the ESP_SSD1306 library, available here: https://github.com/somhi/ESP_SSD1306 After downloading and playing around, I was able to get the display to show the current temperature everytime it’s measured. I did however have a few problems:
- Initially, I couldn’t get anything on to show on the display. It turns out that this was due to the “Wire.begin()” function call in the ESP_SSD1306 library. It was simply using the default I2C pins, which clearly were different on my board. Simple enough to fix, simply change the above line to “Wire.begin(2, 14)” and problem solved. It would be good to be able to choose the I2C pins in the default library, so might look into upgrading the library at some point.
- Ticker.h library and the ESP_SSD1306 don’t seem to get on well together. I was unable to figure out why, but everytime I tried to display something on the display in the middle of the Ticker function, the device would reset. I never did find a solution to this (might look into it in the near future), so instead I got rid of the Ticker library and used something else.
Finite State Machine
I always intended to add a finite state machine in this project, but though I’d be looking into it at a later date. However, due to the problems with the Ticker library, I decided to get started with it sooner rather than later.
I’ve used FMSs before in AVR programs, but not directly in the Arduino environment, but I’m still learning the “Arduino-FSM” library, available here: https://github.com/jonblack/arduino-fsm
For now the FSM is very simple and probably a bit over-kill, but later I’ll use it to allow me to set the desired temperature via a rotary encoder, and potentially add a small menu, etc.
For now, the FSM has only 1 state, 1 transition, and 1 trigger.
- The state is called “state_waiting”, during which nothing actually happens and the device is simply waiting for something to happen – in this case for a timer to say it’s time to obtain the current temperature.
- The trigger is called “TEMPERATURE_TIME_TRIGGER”. It’s not very catchy, but gets the job done. This trigger happens once every minute (can be easily changed) and triggers the FSM to obtain the current temperature.
- Finally, the transition is called “transition_getting_temperature”. This transition goes and gets the current temperature, displays it on the screen, and sends it via MQTT. This transition goes from “state_waiting” back to “state_waiting”, where the FSM waits for something new to happen.
The diagram below shows the FSM in a way that should be much easier to understand.
The code has been uploaded to the Github project here: https://github.com/nerobot/Home-Automation/tree/master/ESP8266%20temperature%20sensor It’s currently quite messy and will be cleaned up soon, but for now, it might be useful to someone.
Below, you can see a photograph of the OLED displaying the current temperature. The circuit is currently very messy, but does the job.
The next step is to add the rotary encoder. I hope to get the following done soon:
- Use the rotary encoder to choose the current desired temperature.
- Show the desired – along with actual – temperature on the OLED.
- Send the chosen desired temperature to node-red using MQTT, and update the shown desired temperature from node-red (again using MQTT).