Navigation

    M5Stack Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. kulmam92
    3. Best
    K
    • Continue chat with kulmam92
    • Start new chat with kulmam92
    • Flag Profile
    • Profile
    • Following
    • Followers
    • Blocks
    • Topics
    • Posts
    • Best
    • Groups

    Best posts made by kulmam92

    • RE: AWS IoT Edukit Core M5 - Micropython support?

      I was able to send data to AWS IoT Core using the below code. If you want to run this code from VScode + Pymakr, follow this guide: MicroPython: Program ESP32/ESP8266 using VS Code and Pymakr

      import network
      from umqtt.simple import MQTTClient
      import time
      import sys
      import os
      import json
      
      # configure your wifi credentials
      WIFI_SSID = "<YOUR_WIFI_SSID_GOES_HERE>"
      WIFI_PASS = "<YOUR_WIFI_PASSWORD_GOES_HERE>"
      
      # configure your iot credentials
      MQTT_ENDPOINT = "XXXXXX.iot.XXXXX.amazonaws.com"
      MQTT_PORT = 8883
      MQTT_QOS = 0
      # thing-id
      MQTT_CLIENT = "mycore2"
      
      THING_CLIENT_CERT = "/flash/res/core2-certificate.pem.crt"
      THING_PRIVATE_KEY = "/flash/res/core2-private.pem.key"
      
      # topic
      MQTT_TOPIC = "mycore2/env"
      MQTT_SUB_TOPIC = "mycore2/command"
      
      info = os.uname()
      
      def connect_to_wifi(ssid, password):
          sta_if = network.WLAN(network.STA_IF)
          if not sta_if.isconnected():
              sta_if.active(True)
              sta_if.connect(ssid, password)
              while not sta_if.isconnected():
                  pass
          ip_address = sta_if.ifconfig()[0]
          return ip_address
      
      def get_key(key_path):
          with open(key_path, "r") as f:
              key = f.read()
          return key
      
      def connect_to_mqtt():
          print("CONNECTING TO MQTT BROKER...")
          global client
          client = MQTTClient(
              client_id=MQTT_CLIENT,
              server=MQTT_ENDPOINT,
              port=MQTT_PORT,
              keepalive=0,
              ssl=True,
              ssl_params={
                  "cert": get_key(THING_CLIENT_CERT),
                  "key": get_key(THING_PRIVATE_KEY),
                  "server_side": False
              }
          )
          client.set_callback(subscription_call_back)
          try:
              client.connect()
              print("MQTT BROKER CONNECTION SUCCESSFUL")
          except Exception as e:
              print("MQTT CONNECTION FAILED: {}".format(e))
              sys.exit()
          client.subscribe(MQTT_SUB_TOPIC)
          print("SUBSCRIBED TO TOPIC: {}".format(MQTT_SUB_TOPIC))
          return
      
      def subscription_call_back(topic, msg):
          # confirm state update
          print("RECEIVING MESSAGE: {} FROM TOPIC: {}".format(
              msg.decode("utf-8"), topic.decode("utf-8")))
          return
      
      def publish_message(topic, msg, qos=MQTT_QOS):
          client.publish(
              topic=topic,
              msg=msg,
              qos=qos)
          print("PUBLISHING MESSAGE: {} TO TOPIC: {}".format(msg, topic))
          return
      
      # connect to wifi
      print("CONNECTING TO WIFI NETWORK")
      ip_address = connect_to_wifi(WIFI_SSID, WIFI_PASS)
      print("CONNECTED SUCCESSFULLY")
      print("YOUR IP ADDRESS IS: {}".format(ip_address))
      
      # connect to mqtt
      print("CONNECTING TO MQTT BROKER")
      connect_to_mqtt()
      print("SUBSCRIBED TO TOPIC")
      while True:
          client.check_msg()
      
          tf = power.getTempInAXP192()
          msg = str(json.dumps({
              "client": MQTT_CLIENT,
              "device": {
                  "uptime": time.ticks_ms(),
                  "hardware": info[0],
                  "firmware": info[2]
              },
              "sensors": {
                  "TempInAXP192": tf
              },
              "status": "online",
          }))
          publish_message(MQTT_TOPIC, msg)
          time.sleep(5)
      
      posted in Core2 for AWS
      K
      kulmam92