{"id":771,"date":"2022-12-17T16:48:24","date_gmt":"2022-12-17T08:48:24","guid":{"rendered":"http:\/\/wordpress.loc\/?p=771"},"modified":"2022-12-17T16:49:41","modified_gmt":"2022-12-17T08:49:41","slug":"esp32_ch2-21","status":"publish","type":"post","link":"https:\/\/www.nmking.io\/index.php\/2022\/12\/17\/771\/","title":{"rendered":"\u4f7f\u7528Python\u8b80\u53d6ESP32CAM\u63a8\u64adMQTT\u7684\u5f71\u50cf"},"content":{"rendered":"\n<figure class=\"wp-block-pullquote\"><blockquote><p>ESP32CAM\u53ef\u4ee5\u900f\u904eMQTT\u9054\u5230\u76f4\u64ad\u7684\u529f\u80fd\uff0c\u5c07\u5f71\u50cf\u63a8\u64ad\u5230\u624b\u6a5f\u4e0a\uff0cAndroid\u624b\u6a5f\u53ef\u4ee5\u5229\u7528Mqtt dash\u4f86\u63a5\u6536\u5f71\u50cf\uff0c\u5c31\u53ef\u4ee5\u5728\u4e16\u754c\u4e0a\u4efb\u4f55\u5730\u65b9\u770b\u5230ESP32CAM\u7684\u5f71\u50cf\u4e86<\/p><\/blockquote><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6211\u7684ESP32\u5be6\u505a\u66f8\u7c4d\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.nmking.io\/index.php\/2022\/11\/10\/esp32_ch1-2\/\" target=\"_blank\">\u6211\u51fa\u66f8\u4e86 ESP32 \u7269\u806f\u7db2\u5c08\u984c<\/a><br>\u535a\u5ba2\u4f86\u7db2\u5740\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.books.com.tw\/products\/0010901195\" target=\"_blank\">https:\/\/www.books.com.tw\/products\/0010901195<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p>ESP32CAM\u53ef\u4ee5\u900f\u904eMQTT\u9054\u5230\u76f4\u64ad\u7684\u529f\u80fd\uff0c\u5c07\u5f71\u50cf\u63a8\u64ad\u5230\u624b\u6a5f\u4e0a\uff0cAndroid\u624b\u6a5f\u53ef\u4ee5\u5229\u7528Mqtt dash\u4f86\u63a5\u6536\u5f71\u50cf\uff0c\u5c31\u53ef\u4ee5\u5728\u4e16\u754c\u4e0a\u4efb\u4f55\u5730\u65b9\u770b\u5230ESP32CAM\u7684\u5f71\u50cf\u4e86<\/p>\n\n\n\n<p>\u672c\u90e8\u4efd\u8acb\u53c3\u95b1\uff1a<a href=\"https:\/\/www.nmking.io\/index.php\/2022\/12\/15\/esp32_ch2-16\/\">ESP32CAM <\/a><a href=\"https:\/\/www.nmking.io\/index.php\/2022\/12\/15\/esp32_ch2-16\/\" target=\"_blank\" rel=\"noreferrer noopener\">MQTT<\/a><a href=\"https:\/\/www.nmking.io\/index.php\/2022\/12\/15\/esp32_ch2-16\/\">\u76f4\u64ad<\/a><\/p>\n\n\n\n<p>\u4e0d\u904e\u6709\u4e9biOS\u4f7f\u7528\u8005\u5c31\u6c92\u6709\u5408\u9069\u7684APP\u53ef\u4ee5\u7528\uff0c\u5c31\u9023PC\u4e0a\u4e5f\u6c92\u6709\u5408\u9069\u7684MQTT Client\u53ef\u4ee5\u7528\uff0c\u90a3\u5c31\u81ea\u5df1\u5beb\u4e00\u500b\u5427\uff5e<\/p>\n\n\n\n<p>\u672c\u7a0b\u5f0f\u63a1\u7528Python\u8a9e\u6cd5\uff0c\u8acb\u5148\u81ea\u884cpip\u5b89\u88ddOpencv\u53capaho-mqtt\u7a0b\u5f0f\u5eab\uff0c\u7a0b\u5f0f\u57f7\u884c\u524d\uff0c\u8acb\u4fee\u6539MQTT Broker\u53caSubTopic1\u7b49\u53c3\u6578<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u8acb\u5148\u5b89\u88dd1.paho-mqtt\u30012.opencv\nimport paho.mqtt.client as mqtt\nimport cv2\nMqttBroker=\"mqtt.eclipseprojects.io\"\nMqttPort=1883\nSubTopic1=\"youjunjer1030\/storage308\/pic\"\n \n#\u8a2d\u5b9a\u9023\u7dda\u6210\u529f\u6642\u7684Callback\ndef on_connect(client, userdata, flags, rc):\n    print(\"\u9023\u7dda\u7d50\u679c\uff1a\" + str(rc))\n    #\u8a02\u95b1\u4e3b\u984c\n    client.subscribe(SubTopic1)\n    \n#\u8a2d\u5b9a\u8a02\u95b1\u66f4\u65b0\u6642\u7684Callback\ndef on_message(client, userdata, msg):\n    f = open('receive.jpg','wb+') #\u958b\u555f\u6a94\u6848\n    f.write(msg.payload)#\u5beb\u5165\u6a94\u6848\n    f.close()#\u95dc\u9589\u6a94\u6848\n    #\u986f\u793a\u5f71\u50cf\u6a94\n    img=cv2.imread('receive.jpg')\n    #img=cv2.resize(img,(640,480))\n    cv2.imshow('image', img)\n    key=cv2.waitKey(1)\n    # \u6309q\u96e2\u958b\n    if key &amp; 0xFF == ord('q'):\n        exit()\n    print('image received')\n \n#\u8a2d\u5b9aMqtt\u9023\u7dda    \nclient = mqtt.Client()\nclient.on_connect = on_connect\nclient.on_message = on_message\nclient.connect(MqttBroker, MqttPort, 60)\n#\u7b49\u5019\u8a02\u95b1\nclient.loop_forever()\n<\/code><\/pre>\n\n\n\n<p>\u4e0d\u904e\u76ee\u524d\u6709\u4e00\u500b\u5c0f\u7f3a\u9ede\uff0c\u5c31\u662f\u4f7f\u7528Opencv\u7684imshow\u6703\u6709\u9ede\u5361\u5361\u7684\uff0c\u4ee5\u5f8c\u518d\u4f86\u627e\u5176\u4ed6\u986f\u793a\u5716\u7247\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1628081512-1513009165-g_n.png?resize=839%2C632&#038;ssl=1\" alt=\"\" width=\"839\" height=\"632\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>ESP32CAM\u53ef\u4ee5\u900f\u904eMQTT\u9054\u5230\u76f4\u64ad\u7684\u529f\u80fd\uff0c\u5c07\u5f71\u50cf\u63a8\u64ad\u5230\u624b\u6a5f\u4e0a\uff0cAndroid\u624b\u6a5f\u53ef\u4ee5\u5229\u7528Mqtt das [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1,26,5],"tags":[],"class_list":["post-771","post","type-post","status-publish","format-standard","hentry","category-esp32","category-esp32cam","category-python"],"blocksy_meta":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":1,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/771\/revisions\/772"}],"wp:attachment":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}