AWS Host example
Introduction
In the AWS host mode example, two Opulinks devices are used: one of the Opulinks devices plays as a slave device controlled by another host. The other plays as the host will send an AT command through UART to request Opulinks device to perform a sleeping mechanism with BLE connection, WiFi connection, and cloud connection.
We provide 6 host examples running on OPL2500. The host will interpret different situations. The following chapters will be a detailed description of each example.
Architecture
Testing
We have prepared six examples for your reference, each of which can be demonstrated by placing an AT command on the host side.
It is necessary to set the AP name and password in app_main.c
due to the automated actions in the host example. If there is no password, set the AP_PASSWORD
as ""
.
#define AP_NAME ("XXXX")
#define AP_PASSWORD ("")
The slave module should not be enabled OPL_DATA_ENABLED
.
// Slave module
// OPL Data Protocol
#ifndef OPL_DATA_ENABLED
#define OPL_DATA_ENABLED (0)
#endif
At first, after the slave is established successfully, it will send an acknowledgment message to the host and display "OPL module ready". Once received, the host will be ready to proceed and display "DEVICE_READY".
Important
After each restart, you must upload the credentials and set the endpoint.
-
Upload certificate
Users need to upload certificates in order: root ca → client cert → private key. Please refer to Certificate Upload step 2.
-
Endpoint setting
at+mqtturlset=<endpoint>,<port>,<auto connect flag>
Host example one: Wakeup Slave via IO Include Smart sleep, Timer Sleep, and Deep Sleep
In this example, we will demonstrate how the host can wake up the slave device from its sleep mode using IO. We will cover various types of sleep modes, including smart sleep, timer sleep, and deep sleep. To achieve this, the host will send commands to the slave device using the AT command and the slave will transfer the ack message to the host via UART. Once the BLE connection is established, the device will connect to WiFi and MQTT, and then enter different types of sleep modes in order.
AT command
Important
The user gives the AT command on the host side, not the slave side.
at+example=1
Process
Once the host receives the at+example=1
, it will start the following actions.
Note
BLE connection and BLE disconnection need to be accomplished by the phone APP. Others will execute automatically.
- BLE advertise
If BLE is connected already, the host will start with the WiFi connection(WiFi scan and WiFi connect). - BLE Connect
- WiFi Scan
- WiFi Connect
- MQTT Connect
- MQTT subscribe topic
-
MQTT publish
-
Slave Smart Sleep
Once the host receives the ack message "MQTT_PUB_DATA", the host will initiate a sleep slave timer, which is presently set at 5 seconds. When the timer times out, the host will send a command
at+sleepmode=1
to the slave to make it enter smart sleep. -
Host wakeup slave by IO
After the host receives the ack message "SMART_SLEEP", which means the slave enters smart sleep mode. The host will then activate a timer set for 8 seconds. After the timer expires, the host will wake up the slave device by triggering an IO signal.
-
Slave timer sleep
Once the host receives the ack message "SMART_SLEEP_WAKEUP", the host will initiate a sleep slave timer, which is presently set at 5 seconds. When the timer times out, the host will send a command
at+sleepmode=2,10000
to the slave to make it enter timer sleep and the sleeping time will be 10s. -
Host wakeup slave by IO
After the host receives the ack message "TIMER_SLEEP", which means the slave enters timer sleep mode. The host will then activate a timer set for 8 seconds, which is shorter than the sleeping time. Consequently, the slave will be wake up by the host via IO when the timer timeout.
-
BLE advertise
- BLE Connect
- WiFi Scan
- WiFi Connect
- MQTT Connect
- MQTT subscribe topic
- MQTT publish
-
Slave deep sleep
Once the host receives the ack message "MQTT_PUB_DATA", the host will initiate a sleep slave timer, which is presently set at 5 seconds. When the timer times out, the host will send a command
at+sleepmode=3
to the slave to make it enter deep sleep. -
Host wakeup slave by IO
After the host receives the ack message "DEEP_SLEEP", which means the slave enters timer sleep mode. The host will then activate a timer set for 8 seconds. After the timer timeout, the host will wake up the slave device by triggering an IO signal and the slave will be restarted. After the host receives the restart message "DEVICE_READY", it can continue with other examples.
Among in of these, MQTT Connect(step 5, step 16) contains the following:
- MQTT certificate upload
The file and the file length need to be uploaded before executing each example.- Root CA
- Client Cert
- Private Key
- MQTT client ID set
- MQTT keeps alive set
- MQTT last will set
- MQTT initial
-
MQTT connect
The endpoint also needs to be set before executing each example.
Host example two: Wakeup Slave via BLE Connect and Disconnect
Example two describes the situation about slave wakeup by BLE connect event and BLE disconnect event. Unlike the first example, the device does not need to wait for a BLE connection to start scanning for WiFi. Once the device connects to WiFi and MQTT, both the slave and host devices enter smart sleep. However, if a BLE connect or disconnect event is detected, the slave device will wake up from its sleep mode. To notify the host device regarding the event message, the slave device will trigger the host device via IO. Upon receiving the notification, the host responds at+hostready
to become ready to receive the event ack message from the slave device.
AT command
at+example=2
Process
-
BLE advertise
Different from example one, the device doesn't need to connect BLE before the WiFi connection.
2. WiFi Scan
3. WiFi Connect
4. MQTT connect
5. MQTT subscribe topic
6. MQTT publish
7. Slave smart sleep
8. Host smart sleep
9. BLE connect event wakeup slaveWhen a BLE connection occurs, the slave exits sleep mode and triggers the host. Once the host is triggered, it replies with "at+hostready" to the slave, which then passes an ACK message back to the host.
-
Slave wakeup host by IO
- Host ready and get the ack message from slave
- Slave smart sleep
- Host smart sleep
- BLE disconnect event wakeup slave
- Slave wakeup host by IO
-
Host ready and get the ack message from slave
-
MQTT disconnect
-
WiFi disconnect
The device will disconnect WiFi and MQTT at the end of the example.
Host example three: Wakeup Slave via WiFi disconnect
The third example is similar to the second example, except that the slave will connect BLE before entering smart sleep and it is woken up by a network disconnect event.
AT command
at+example=3
Process
- BLE advertise
- BLE connect
- WiFi Scan
- WiFi Connect
- MQTT connect
- MQTT subscribe topic
- MQTT publish
- Slave smart sleep
- Host smart sleep
- WiFi disconnect event wakeup slave
- Slave wakeup host by IO
-
Host ready and get the ack message from slave
Because wifi disconnection will cause MQTT disconnection, the final Ack message which host receives will contain both MQTT disconnection and WiFi disconnection.
Host example four: Wakeup Slave via MQTT disconnect
In example four, the slave will be wake up by MQTT disconnect event.
AT command
at+example=4
Process
- BLE advertise
- BLE connect
- WiFi Scan
- WiFi Connect
- MQTT connect
- MQTT subscribe topic
- MQTT publish
- Slave smart sleep
- Host smart sleep
- MQTT disconnect event wakeup slave
- Slave wakeup host by IO
- Host ready and get the ack message from slave
- WiFi disconnect
Host example five: Wakeup Slave via MQTT receives data
In example five, slave will be wakeup by MQTT receive data event.
AT command
at+example=5
Process
- BLE advertise
- BLE connect
- WiFi Scan
- WiFi Connect
- MQTT connect
- MQTT subscribe topic
- MQTT publish
- Slave smart sleep
- Host smart sleep
- MQTT receives data wakeup slave
Please refer to MQTT receive data. - Slave wakeup host by IO
- Host ready and get the ack message from slave
- WiFi disconnect
- MQTT disconnect
Host example six: Wakeup Slave via Timer Sleep Timeout
Different from the previous example, in example six, the slave will enter timer sleep and will be woken up when the timer timeout.
AT command
at+example=6
Process
- BLE advertise
- BLE connect
- WiFi Scan
- WiFi Connect
- MQTT connect
- MQTT subscribe topic
- MQTT publish
- Slave timer sleep
- Host smart sleep
- Timer sleep timeout wakeup slave
- Slave wakeup host by IO
- Host ready and get the ack message from slave