Close

MQTT Communication to Adafruit IO Using Dual-Port SP2302 (W5500)

securepiSecurePi wrote 06/05/2025 at 07:40 • 2 min read • Like

Overview

Dual Ethernet SP2302 platform to publish data to Adafruit IO over MQTT, utilizing:

field-to-cloud architecture where the data-collection interface and Internet interface are physically isolated — a common pattern in industrial IoT gateways.

What You’ll Need

Step 1: Install MQTT Library on SP2302

bash
sudo apt update
sudo apt install python3-pip -y
pip3 install paho-mqtt

Step 2: Adafruit IO MQTT Setup

Required Settings:

Step 3: Python Demo Script for Publishing Data

python
# sp2302_mqtt_to_adafruitio.py

import time
import paho.mqtt.client as mqtt
import random

# --- Adafruit IO MQTT Configuration ---
ADAFRUIT_IO_USERNAME = 'your_adafruit_username'
ADAFRUIT_IO_KEY = 'your_aio_key'
ADAFRUIT_IO_FEED = 'sensor1'

MQTT_BROKER = 'io.adafruit.com'
MQTT_PORT = 1883
MQTT_TOPIC = f"{ADAFRUIT_IO_USERNAME}/feeds/{ADAFRUIT_IO_FEED}"

# --- Bind MQTT client to eth0 explicitly (optional) ---
MQTT_LOCAL_BIND_IP = '192.168.1.100'  # eth0 IP (Internet-facing)

client = mqtt.Client()
client.username_pw_set(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Optional: bind to eth0 to ensure traffic doesn't go through eth1
client.bind_address = MQTT_LOCAL_BIND_IP

client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.loop_start()

def main():    print(f"[✓] Publishing data to Adafruit IO topic: {MQTT_TOPIC}")    while True:        simulated_value = random.randint(0, 100)        client.publish(MQTT_TOPIC, simulated_value)        print(f"[>] Sent: {simulated_value}")        time.sleep(5)

if __name__ == "__main__":    main()

Step 4: Run the Script

bash

python3 sp2302_mqtt_to_adafruitio.py

Check your Adafruit IO dashboard — your sensor1 feed will now update every 5 seconds with simulated values.

Verifying Network Routing

Ensure:

Results

Use Cases

Use Caseeth0 Purposeeth1 Purpose
Field-to-Cloud GatewayCloud (MQTT)Modbus/field sensor
Secure Remote MonitoringMQTT outbound onlyInternal OT network
MQTT Bridging / FirewallWANLAN
Redundant Link (failover)Primary cloudLocal fallback

Optional: Enable TLS for MQTT (More Secure)

Change port to 8883 and add:

python

client.tls_set()  # Uses default certs

Summary

Like

Discussions