---
# System prepended metadata

title: IoT Lab 2 Report

---

# IoT Lab 2 Report Team01

### Members
:::success
Guo Ziniu 1004890
Meng Fanyi 1004889
Wang Zhuoran 1004867
Wang Shiyu 1004874
:::

### 1. Analyzing the working procedures 
- UML
![](https://hackmd.io/_uploads/HJH99Kdga.png)
- explaination
    - We did not analyze the code for Wi-Fi and certificates configuration, what we have focused on:
        - MQTT (set up connection between esp32 and AWS)
        - How to read data from temperature and sent it to the cloud
        - How to extract data from incoming MQTT packet and display on the oled screen.
    - ESP32 MakePython board both publishes messages to a topic and subscribed to the same topic.
        1. ESP32 first initialize MQTT library, where an Callback function (which is defined to handlle incoming message) is bound.
        2. Then ESP32 will connect to the MQTT broker.
        3. Once ESP32 has connected to the MQTT broker, execute subscribe/publish loop.
        4. Within the loop:
            - ESP32 will subscribe to the broker first.
            - ESP32 will call functions in ds18b20.c to get temperature value from the temperature sensor.
            - Craft the MQTT packet and publish to the broker.
            - The broker will forward the message to all clients subscribed to the topic once receiving publish message.
            - Once the incoming message is forwared to ESP32(because it has subscribed to the broker), the callback function will be triggered to handle incoming message
            - The handleIncomingPublish function will extract the payload from MQTT packet and call display functions in ssd1306.c to display the temperature on the oled screen.
            - Because we set maxPublishCount to 5, once ESP32 has published and received 5 messages, it will unsubscribe from the topic and jump out of the loop.
        5. Finally, we can end TLS session and close TCP connection with the broker. 

### 2. Protocols used:
- MQTT for communication between ESP32 and AWS
- I2C to control oled screen.
- 1-wire protocol to read from temperature sensor. 
- PEAP for secure authentication within SUTD Wi-Fi.

### 3. Troubles Encountered
- Connect to AWS always failed
    - Certs name incorrect.
        - Replace the certificates
    - AWS things policy did not attach.
        - Attach the policy

### 4. Demonstration.
[Video](https://drive.google.com/file/d/1vchNA7jpcMMMhyrPP3srQJa4cfzqpwEu/view?usp=drive_link)

### 5. Bonus: a self-selected IoT application that uses ESP32 and AWS IOT Core.
- Sensor used: HC-SR04 Ultrasonic Sensor
- Configuration:
    - TRIGGER_GPIO 5
    - ECHO_GPIO 18
- We reused most of the code for task2, modifications are:
    - Include ultrasonic.c and ultrasonic.h found from [Github link](https://github.com/ESP32Tutorials/HC-SR04-Ultrasonic-Sensor-with-ESP32-ESP-IDF/tree/main/Ultrasonic_sensor_oled/components/ultrasonic) which provides us functions to measure distance.
    - Add "ultrasonic.c" to line "SRCS" in CMakeLists.txt under main folder (add ultrasonic.c to source file)
    - Initialize ultrasonic sensor under publishToTopic function.
    - Within publishToTopic function, call ultrasonic_measure from ultrasonic.c file to measure distance.
    - Craft the MQTT packets and publish to the broker. (Similar to task2)
    - Modify the handleIncomingPublish in callback function. Once receiving incoming publish packet from broker, extract the payload and display on oled screen. (Similar to task2)
- Since we have analyzed the code and understood the procedure, it is not difficult to use the ultrasonic sensor in the same way with few modifications.

**Demonstration for ultrasonic sensor**
[Video](https://drive.google.com/file/d/1omMSi0TVXWEmsP5UeN4vrnhGczG0ySsN/view?usp=drive_link)
