def xugu(): global value, ret, types while True: if ret == 0: xugu_pin = Pin(pin, Pin.IN) value = xugu_pin.read_digital() ret = None if ret == 1: xugu_pin = Pin(pin, Pin.ANALOG) value = xugu_pin.read_analog() ret = None if types in type_D_list: xugu_pin = Pin(pin, Pin.OUT) if value == 0: xugu_pin.write_digital(value) else: value = 1 xugu_pin.write_digital(value) types = None if types in type_A_list: xugu_pin = Pin(pin, Pin.OUT) xugu_pin.write_analog(value) types = None if types in type_S_list: xugu_servo = Servo(pin) xugu_servo.write_angle(value) types = None
import siot import time from xugu import Pin # 从 xugu 库中导入 Pin类 p = Pin("A0", Pin.ANALOG) # 初始化 A0 引脚,设置为输入模式 SERVER = "192.168.0.101" #MQTT服务器IP CLIENT_ID = "" #在SIoT上,CLIENT_ID可以留空 IOT_pubTopic = 'DIY/TEST01' #“topic”为“项目名称/设备名称” IOT_UserName = '******' #用户名 IOT_PassWord = '******' #密码 siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) def sub_cb(client, userdata, msg): print("\nTopic:" + str(msg.topic) + " Message:" + str(msg.payload)) siot.connect() siot.set_callback(sub_cb) siot.getsubscribe(IOT_pubTopic) siot.loop() while True: TDS = p.read_analog() #读取 A0 引脚的模拟量 siot.publish(IOT_pubTopic, "%d" % TDS) time.sleep(1)
from xugu import Pin, XuguLog, SerialMgt # 从 xugu 库中导入 Pin、XuguLog、SerialMgt 类 import time # 导入时间模块 test = 20 # 计数 p = Pin("A0", Pin.ANALOG) # 初始化 A0 引脚,设置为输入模式 ser = SerialMgt() #初始化串口 f = XuguLog( 'read_analog_20_times.log') #当不指定路径时,log文件保存在与digital_input.py同级路径下 #f=XuguLog('/home/scope/analog_input.log') # 初始化日志对象,当指定路径时log文件保存在指定路径下 while test > 0: value = p.read_analog() #读取 A0 引脚的电压值 f.write(str(value)) # 将变量 value 写入到日志文件中 print(value) # 将value的值打印到终端 ser.write(str(value).encode()) # 将value的值写入串口 test -= 1 # 计数自减1 time.sleep(0.5) # 持续500ms
from xugu import Pin #从xugu库中导入Pin类 import time #导入时间模块 lightSensor_pin = "a1" #定义模拟输入引脚 led_pin = 11 #定义LED控制引脚 lightSensor = Pin(lightSensor_pin, Pin.ANALOG) #创建Pin对象 led = Pin(led_pin, Pin.OUT) #创建Pin对象 test = 30 #计数 while test > 0: value = lightSensor.read_analog() #读取A1脚的模拟输入值 print(value) if value > 200: #判断A1脚获取的值是否大于200 led.write_digital(1) #点亮蓝色的LED灯 else: led.write_digital(0) #熄灭LED灯 time.sleep(1) #睡眠1秒 test -= 1 #计数自减1 led.write_digital(0)
import siot #导入siot库 import time #导入time库 from xugu import Pin # 从 xugu 库中导入 Pin类 p = Pin("A0", Pin.ANALOG) # 初始化 A0 引脚,设置为输入模式 SERVER = "192.168.0.101" #MQTT服务器IP CLIENT_ID = "" #在SIoT上,CLIENT_ID可以留空 IOT_pubTopic = 'DIY/TEST01' #“topic”为“项目名称/设备名称” IOT_UserName = '******' #用户名 IOT_PassWord = '******' #密码 siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) #将上述配置信息作为siot客户端参数 siot.connect() #连接siot服务器 while True: TDS = p.read_analog() #读取 A0 引脚的模拟量并赋值给变量TDS siot.publish(IOT_pubTopic, "%d" % TDS) #发布检测到的TDS值 time.sleep(1) #等待1秒
from xugu import Pin #从xugu库中导入Pin类 import time #导入时间模块 potentiometer_pin = "a0" #定义模拟输入引脚 potentiometer = Pin(potentiometer_pin, Pin.ANALOG) #创建Pin对象 test = 30 #计数 while test > 0: value = potentiometer.read_analog() #读取A0脚的模拟输入值 print(value) #将读取的值打印到终端 time.sleep(1) #睡眠1秒 test -= 1 #计数自减1
from xugu import Pin #从xugu库中导入Pin类 p = Pin("A0",Pin.ANALOG) #创建Pin对象,并初始化A0引脚 p10 = Pin(10,Pin.PWM) #创建Pin对象,并初始化10号引脚 p10.write_analog(int(p.read_analog()/4)) #根据A0的模拟电位值,设置对应引脚的电位值