0

The goal is to capture smartphone accelerometer data (X, Y, Z axes) published to the Arduino IoT Cloud by the Arduino IoT Remote mobile app, and then process it in Python and log them into three different csv files for variables (Accelerometer_X, Accelerometer_Y, Accelerometer_Z). I can connect and authenticate, but on_message never fires even though the Arduino IoT Cloud UI shows the accelerometer values updating in real time.

To extend this functionality, I created a separate device in Arduino IoT Cloud called PythonLogger, along with a dedicated Thing that contains three float variables mirroring the accelerometer axes. These PythonLogger variables were synced with the iPhone Thing’s variables so that, in theory, they should reflect the same values that the phone is publishing. The PythonLogger device provides me with a Device ID and Secret, which I use as authentication credentials in my Python client.

Code I used for the Python Script to log the values from IoT cloud to the csv files

from paho.mqtt import client as mqtt
import ssl
from datetime import datetime, timezone

# Credentials (from PythonLogger device)
DEVICE_ID     = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
DEVICE_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

BROKER, PORT  = "iot.arduino.cc", 8884

# Thing + properties (IDs from Arduino IoT Cloud dashboard)
THING_ID = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
PROP_X   = "propid-for-Accelerometer_X"
PROP_Y   = "propid-for-Accelerometer_Y"
PROP_Z   = "propid-for-Accelerometer_Z"

BASE = f"/v2/things/{THING_ID}/properties"
TOPICS = [
    f"{BASE}/{PROP_X}",
    f"{BASE}/{PROP_X}/publish",
    f"{BASE}/{PROP_Y}",
    f"{BASE}/{PROP_Y}/publish",
    f"{BASE}/{PROP_Z}",
    f"{BASE}/{PROP_Z}/publish",
    f"{BASE}/+",
    f"{BASE}/+/publish",
]

def ts(): return datetime.now(timezone.utc).isoformat()

def on_connect(client, userdata, flags, rc, props=None):
    print("Connected:", rc)  # 0 = success
    client.subscribe([(t, 0) for t in TOPICS])
    print("Subscribed to:")
    for t in TOPICS:
        print("  ", t)

def on_message(client, userdata, msg):
    print("RX", ts(), msg.topic, "->", msg.payload.decode("utf-8", "replace"))

client = mqtt.Client(client_id=DEVICE_ID, callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
client.username_pw_set(DEVICE_ID, DEVICE_SECRET)
client.tls_set(cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLS_CLIENT)

client.on_connect = on_connect
client.on_message = on_message

client.connect(BROKER, PORT, keepalive=60)

try:
    client.loop_forever()
except KeyboardInterrupt:
    print("Shutting down...")
    client.disconnect()

With this code the client authenticates successfully but wont log data into the csv files and instead shows me this error.

Connected: Not authorized subscribing…

or

Connection failed [WinError 10054] An existing connection was forcibly closed by the remote host, retrying...

Any idea why I cant get the Script to get the values from IoT cloud?

4
  • "With this code the client authenticates successfully" are you sure? "Connected: Not authorized" indicates that the server returned 0x05 ("The Client is not authorized to connect" - on_connect is called even if the connection was unsuccessful). Have you confirmed you can connect using a tool like MQTTX? Commented Sep 17 at 20:34
  • @Brits It's 50/50 sometimes I get the client to connect but I still cannot the values to be logged in the csv files that are generated. I haven't used MQTTX as this is still a uni project we are advised that this can be done without any external MQTT broker. Commented Sep 18 at 3:20
  • 2
    I suspect you are not meant to be subscribing to the topics that end /piblish Commented Sep 18 at 5:29
  • 2
    @Goose28 MQTTX is just a tool you can use to connect to an MQTT broker and publish/subscribe (if that works, then you know the issue is probably in your code). Commented Sep 18 at 10:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.