Device-to-Device Communication
How can device-to-device communication be achieved through Waveshare Cloud?
Communication Principle
Publish/Subscribe Pattern
The Publish-Subscribe pattern is a messaging pattern that decouples the sender (publisher) from the receiver ( subscriber), allowing them to communicate without direct connections or knowledge of each other's existence.
In MQTT's Publish/Subscribe pattern, a central role called the broker is responsible for routing and distributing all messages. Publishers send messages with topics to the broker, and subscribers subscribe to topics to receive messages of interest.
In MQTT, topics and subscriptions cannot be registered or created in advance, so the broker cannot predict whether there will be subscribers for a particular topic or how many subscribers there will be. Therefore, messages are only forwarded to current subscribers, and if there are no subscribers, the message is discarded.
MQTT Publish/Subscribe pattern consists of 4 main components: Publisher, Subscriber, Broker, and Topic.
Publisher
Responsible for publishing messages to a topic. Publishers can only send data to one topic at a time, and they don't need to care about whether subscribers are online when publishing messages.
Subscriber
Subscribers receive messages by subscribing to topics. Subscribers can subscribe to multiple topics at once. MQTT also supports load balancing subscriptions among multiple subscribers using shared subscriptions.
Broker
Responsible for receiving messages from publishers and forwarding them to subscribers based on the conditions. The broker also needs to handle client-initiated requests such as connection, disconnection, subscription, and unsubscribing.
Topic
Topics are the foundation for message routing in MQTT. They resemble URL paths and use slashes (/) for hierarchy, e.g., sensor/1/temperature. A topic can have multiple subscribers, and the broker forwards messages to all subscribers under that topic. A topic can also have multiple publishers, and the broker forwards messages in the order they arrive.

Message Routing in MQTT Publish/Subscribe
In the MQTT Publish/Subscribe pattern, a client can be both a publisher and a subscriber, and it can have both roles simultaneously. When a client publishes a message, it is sent to the broker, and the broker routes the message to all subscribers interested in that topic. When a client subscribes to a topic, it receives all messages forwarded by the broker under that topic. The broker routes messages based on the topics they belong to.
Waveshare Cloud's message filtering works through topics: subscribers subscribe to topics they are interested in, and publishers include their own topics in all messages. The broker determines which subscribers to forward messages to based on the message's topic.

ESP32 Code Implementation
Get Address Parameters

Code Reference: ESP32 Connects to Waveshare Cloud SDK (Recommended to copy the entire code below)
Here, the control parameters for the message callback are defined as
Complete code
SIM7028 NB-IoT HAT Command Implementation
Get SIM7028 Client ID

Command Set
Command | Response | Continue Input? | Input Value | Explanation |
---|---|---|---|---|
AT+CMQTTSTART | +CMQTTSTART: 0 | —— | Enable MQTT functionality | |
AT+CMQTTACCQ=0,"82b52f8b",0 | OK | —— | Set Client ID82b52f8b represents the Client ID obtained from the platform, the last 0 represents the use of the MQTT protocol (non-SSL) | |
AT+CMQTTCONNECT=0, tcp://mqtt.waveshare.cloud: 1883,20,1 | +CMQTTCONNECT: 0,0 | —— | Connect to the MQTT server, the return value 0,0 represents a successful connection | |
AT+CMQTTSUB=0,17,1 | > | Pub/1/37/19ba7e8b | ESP32 Pub Topic | |
AT+CMQTTTOPIC=0,17 | > | Sub/1/37/19ba7e8b | ESP32 Sub Topic | |
AT+CMQTTPAYLOAD=0,22 | > | {"data":{"builtIn":1}} | Enter the message body | |
AT+CMQTTPUB=0,1,60 | +CMQTTPUB: 0,0 | —— | Send message |
Command Example
