Tag Archives: ssd1306

0.96″ OLED SSD1306 display and Raspberry Pi

[Update: 2019-03-30] Our reader, Paweł Kopacz, noticed that pip, setuptools and wheel are also needed
Quick and visually compelling projects don’t come easy ? Naaah, just like last time – let’s get on a very nice and easy project – attaching display onto Raspberry Pi. As we were chating on the Malinowe Pi Group, Hubert Wu pointed me to a OLED 128×64 display, monochrome. With a quite decent resolution of 128×64 and based on OLED technology this is a nice 0.96 inch display. Well, my first computer was running at Hi-Res of 640×400 and monochrome too. Priced at $3-$5 pops-up as: ”0.96″ I2C IIC Serial 128X64” in popular stores and is clearly based on original Adafruit SSD1306 – which would be my first choice!

Wyświetlacz OLED SSD1306 - grafika
A OLED SSD1306 display

What will you need?

Software:

  • git, python-devel, python-pip (installable under Raspbian on RPi)
  • Library with examples from Adafruit: Adafruit_Python_SSD1306

Hardware:

Usage

This external display connected to RPi (and which is independent from HDMI or Composite video outputs), is very useful for small projects – and for me – when need to know what the IP address is 🙂

Connecting

First let’s connect power – VCC and GND to 3,3 and GND on RPi, then SDA and SCL to GPIO0 (GPIO 2 @ RPi B+, 2 i 3) and GPIO1 (GPIO 3 @ RPi B+, 2 i 3):

OLED SSD1306 display - connecting to RPi...
OLED SSD1306 display – connecting to RPi…

The wiring is quite simple and all pins are adjacent (well we omit GPIO4!). During first wiring attempt i mixed up VCC and GND! RPi greeted me with low light on it’s green LED, but all was fine after proper wiring. This is the view from the display:

OLED SSD1306 - wiring
OLED SSD1306 – wiring

Installation

On the software side we are going to turn on the I2C (GPIOO or GPIO2 and GPIO1 or GPIO3) in our RPi, by means of running raspi-config, then choose Interfacing Options–>>I2C–>Enable
After reboot – we should install the software, but first – let’s update the system:

sudo apt-get update && sudo apt-get upgrade

Next the software:

sudo apt-get install build-essential python-dev python-pip python-imaging python-smbus git

Then the RPi.GPIO:

sudo pip install RPi.GPIO

Now – let’s get via git the Adafruit library with examples:

sudo python -m pip install --upgrade pip setuptools wheel
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install

Test!

Let’s find out if this works…

cd Adafruit_Python_SSD1306/examples/
nano shapes.py

Now edit the shapes.py and find the line that tells it to use the I2C version of the display:

disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_bus=1)

This is correct, add “#’ at the begging of the other lines that define the ‘disp‘ variable
Run:

sudo python shapes.py

Wow, it works! There’re more examples – a very nice – animate.py and image.py, just don’t forget to edit them to use the proper definition of the ‘disp’.
As bonus – we can use our own fonts (TTF format) – i.e.: 16pix height:

Wyświetlacz OLED SSD1306 - tekst
OLED SSD1306 – own font/text

The image.py allows you to display monochrome picture in PPM – use GIMP to save and use dithering to get acceptable results:

Wyświetlacz OLED SSD1306 - grafika
OLED SSD1306 – graphics

Well, as you see – the input characteristics of the image matters, but at leas you can see the logo

Now all that is left is to use python or bash to display IP address of you RPi!

Wyświetlacz OLED SSD1306 - tekst
OLED SSD1306 – text

ESP8266 and SMOG part three: sensor protection, cloud upload

Last time we updated our simple SMOG sensor with display (OLED) and added BME 280 to verify the SMOG measurements – the environmental sensor – as we know that humidity over 70% can add up to the SMOG.
It’s time now to protect our sensor.
Continue reading ESP8266 and SMOG part three: sensor protection, cloud upload

Domoticz part 3.1: ESP Easy on ESP8266 – remote and local OLED display with temperature, humidity, light – weather station

