For Reservation Call : 1.888.222.5847

Home » Truck Tweeting

Truck Tweeting

DIY Truck Telematics DAF UKI have (as promised) added the code for this RaspberryPi project below.  It is quite messy and I am sure there is a much simpler and cleaner way of achieving the results I have but I had only 6 weeks or so programming experience.

To see how the wiring was achieved and the actual control panel with the switches connected to the RaspberryPi please check out this video on YouTube.

It makes some beeping noise when you run it, then fires a Tweet to Twitter to let me know it’s working.  The it makes more beeps and it’s waiting for a button press.  Then it beeps to let me know it’s been pressed and the code is doing it’s bit, and Tweets as well as emailing some text to the office (email address).  Here is the next project to use bar code scanning.

As stated, it’s very messy code!

You will need to install some of the modules to make this project run and of course some switches, LED’s and a buzzer to replicate what I have done.  Publishing this code is intended for you to rip out whatever lines you wish for yourselves.  Please do let me know if you use any of it, or if it has been of help https://twitter.com/idatatruck

As you can see, four switches have outcomes with two lights (the red one is just a 3v feed through a resistor and the green one is on GPIO24.  Switches are on GPIO 18,17,27,22 again in series with a 10k resistor.

When pasting the tabs and indents may need to be re-inserted and cleaned up.

STARTS HERE.

import RPi.GPIO as GPIO
import tweepy, sys, subprocess, pygame, urllib, time
import os
import smtplib
comd = 1
PushTime=time.strftime(“%H%S%M”,time.localtime())

#Splash Screen Set Up

pygame.init()
theFont=pygame.font.Font(None,72)
screen = pygame.display.set_mode([320, 200])
pygame.display.set_caption(‘iDataTruck SEV031′)

#Interface connection pin set up

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.OUT) #Buzzer Out Pin Ref
GPIO.setup(24, GPIO.OUT) #Green LED Pin Ref

# SetUp Tweepy Authorisation

# Consumer keys and access tokens, used for OAuth
consumer_key = ‘My Secret Key (not shown)
consumer_secret = ‘My Consumer Secret (not shown)
access_token = ‘My Access Token (not shown)
access_token_secret = ‘My Access Token Secret (not shown)

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(‘My Secret Key (not shown)‘,’My Consumer Secret (not shown)’ )
auth.set_access_token(‘My Access Token (now shown)‘ , ”My Access Token Secret (not shown)’)

# Creation of the actual interface, using authentication

api = tweepy.API(auth)

# Some start up beeping

for OnOK in range(0, 3):
time.sleep(.05)
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.2)
GPIO.output(23, 0)
GPIO.output(24, 0)

#  The next line sends a Tweet!

api.update_status(“ID{0} UK #TechHack iDataTruck Re-starting – SEV031 www.idatatruck.co.uk for more information.”.format(PushTime))

#  Some more beeping if the Tweet is sent and returns OK. (some error handling code here would be good)
for OnSK in range(0, 2):
time.sleep(.05)
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.1)
GPIO.output(23, 0)
GPIO.output(24, 0)

time.sleep(.1)
GPIO.output(24, 1)
time.sleep(.2)
GPIO.output(24, 0)

print (“READY”)

