一、【项目介绍】
一直以来对冬天是又爱又恨,爱是因为冬天可以让人“慢”下来,但冬季的寒冷,让小朋友们缺少运动,宝贝的体质下降,让宝爸宝妈们非常担心,今天分享《父与子的急速攀登》项目,让宝爸和宝贝一起游戏,让我们的宝贝在寒冷的冬天里爱上运动!同时,感谢DF搭建平台——暖冬行动。
二、【功能介绍】:
1.项目由掌控板、舵机、灯带、掌控宝、扩展板、锂电池等组成;舵机接掌控板P16口,红色灯带接掌控板p13口,绿色灯带接掌控板p8口.
2.当上电后,wifi连接成功,物联网连接成功,显示:"【SIOT】父与子的急速攀登项目",
3.由于掌控板具有三轴加速度传感器感知物体运动的方位,作为运动手柄的发送端,即红蓝宝贝分别带着掌控板同时跳动后,接收端控制不同颜色的灯带分别点亮。
4.如果红方宝贝跳跃的次数达到12次,点亮全部灯带,绿方宝贝灯带强制全部关闭,舵机的指针指向红方,红方获胜计一分;
5.如果绿方宝贝跳跃的次数达到12次,点亮全部灯带,红方宝贝灯带强制全部关闭,舵机的指针指向红方,红方获胜计一分; 掌控板计分成功后,舵机复位,游戏继续开始。
6.通过该项目,让宝贝们喜欢有氧运动,增强体质,保护自己,让家人放心!
材料清单
- 掌控板 X3
- 掌控宝 X2
- 灯带 X2
- 舵机 X1
- goodmake扩展板 X1
- 锂电池 X1
- 乐高积木套件 X1
三、【作品演示】
四、【制作过程】
(一)外观搭建
1.项目接受端的底座由20*20块提供;
2.灯带支架由1*7梁和1*10梁2套、15孔平梁4个、“L”型的梁4个、5孔平梁2个、13孔平梁4个组成;
3.舵机的支架:2*4的块4个组成;
4.接受端掌控板的底座1*4块1个;
(二)SIOT服务器架设
1.SIoT下载地址:
2.准备:SIoT是一个绿色软件,将下载的压缩包解压并打开,双击运行SIoT_windows_1_2(不要关闭小黑窗口),服务器开始运行,查看本机IP:192.168.0.103
3、在浏览器输入网址127.0.0.1:8080,登陆 账号siot,密码:dfrobot,
4、说明:账号密码可以通过config.json文件进行修改。
5、项目中设备id在编写程序时系统根据主题自动建立,也可以编写程序实现。
(三)编写程序
程序分:掌控板red发送端,掌控板green发送端,项目接受端三部分组成。
1.项目接收端程序
步骤1 连接设备,烧录固件
1.程序通过最新的mpythonx-0.6.0编写,下载地址
2.打开mPythonx,数据线连接掌控板,选择串口com20,烧录固件2020-7-14
步骤2 添加wifi和siot对应模块
在wifi模块中添加wifi名称、密码和“物联网”模块中,选取siot对应模块。
步骤3 接收端主程序和子程序
接受端主程序,上电后,初始相关参数,显示无线网连接成功!物联网连接成功!
siot服务器地址:192.168.0.103(根据每次电脑开机后,启动siot1.2观察后,填入ip地址)
账号:siot ;密码: dfrobot。
屏幕显示【SIOT】父与子的急速攀登项目三秒后,进入运动计分模式,等待接受信息。
当收到SIOT主题1发出的信息,当mPython/001为red时,点亮红灯,当mPython/001为green时,点亮绿灯。
当个人灯带全部点亮时,舵机转向自己,计一分后,灯带全部熄灭,重新开始。
即如果红灯最先全亮,绿灯熄灭, 如果绿灯最先全亮,红灯熄灭。
2.项目掌控板red发送端程序
3.项目掌控板geen发送端程序
五、【总结】
今天分享《父与子的急速攀登》项目,让宝爸和宝贝一起在寒冷的冬天里爱上运动!
1.外观可以利用激光切割lasermaker设计一个外壳,让项目更美观
2.舵机转向角度185和75之间,避免舵机损坏,在测试由于转向过大导致有一个舵机出现问题
最后,感谢DF搭建平台——暖冬行动让创客活动没有冬天。
六、【资源】:
1.掌控板geen发送端程序
from mpython import *
import network
from siot import iot
import time
from machine import Timer
my_wifi = wifi()
my_wifi.connectWiFi("88888888", "88888888")
_siot_connected = False
def _siot_callback(_siot_topic, _siot_msg):pass
_topic_1 = "mPython/001"
_topic_2 = ""
_topic_3 = ""
_topic_4 = ""
_topic_5 = ""
_is_shaked = _is_thrown = False
_last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
def on_shaked():pass
def on_thrown():pass
tim11 = Timer(11)
def timer11_tick(_):
global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
if _is_shaked:
_count_shaked += 1
if _count_shaked == 5: _count_shaked = 0
if _is_thrown:
_count_thrown += 1
if _count_thrown == 10: _count_thrown = 0
if _count_thrown > 0: return
x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
_is_thrown = (x * x + y * y + z * z < 0.25)
if _is_thrown: on_thrown();return
if _last_x == 0 and _last_y == 0 and _last_z == 0:
_last_x = x; _last_y = y; _last_z = z; return
diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
_last_x = x; _last_y = y; _last_z = z
if _count_shaked > 0: return
_is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
if _is_shaked: on_shaked()
tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)
def on_shaked():
global y
y = True
if my_wifi.sta.isconnected():
oled.fill(0)
oled.DispChar("无线网连接成功!", 16, 16, 1)
oled.show()
_siot = iot("", "192.168.0.101", user="siot", password="dfrobot")
_siot.set_callback(_siot_callback)
try:
_siot.connect()
_siot_connected = True
except OSError as err:
print("\nOSError: {}".format(err))
if _siot_connected:
oled.fill(0)
oled.DispChar("物联网连接成功!", 16, 16, 1)
oled.show()
_siot.getsubscribe("mPython/001")
_siot.loop()
oled.fill(0)
oled.DispChar("【SIOT】", 0, 0, 1)
oled.DispChar("父与子的急速攀登项目", 0, 24, 1)
oled.DispChar("green控制端", 24, 40, 1)
oled.show()
y = False
while True:
if y:
_siot.publish(_topic_1, str("green"))
y = False
time.sleep(0.5)
2.掌控板red发送端程序
from mpython import *
import network
import ntptime
from siot import iot
import time
from machine import Timer
my_wifi = wifi()
my_wifi.connectWiFi("*******", "*******")
_siot_connected = False
def _siot_callback(_siot_topic, _siot_msg):pass
_topic_1 = "mPython/001"
_topic_2 = ""
_topic_3 = ""
_topic_4 = ""
_topic_5 = ""
_is_shaked = _is_thrown = False
_last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
def on_shaked():pass
def on_thrown():pass
tim11 = Timer(11)
def timer11_tick(_):
global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
if _is_shaked:
_count_shaked += 1
if _count_shaked == 5: _count_shaked = 0
if _is_thrown:
_count_thrown += 1
if _count_thrown == 10: _count_thrown = 0
if _count_thrown > 0: return
x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
_is_thrown = (x * x + y * y + z * z < 0.25)
if _is_thrown: on_thrown();return
if _last_x == 0 and _last_y == 0 and _last_z == 0:
_last_x = x; _last_y = y; _last_z = z; return
diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
_last_x = x; _last_y = y; _last_z = z
if _count_shaked > 0: return
_is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
if _is_shaked: on_shaked()
tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)
def on_shaked():
global x
x = True
ntptime.settime(8, "time.windows.com")
if my_wifi.sta.isconnected():
oled.fill(0)
oled.DispChar("无线网连接成功!", 16, 16, 1)
oled.show()
_siot = iot("", "192.168.0.101", user="siot", password="dfrobot")
_siot.set_callback(_siot_callback)
try:
_siot.connect()
_siot_connected = True
except OSError as err:
print("\nOSError: {}".format(err))
if _siot_connected:
oled.fill(0)
oled.DispChar("物联网连接成功!", 16, 16, 1)
oled.show()
_siot.getsubscribe("mPython/001")
_siot.loop()
oled.fill(0)
oled.DispChar("【SIOT】", 0, 0, 1)
oled.DispChar("父与子的急速攀登项目", 0, 24, 1)
oled.DispChar("red控制端", 32, 40, 1)
oled.show()
x = False
while True:
if x:
_siot.publish(_topic_1, str("red"))
x = False
time.sleep(0.5)
3.掌控板接受端程序
from mpython import *
import network
import ntptime
from siot import iot
from servo import Servo
import neopixel
import time
import framebuf
import font.digiface_30
my_wifi = wifi()
my_wifi.connectWiFi("TP-LINK_kang", "Rzeg@126.com")
_siot_connected = False
def _siot_callback_1(_):pass
def _siot_callback_2(_):pass
def _siot_callback_3(_):pass
def _siot_callback_4(_):pass
def _siot_callback_5(_):pass
def _siot_callback(_siot_topic, _siot_msg):
_siot_topic = str(_siot_topic, "utf-8")
_siot_msg = str(_siot_msg, "utf-8")
if _siot_topic == _topic_1: return _siot_callback_1(_siot_msg)
elif _siot_topic == _topic_2: return _siot_callback_2(_siot_msg)
elif _siot_topic == _topic_3: return _siot_callback_3(_siot_msg)
elif _siot_topic == _topic_4: return _siot_callback_4(_siot_msg)
elif _siot_topic == _topic_5: return _siot_callback_5(_siot_msg)
_topic_1 = "mPython/001"
_topic_2 = ""
_topic_3 = ""
_topic_4 = ""
_topic_5 = ""
my_r = neopixel.NeoPixel(Pin(Pin.P13), n=12, bpp=3, timing=1)
my_g = neopixel.NeoPixel(Pin(Pin.P8), n=12, bpp=3, timing=1)
servo_16 = Servo(16, min_us=750, max_us=2250, actuation_range=180)
def display_font(_font, _str, _x, _y, _wrap, _z=0):
_start = _x
for _c in _str:
_d = _font.get_ch(_c)
if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d[2]
def _siot_callback_1(_siot_msg):
global b, i, a, x, d, c
print(_siot_msg)
print(x)
print(i)
time.sleep_ms(200)
if _siot_msg == "green":
x = x + 1
if x < 13:
my_g[(x - 1)] = (51, 204, 0)
my_g.write()
else:
my_g.fill( (0, 0, 0) )
my_g.write()
x = int(0)
d = d + 1
# 当x=12时,舵机转向自己即=175,b=+1
# 否则i=0时,舵机复位,=90
if x == 12:
servo_16.write_angle(170)
b = b + 1
elif x == 0:
servo_16.write_angle(80)
if _siot_msg == "red":
i = i + 1
if i < 13:
my_r[(i - 1)] = (255, 0, 0)
my_r.write()
else:
my_r.fill( (0, 0, 0) )
my_r.write()
i = int(0)
c = c + 1
# 当i=12时,舵机转向自己即=0,a=+1
# 否则i=0时,舵机复位,=90
if i == 12:
servo_16.write_angle(0)
a = a + 1
elif i == 0:
servo_16.write_angle(80)
ntptime.settime(8, "time.windows.com")
if my_wifi.sta.isconnected():
oled.fill(0)
oled.DispChar("无线网连接成功!", 16, 16, 1)
oled.show()
_siot = iot("", "192.168.0.103", user="siot", password="dfrobot")
_siot.set_callback(_siot_callback)
try:
_siot.connect()
_siot_connected = True
except OSError as err:
print("\nOSError: {}".format(err))
if _siot_connected:
oled.fill(0)
oled.DispChar("物联网连接成功!", 16, 16, 1)
oled.show()
_siot.getsubscribe("mPython/001")
_siot.loop()
a = int(0)
b = int(0)
# 红方的获胜分数
c = int(0)
# 绿方的获胜分数
d = int(0)
# 控制绿灯带的亮和灭数
x = int(0)
# 控制红灯带数目
i = int(0)
servo_16.write_angle(90)
my_r.fill( (0, 0, 0) )
my_r.write()
my_g.fill( (0, 0, 0) )
my_g.write()
my_r.brightness(100 / 100)
my_g.brightness(100 / 100)
oled.fill(0)
oled.DispChar("【SIOT】", 0, 0, 1)
oled.DispChar("父与子的急速攀登项目", 0, 24, 1)
oled.show()
time.sleep(3)
while True:
oled.fill(0)
oled.rect(1, 1, 126, 62, 1)
oled.vline(65, 1, 63, 1)
display_font(font.digiface_30, (str(c)), 8, 8, False, 2)
display_font(font.digiface_30, (str(d)), 70, 8, False, 2)
oled.show()
# 如果红灯最先亮,绿灯熄灭
# 如果绿灯最先亮,红灯熄灭
if a > b:
my_g.fill( (0, 0, 0) )
my_g.write()
x = int(0)
a = int(0)
elif a < b:
my_r.fill( (0, 0, 0) )
my_r.write()
i = int(0)
b = int(0)
gray66662020.11.23
很棒的项目,赞一个