- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdweetProBiDirExample.py
More file actions
Latest commit
69 lines (62 loc) · 2.31 KB
/
Copy pathdweetProBiDirExample.py
File metadata and controls
69 lines (62 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Plant Spinner DweetPro
importRPi.GPIOasGPIO
importtime
importdatetime
importrequests
importjson
start=datetime.datetime.now()
send_url='https://dweetpro.io:443/v2/dweets'
get_url='https://dweetpro.io:443/v2/dweets/latest'
dweet_header= { 'X-DWEET-AUTH' : 'eyJyb2XXXXXXXXXXXX51351c3b', 'content-type' : 'application/json'}
thing_name='testie'
thing_key='AsiBr-XXXXX-XXxxX-XXxxx-xXXXX-Xxxxx-XxxxX-XXxXX-xxxxx-X-XX'
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set variables for the GPIO motor pins
pinMotorAForwards=10
# How many times to turn to pin on and off each second
Frequency=20
# How long the pin stays on each cycle as a percent
DutyCycleA=90
# Setting the duty cycle to 0 means the motors will not turn
Stop=0
# Set the GPIO Pin mode
GPIO.setup(pinMotorAForwards, GPIO.OUT)
# Set the GPIO to software PWM at 'Frequency' Hertz
pwmMotorAForwards=GPIO.PWM(pinMotorAForwards, Frequency)
# Start the SW PWM with a duty cycle of 0
pwmMotorAForwards.start(Stop)
# Turn the motor off
defStopMotors():
pwmMotorAForwards.ChangeDutyCycle(Stop)
# Turn the motor forwards
defForwards():
pwmMotorAForwards.ChangeDutyCycle(DutyCycleA)
whileTrue:
current=datetime.datetime.now()
diff=current-start
ifint(diff.total_seconds()) %5==0:
print('5 secs')
time.sleep(1)
try:
ret=requests.get(get_url, params={"thing":thing_name,"key":thing_key}, headers=dweet_header) #get latest dweet to check if spin toggle requested
r=ret.json()
latest=r['with'][0]['content']
print(latest)
if'spin_toggle'inlatest:
iflatest['spin_toggle'] ==1:
print('toggling spin')
timestamp=datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
Forwards()
time.sleep(3)
StopMotors()
reset_payload= {
"thing": thing_name,
"key": thing_key,
"content": {"Timestamp":str(timestamp), "spin_toggle":0 }
}
t=requests.post(send_url, data=json.dumps(reset_payload), headers=dweet_header) #send toggle reset
time.sleep(1)
except:
pass
GPIO.cleanup()