while True:
GPIO.output(24, 1)
time.sleep(.5)
GPIO.output(24, 0)
time.sleep(.2)
GPIO.output(24, 1)
time.sleep(.1)
GPIO.output(24, 0)
time.sleep(.1)
input_state = GPIO.input(17)
if input_state == False:
print(‘Unloaded Button Pressed’)
GPIO.output(23, 1)
time.sleep(.5)
GPIO.output(23, 0)
time.sleep(.2)
GPIO.output(24, 1)
time.sleep(.15)
GPIO.output(24, 0)
gmail_user = “YOUR_EMAIL_ADDRESS@gmail.com
gmail_pwd = “YOUR_GMAIL_PASSWORD
FROM = ‘YOUR_EMAIL_ADDRESS@gmail.com
TO = [‘THEPERSONTOTELL@WEBSITE.COM‘]
SUBJECT = “|SEV031 Tipped and departing”
TEXT = “Shipping container cargo unloaded and doors closed – Box is empty, departing in 2 mins”
message = “””\From: %s\nTo: %s\nSubject: %s\n\n%s
“”” % (FROM, “, “.join(TO), SUBJECT, TEXT)
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP(“smtp.gmail.com”, 587) #or port 465 doesn’t seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
comd+=1
PushTime=time.strftime(“%H%S%M”,time.localtime())
api.update_status(“ID{0} Shipping container unloaded and doors closed – Box is empty, departing in 2 mins – SEV031″.format(PushTime))
theTime=time.strftime(“%H:%M:%S”,time.localtime())
timeText=theFont.render(str(PushTime), True,(255,255,255),(10,0,0))
screen.blit(timeText,(80,60))
pygame.display.update()
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.5)
for aOnOK in range(0, 5):
GPIO.output(23, 0)
GPIO.output(24, 0)
time.sleep(.02)
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.03)
GPIO.output(23, 0)
GPIO.output(24, 0)
print (“Sent ID”),PushTime
print (“READY”)
input_state = GPIO.input(18)
if input_state == False:
print(‘Loaded And Departing Button Pressed’)
GPIO.output(23, 1)
time.sleep(.5)
GPIO.output(23, 0)
time.sleep(.2)
GPIO.output(24, 1)
time.sleep(.15)
GPIO.output(24, 0)
gmail_user = “YOUR_EMAIL_ADDRESS@gmail.com
gmail_pwd = “YOUR_GMAIL_PASSWORD
FROM = ‘YOUR_EMAIL_ADDRESS@gmail.com
TO = [‘THEPERSONTOTELL@WEBSITE.COM‘]
SUBJECT = “SEV 031 Loaded, Seal On, Now Departing”
TEXT = “Container now loaded with cargo by customer at collection address and sealed, leaving gate/site/address in next 2 mins”
message = “””\From: %s\nTo: %s\nSubject: %s\n\n%s
“”” % (FROM, “, “.join(TO), SUBJECT, TEXT)
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP(“smtp.gmail.com”, 587) #or port 465 doesn’t seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
comd+=1
PushTime=time.strftime(“%H%S%M”,time.localtime())
api.update_status(“ID{0} Shippng container now loaded by customer and sealed, departing 2 mins – SEV031″.format(PushTime))
theTime=time.strftime(“%H:%M:%S”,time.localtime())
timeText=theFont.render(str(PushTime), True,(255,255,255),(10,0,0))
screen.blit(timeText,(80,60))
pygame.display.update()
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.5)
for aOnOK in range(0, 5):
GPIO.output(23, 0)
GPIO.output(24, 0)
time.sleep(.02)
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.03)
GPIO.output(23, 0)
GPIO.output(24, 0)
print (“Sent ID”),PushTime
print (“READY”)
input_state = GPIO.input(27)
if input_state == False:
print(‘Box On Trailer Button Pressed’)
GPIO.output(23, 1)
time.sleep(.5)
GPIO.output(23, 0)
time.sleep(.2)
GPIO.output(24, 1)
time.sleep(.15)
GPIO.output(24, 0)
gmail_user = “YOUR_EMAIL_ADDRESS@gmail.com
gmail_pwd = “YOUR_GMAIL_PASSWORD
FROM = ‘YOUR_EMAIL_ADDRESS@gmail.com
TO = [‘THEPERSONTOTELL@WEBSITE.COM’]
SUBJECT = “SEV 031 – Box on/hooked up”
TEXT = “Box is loaded on trailer at port/yard OR just hooked up to trailer in yard/changeover leaving in 3 minutes”
message = “””\From: %s\nTo: %s\nSubject: %s\n\n%s
“”” % (FROM, “, “.join(TO), SUBJECT, TEXT)
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP(“smtp.gmail.com”, 587) #or port 465 doesn’t seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
comd+=1
PushTime=time.strftime(“%H%S%M”,time.localtime())
api.update_status(“ID{0} New container accepted to vehicle at port/depot/yard, leaving in 1 minute – SEV031″.format(PushTime))
theTime=time.strftime(“%H:%M:%S”,time.localtime())
timeText=theFont.render(str(PushTime), True,(255,255,255),(10,0,0))
screen.blit(timeText,(80,60))
pygame.display.update()
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.5)
for aOnOK in range(0, 5):
GPIO.output(23, 0)
GPIO.output(24, 0)
time.sleep(.02)
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.03)
GPIO.output(23, 0)
GPIO.output(24, 0)
print (“Sent ID”),PushTime
print (“READY”)
input_state = GPIO.input(22)
if input_state == False:
print(‘Lift Off Button Pressed’)
GPIO.output(23, 1)
time.sleep(.5)
GPIO.output(23, 0)
time.sleep(.2)
GPIO.output(24, 1)
time.sleep(.15)
GPIO.output(24, 0)
gmail_user = “YOUR_EMAIL_ADDRESS@gmail.com
gmail_pwd = “YOUR_GMAIL_PASSWORD
FROM = ‘YOUR_EMAIL_ADDRESS@gmail.com
TO = [‘THEPERSONTOTELL@WEBSITE.COM‘]
SUBJECT = “SEV 031 – Box lifted off or un-hooked”
TEXT = “Box has just been lifted off trailer at port/depot OR dropped trailer with this container on it in yard at current location”
message = “””\From: %s\nTo: %s\nSubject: %s\n\n%s
“”” % (FROM, “, “.join(TO), SUBJECT, TEXT)
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP(“smtp.gmail.com”, 587) #or port 465 doesn’t seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
comd+=1
PushTime=time.strftime(“%H%S%M”,time.localtime())
api.update_status(“ID{0} Shipping container dropped off at port/depot/yard, now empty – SEV031″.format(PushTime))
theTime=time.strftime(“%H:%M:%S”,time.localtime())
timeText=theFont.render(str(PushTime), True,(255,255,255),(10,0,0))
screen.blit(timeText,(80,60))
pygame.display.update()
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.5)
for aOnOK in range(0, 5):
GPIO.output(23, 0)
GPIO.output(24, 0)
time.sleep(.02)
GPIO.output(23, 1)
GPIO.output(24, 1)
time.sleep(.03)
GPIO.output(23, 0)
GPIO.output(24, 0)
print (“Sent ID”),PushTime
print (“READY”)

#Something to stop the program or handle errors would be good here