ILI9341 Adafruit library support


#1

Hi, I am using an DS18b20 with a relay to control my house temperature and opening or closing the house heaters. I think it would be awesome to have the DS18b20 read value in a TFT display like thee ILI9341. Right now I can do that using cayennee to readd the DDS18b20 and controlling the relay and in the other hand using a simple script in python to read the ds18b20 and displaying it in the ILI9341. The problem is that the script is a separeted thing and it would bee nice to have it all in cayenne. Is there any possbility to do that? How would I start to do it?


#2

Just i case anyone want to do the same, using cayenne and also displaying the temp in a tft, here is the code I am using. Directly from Adafruit (kudos to them)

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola  Permission is hereby granted, free of charge, to any person obtaining a copy  of this software and associated documentation files (the "Software"), to deal  in the Software without restriction, including without limitation the rights  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  copies of the Software, and to permit persons to whom the Software is  furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import Adafruit_ILI9341 as TFT
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.SPI as SPI
import os
import glob
import time
 
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
 
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
 
def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines
 
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0        
        return temp_c

# Raspberry Pi configuration.
DC = 24
RST = 25
SPI_PORT = 0
SPI_DEVICE = 0


readtempnew = 'aa'
readtempold = 'bb'

# Create TFT LCD display class.
disp = TFT.ILI9341(DC, rst=RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=64000000))

# Initialize display.
disp.begin()

# Clear the display to a red background.
# Can pass any tuple of red, green, blue values (from 0 to 255 each).
# disp.clear((255, 0, 0))

# Alternatively can clear to a black screen by calling:
disp.clear()

# Get a PIL Draw object to start drawing on the display buffer.
draw = disp.draw()


# Load default font.
font = ImageFont.load_default()

# Alternatively load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype('droid.ttf', 40)

# Define a function to create rotated text.  Unfortunately PIL doesn't have good
# native support for rotated fonts, but this function can be used to make a
# text image and rotate it so it's easy to paste in the buffer.
def draw_rotated_text(image, text, position, angle, font, fill=(255,255,255)):
    # Get rendered font width and height.
    draw = ImageDraw.Draw(image)
    width, height = draw.textsize(text, font=font)
    # Create a new image with transparent background to store the text.
    textimage = Image.new('RGBA', (width, height), (0,0,0,0))
    # Render the text.
    textdraw = ImageDraw.Draw(textimage)
    textdraw.text((0,0), text, font=font, fill=fill)
    # Rotate the text image.
    rotated = textimage.rotate(angle, expand=1)
    # Paste the text into the image, using it as a mask for transparency.
    image.paste(rotated, position, rotated)




while True:
    disp.clear((0, 0, 0))
    #print(read_temp())
    #temperatura = 'afa' + str(read_temp())
    #print(temperatura)
    # Write two lines of white text on the buffer, rotated 90 degrees counter clockwise.
    readtempnew = str(round(read_temp()))
    if readtempnew != readtempold:
        font = ImageFont.truetype('droid.ttf', 40)
        draw_rotated_text(disp.buffer, 'TEMPERATURA', (20, 5), 90, font, fill=(255,255,255))
        font = ImageFont.truetype('droid.ttf', 80)
    
        draw_rotated_text(disp.buffer, readtempnew, (90, 50), 90, font, fill=(255,255,255))
        readtempold = readtempnew
    # Write buffer to display hardware, must be called to make things visible on the
    # display!
        disp.display()
    time.sleep(2)