跳到正文

算法·理论

物联网代码

hubenyu2010约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年4月7日
收录
2025年12月12日
更新
2026年11月3日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

Lesson 1

Python
# -*- coding: utf_8 -*-
from npython import *           # 导入npython模块
#在下面开始写你自己的代码
while True:
	a1 = light.read()#光线
	oled.print(1,1,'当前光线:' + str(a1),1) #屏幕显示
	a2 = aht20.read_temp()#环境温度采集
	oled.print(1,2,'当前温度:' + str(a2) + '℃',1)

Lesson 2

Python
# -*- coding: utf_8 -*-
from npython import *           # 导入npython模块
#在下面开始写你自己的代码
while True:
	a1 = sound.read()#声音(噪音)采集
	if a1>2000:
		led.on()#LED开
		oled.print(1,1,'当前声音:' + str(a1),1) #屏幕显示
		time.sleep(0.5)#延时
	else:
		led.off()#LED关
		oled.print(1,1,'当前声音:' + str(a1),1) #屏幕显示
		time.sleep(0.5)#延时

拓展1

Python
# -*- coding: utf_8 -*-
from npython import *           # 导入npython模块
#在下面开始写你自己的代码
while True:
	a1 = button.get_a()
	if a1:
		oled.print(1,1,'你按了按键A',1) #屏幕显示
		rgb.write_left(255,0,0)#左侧RGB灯
		rgb.write_right(0,255,0) #右侧RGB灯

	# rgb.write_left(x,y,z) x份红色,y份绿色, z份蓝色
	# button.get_a()#按键A

拓展2

Python
# -*- coding: utf_8 -*-
from npython import *           # 导入npython模块
#在下面开始写你自己的代码
rgb.write_left(0,0,0)#左侧RGB灯
rgb.write_right(0,0,0) #右侧RGB灯
sum = 0;
while True:
	a1 = button.get_a()
	if a1:
		oled.print(1,1,'你按了按键A',1) #屏幕显示
		rgb.write_left(255,0,0)#左侧RGB灯
		rgb.write_right(0,255,0) #右侧RGB灯
		time.sleep(1)#延时
		rgb.write_left(0,0,0)#左侧RGB灯
		rgb.write_right(0,0,0) #右侧RGB灯
		sum = sum + 1;
	# rgb.write_left(x,y,z) x份红色,y份绿色, z份蓝色
	# button.get_a()#按键A
	oled.print(1,1,str(sum),1) #屏幕显示

Lesson 3

Python
# -*- coding: utf_8 -*-
from npython import *           # 导入npython模块
#在下面开始写你自己的代码
while True:
	if button.get_a() == 1:
		oled.print(1,1,"按键A已按下",1) #屏幕显示
		rgb.write_right(255,0,0)#右侧RGB灯
	else:
		oled.print(1,1,"按键A已松开",1) #屏幕显示
		rgb.write_right(0,0,0)#右侧RGB灯
原文