Commit cf2cd9f8 by Lambok Sinaga

no message

parent fbbe41fb
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
def beep(pin,beeps,beep_time,time_between_beep):
GPIO.setup(pin, GPIO.OUT)
n = int(beeps)
for i in range(n):
GPIO.output(pin, GPIO.HIGH)
time.sleep(beep_time)
GPIO.output(pin, GPIO.LOW)
time.sleep(time_between_beep)
return n
if __name__ == "__main__":
beep(2)
GPIO.cleanup()
import time
import RPi
class DHT11Result:
'DHT11 sensor result returned by DHT11.read() method'
ERR_NO_ERROR = 0
ERR_MISSING_DATA = 1
ERR_CRC = 2
error_code = ERR_NO_ERROR
temperature = -1
humidity = -1
def __init__(self, error_code, temperature, humidity):
self.error_code = error_code
self.temperature = temperature
self.humidity = humidity
def is_valid(self):
return self.error_code == DHT11Result.ERR_NO_ERROR
class DHT11:
'DHT11 sensor reader class for Raspberry'
__pin = 0
def __init__(self, pin):
self.__pin = pin
def read(self):
RPi.GPIO.setup(self.__pin, RPi.GPIO.OUT)
# send initial high
self.__send_and_sleep(RPi.GPIO.HIGH, 0.05)
# pull down to low
self.__send_and_sleep(RPi.GPIO.LOW, 0.02)
# change to input using pull up
RPi.GPIO.setup(self.__pin, RPi.GPIO.IN, RPi.GPIO.PUD_UP)
# collect data into an array
data = self.__collect_input()
# parse lengths of all data pull up periods
pull_up_lengths = self.__parse_data_pull_up_lengths(data)
# if bit count mismatch, return error (4 byte data + 1 byte checksum)
if len(pull_up_lengths) != 40:
return DHT11Result(DHT11Result.ERR_MISSING_DATA, 0, 0)
# calculate bits from lengths of the pull up periods
bits = self.__calculate_bits(pull_up_lengths)
# we have the bits, calculate bytes
the_bytes = self.__bits_to_bytes(bits)
# calculate checksum and check
checksum = self.__calculate_checksum(the_bytes)
if the_bytes[4] != checksum:
return DHT11Result(DHT11Result.ERR_CRC, 0, 0)
# ok, we have valid data, return it
return DHT11Result(DHT11Result.ERR_NO_ERROR, the_bytes[2], the_bytes[0])
def __send_and_sleep(self, output, sleep):
RPi.GPIO.output(self.__pin, output)
time.sleep(sleep)
def __collect_input(self):
# collect the data while unchanged found
unchanged_count = 0
# this is used to determine where is the end of the data
max_unchanged_count = 100
last = -1
data = []
while True:
current = RPi.GPIO.input(self.__pin)
data.append(current)
if last != current:
unchanged_count = 0
last = current
else:
unchanged_count += 1
if unchanged_count > max_unchanged_count:
break
return data
def __parse_data_pull_up_lengths(self, data):
STATE_INIT_PULL_DOWN = 1
STATE_INIT_PULL_UP = 2
STATE_DATA_FIRST_PULL_DOWN = 3
STATE_DATA_PULL_UP = 4
STATE_DATA_PULL_DOWN = 5
state = STATE_INIT_PULL_DOWN
lengths = [] # will contain the lengths of data pull up periods
current_length = 0 # will contain the length of the previous period
for i in range(len(data)):
current = data[i]
current_length += 1
if state == STATE_INIT_PULL_DOWN:
if current == RPi.GPIO.LOW:
# ok, we got the initial pull down
state = STATE_INIT_PULL_UP
continue
else:
continue
if state == STATE_INIT_PULL_UP:
if current == RPi.GPIO.HIGH:
# ok, we got the initial pull up
state = STATE_DATA_FIRST_PULL_DOWN
continue
else:
continue
if state == STATE_DATA_FIRST_PULL_DOWN:
if current == RPi.GPIO.LOW:
# we have the initial pull down, the next will be the data pull up
state = STATE_DATA_PULL_UP
continue
else:
continue
if state == STATE_DATA_PULL_UP:
if current == RPi.GPIO.HIGH:
# data pulled up, the length of this pull up will determine whether it is 0 or 1
current_length = 0
state = STATE_DATA_PULL_DOWN
continue
else:
continue
if state == STATE_DATA_PULL_DOWN:
if current == RPi.GPIO.LOW:
# pulled down, we store the length of the previous pull up period
lengths.append(current_length)
state = STATE_DATA_PULL_UP
continue
else:
continue
return lengths
def __calculate_bits(self, pull_up_lengths):
# find shortest and longest period
shortest_pull_up = 1000
longest_pull_up = 0
for i in range(0, len(pull_up_lengths)):
length = pull_up_lengths[i]
if length < shortest_pull_up:
shortest_pull_up = length
if length > longest_pull_up:
longest_pull_up = length
# use the halfway to determine whether the period it is long or short
halfway = shortest_pull_up + (longest_pull_up - shortest_pull_up) / 2
bits = []
for i in range(0, len(pull_up_lengths)):
bit = False
if pull_up_lengths[i] > halfway:
bit = True
bits.append(bit)
return bits
def __bits_to_bytes(self, bits):
the_bytes = []
byte = 0
for i in range(0, len(bits)):
byte = byte << 1
if (bits[i]):
byte = byte | 1
else:
byte = byte | 0
if ((i + 1) % 8 == 0):
the_bytes.append(byte)
byte = 0
return the_bytes
def __calculate_checksum(self, the_bytes):
return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
File added
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
def distance(GPIO_ECHO, GPIO_TRIGGER):
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
TimeElapsed = StopTime - StartTime
distance = (TimeElapsed * 34300) / 2
return distance
if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f cm" % dist)
time.sleep(1)
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/test
sudo python run_api.py
cd /
import RPi.GPIO as GPIO
import time
RedPin = 20
YellowPin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(RedPin, GPIO.OUT)
GPIO.setup(YellowPin, GPIO.OUT)
def control_led(colour,mode):
if colour == "red":
pin = RedPin
elif colour == "yellow":
pin = YellowPin
else:
return 0
if mode == "on":
mod = GPIO.HIGH
else:
mod = GPIO.LOW
GPIO.output(pin, mod)
return 1
File added
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 311-096-014
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
def turn_on(pin):
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
def turn_off(pin):
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
if __name__ == "__main__":
try:
while (True):
turn_on(1)
time.sleep(2)
turn_on(2)
time.sleep(2)
turn_off(1)
time.sleep(2)
turn_off(2)
time.sleep(2)
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
#GPIO.output(pin, GPIO.LOW)
turn_off(1)
GPIO.cleanup()
File added
from flask import Flask, jsonify
from relay import relay as rel
from buzzer import buzzer
from distance_sensor import ping
from servo import servo
from dht11 import dht11
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
app = Flask(__name__)
@app.route('/relay/<pin>/<mode>')
def relay(pin,mode):
if mode == "on":
rel.turn_on(int(pin))
else:
rel.turn_off(int(pin))
return jsonify({'status':'ok'})
@app.route('/buzzer/<pin>/<beeps>/<beep_time>/<time_between_beep>')
def buzz(pin,beeps,beep_time,time_between_beep):
buzzer.beep(int(pin), int(beeps), float(beep_time), float(time_between_beep))
return jsonify({'status':'ok'})
@app.route('/ping/<echo>/<trigger>')
def dist(echo,trigger):
jarak = ping.distance(int(echo),int(trigger))
return jsonify({'data':jarak})
@app.route('/servo/<pin>/<angle>')
def servo_run(pin,angle):
servo.SetAngle(int(pin), int(angle))
return jsonify({'status':'ok'})
@app.route('/dht11/<pin>')
def dht_run(pin):
instance = dht11.DHT11(int(pin))
while True:
result = instance.read()
if result.is_valid():
break
#data = jsonify({'temperature':result.temperature,'humidity':result.humidity})
data = jsonify({'data':{'temperature':result.temperature,'humidity':result.humidity}})
return data
if __name__ == "__main__":
try:
app.run(host='0.0.0.0',port=5000,debug=True)
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
GPIO.cleanup()
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
def SetAngle(pin,angle):
GPIO.setup(pin, GPIO.OUT)
pwm=GPIO.PWM(pin, 50)
pwm.start(0)
duty = angle / 18 + 2
GPIO.output(pin, True)
pwm.ChangeDutyCycle(duty)
sleep(1)
GPIO.output(pin, False)
pwm.ChangeDutyCycle(0)
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment