{"id":1446,"date":"2024-06-27T21:15:15","date_gmt":"2024-06-27T13:15:15","guid":{"rendered":"http:\/\/www.liujh168.com\/?p=1446"},"modified":"2025-04-30T08:09:31","modified_gmt":"2025-04-30T00:09:31","slug":"controller","status":"publish","type":"post","link":"https:\/\/www.liujh168.com\/index.php\/2024\/06\/27\/controller\/","title":{"rendered":"\u7269\u8054\u7f51\u70b9\u706f\u5927\u5e08\uff08\u56db\uff09\u667a\u80fd\u706f\u63a7\u5236\u5668\u8f6f\u4ef6"},"content":{"rendered":"<p>\u4e00\u3001\u7f16\u5199\u5ba2\u6237\u7aef\u4ee3\u7801<br \/>\n<!--more--><br \/>\n\u4e8c\u3001\u7f16\u5199\u8bbe\u5907\u7aef\u63a7\u5236\u4ee3\u7801<br \/>\n[codesyntax lang=&#8221;python&#8221;]<br \/>\nfrom machine import Pin, Timer<br \/>\nfrom neopixel import NeoPixel<br \/>\nfrom simple import MQTTClient<br \/>\nimport time<br \/>\nimport network<\/p>\n<p>#\u5b9a\u4e49RGB\u63a7\u5236\u5bf9\u8c61<br \/>\n#\u63a7\u5236\u5f15\u811a\u4e3a16\uff0cRGB\u706f\u4e32\u80545\u4e2a<br \/>\npin=16<br \/>\nrgb_num=5<br \/>\nrgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)  <\/p>\n<p>#\u5b9a\u4e49RGB\u989c\u8272<br \/>\nRED = (255, 0, 0)<br \/>\nORANGE = (255, 165, 0)<br \/>\nYELLOW = (255, 150, 0)<br \/>\nGREEN = (0, 255, 0)<br \/>\nBLUE = (0, 0, 255)<br \/>\nINDIGO = (75, 0, 130)<br \/>\nVIOLET = (138, 43, 226)<br \/>\nBLACK = (0, 0, 0)<\/p>\n<p>COLORS = (RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET)<\/p>\n<p>def RGB_led_off():<br \/>\n    for i in range(rgb_num):<br \/>\n        rgb_led[i]=(0, 0, 0)<br \/>\n        rgb_led.write()<\/p>\n<p>def RGB_led_on():<br \/>\n    for color in COLORS:<br \/>\n        for i in range(rgb_num):<br \/>\n            rgb_led[i]=(color[0], color[1], color[2])<br \/>\n            rgb_led.write()<br \/>\n            time.sleep_ms(100)<br \/>\n        time.sleep_ms(1000)<\/p>\n<p>#\u4ee5\u4e0bMQTT\u76f8\u5173<br \/>\nmsg = &#8220;&#8221;<br \/>\nmsg_count=0<br \/>\nled1 = Pin(15, Pin.OUT) # \u5b9a\u4e49LED\u63a7\u5236\u5bf9\u8c61<\/p>\n<p># \u914d\u7f6e\u4fe1\u606f<br \/>\nCONFIG = {<br \/>\n    &#8220;ssid&#8221;: &#8220;liujh&#8221;,<br \/>\n    &#8220;password&#8221;: &#8220;&#8221;,<br \/>\n    &#8220;mqtt_server&#8221;: &#8220;&#8221;,<br \/>\n    &#8220;mqtt_port&#8221;: 1083,<br \/>\n    &#8220;client_id&#8221;: &#8220;ESP32_Client&#8221;,<br \/>\n    &#8220;topic&#8221;: &#8220;mytest&#8221;,<br \/>\n    &#8220;mqtt_broker_username&#8221;: &#8220;ljh&#8221;,<br \/>\n    &#8220;mqtt_broker_password&#8221;: &#8220;&#8221;<br \/>\n}<\/p>\n<p># WIFI\u8fde\u63a5\u51fd\u6570<br \/>\ndef wifi_connect(ssid, password, retries=5, delay=15):  # Wi-Fi\u8fde\u63a5\u51fd\u6570<br \/>\n    wlan = network.WLAN(network.STA_IF)  # STA\u6a21\u5f0f<br \/>\n    wlan.active(True)  # \u6fc0\u6d3b\u7f51\u7edc\u63a5\u53e3<br \/>\n    start_time = time.time()  # \u8bb0\u5f55\u5f00\u59cb\u8fde\u63a5\u7684\u65f6\u95f4<\/p>\n<p>    if not wlan.isconnected():<br \/>\n        print(&#8220;Connecting to network&#8230;&#8221;)<br \/>\n        wlan.connect(ssid, password)  # \u8fde\u63a5\u5230\u6307\u5b9aWIFI<br \/>\n        while not wlan.isconnected():<br \/>\n            led1.value(1)  # LED\u95ea\u70c1\u8868\u793a\u6b63\u5728\u5c1d\u8bd5\u8fde\u63a5<br \/>\n            time.sleep_ms(300)<br \/>\n            led1.value(0)<br \/>\n            time.sleep_ms(300)<\/p>\n<p>            # \u8d85\u8fc715\u79d2\u672a\u8fde\u63a5\u6210\u529f\uff0c\u89c6\u4e3a\u8d85\u65f6<br \/>\n            if time.time() &#8211; start_time > delay:<br \/>\n                print(&#8220;WIFI Connect Timeout!&#8221;)<br \/>\n                led1.value(0)  # \u8d85\u65f6\u540e\u5173\u95edLED<br \/>\n                return False<\/p>\n<p>    led1.value(1)  # LED\u5e38\u4eae<br \/>\n    print(&#8220;Network connected!&#8221;)<br \/>\n    print(&#8220;Network information:&#8221;, wlan.ifconfig())<br \/>\n    return True<\/p>\n<p>#\u8bbe\u7f6e MQTT \u56de\u8c03\u51fd\u6570,\u6709\u4fe1\u606f\u65f6\u5019\u6267\u884c<br \/>\ndef mqtt_callback(topic,msg):<br \/>\n    # \u5c06\u5b57\u8282\u7c7b\u578b\u7684\u6d88\u606f\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<br \/>\n    message_str = msg.decode(&#8216;utf-8&#8217;)<br \/>\n    print(&#8220;topic: {}&#8221;.format(topic))<br \/>\n    print(&#8220;msg: {}&#8221;.format(message_str))<br \/>\n    print(f&#8221;Received message from topic {topic}: {message_str}&#8221;)<br \/>\n    if message_str == &#8220;on&#8221;:<br \/>\n        print(&#8220;\u8981\u6211\u5f00\u706f\u4e86\u54e6!&#8221;)<br \/>\n        RGB_led_on()<br \/>\n        client.publish(CONFIG[&#8216;topic&#8217;], &#8220;switch on! switch on! switch on! switch on! switch on! &#8220;)  #\u8fd9\u91cc\u4e00\u53d1\u4e2d\u6587\u5c31\u51fa\u9519\uff0c\u539f\u56e0\u4e0d\u660e<br \/>\n    if message_str == &#8220;off&#8221;:<br \/>\n        print(&#8220;\u8981\u5173\u706f\u554a\uff1f&#8221;)<br \/>\n        RGB_led_off()<br \/>\n        client.publish(CONFIG[&#8216;topic&#8217;], &#8220;switch off! switch off! switch off! switch off! switch off! &#8220;)<br \/>\n    if message_str == &#8220;bye&#8221;:  # \u73b0\u5728msg\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4e0e\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83<br \/>\n        print(f&#8221;\u518d\u89c1\u6d88\u606f\u6765\u4e86\u5566  &#8212;>  {topic}: {message_str}&#8221;)<br \/>\n        client.publish(CONFIG[&#8216;topic&#8217;], &#8220;no bye bye!&#8221;)<\/p>\n<p># \u4e3b\u7a0b\u5e8f<br \/>\nif __name__ == &#8220;__main__&#8221;:<br \/>\n    if wifi_connect(CONFIG[&#8216;ssid&#8217;], CONFIG[&#8216;password&#8217;]):<br \/>\n        try:<br \/>\n            SERVER=&#8221;&#8221;<br \/>\n            PORT=1083<br \/>\n            CLIENT_ID=&#8221;ESP32_Client&#8221;  #\u5ba2\u6237\u7aefID<br \/>\n            TOPIC=&#8221;mytest&#8221;  #TOPIC\u540d\u79f0<br \/>\n            client = MQTTClient(CLIENT_ID, SERVER, PORT, &#8220;&#8221;, &#8220;&#8221;)  #\u5efa\u7acb\u5ba2\u6237\u7aef<br \/>\n            #client = MQTTClient(CONFIG[&#8216;client_id&#8217;], CONFIG[&#8216;mqtt_server&#8217;], CONFIG[&#8216;mqtt_port&#8217;],<br \/>\n            #                    CONFIG[&#8216;mqtt_broker_username&#8217;], CONFIG[&#8216;mqtt_broker_password&#8217;], keepalive=60)<br \/>\n            client.set_callback(mqtt_callback)  #\u914d\u7f6e\u56de\u8c03\u51fd\u6570<br \/>\n            client.connect()<br \/>\n            client.subscribe(TOPIC)  #\u8ba2\u9605\u4e3b\u9898<br \/>\n            client.publish(TOPIC, CONFIG[&#8216;client_id&#8217;])<br \/>\n            print(f&#8221;MQTT Connected \u3001Subscribed and first publish message ->  {CONFIG[&#8216;topic&#8217;]}:{CONFIG[&#8216;client_id&#8217;]}&#8221;)<br \/>\n        except Exception as e:<br \/>\n            print(e)<\/p>\n<p>        while True:<br \/>\n            msg_count += 1<br \/>\n            msg = f&#8221;Message {msg_count} from ESP32&#8243;<br \/>\n            if msg_count % 6  ==0 :<br \/>\n                try:<br \/>\n                    client.publish(CONFIG[&#8216;topic&#8217;], msg)<br \/>\n                    print(f&#8221;send message to topic  {CONFIG[&#8216;topic&#8217;]}:{msg}&#8221;)<br \/>\n                except Exception as e:<br \/>\n                    print(f&#8221;MQTT connection failed: {e}&#8221;)<\/p>\n<p>            client.check_msg()<br \/>\n            print(msg)<br \/>\n            time.sleep(1)  # \u4f11\u772010\u79d2<\/p>\n<p>[\/codesyntax]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u7f16\u5199\u5ba2\u6237\u7aef\u4ee3\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,1],"tags":[],"class_list":["post-1446","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-unknown"],"_links":{"self":[{"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/posts\/1446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/comments?post=1446"}],"version-history":[{"count":6,"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/posts\/1446\/revisions"}],"predecessor-version":[{"id":1568,"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/posts\/1446\/revisions\/1568"}],"wp:attachment":[{"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/media?parent=1446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/categories?post=1446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liujh168.com\/index.php\/wp-json\/wp\/v2\/tags?post=1446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}