Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
raspi pi OLED code showing IP and temperature + humidity
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Meant for use with the Raspberry Pi and an Adafruit monochrome OLED display!
# This was coded by David Vassallo, modified from code by The Raspberry Pi Guy
# Imports all of the necessary modules
import gaugette.ssd1306
import time
import sys
import socket
import fcntl
import struct
from time import sleep
import dhtreader
import cPickle as pickle
# IMPORTANT - YOU MAY NEED TO CHANGE THIS PART!!!!!
# Sets our variables to be used later
# CHANGE THE BELOW IF YOU ARE NOT USING A DHT22 but some other model
DHT22 = 22
dev_type = DHT22
# CHANGE THE BELOW IF YOU ARE USIG A DIFFERENT GPIO PORT FOR THE DHT SENSOR
dhtpin = 23
# SGHOULDNT BE CHANGED UNLESS YOU WIRED THE OLED SCREEN DIFFERENTLY TO THE ADAFRUIT GUIDE
RESET_PIN = 15
DC_PIN = 16
# This function allows us to grab any of our IP addresses
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
led = gaugette.ssd1306.SSD1306(reset_pin=RESET_PIN, dc_pin=DC_PIN)
led.begin()
led.clear_display()
for i in range (0,5):
textTempHeader = "Temperature:"
textHumidHeader = "Humidity:"
try:
dhtreader.init()
temp, humid = dhtreader.read(dev_type,dhtpin)
with open('/home/pi/flask/sensors.pickle', 'w') as f:
pickle.dump([temp, humid], f)
temp = "{} *C".format(temp)
humid = "{} %".format(humid)
except:
temp = ('No Reading!')
humid = ('No Reading!')
# The actual printing of TEXT
led.clear_display()
led.draw_text2(0,0,textTempHeader,1)
led.draw_text2(0,8,temp,1)
led.draw_text2(0,16, textHumidHeader, 1)
led.draw_text2(0,25,humid,1)
led.display()
sleep(9)
led = gaugette.ssd1306.SSD1306(reset_pin=RESET_PIN, dc_pin=DC_PIN)
led.begin()
led.clear_display()
try:
TEXTwlan = get_ip_address('wlan0')
except IOError:
TEXTwlan = ('NO WIRELESS!')
try:
TEXTlan = get_ip_address('eth0')
except IOError:
TEXTlan = ('NO INTERNET!')
# The actual printing of TEXT
led.clear_display()
ipWLAN = 'Your WLAN IP Address:'
ipLAN = 'Your LAN IP Address:'
led.draw_text2(0,25,TEXTlan,1)
led.draw_text2(0,8,TEXTwlan,1)
led.draw_text2(0,0,ipWLAN,1)
led.draw_text2(0,16, ipLAN, 1)
led.display()
sleep(9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment