To bridge LoRaWAN sensor data to the IPFS storage layer, we utilize an MQTT listener (to catch data from the LoRaWAN Network Server like The Things Network or ChirpStack) and a Python IPFS client.
The following script acts as a “Data Courier.” It listens for incoming packets, hashes them, and pins the metadata to IPFS, ensuring the GreenWeaver data remains immutable and sovereign.
Python: LoRaWAN to IPFS Bridge Script
import paho.mqtt.client as mqtt
import json
import requests
import os
from datetime import datetime
# --- CONFIGURATION ---
# LoRaWAN (TTN/ChirpStack) Settings
MQTT_BROKER = "eu1.cloud.thethings.network" # Or your ChirpStack IP
MQTT_PORT = 1883
MQTT_USER = "planet-hemp-app-id@ttn"
MQTT_PASS = "NNSXS.YOUR_ACCESS_KEY_HERE"
MQTT_TOPIC = "v3/+/devices/+/up" # Listen to all device uplinks
# IPFS Node Settings (Local node or Pinata/Infura)
IPFS_RPC_URL = "http://127.0.0.1:5001/api/v0/add"
def upload_to_ipfs(payload):
"""Pins the sensor data JSON to IPFS and returns the CID."""
try:
# Prepare the data as a file-like object
files = {
'file': ('sensor_data.json', json.dumps(payload))
}
response = requests.post(IPFS_RPC_URL, files=files)
response.raise_for_status()
# Extract the Content Identifier (CID)
cid = response.json().get('Hash')
return cid
except Exception as e:
print(f"IPFS Upload Error: {e}")
return None
def on_connect(client, userdata, flags, rc):
print(f"Connected to LoRaWAN Broker with result code {rc}")
client.subscribe(MQTT_TOPIC)
def on_message(client, userdata, msg):
"""Triggered when a LoRaWAN packet arrives."""
try:
# 1. Parse LoRaWAN Payload
data = json.loads(msg.payload.decode('utf-8'))
# Extract relevant telemetry (e.g., Soil Moisture, CO2)
decrypted_payload = data.get('uplink_message', {}).get('decoded_payload', {})
device_id = data.get('end_device_ids', {}).get('device_id')
# 2. Enrich with Metadata for the Global Hemp Ledger
ipfs_metadata = {
"device_id": device_id,
"timestamp": datetime.utcnow().isoformat(),
"telemetry": decrypted_payload,
"network": "PlanetHemp_LoRa_Net_V1"
}
# 3. Bridge to IPFS
cid = upload_to_ipfs(ipfs_metadata)
if cid:
print(f"✅ Data Sovereignized! Device: {device_id} | CID: {cid}")
# Optional: Log CID to your local Hemp Ledger database here
except Exception as e:
print(f"Error processing message: {e}")
# --- INITIALIZE BRIDGE ---
client = mqtt.Client()
client.username_pw_set(MQTT_USER, MQTT_PASS)
client.on_connect = on_connect
client.on_message = on_message
print("Starting Planet Hemp LoRa-to-IPFS Gateway...")
client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.loop_forever()
How it Works (The Homeostatic Loop)
-
The Receptor (LoRaWAN): The script subscribes to the MQTT broker. Every time an Eco-Guardian sensor sends soil moisture or $CO_2$ levels via LoRaWAN, this script catches the “uplink.”
-
The Signal (JSON): It formats the raw telemetry into a standardized JSON object, including a UTC timestamp and device ID.
-
The Storage (IPFS): The JSON is pushed to your local or remote IPFS node.
-
The Imprint (CID): IPFS returns a Content Identifier (CID). This CID is the “digital proof” that the data exists and has not been tampered with.
Hardware Requirements
-
Gateway: A Raspberry Pi 4 with a LoRaWAN HAT (e.g., RAK2245).
-
IPFS Node: Ensure your IPFS daemon is running (
ipfs daemon) on the same machine or a server in your Data Commons.