Previously we played around the SSD1306 OLED display as side display for Raspberry Pi. The author of ESP Easy – Richard ten Klooster (https://github.com/ESP8266nu/ESPEasy) – recently updated his project to include new sensors, so today we will update our DHT22 enabled remote sensor for Domoticz with display and light metering. Our weather station will get a remote display.

I’ve created such bundle of sensors to allow both temperature and humidity measurement small shack for chopped wood but also to light the way to it after dark via LED lamp. Also I was interested in stability of ESPEasy with DHT22, BH 1750 and OLED I2C display.

What will you need?

Software & Hardware

Let’s do it!

Connecting the display is very easy – event if you didn’t the previous example for Raspberry Pi. After logging in to ESP – ESPEasy – we choose the PINs that will work as I2C bus. This example uses the default: GPIO 4 & 5. Let’s connect our display to +3,3V, GND, and SDL & SCL to GPIO 4 & 5. The configuration is as follows:

ESP Easy - SSD1306
ESP Easy – SSD1306

Pay attention – this is how the sensor was called and the values that we defined – we need to enter them again just the same for the display for ESPEasy to get data and to display them correctly:

ESPEasy DHT22 - config
ESPEasy DHT22 – config

The first value is the address on the I2C bus – to find out if we have correct one go to Tools->I2C Scan, which gives you the address – and notice that example shows another – connected to the I2C bus sensor – Lux – BH1750.:

ESP Easy - Scan i2c
ESP Easy – Scan i2c

Out address is (hexadecimal): 0x3c!
If all is correct – we should see “ESPEasy” on the screen (upside down is still fine).

Next – we move to display configuration – direction, which line are used for displaying – but first let’s try to display some data from external source:

Remote and local display

ESPEasy allows you to display both local sensor dana or any other data via unprotected method – using just http URL.
What is possible – first we can turn on/off the display. In this example the IP address of ESP8266 running ESPEasy is 10.20.30.40. We can turn off the display (i.e. it’s too bright in the night)

http://10.20.30.40/control?cmd=oledcmd,off

Then to turn in on:

http://10.20.30.40/control?cmd=oledcmd,on

Clearing the display:

http://10.20.30.40/control?cmd=oledcmd,clear

Now, to us it in our project – we should use the curl to send data from Raspberry Pi:

curl http://10.20.30.40/control?cmd=oledcmd,row,column,our_text

An example:

curl http://10.20.30.40/control?cmd=oledcmd,1,1,My%20login:%20$LOGNAME

Finally, we will use this function to display data from other sensor that our Raspberry Pi has data of – the opposite of what we usually use the ESP for!

But first – we should get the data from our already connected DHT22 and lux sensor. Let’s type the name of our sensor in Line 1. The Line 2 could hold the info about uptime – and time – we know that device is working and has proper communications with NTP.

Where to set NTP ? Go to NTP in Tools and choose your favourite NTP server – i.e. pool.ntp.org.
We got time & uptime – that’s easy. Displaying variable takes more effort – we need to use the percentage as brackets (%). Take a peek at Wiki page for examples.
We’re ready to display the data from DHT22. It’s time to check what was it’s name in the configuration:

ESPEasy DHT22 - config
ESPEasy DHT22 – config

– and here it’s name is: AmicaTempHum. It’s a bit different we need “[ ]”, and the variables (we have two of them when talking to DHT 22) are separated by (#): [AmicaTempHum#Humidity]. This time the “%” is used to tell you that humidity is in % – so it’s supposed to be displayed.

The same approach should be used for temperature. Since by default we set 300 seconds of update time – we should get on the screen, after a while:

ESP8622 with SSD1306 (OLED) display working on ESPEasy
ESP8622 with SSD1306 (OLED) display working on ESPEasy

OPTIONAL: Since we can have quite a lot devices on I2C bus – let’s connect in paralel lux meter – BH1750. Meaning: VCC 3,3V to VCC, SDA & SCL to SDA & SCL finnaly GND to GND. Just like DHT22 it needs tiny amounts of power so the Amica NodeMCU can handle it safely. If we connected it properly we should get this info:

ESP8622 with SSD1306 (OLED) display, DHT22, BH1750 under ESPEasy
ESP8622 with SSD1306 (OLED) display, DHT22, BH1750 under ESPEasy

So, next photo show how much light I’m using here:

ESP8622 with SSD1306 (OLED) display, DHT22, BH1750 under ESPEasy

Beware – if you’re getting values like 54612 lux – the sensor is NOT working properly (check connections).

This is it – domoticz needs to be updated with new virtual Lux sensor (the virtuals for DHT22 have been added last time). So what to do with light intensity sensor ? Using the same approach as with the script for opening garage – we can turn on some LED lamps if it gets dark enough. Or use ‘Events’ in domoticzu if you like GUI approach.

But wait! People commenting this brought up interesting idea:

Displaying other values FROM domoticz on remote sensor

Looking at our example we see that Line 7 is empty. Originally I wanted to add the baraometer, but the shop canceled the order. I have other one on RPi – so why not use this data – atmospheric pressure cannot be that different, right? The BMP180 gives me temperature and pressure in hPa. I don’t care about the temperature, but the pressure is interesting. Since we already brought up the garage exampe  you might remember that a LUA script w domoticz can give you data and allows you to use it:

  • we need to get the pressure data
  • cut it (remove extra info)
  • display it on SSD1306 attached to ESP8266

The LUA script will use the round function – single digit, and will be launched every minute (or smarter – at any change then if with devicechanged) – so let’s use the time option.
In my system this sensor is named ‘TemperaturaCisnienie’ so remember the name of the script has to match it:

script_time_TemperaturaCisnienie.lua

IP address needs to mathc you network and script goes to /home/pi/domoticz/scripts/lua:

function round(num, idp)
 local mult = 10^(idp or 0)
 return math.floor(num * mult + 0.5) / mult
end
hektopaskale=round(otherdevices_barometer['TemperaturaCisnienie'],1)
commandArray = {}
commandArray['OpenURL']='10.20.30.40/control?cmd=oledcmd,7,1,Baro:'..hektopaskale..'hPa '
return commandArray

NOTE: Newer version of ESP Easy (inscluding ESP Easy Mega), use “oled” instead of “oledcmd”, as it was noted by one of the commentators.

So after a while you will get pressure on the screen.

Another example from comments discussion:
I you have the termometer from previous example about attaching termometers to domoticz – corresponding script is here (remember about the name!)

local NazwaCzujnika="ZKominka"
commandArray = {}

if devicechanged[NazwaCzujnika] then
    commandArray['OpenURL']='10.20.30.40/control?cmd=oledcmd,7,1,Temp:'..otherdevices_svalues[NazwaCzujnika]..'*C'
   end
return commandArray

This time we only update on temperature CHANGE.