{"id":486994,"date":"2026-07-11T23:19:25","date_gmt":"2026-07-11T23:19:25","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=486994"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=486994","title":{"rendered":"IoT \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043d\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b \u0438 \u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u044b"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0438 \u0434\u043b\u044f \u043a\u043e\u0433\u043e \u043d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e IoT\u2011\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u044e\u0442 \u0438\u0445 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043e\u043d\u0438 \u0436\u0451\u0441\u0442\u043a\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e, \u0442.\u0435. \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0431\u0440\u0435\u043d\u0434\u0430. \u0413\u0430\u0434\u0436\u0435\u0442\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b \u0441 \u043f\u0430\u0440\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u043d\u043e \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u00a0\u043c\u043e\u0433\u0443\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0441\u00a0\u0434\u0440\u0443\u0433\u043e\u043c \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0448\u043b\u044e\u0437\u043e\u0432. \u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0441\u0432\u044f\u0437\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0442\u043e\u0436\u0435 \u043d\u0435\u0442. \u0418\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432: Zigbee, Matter, Z\u2011Wave, Bluetooth, Wi-Fi \u2014 \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0443\u043c\u043d\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043f\u043b\u043e\u0445\u043e \u0434\u0440\u0443\u0436\u0430\u0442 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439.<\/p>\n<p>\u0415\u0441\u0442\u044c \u043b\u0438 \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438?<\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0434\u0430.<\/p>\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u043e \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u0435 MixRech \u0441 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a\u043e\u043c \u0438 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u043e\u043c. <a href=\"https:\/\/habr.com\/ru\/articles\/1030480\" rel=\"noopener noreferrer nofollow\">\u0421\u0441\u044b\u043b\u043a\u0430 \u0442\u0443\u0442<\/a>. \u0418 \u0442\u0430\u043c \u0435\u0441\u0442\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440 MixChat, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u043c\u043d\u044b\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0447\u0430\u0442 \u0431\u043e\u0442\u0430 SmarMix \u0432 \u0441\u0430\u043c\u043e\u043c \u0436\u0435 MixChat.<\/p>\n<h3>SmartMix \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0443\u043c\u043d\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0432 \u043e\u0434\u0438\u043d \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 MQTT<\/h3>\n<p>\u041c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440 MixChat \u0438 \u043c\u0438\u043a\u0440\u043e\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 ESP32 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 MQTT. <\/p>\n<p>\u0412\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 MQTT \u0441\u0435\u0440\u0432\u0435\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 Raspberry Pi \u0438\u043b\u0438 \u0432 \u043e\u0431\u043b\u0430\u043a\u0435), \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0432\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c SmartMix.<\/p>\n<p>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/p>\n<p>\u0417\u0430\u0439\u0434\u044f \u0432 \u0447\u0430\u0442 \u0441\u043e SmartMix, \u0432\u044b \u0432\u0432\u043e\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b MQTT \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u043e \u043a\u043d\u043e\u043f\u043a\u0435 \u0432\u0432\u0435\u0440\u0445\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435. \u041f\u043e\u0442\u043e\u043c \u0432\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435\u0441\u044c \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f ESP32: \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c\/\u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0441\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434, \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u0443 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d.<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/92a\/c75\/ec5\/92ac75ec5ab9fcbce3c02f892b82f93f.png\" alt=\"\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0440\u0432\u0435\u0440\u0430\" title=\"\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0440\u0432\u0435\u0440\u0430\" width=\"1080\" height=\"721\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/92a\/c75\/ec5\/92ac75ec5ab9fcbce3c02f892b82f93f.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/92a\/c75\/ec5\/92ac75ec5ab9fcbce3c02f892b82f93f.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2a8\/beb\/d89\/2a8bebd89db879ccf0135eb6b2cb345f.png\" alt=\"\" title=\"\" width=\"1090\" height=\"728\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/2a8\/beb\/d89\/2a8bebd89db879ccf0135eb6b2cb345f.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2a8\/beb\/d89\/2a8bebd89db879ccf0135eb6b2cb345f.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e\u043c.<\/figcaption><\/div>\n<\/figure>\n<p>SmartMix \u043d\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e. \u0410 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a.<\/p>\n<h3>ESP32 \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 SmartMix, \u0432\u0430\u0448\u0438 \u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435: \u043f\u0430\u0440\u043e\u043b\u0438, \u043d\u043e\u043c\u0435\u0440\u0430 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432, \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u044b\u0435 \u043f\u043e\u0447\u0442\u044b \u0438\u043b\u0438 \u0446\u0438\u0444\u0440\u043e\u0432\u044b\u0435 ID \u043b\u044e\u0434\u0435\u0439 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438. <\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u0432\u043e\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0432\u043e\u0435\u0433\u043e MQTT \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0432.\u0442.\u0447. \u0438 \u0442\u043e\u043f\u0438\u043a\u0438 \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f, \u043e\u043d\u0438 \u0448\u0438\u0444\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043a\u044d\u0448 \u043f\u043e API \u0437\u0430\u043f\u0440\u043e\u0441\u0443:<\/p>\n<pre><code class=\"python\">class SaveMqttView(generics.GenericAPIView):    serializer_class = SaveMqttSerializer    authentication_classes = [JWTAuthentication, CsrfExemptSessionAuthentication]    permission_classes = [IsAuthenticated]    def post(self, request, *args, **kwargs):        serializer = self.get_serializer(data=request.data)        serializer.is_valid(raise_exception=True)        user = request.user.id        server = serializer.validated_data['server']        port = serializer.validated_data['port']        client_id = serializer.validated_data['client_id']        mqtt_username = serializer.validated_data['mqtt_username']        mqtt_password = serializer.validated_data['mqtt_password']        topic_sent = serializer.validated_data['topic_sent']        topic_subscribe = serializer.validated_data['topic_subscribe']        mqtt_values = list(request.session.values())        for value in mqtt_values:            if value == server or value == mqtt_username:                return Response({\"message\": \"\u041e\u0448\u0438\u0431\u043a\u0430. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f\"                                 \" \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443\"}, status=403)        key = gen_key(user, salt)        request.session[f'server_{user}'] = encrypt_text(server, key).decode('utf-8')        request.session[f'port_{user}'] = port        request.session[f'client_id_{user}'] = client_id        request.session[f'mqtt_username_{user}'] = encrypt_text(mqtt_username, key).decode('utf-8')        request.session[f'mqtt_password_{user}'] = encrypt_text(mqtt_password, key).decode('utf-8')        request.session[f'topic_sent_{user}'] = topic_sent        request.session[f'topic_subscribe_{user}'] = topic_subscribe        return Response({\"message\": \"\u0414\u0430\u043d\u043d\u044b\u0435 MQTT \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u044b\"})<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 MQTT-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u043f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435\u0441\u044c \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443.<\/p>\n<p>\u0412 \u0431\u043e\u0442\u0435 \u0437\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 MQTT. \u0412\u043e\u0442 \u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<p>connection()\u00a0\u2014 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0441\u0432\u044f\u0437\u044c \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.<\/p>\n<p>publish(msg)\u00a0\u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0432 \u0442\u043e\u043f\u0438\u043a\u00a0topic_sent.<\/p>\n<p>subscribe()\u00a0\u2014 \u0441\u043b\u0443\u0448\u0430\u0435\u0442 \u0442\u043e\u043f\u0438\u043a\u00a0topic_subscribe, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 ESP32.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import osimport timeimport sysfrom dotenv import load_dotenvfrom paho.mqtt import client as mqtt_clientfrom .cifer import gen_key, decrypt_textload_dotenv()salt = os.getenv('CIFER_SALT')class MQTT:    def __init__(self, mqtt_dict, client, user_id, exit_flag, msg):        key = gen_key(user_id, salt)        self.client = client        self.exit_flag = exit_flag        self.msg = msg        self.server = decrypt_text(mqtt_dict[f'server_{user_id}'], key).decode('utf-8')        self.port = mqtt_dict[f'port_{user_id}']        self.mqtt_username = decrypt_text(mqtt_dict[f'mqtt_username_{user_id}'], key).decode('utf-8')        self.mqtt_password = decrypt_text(mqtt_dict[f'mqtt_password_{user_id}'], key).decode('utf-8')        self.topic_sent = mqtt_dict[f'topic_sent_{user_id}']        self.topic_subscribe = mqtt_dict[f'topic_subscribe_{user_id}']    def connection(self):        self.client.username_pw_set(self.mqtt_username, self.mqtt_password)        # self.client.on_connect = \"Connected to MQTT Broker!\"        if self.server and self.port:            self.client.connect(self.server, int(self.port))            print(\"Connect to server\")        else:            print(\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438. \u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\")    def publish(self, msg):        # \u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 MQTT        if self.topic_sent:            result = self.client.publish(self.topic_sent, msg)            status = result[0]            if status == 0:                print(f\"\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e `{msg}` \u0432 \u0442\u043e\u043f\u0438\u043a `{self.topic_sent}`\")            else:                print(f\"\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0442\u043e\u043f\u0438\u043a {self.topic_sent}\")        else:            print(\"\u041e\u0448\u0438\u0431\u043a\u0430. \u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438\")    def on_connect(self, client, userdata, flags, rc):        if rc == 0:            self.client.subscribe(self.topic_subscribe)        else:            print(\"Failed to connect, return code %d\\n\", rc)    def subscribe(self):        try:            if self.server and self.port:                self.client.username_pw_set(self.mqtt_username, self.mqtt_password)                self.client.on_connect = self.on_connect                self.client.on_message = self.on_message                self.client.connect(self.server, int(self.port))                self.client.loop_start()  # \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0432 \u0444\u043e\u043d\u0435                start = time.time()                while not self.exit_flag:                    time.sleep(0.1)                self.exit_flag = False                print(time.time() - start)                self.client.loop_stop()  # \u041e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0446\u0438\u043a\u043b                print(self.client.is_connected)                self.client.disconnect()            else:                print(\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f. \u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\")        except KeyboardInterrupt:            print(\"\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0440\u0432\u0430\u043d\u043e\")            sys.exit()    def on_message(self, client, userdata, message):        self.msg = message.payload.decode()        print('MQTT MESSAGE:', message.payload.decode())        self.exit_flag = True    def print_message(self):        print(self.msg)        return self.msgdef return_mqtt(mqtt_dict, user_id):    mqtt = MQTT(mqtt_dict, mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1,                mqtt_dict[f'client_id_{user_id}']), user_id,False, '')    return mqtt<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a\u043e\u043c \u0438 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u043e\u043c <a href=\"https:\/\/github.com\/Mik-23\/browser.git\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MQTT-\u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0432\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e MQTT:<\/p>\n<pre><code class=\"python\">mqtt_dict = dict(request.session.items())send_to_mqtt(mqtt_dict, sender_id, content) <\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<pre><code class=\"python\">def send_to_mqtt(mqtt_dict, user_id, msg):    mqtt = return_mqtt(mqtt_dict, user_id)    mqtt.connection()    mqtt.publish(msg)<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0434\u043e\u0445\u043e\u0434\u0438\u0442 \u0434\u043e ESP32, \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u043d\u0430 ESP32. \u0421 ESP32 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u044b).<\/p>\n<p>ESP32 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043a MQTT \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>connect &#8212; \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0441\u0432\u044f\u0437\u044c \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.<\/p>\n<p>set_callback &#8212; \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0442\u043e\u043f\u0438\u043a topic_subscribe.<\/p>\n<p>on_message &#8212; \u043b\u043e\u0432\u0438\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043e\u0442 \u0431\u043e\u0442\u0430 SmartMix<\/p>\n<p>subscribe &#8212; \u0441\u043b\u0443\u0448\u0430\u0435\u0442 \u0442\u043e\u043f\u0438\u043a topic_sent \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0442 \u0431\u043e\u0442\u0430 SmartMix.<\/p>\n<p>check_msg &#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 \u0442\u043e\u043f\u0438\u043a\u0430 topic_sent \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u044b ESP32 \u0441 MQTT:<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import osimport networkimport timefrom umqtt.simple import MQTTClientfrom micro import flashing_led, led_off, show_temperatureprint(os.getcwd())print(os.listdir())server = 'MQTT_SERVER'port = 'MQTT_PORT'topic = 'MQTT_TOPIC'topic_sub = 'MQTT_TOPIC_ANSWER'client_id = 'MQTT_CLIENT_ID'username = 'MQTT_USERNAME'password = 'MQTT_PASSWORD'sta_if = network.WLAN(network.STA_IF)if not sta_if.isconnected():    # \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a Wi-Fi    print(\"Connect to  Wi-Fi...\")    sta_if.active(True)    print('Wi-Fi Interface states:')    print('Active:', sta_if.active())    print('Connected:', sta_if.isconnected())    timeout = 20    start = time.time()    sta_if.connect('WIFI_SSID', 'WIFI_PASSWORD')    while not sta_if.isconnected():        print(\"Wating...\")        time.sleep(1)        if time.time() - start &gt; timeout:            print(\"Connection is timeout\")            break    print(\"Connected. IP:\", sta_if.ifconfig())else:     # \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a Wi-Fi \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e    print(\"Already connected, IP:\", sta_if.ifconfig())def on_message(topic, msg):    answer = ''    # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439    print(msg)    if msg == b'led_on':        flashing_led()        answer = '\u0421\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434 \u0432\u043a\u043b\u044e\u0447\u0451\u043d'        client.publish(topic_sub, bytes(answer, 'utf-8'))        print(answer)    elif msg == b'led_off':        led_off()        answer = '\u0421\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d'        client.publish(topic_sub, bytes(answer, 'utf-8'))        print(answer)    elif msg == b'temp':        show_temperature()        answer = show_temperature()        print(answer)        client.publish(topic_sub, bytes(answer, 'utf-8'))client = MQTTClient(client_id, server, port, username, password)client.connect()client.set_callback(on_message)client.subscribe(topic)last_ping = time.time()ping_interval = 30while True:    client.check_msg()    now = time.time()    if now - last_ping &gt; ping_interval:        client.ping()        last_ping = now<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e ESP32 <a href=\"https:\/\/github.com\/Mik-23\/esp32-for-smartmix.git\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0418 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 ESP32: &#171;\u0421\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434 \u0432\u043a\u043b\u044e\u0447\u0451\u043d&#187;, &#171;\u0421\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d&#187; \u0438\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u044b.<\/p>\n<pre><code class=\"python\">salt = os.getenv('CIFER_SALT')key = gen_key(chat_id, salt)message_content = get_mqtt(mqtt_dict, sender_id)message_bot = Message(    sender_bot_id=bot.id,    content=encrypt_text(message_content, key),    chat_id=chat_id,)<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<pre><code class=\"python\">def get_mqtt(mqtt_dict, user_id):    mqtt = return_mqtt(mqtt_dict, user_id)    mqtt.subscribe()    return mqtt.print_message()<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 \u043a\u044d\u0448\u0435 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b Redis, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442 \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435 \u0438 \u0437\u0430\u0449\u0438\u0449\u0451\u043d \u043f\u0430\u0440\u043e\u043b\u0435\u043c. \u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0441 Redis \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a\u043e\u043c, \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u043e\u043c \u0438 Nginx.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"yaml\">version: '3.8'services:   mixrech:      build: .\/mixrech      command: sh -c \"python manage.py makemigrations &amp;&amp; python manage.py migrate &amp;&amp; python manage.py collectstatic --noinput &amp;&amp; gunicorn mixrech.wsgi:application --bind 0.0.0.0:8000 --timeout 120\"      volumes:      - .:\/app      - mixrech_static:\/mixrech\/staticfiles\/            env_file:      - .env      ports:         - \"8000:8000\"   mixchat:      build: .\/mixchat      command: sh -c \"python manage.py migrate &amp;&amp; python manage.py collectstatic --noinput --verbosity 2 &amp;&amp; gunicorn mixchat.wsgi:application --bind 0.0.0.0:8001 --timeout 120\"      volumes:      - .:\/app      - mixchat_static:\/mixchat\/staticfiles\/      - media_data:\/mixchat\/main\/media_v4\/      environment:      - MEDIA_ROOT=\/mixchat\/main\/media_v4\/      env_file:      - .env      ports:         - \"8001:8001\"   redis:     image: redis:7-alpine     container_name: mixchat_redis     restart: unless-stopped     ports:      - \"6379:6379\"      volumes:      - redis_data:\/data     command: redis-server --requirepass ${REDIS_PASSWORD} --appendonly yes      networks:      - default     environment:      - REDIS_PASSWORD=${REDIS_PASSWORD}            nginx:      image: nginx:1.25.3      volumes:         - .\/nginx.conf:\/etc\/nginx\/nginx.conf:ro         - media_data:\/app\/media\/         - mixrech_static:\/app\/staticfiles\/mixrech\/         - mixchat_static:\/app\/staticfiles\/mixchat\/         - \/etc\/letsencrypt\/live\/mixrech.com\/fullchain.pem:\/etc\/letsencrypt\/live\/mixrech.com\/fullchain.pem         - \/etc\/letsencrypt\/live\/mixrech.com\/privkey.pem:\/etc\/letsencrypt\/live\/mixrech.com\/privkey.pem               depends_on:        - mixrech        - mixchat      ports:        - \"80:80\"        - \"443:443\"volumes:   mixrech_static:   mixchat_static:   media_data:   redis_data:<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<\/div>\n<\/details>\n<h3>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<p>\u0421\u043e\u043b\u044c (CIFER_SALT) \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0443. \u042d\u0442\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0443\u0442\u0435\u0447\u043a\u0443 \u043f\u0430\u0440\u043e\u043b\u0435\u0439.<\/p>\n<p>\u0428\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0435 \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u043d\u043e \u0432\u043e \u0432\u0441\u0435\u0445 \u0447\u0430\u0442\u0430\u0445.<\/p>\n<pre><code class=\"python\">import base64from dotenv import load_dotenvfrom cryptography.fernet import Fernetfrom cryptography.hazmat.primitives.kdf.scrypt import Scryptload_dotenv()def gen_key(id, salt):    kdf = Scrypt(salt=salt.encode('utf-8'), length=32, n=2**14, r=8, p=1)    key = kdf.derive(str(id).encode())    return base64.urlsafe_b64encode(key)def encrypt_text(text, key):    fer = Fernet(key)    encrypt_data = fer.encrypt(text.encode('utf-8'))    return encrypt_datadef decrypt_text(text, key):    fer = Fernet(key)    decrypt_data = fer.decrypt(text)    return decrypt_datadef encrypt_file(file_path, key):    fer = Fernet(key)    with open(file_path, 'rb') as f:        data = f.read()    encrypt_data = fer.encrypt(data)    with open(file_path, 'wb') as f:        f.write(encrypt_data)def decrypt_file(file_path, key):    fer = Fernet(key)    with open(file_path, 'rb') as f:        data = f.read()    decrypt_data = fer.decrypt(data)    with open(file_path, 'wb') as f:        f.write(decrypt_data)<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<h3>\u0418\u0442\u043e\u0433<\/h3>\n<p>\u0427\u0430\u0442-\u0431\u043e\u0442 SmartMix \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u0431\u0435\u0437 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u043a \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e, \u0430 \u0441\u0430\u043c\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<p>\u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u043e\u043f\u0440\u043e\u0441\u0430\u043c!<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1058210\/\">https:\/\/habr.com\/ru\/articles\/1058210\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041d\u0438 \u0434\u043b\u044f \u043a\u043e\u0433\u043e \u043d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e IoT\u2011\u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u044e\u0442 \u0438\u0445 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043e\u043d\u0438 \u0436\u0451\u0441\u0442\u043a\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e, \u0442.\u0435. \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0431\u0440\u0435\u043d\u0434\u0430. \u0413\u0430\u0434\u0436\u0435\u0442\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b \u0441 \u043f\u0430\u0440\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u043d\u043e \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u00a0\u043c\u043e\u0433\u0443\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0441\u00a0\u0434\u0440\u0443\u0433\u043e\u043c \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0448\u043b\u044e\u0437\u043e\u0432. \u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0441\u0432\u044f\u0437\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0442\u043e\u0436\u0435 \u043d\u0435\u0442. \u0418\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432: Zigbee, Matter, Z\u2011Wave, Bluetooth, Wi-Fi \u2014 \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0443\u043c\u043d\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043f\u043b\u043e\u0445\u043e \u0434\u0440\u0443\u0436\u0430\u0442 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439.\u0415\u0441\u0442\u044c \u043b\u0438 \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438?\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0434\u0430.\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u043e \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u0435 MixRech \u0441 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a\u043e\u043c \u0438 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u043e\u043c. \u0421\u0441\u044b\u043b\u043a\u0430 \u0442\u0443\u0442. \u0418 \u0442\u0430\u043c \u0435\u0441\u0442\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440 MixChat, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u043c\u043d\u044b\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0447\u0430\u0442 \u0431\u043e\u0442\u0430 SmarMix \u0432 \u0441\u0430\u043c\u043e\u043c \u0436\u0435 MixChat.SmartMix \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0443\u043c\u043d\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0432 \u043e\u0434\u0438\u043d \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 MQTT\u041c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440 MixChat \u0438 \u043c\u0438\u043a\u0440\u043e\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 ESP32 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 MQTT. \u0412\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 MQTT \u0441\u0435\u0440\u0432\u0435\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 Raspberry Pi \u0438\u043b\u0438 \u0432 \u043e\u0431\u043b\u0430\u043a\u0435), \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0432\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c SmartMix.\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?\u0417\u0430\u0439\u0434\u044f \u0432 \u0447\u0430\u0442 \u0441\u043e SmartMix, \u0432\u044b \u0432\u0432\u043e\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b MQTT \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u043e \u043a\u043d\u043e\u043f\u043a\u0435 \u0432\u0432\u0435\u0440\u0445\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435. \u041f\u043e\u0442\u043e\u043c \u0432\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435\u0441\u044c \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f ESP32: \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c\/\u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0441\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434, \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u0443 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d.\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e\u043c.SmartMix \u043d\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e. \u0410 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a.ESP32 \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 SmartMix, \u0432\u0430\u0448\u0438 \u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435: \u043f\u0430\u0440\u043e\u043b\u0438, \u043d\u043e\u043c\u0435\u0440\u0430 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432, \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u044b\u0435 \u043f\u043e\u0447\u0442\u044b \u0438\u043b\u0438 \u0446\u0438\u0444\u0440\u043e\u0432\u044b\u0435 ID \u043b\u044e\u0434\u0435\u0439 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438. \u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u0432\u043e\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0432\u043e\u0435\u0433\u043e MQTT \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0432.\u0442.\u0447. \u0438 \u0442\u043e\u043f\u0438\u043a\u0438 \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f, \u043e\u043d\u0438 \u0448\u0438\u0444\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043a\u044d\u0448 \u043f\u043e API \u0437\u0430\u043f\u0440\u043e\u0441\u0443:class SaveMqttView(generics.GenericAPIView):    serializer_class = SaveMqttSerializer    authentication_classes = [JWTAuthentication, CsrfExemptSessionAuthentication]    permission_classes = [IsAuthenticated]    def post(self, request, *args, **kwargs):        serializer = self.get_serializer(data=request.data)        serializer.is_valid(raise_exception=True)        user = request.user.id        server = serializer.validated_data[&#8216;server&#8217;]        port = serializer.validated_data[&#8216;port&#8217;]        client_id = serializer.validated_data[&#8216;client_id&#8217;]        mqtt_username = serializer.validated_data[&#8216;mqtt_username&#8217;]        mqtt_password = serializer.validated_data[&#8216;mqtt_password&#8217;]        topic_sent = serializer.validated_data[&#8216;topic_sent&#8217;]        topic_subscribe = serializer.validated_data[&#8216;topic_subscribe&#8217;]        mqtt_values = list(request.session.values())        for value in mqtt_values:            if value == server or value == mqtt_username:                return Response({&#171;message&#187;: &#171;\u041e\u0448\u0438\u0431\u043a\u0430. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f&#187;                                 &#187; \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443&#187;}, status=403)        key = gen_key(user, salt)        request.session[f&#8217;server_{user}&#8217;] = encrypt_text(server, key).decode(&#8216;utf-8&#8242;)        request.session[f&#8217;port_{user}&#8217;] = port        request.session[f&#8217;client_id_{user}&#8217;] = client_id        request.session[f&#8217;mqtt_username_{user}&#8217;] = encrypt_text(mqtt_username, key).decode(&#8216;utf-8&#8242;)        request.session[f&#8217;mqtt_password_{user}&#8217;] = encrypt_text(mqtt_password, key).decode(&#8216;utf-8&#8242;)        request.session[f&#8217;topic_sent_{user}&#8217;] = topic_sent        request.session[f&#8217;topic_subscribe_{user}&#8217;] = topic_subscribe        return Response({&#171;message&#187;: &#171;\u0414\u0430\u043d\u043d\u044b\u0435 MQTT \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u044b&#187;})\u0414\u0430\u043d\u043d\u044b\u0435 MQTT-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u043f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435\u0441\u044c \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443.\u0412 \u0431\u043e\u0442\u0435 \u0437\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 MQTT. \u0412\u043e\u0442 \u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:connection()\u00a0\u2014 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0441\u0432\u044f\u0437\u044c \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.publish(msg)\u00a0\u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0432 \u0442\u043e\u043f\u0438\u043a\u00a0topic_sent.subscribe()\u00a0\u2014 \u0441\u043b\u0443\u0448\u0430\u0435\u0442 \u0442\u043e\u043f\u0438\u043a\u00a0topic_subscribe, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 ESP32.\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442import osimport timeimport sysfrom dotenv import load_dotenvfrom paho.mqtt import client as mqtt_clientfrom .cifer import gen_key, decrypt_textload_dotenv()salt = os.getenv(&#8216;CIFER_SALT&#8217;)class MQTT:    def __init__(self, mqtt_dict, client, user_id, exit_flag, msg):        key = gen_key(user_id, salt)        self.client = client        self.exit_flag = exit_flag        self.msg = msg        self.server = decrypt_text(mqtt_dict[f&#8217;server_{user_id}&#8217;], key).decode(&#8216;utf-8&#8242;)        self.port = mqtt_dict[f&#8217;port_{user_id}&#8217;]        self.mqtt_username = decrypt_text(mqtt_dict[f&#8217;mqtt_username_{user_id}&#8217;], key).decode(&#8216;utf-8&#8242;)        self.mqtt_password = decrypt_text(mqtt_dict[f&#8217;mqtt_password_{user_id}&#8217;], key).decode(&#8216;utf-8&#8242;)        self.topic_sent = mqtt_dict[f&#8217;topic_sent_{user_id}&#8217;]        self.topic_subscribe = mqtt_dict[f&#8217;topic_subscribe_{user_id}&#8217;]    def connection(self):        self.client.username_pw_set(self.mqtt_username, self.mqtt_password)        # self.client.on_connect = &#171;Connected to MQTT Broker!&#187;        if self.server and self.port:            self.client.connect(self.server, int(self.port))            print(&#171;Connect to server&#187;)        else:            print(&#171;\u041e\u0448\u0438\u0431\u043a\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438. \u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430&#187;)    def publish(self, msg):        # \u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 MQTT        if self.topic_sent:            result = self.client.publish(self.topic_sent, msg)            status = result[0]            if status == 0:                print(f&#187;\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e `{msg}` \u0432 \u0442\u043e\u043f\u0438\u043a `{self.topic_sent}`&#187;)            else:                print(f&#187;\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0442\u043e\u043f\u0438\u043a {self.topic_sent}&#187;)        else:            print(&#171;\u041e\u0448\u0438\u0431\u043a\u0430. \u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438&#187;)    def on_connect(self, client, userdata, flags, rc):        if rc == 0:            self.client.subscribe(self.topic_subscribe)        else:            print(&#171;Failed to connect, return code %d\\n&#187;, rc)    def subscribe(self):        try:            if self.server and self.port:                self.client.username_pw_set(self.mqtt_username, self.mqtt_password)                self.client.on_connect = self.on_connect                self.client.on_message = self.on_message                self.client.connect(self.server, int(self.port))                self.client.loop_start()  # \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0432 \u0444\u043e\u043d\u0435                start = time.time()                while not self.exit_flag:                    time.sleep(0.1)                self.exit_flag = False                print(time.time() &#8212; start)                self.client.loop_stop()  # \u041e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0446\u0438\u043a\u043b                print(self.client.is_connected)                self.client.disconnect()            else:                print(&#171;\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f. \u041d\u0435 \u0432\u0435\u0440\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430&#187;)        except KeyboardInterrupt:            print(&#171;\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0440\u0432\u0430\u043d\u043e&#187;)            sys.exit()    def on_message(self, client, userdata, message):        self.msg = message.payload.decode()        print(&#8216;MQTT MESSAGE:&#8217;, message.payload.decode())        self.exit_flag = True    def print_message(self):        print(self.msg)        return self.msgdef return_mqtt(mqtt_dict, user_id):    mqtt = MQTT(mqtt_dict, mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1,                mqtt_dict[f&#8217;client_id_{user_id}&#8217;]), user_id,False, &#187;)    return mqtt\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a\u043e\u043c \u0438 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u043e\u043c \u0437\u0434\u0435\u0441\u044c.\u041f\u043e\u0441\u043b\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MQTT-\u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0432\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e MQTT:mqtt_dict = dict(request.session.items())send_to_mqtt(mqtt_dict, sender_id, content) def send_to_mqtt(mqtt_dict, user_id, msg):    mqtt = return_mqtt(mqtt_dict, user_id)    mqtt.connection()    mqtt.publish(msg)\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0434\u043e\u0445\u043e\u0434\u0438\u0442 \u0434\u043e ESP32, \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u043d\u0430 ESP32. \u0421 ESP32 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u044b).ESP32 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043a MQTT \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:connect &#8212; \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0441\u0432\u044f\u0437\u044c \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.set_callback &#8212; \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0442\u043e\u043f\u0438\u043a topic_subscribe.on_message &#8212; \u043b\u043e\u0432\u0438\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043e\u0442 \u0431\u043e\u0442\u0430 SmartMixsubscribe &#8212; \u0441\u043b\u0443\u0448\u0430\u0435\u0442 \u0442\u043e\u043f\u0438\u043a topic_sent \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0442 \u0431\u043e\u0442\u0430 SmartMix.check_msg &#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 \u0442\u043e\u043f\u0438\u043a\u0430 topic_sent \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435.\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u044b ESP32 \u0441 MQTT:\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442import osimport networkimport timefrom umqtt.simple import MQTTClientfrom micro import flashing_led, led_off, show_temperatureprint(os.getcwd())print(os.listdir())server = &#8216;MQTT_SERVER&#8217;port = &#8216;MQTT_PORT&#8217;topic = &#8216;MQTT_TOPIC&#8217;topic_sub = &#8216;MQTT_TOPIC_ANSWER&#8217;client_id = &#8216;MQTT_CLIENT_ID&#8217;username = &#8216;MQTT_USERNAME&#8217;password = &#8216;MQTT_PASSWORD&#8217;sta_if = network.WLAN(network.STA_IF)if not sta_if.isconnected():    # \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a Wi-Fi    print(&#171;Connect to  Wi-Fi&#8230;&#187;)    sta_if.active(True)    print(&#8216;Wi-Fi Interface states:&#8217;)    print(&#8216;Active:&#8217;, sta_if.active())    print(&#8216;Connected:&#8217;, sta_if.isconnected())    timeout = 20    start = time.time()    sta_if.connect(&#8216;WIFI_SSID&#8217;, &#8216;WIFI_PASSWORD&#8217;)    while not sta_if.isconnected():        print(&#171;Wating&#8230;&#187;)        time.sleep(1)        if time.time() &#8212; start &gt; timeout:            print(&#171;Connection is timeout&#187;)            break    print(&#171;Connected. IP:&#187;, sta_if.ifconfig())else:     # \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a Wi-Fi \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e    print(&#171;Already connected, IP:&#187;, sta_if.ifconfig())def on_message(topic, msg):    answer = &#187;    # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439    print(msg)    if msg == b&#8217;led_on&#8217;:        flashing_led()        answer = &#8216;\u0421\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434 \u0432\u043a\u043b\u044e\u0447\u0451\u043d&#8217;        client.publish(topic_sub, bytes(answer, &#8216;utf-8&#8242;))        print(answer)    elif msg == b&#8217;led_off&#8217;:        led_off()        answer = &#8216;\u0421\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d&#8217;        client.publish(topic_sub, bytes(answer, &#8216;utf-8&#8242;))        print(answer)    elif msg == b&#8217;temp&#8217;:        show_temperature()        answer = show_temperature()        print(answer)        client.publish(topic_sub, bytes(answer, &#8216;utf-8&#8217;))client = MQTTClient(client_id, server, port, username,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-486994","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/486994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486994"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/486994\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}