-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_wemos_ADXL345.py
60 lines (53 loc) · 1.26 KB
/
board_wemos_ADXL345.py
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
#####################
try:
import cmd
machine.reset()
except:
pass
#####################
from webduino import *
from machine import Pin,I2C
import time,os
from machine import Pin,I2C
import adxl345
import time
i2c = I2C(scl=Pin(4),sda=Pin(5), freq=10000)
adx = adxl345.ADXL345(i2c)
gnd = Pin(15,Pin.OUT)
gnd.off()
# wemos
wemos = Board('adxl345')
wemos.connect("KingKit_2.4G","webduino")
def runCode(ctrl):
global sampleRate
sampleRate=int(ctrl)
print("start...",wemos.deviceId)
#wemos.onMsg("wa5499/adxl345",runCode)
sampleRate = 50
val = 10
lastVal = 0
while True:
x=adx.xValue
y=adx.yValue
z=adx.zValue
#print('The acceleration info of x, y, z are:%d,%d,%d'%(x,y,z))
roll,pitch = adx.RP_calculate(x,y,z)
if(abs(roll)<15 and abs(pitch)<15):
val = 1
elif(abs(roll)>165 and abs(pitch)<15):
val = 6
elif(pitch>-90 and pitch<-70):
val = 2
elif(roll<0 and abs(roll)>80 and abs(pitch)<10):
val = 3
elif(roll>70 and abs(pitch)<10):
val = 4
elif(pitch>70):
val = 5
if val != lastVal:
lastVal = val
#print('roll=',roll,',pitch=',pitch)
print(val)
wemos.mqtt.pub("wa5499/adxl345",str(val))
time.sleep_ms(sampleRate)
wemos.check()