Sumit Kumar
Published © MIT

Tuya MQTT with ESP32

Use ESP32 with Tuya IoT platform and MQTT

BeginnerProtip2 hours19,598
Tuya MQTT with ESP32

Things used in this project

Hardware components

M5Stack Core2 ESP32 IoT Development Kit
M5Stack Core2 ESP32 IoT Development Kit
×1

Software apps and online services

Tuya IoT Platform

Story

Read more

Schematics

Layout

Code

Micropython Code

MicroPython
from m5stack import *
from m5stack_ui import *
from uiflow import *
from m5mqtt import M5mqtt
import json


screen = M5Screen()
screen.clean_screen()
screen.set_screen_bg_color(0xFFFFFF)

label0 = M5Label('MQTT Connect', x=13, y=16, color=0x000, font=FONT_MONT_20, parent=None)
timestamp_label = M5Label('Text', x=6, y=217, color=0x000, font=FONT_MONT_14, parent=None)
image0 = M5Img("res/Tuya_Smart.png", x=-10, y=61, parent=None)
switch0 = M5Switch(x=232, y=15, w=70, h=30, bg_c=0xCCCCCC, color=0x0288FB, parent=None)
status = M5Label('Text', x=137, y=172, color=0x5800ff, font=FONT_MONT_18, parent=None)

def switch0_on():
  # global params
  m5mqtt = M5mqtt('tuyalink_6c1b715d5261ff4c7duev7', 'm1.tuyacn.com', 8883, '6c1b715d5261ff4c7duev7|signMethod=hmacSha256,timestamp=1641396180,secureMode=1,accessType=1', 'b71103b496ec7cdf3f58d2e7c787a9b79401dc7b4b34c29f342bde2488bf0e60', 60, ssl = True)
  m5mqtt.subscribe(str('tylink/6c1b715d5261ff4c7duev7/thing/model/get_response'), fun_tylink_6c1b715d5261ff4c7duev7_thing_model_get_response_)
  m5mqtt.start()
  m5mqtt.publish(str('tylink/6c1b715d5261ff4c7duev7/thing/model/get'),str((json.dumps(({'msgId':'35101','time':'1641829440','data':({'format':'simple'})})))))
  pass
switch0.on(switch0_on)

def fun_tylink_6c1b715d5261ff4c7duev7_thing_model_get_response_(topic_data):
  # global params
  timestamp_label.set_text(str(topic_data))
  ret = json.loads(topic_data)
  if ret['code']==0:
    status.set_hidden(False)
    status.set_text_color(0x00ff00)
    status.set_text('Success!')
    rgb.setColorAll(0x00ff00)
  else:
    rgb.setColorAll(0xff0000)
  pass


status.set_hidden(True)

Tuya_CA_Cert.pem

Plain text
-----BEGIN CERTIFICATE-----
MIIHzjCCBrYCCQCGzYVuHuOuMzANBgkqhkiG9w0BAQsFADCCAqYxCzAJBgNVBAYT
AlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMREwDwYDVQQHDAhTYW4gSm9zZTEZMBcG
A1UECgwQVHV5YSBHbG9iYWwgSW5jLjEVMBMGA1UEAwwMKi50dXlhY24uY29tMRUw
EwYDVQQDDAwqLnR1eWFldS5jb20xFTATBgNVBAMMDCoudHV5YXJmLmNvbTEVMBMG
A1UEAwwMKi50dXlhanAuY29tMRUwEwYDVQQDDAwqLnR1eWFpbi5jb20xFTATBgNV
BAMMDCoudHV5YWFzLmNvbTEVMBMGA1UEAwwMKi50dXlhYWYuY29tMRUwEwYDVQQD
DAwqLnR1eWFzYS5jb20xFDASBgNVBAMMCyoud2dpbmUuY29tMRYwFAYDVQQDDA0q
LnR1eWEtaW5jLmNuMRUwEwYDVQQDDAwqLnR1eWF1cy5jb20xEzARBgNVBAMMCiou
dHV5YS5jb20xDTALBgNVBAsMBFR1eWExITAfBgkqhkiG9w0BCQEWEmlvdF93b3Js
ZEB0dXlhLmNvbTEVMBMGA1UdEQwMKi50dXlhdXMuY29tMRUwEwYDVR0RDAwqLnR1
eWFjbi5jb20xFTATBgNVHREMDCoudHV5YWV1LmNvbTEUMBIGA1UdEQwLKi53Z2lu
ZS5jb20xFjAUBgNVHREMDSoudHV5YS1pbmMuY24xFTATBgNVHREMDCoudHV5YWpw
LmNvbTEVMBMGA1UdEQwMKi50dXlhaW4uY29tMRUwEwYDVR0RDAwqLnR1eWFhcy5j
b20xFTATBgNVHREMDCoudHV5YWFmLmNvbTEVMBMGA1UdEQwMKi50dXlhc2EuY29t
MRUwEwYDVR0RDAwqLnR1eWFyZi5jb20xEzARBgNVHREMCioudHV5YS5jb20wIBcN
MTgxMDMxMDUzMD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M1
7ukcujn+X4HHHIfNxwfd6J6/HDOA3GW/bCAA/+0GpDKxv+np00rEHfaYiqrQYcc7
CZTmq8ZFJ0VPQ01hi3GDGSiMsk2jZUU9Ung1Bslg8LGZV8605LJSTZVjPZYBifdE
kkJcmv9fzKHOTwqvlzsUlBbeWEkG5OFgJsYeknf8Olz6fe9EGjXIdwogvFukua38
8ic8gx2s7LtWZLSVGmAWPSrgf/SokXDah1tSFBXrgjiPpTrNg4QNoQ0CAwEAATAN
BgkqhkiG9w0BAQsFAAOCAQEAT5/mBS2IwIIDLI+wMlIQ6sqiQ+MeofR+bOI6oKzA
Oa5QnAST68p0NplFHiLkvgHc9/7SDozTPX/D7OpH5pQJ5/KE+S2T9I8TmE+5APWo
PBX9/6l6ln3vv0N1eT7Stky0MEcvQS1sXykn3cQCCg8/iIYdGw8dENXBR9mDy090
ReZI7KhOY7nl/zQbNGOGXCODDasu9bbIaYNABj1fAgIWFAFRH6BXW8YqdxIaSS+N
qSuWwqmV6cAcksw9DFTDSmr754Bwqug1bsY9TMrMCZEH5mEmOeKRnBxTU1/MUcGJ
8JX5pT9ikKWdOmiDzAhx2VT2KtHqdfu87IaHYlv/Ey7eMQ==
-----END CERTIFICATE-----

Credits

Sumit Kumar

Sumit Kumar

32 projects • 97 followers
19 y/o. My daily routine involves dealing with electronics, code, distributed storage and cloud APIs.

Comments