Skip to content

Instantly share code, notes, and snippets.

@MelulekiDube
Created October 16, 2018 16:21
Show Gist options
  • Save MelulekiDube/1ea53df064a41ab64fc10279cb82b3a5 to your computer and use it in GitHub Desktop.
Save MelulekiDube/1ea53df064a41ab64fc10279cb82b3a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
#imports
import spidev
import time as _time
from array import *
from datetime import datetime, timedelta, time
import RPi.GPIO as GPIO
import atexit
#Define Variables
delay = 0.5
ldr_channel = 0
base = 0
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65000 # The port used by the server
#Create SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000 #setting the maximum frequency of the spi
#define input pins for the buttons:
RESET_SWITCH = 11 #button for reset the timer and cleaning the console
FREQUENCY_CHANGE_SWITCH =15 #button to be used to change the frequency of the application
STOP_START_SWITCH = 29 #the button to be used to start and stop the taking of data
DISPLAY_SWITCH = 33 #button to display the results
#variables to be used in the program
frequency = .5
readings_recorded = [None]*5
timer = 0
recording = False
index =0
def exit_handler():
print("Cleaning up")
GPIO.cleanup()
atexit.register(exit_handler)
def delay():
_time.sleep(2)
def freq_change_handler(channel_number):
global frequency
print("Change frequency presse")
if frequency == .5:
frequency = 1
elif frequency == 1:
frequency = 2
elif frequency == 2:
frequency = .5
delay()
def reset_handler(channel_number):
print("\033[H\033[J")
print("reset pressed")
delay()
def start_stop_handler(channel_number):
global recording
print("Stop/Start pressed\n")
recording = True if (recording == False) else False
delay()
#setting board and gpio pins
GPIO.setmode(GPIO.BOARD)
def readadc(adcnum):
# read SPI data from the MCP3008, 8 channels in total
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
data = ((r[1] & 3) << 8) + r[2]
return data
def readLdr():
ldr_reading = readadc(0)
return ldr_reading
def sendData(time):
global HOST
global PORT
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(str(time).encode)
def wavedetected():
global base
wave = True
time_start = time.time()
while(readLdr()-base>100):
if(time.time()- time_start) > 5:
wave = False
base = readLdr()
break
if(wave):
time_end= time.time()
duration = time_end - time_start)
print("Wave lasted " +str(duration))
sendData(duration)
#send this to the server over a socket
def main():
global base
base=readLdr()
while True:
ldr_value = readLdr()
if(ldr_value - base > 100)
wavedetected()
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment