How to configure in Google Core IoT?

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I've made a python program in Raspberry Pi 3 Model B that's supposed to connect to Google Cloud IoT Core with MQTT-protocol and get configurations. Sending data to Core has worked so far, but I just can't figure out how configuring works! Here's a code that's just supposed to get a config: import time import datetime import jwt import ssl import json import paho.mqtt.client as mqtt time_now = datetime.datetime.utcnow() #make key token = {'iat' : time_now , 'exp' : time_now + datetime.timedelta(minutes=60), 'aud' : "[PROJECT]"} r = open("[PRIVATE KEY]", 'r') pub_key = r.read() jwt_key = jwt.encode(token, pub_key, algorithm='RS256') #connect to iot-core client = mqtt.Client(client_id='projects/[PROJECT]/locations/[LOCATION]/registries/[REGISTER]/devices/[DEVICE]') client.username_pw_set(username='unused', password=jwt_key) client.tls_set(ca_certs='/home/pi/.local/lib/python2.7/site-packages/grpc/_cython/_credentials/roots.pem', tls_version=ssl.PROTOCOL_TLSv1_2) client.connect("mqtt.googleapis.com", 8883) #configure and change state state = 0 print state #naturally shows 0 print client.subscribe([DEVICE]/config, qos=1) #doesn't print custom config either, just (0,1) print state #still shows 0 configuration in Iot Core device id is: { "state": 1 } Even after running the program, the device's "state"-variable stays at 0 and the Core's Configuration & State History state that the CONFIG is "Not yet acknowledged by the device" How do I get the device's "state" variable change from 0 to 1 from Core?

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ You have done the half of the job. 2 remarks. It's maybe a detail, but you name your private key pub_key. And you don't close the file. Based on this tutorial, you only subscribe to the MQTT. The return tuple (0,1) means MQTT_ERR_SUCCESS on QOS = 1. Thus your are connected. Great! Now, do the second part: consume the messages in the channel, and make your logic (according with the received message, change the state in your app if it's your use case)

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links