These are the build instructions for the dayPi. This is the component of the elephant detection device that will photograph elephants during the daytime, and will provide mobile connectivity!
This is the code to run on the dayPi - incorporating all the different pieces of code we already developed!
## dayPi code v1.2
## Here are our functions
######## Light sensor function
def CheckLightCondition():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
light_sensor = GPIO.input(11)
if (light_sensor == True):
light_condition = "NIGHT"
else:
light_condition = "DAY"
GPIO.cleanup()
return light_condition
##############################################
######## Check PIR function
def CheckPIR():
# dependencies are RPi.GPIO and time
# returns whats_here with "NOTHING HERE" or "SOMETHING HERE"
time.sleep(1)
#don't rush the PIR!
GPIO.setmode(GPIO.BOARD)
# set numbering system for GPIO PINs are BOARD
GPIO.setup(7, GPIO.IN)
# set up number 7 PIN for input from the PIR
# need to adjust if you connected PIR to another GPIO PIN
try:
val = GPIO.input(7)
if (val == True):
whats_here = "something_here"
#PIR returned HIGH to GPIO PIN, so something here!
if (val == False):
whats_here = "nothing_here"
#PIR returned LOW to GPIO PIN, so something here!
#GPIO.cleanup()
ERROR2 = "no error"
except:
ERROR2 = "PIR error"
#something went wrong, return error code 2 with info for debugging
GPIO.cleanup()
return whats_here
######################################################
######################################################
###### 3. Is this an elephant?
#We send our images obtained by CaptureImage() to the elephant detector
#software, we get back our top 5 results. So the images we are going to
#send will be '/home/pi/suspects/suspected_elephant1.jpg to suspected_elephant10.jpg
#we can move the confirmed elephant photos to a new directory and delete those
#in the suspects directory afterwards
#def IsThisElephant():
#with the os method to run the label_image.py code from TensorFlow team
#top5_things = os.system(python label_image.py --graph=retrained_graph.pb
# --labels=retrained_labels.txt
#--image=/home/pi/suspects/suspected_elephant1.jpg)
#now what is in the string top5_things?
#we need to search through the string for our elephant types and the
#probabilites
# global elephant_type
# so the elephant type is a global variable so we can access it outside
# of this function
# returns is_it_elephant with "YES" or "NO"
# archive the confirmed elephants
# delete the suspected elephant photos
#######################################################################
###### 4. Deter elephants!
### This is going to run if IsThisElephant returned YES and we have got yes_audio
### hard-coded as yes. Remember this is PYTHON 3.x.x CODE!
def DeterElephants():
#we setup as client, to message the deter device telling it to perform deter
message = "yes_audio"
serverMACAddress = '43:43:A1:12:1F:AC'
port = 9
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress, port))
s.send(bytes(message, 'UTF-8'))
s.close()
# now we sent the message, so we set up as server and wait for message back
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
client, clientInfo = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data)
# echo back to client so it knows we got the data
except:
print("Closing socket")
client.close()
s.close()
# so we got a message back. If it was "done" we know the deter was done
# and we can set deter_done as 1
if data == "done":
deter_done = 1
else:
deter_done = 0
return deter_done
#################################
#### Send out SMS: Since we don't have mobile connectivity here, we act
#as a client, sending message to dayPi with elephant type
# we need socket and time. Pass global variable of elephant_type to this
def SendMessageToDayPiForSMS(typeofelephant):
UDP_IP = "169.254.105.132"
UDP_PORT = 5005
MESSAGE= typeofelephant
#print("Debug: UDP_IP is ", UDP_IP)
#print("Debug: UDP_PORT is ", UDP_PORT)
#time.sleep(2)
#print("Debug: Sending my message")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
# we could wait for a confirmation message back by becoming server now
# if we wanted to.
######
import time
import socket
import RPi.GPIO as GPIO
import os
from picamera import PiCamera
SendMessageToDayPiForSMS(typeofelephant = "herd")
while True:
day_or_night = CheckLightCondition()
print(day_or_night)
while day_or_night == "DAY":
day_or_night = CheckLightCondition()
#It's night-time so we are in a night while loop until it becomes light!
print("We are running it is night")
#now let's check the PIR and get into our PIR while loop
#remember it depends if the PIR returns "something_here" or "nothing_here"
PIR = CheckPIR()
if PIR == "something_here":
print("something spotted - now take images")
# 1. So first activate IR illuminator
#GPIO.setmode(GPIO.BOARD)
#GPIO numbering as board
#GPIO.setup(7, GPIO.OUT)
#setup GPIO 7 for switching on the IR illumination device circuit
#GPIO.ouput(7, 1)
# output from 7 is HIGH i.e. 3.3v
#print("PIN 7 is output HIGH")
# 2. Now let's capture 10 images!
camera = None
camera = PiCamera()
i = 0
# set counter and i to zero
counter = 1
try:
while i <5:
#we run this loop only 5 times. I.e. acquire 5 images for sending
#to elephant detector software
counter = counter + 1
camera.capture('/home/pi/suspects/suspected_elephant%s.jpg' % counter)
#take the image and name file with the counter value at end
i = i+1
# increment the counter and i
time.sleep(3)
# take a break for 3 seconds
os.system("rm -rf suspects/suspected*.jpg")
#delete the files, but would do this after passing to
#detector
time.sleep(5)
except:
print("error")
#send images to detector now
#if we go elephants then
#notify deter device we have hard-coded "yes_audio" as the message
#it's python 3.x.x and this is written in python 2.x.x so we
#call with os.system
os.system("python3 notify_deter.py")
#notify users by SMS if elephant found and send type of elephant
#that we spotted
SendMessageToDayPiForSMS(typeofelephant = "herd")
if PIR == "nothing_here":
print("nothing spotted - keep watching")
else:
print("it's night! Time for us to stop!")
#set up server to listen for SMS messages from nightPi
UDP_IP = "169.254.109.48"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
print("Debug: I am the dayPi camera elephant detection pi")
time.sleep(2)
print("Debug: I am waiting now for SMS messages from nightPi!")
while True:
data = sock.recvfrom(1024)
print("got following message from server:", data[0])
[not quite complete]
See the GitHub here:
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.