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

aws_host

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".

slave

host

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 ready

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.

  1. BLE advertise
    If BLE is connected already, the host will start with the WiFi connection(WiFi scan and WiFi connect).
  2. BLE Connect
    host_ex1_ble
  3. WiFi Scan
  4. WiFi Connect host_ex1_wifi
  5. MQTT Connect
  6. MQTT subscribe topic
  7. MQTT publish host_ex1_mqtt2

  8. 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.

  9. 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.

    host_ex1_5

  10. 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.

  11. 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.

    host_ex1_6

  12. BLE advertise

  13. BLE Connect
  14. WiFi Scan
  15. WiFi Connect
  16. MQTT Connect
  17. MQTT subscribe topic
  18. MQTT publish
  19. 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.

  20. 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.

    host_ex1_7

Among in of these, MQTT Connect(step 5, step 16) contains the following:

  1. MQTT certificate upload
    The file and the file length need to be uploaded before executing each example.
    1. Root CA
    2. Client Cert
    3. Private Key
  2. MQTT client ID set
  3. MQTT keeps alive set
  4. MQTT last will set
  5. MQTT initial
  6. MQTT connect
    The endpoint also needs to be set before executing each example.

    host_ex1_3

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

  1. BLE advertise

    Different from example one, the device doesn't need to connect BLE before the WiFi connection.

    host_ex2_1 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 slave

    When 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.
    host_ex2_2

  2. Slave wakeup host by IO

  3. Host ready and get the ack message from slave
  4. Slave smart sleep
  5. Host smart sleep
  6. BLE disconnect event wakeup slave
  7. Slave wakeup host by IO
  8. Host ready and get the ack message from slave

    host_ex2_3

  9. MQTT disconnect

  10. WiFi disconnect

    The device will disconnect WiFi and MQTT at the end of the example. host_ex2_4

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

  1. BLE advertise
  2. BLE connect
  3. WiFi Scan
  4. WiFi Connect
  5. MQTT connect
  6. MQTT subscribe topic
  7. MQTT publish
  8. Slave smart sleep
  9. Host smart sleep
  10. WiFi disconnect event wakeup slave
  11. Slave wakeup host by IO
  12. 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

  1. BLE advertise
  2. BLE connect
  3. WiFi Scan
  4. WiFi Connect
  5. MQTT connect
  6. MQTT subscribe topic
  7. MQTT publish
  8. Slave smart sleep
  9. Host smart sleep
  10. MQTT disconnect event wakeup slave
  11. Slave wakeup host by IO
  12. Host ready and get the ack message from slave
  13. 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

  1. BLE advertise
  2. BLE connect
  3. WiFi Scan
  4. WiFi Connect
  5. MQTT connect
  6. MQTT subscribe topic
  7. MQTT publish
  8. Slave smart sleep
  9. Host smart sleep
  10. MQTT receives data wakeup slave
    Please refer to MQTT receive data.
  11. Slave wakeup host by IO
  12. Host ready and get the ack message from slave
  13. WiFi disconnect
  14. 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

  1. BLE advertise
  2. BLE connect
  3. WiFi Scan
  4. WiFi Connect
  5. MQTT connect
  6. MQTT subscribe topic
  7. MQTT publish
  8. Slave timer sleep
  9. Host smart sleep
  10. Timer sleep timeout wakeup slave
  11. Slave wakeup host by IO
  12. Host ready and get the ack message from slave