2021年4月16日 星期五

物聯網MQTT自動泡泡機

今天到中興高中教智慧農業課程,帶領學生DIY,把泡泡機當水車用,學生和老師都玩的很高興。

自動泡泡機 DIY材料包:https://www.icshop.com.tw/product-page.php?28134

MicroPython MQTT教材:https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/

L298N驅動兩個馬達:https://sites.google.com/site/zsgititit/home/arduino/arduino-shi-yongl298n-qu-dong-liang-ge-ma-da



修改程式碼:

 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
def sub_cb(topic, msg):
  from machine import Pin
  led = Pin(14, Pin.OUT)
  led2 = Pin(16, Pin.OUT)
  led3 = Pin(4, Pin.OUT)
  led4 = Pin(5, Pin.OUT)
  print((topic, msg))
  if topic == b'notification':
    print('ESP received hello message')
    if msg == b'on':
      print('on')
      led.value(1)
      led2.value(0)
    elif msg == b'off':
      print('off')
      led.value(0)
      led2.value(0)
    elif msg == b'on2':
      print('on2')
      led3.value(1)
      led4.value(0)
    elif msg == b'off2':
      print('off3')
      led3.value(0)
      led4.value(0)