During COVID19, the government in Japan has issued a state of emergency. To prevent the spread of new coronavirus infection, the Japanese-style Pub “Izakaya” has been required to shorten business hours, to wear a mask in the shop. And the “Izakaya” has been demanded to prevent infection by droplets at conversation, to keep rooms reasonably ventilated.
The shop master cooks the dishes, and the staff members bring them to the shop customers. Furthermore, they, abide by the government decision, have to induce the shop customers according to check the number of shop customers. The shop master and staff members feel burdened about the induction, because of difficulty to induce and no tool for the measurement.
My solution is to give the tool to collect the state about the shop customers and analyze the status, timely making a judgment and reducing the shop master and staff members workload.
2. How I am Trying to Solve It?In the room ventilation monitoring system, M5Stack Core2 for AWS collects data from the CO2 sensor and built-in microphone, sends collected data regularly to AWS IoT Core. M5Stack Core2 for AWS is received the analyzed result data from AWS IoT Core and displays the analyzed result data.
The CO2 sensor is MH-Z19C NDIR connected using UART. The built-in microphone detects sound made by the shop customers. M5Stack Core2 for AWS displays these data on the built-in IPS LCD screen.
M5Stack Core2 for AWS uploads the collected data to AWS Lambda through AWS IoT Core using the MQTT protocol. AWS Lambda sends result data of the comparison between the collected data and the predefined threshold as a notification to M5Stack Core2 for AWS, through AWS IoT Core. M5Stack Core2 for AWS notifies for taking necessary action to the shop master and staff members based on the notification.
M5Stack Core2 for AWS receives the notification using the MQTT protocol. M5Stack Core2 for AWS uses that notification, lits the built-in RGB LED, vibrates the built-in vibration motor, and notifies the shop master and staff members.
3. Hardware Set UpHere is a connection diagram of M5Stack Core2 for AWS and CO2 sensor ”MH-Z19C NDIR”. The CO2 sensor ”MH-Z19C NDIR” connects to "Port C" of M5Stack Core2 for AWS using the UART.
Here is the connection image of assembled M5Stack Core2 for AWS and the CO2 sensor” MH-Z19C NDIR” below.
The program code on M5Stack Core2 for AWS is based on the "Smart-Thermostat" of "m5stack/Core2-for-AWS-IoT-EduKit." The development platform uses PlatformIO of the Visual Studio Code extension built on Windows 10.
The notification received in M5Stack Core2 for AWS shows three alert levels decided on the value of sound and CO2 from the sensor as follows. The program code lits the built-in RGB LED and vibrates the built-in vibration motor according to the notification.
The code below defines a CO2 variable called co2Handler of type jsonStruct_t used as an alert decision.
jsonStruct_t co2Handler;
co2Handler.cb = NULL;
co2Handler.pKey = "co2";
co2Handler.pData = &co2data;
co2Handler.type = SHADOW_JSON_UINT16;
co2Handler.dataLength = sizeof(uint16_t);
…
rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &soundHandler, &co2Handler, &hvacStatusActuator);
…
The below code represents a callback function for the hvacStatus actuator. The callback function executes when the device receives a new message. At Alert level 1 the code lits the RED RGB LED and vibrates the vibration motor. At Alert level 2 the code lits the BLUE RGB LED.
…
void hvac_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext)
{
IOT_UNUSED(pJsonString);
IOT_UNUSED(JsonStringDataLen);
ESP_LOGI(TAG, "************* hvac_Callback");
char *status = (char *)(pContext->pData);
if (pContext != NULL)
{
ESP_LOGI(TAG, "Delta - hvacStatus state changed to %s", status);
}
if (strcmp(status, ALML1) == 0)
{
ESP_LOGI(TAG, "setting side LEDs to red");
Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_LEFT, 0xFF0000);
Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_RIGHT, 0xFF0000);
Core2ForAWS_Sk6812_Show();
Axp192_EnableLDO3(true);
}
else if (strcmp(status, ALML2) == 0)
{
ESP_LOGI(TAG, "setting side LEDs to blue");
Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_LEFT, 0x0000FF);
Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_RIGHT, 0x0000FF);
Core2ForAWS_Sk6812_Show();
Axp192_EnableLDO3(false);
}
else
{
ESP_LOGI(TAG, "clearing side LEDs");
Core2ForAWS_Sk6812_Clear();
Core2ForAWS_Sk6812_Show();
Axp192_EnableLDO3(false);
}
}
…
4.2 CO2 Sensor CodeThe CO2 sensor is connected to "Port C" of M5Stack Core2 for AWS. Here is the configuration menu for using "Port C." Use the direction keys on your keyboard to go to “Component config ?> Core2 for AWS hardware enable >Expantion Ports A, B, C.”
The CO2 value receives in the “uart_rx_task” task after the code sends the request command in the “uart_tx_task” task.
…
esp_err_t err = Core2ForAWS_Port_PinMode(PORT_C_UART_TX_PIN, UART);
if (err == ESP_OK)
{
Core2ForAWS_Port_C_UART_Begin(9600);
xTaskCreate(uart_rx_task, "uart_rx", 1024 * 2, NULL, 1, NULL);
xTaskCreate(uart_tx_task, "uart_tx", 1024 * 2, NULL, 1, NULL);
}
…
…
uint8_t txdata[BUF_SIZE] = {0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
…
void uart_tx_task(void *arg)
{
while (1)
{
Core2ForAWS_Port_C_UART_Send((char *)&txdata[0], BUF_SIZE);
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void uart_rx_task(void *arg)
{
int rxBytes;
while (1)
{
rxBytes = Core2ForAWS_Port_C_UART_Receive(rxdata);
if (rxBytes > 0)
{
esp_log_buffer_hex("Read data:", rxdata, rxBytes);
uint co2 = rxdata[2];
co2 = co2 * 255 + rxdata[3];
ESP_LOGI(TAG, "CO2: %d", co2);
…
}
vTaskDelay(pdMS_TO_TICKS(800)); // Read more frequently than transmit to ensure the messages are not erased from buffer.
}
}
…
4.3 Display format on the built-in LCDThe sound and the CO2 are displayed on the built-in LCD as follows.
The code converts the sound and the CO2 using the “sprintf” function and displays it using the”lv_textarea_set_text” function.
…
void ui_sensordata_display(uint sound, uint co2)
{
char charbuf[512];
sprintf(charbuf, "Room ventilation\nSound:%d\nCO2:%d\n", sound, co2);
xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
lv_textarea_set_text(out_txtarea, charbuf);
xSemaphoreGive(xGuiSemaphore);
}
…
5. AWS (Amazon Web Services)This system uses AWS IoT Core and AWS Lambda. AWS IoT Core receives sensor data from M5Stack Core2 for AWS and saves device shadow, sends the sensor data to AWS Lambda using the rule. The Lambda compares the sensor data to a predefined value, sends results to M5Stack Core2 for AWS through device shadow as the notification.
M5Stack Core2 for AWS connects to AWS IoT Core using the ATECC608A chip by the following method. After connecting successfully, M5Stack Core2 for AWS sends data to AWS IoT Core.
・Configure the AWS IAM with appropriate permissions
・Use the IoT Provisioning Tool to authenticate M5Stack Core2 for AWS with AWS IoT Core
(1) Configuring AWS Identity and Access Management (IAM)
AWS IAM inputs parameters for generating a policy, a group, and a user, creates an access key id and a secret access key for authentication. AWS IAM generates the policy ” Core2ProvToolAccess”, the group ” Core2provtoolgroup”, and the user ” Core2provtooluser” as follows.
(2) Provisioning of M5Stack Core2 for AWS
The provisioning uses AWS Command Line Interface “AWS CLI.” AWS CLI is input the access key id and the secret access key retrieved from IAM to execute the provisioning.
> aws configure
AWS Access Key ID [****************PL7L]: ()
AWS Secret Access Key [****************4uHF]: ()
Default region name [ap-northeast-1]: ap-northeast-1
Default output format [None]: json
Client certificates must be registered with AWS IoT Core to enable communications between M5Stack Core2 for AWS and AWS IoT Core. The registration of the client certificate uses the python script “registration_helper.py” from the PlatformIO CLI terminal window as follows.
> cd utilities/AWS_IoT_registration_helper
> python registration_helper.py -p <<DEVICE_PORT>>
5.2 Setting IoT Core and LambdaThe IoT rule “Core2IotRule” of AWS IoT Core consist of the rule “calling AWS Lambda function ” to decide the alert level.
The Lambda function “VentilationLambda” transfers the control from AWS IoT Core to communicate the data using the device shadow.
Below script ”index.js” of the Lambda function “VentilationLambda” is node.js script. The script ”index.js” compares the value of sound and CO2 from the device shadow with the predefined value to create the notification. The script”index.js” sends the notification to M5Stack Core2 for AWS through the device shadow.
index.js
var aws = require('aws-sdk');
var endpoint = 'xxxxxxxxxxxx.amazonaws.com';
var thingName = 'yyyyyy';
exports.handler = async (event, context) => {
// TODO implement
console.log('VentilationLambda 01');
var iotdata = new aws.IotData( { endpoint: endpoint } );
console.log("event JSON:", JSON.stringify(event, null, 2));
var params = { thingName: thingName };
iotdata.getThingShadow(params, function (err, data) {
if (!err) {
var payload = JSON.parse(data.payload);
console.log("payload : " + JSON.stringify(payload, null, 2));
var sound = payload.state.reported.sound;
var co2 = payload.state.reported.co2;
console.log("sound : " + sound);
console.log("co2 : " + co2);
//var currentBlinkPattern = payload.state.desired.blinkPattern;
var errorcnt = 0 ;
if(sound > 50){
errorcnt++;
}
if(co2 > 1000){
errorcnt++;
}
var status ;
if(errorcnt >= 2){
status = "ALML1 ";
}
else if(errorcnt == 1){
status = "ALML2 ";
}
else{
status = "NORM ";
}
var desiredState = {
state: {
desired: {
hvacStatus: status
}
}
};
var params = {
thingName: thingName,
payload: JSON.stringify(desiredState)
};
iotdata.updateThingShadow(params, function (err, data) {
if (!err) {
context.succeed();
} else {
context.fail(err);
}
});
}
else{
console.log("getThingShadow error "+err);
}
});
console.log('exports.handler end');
};
6. Result of implementationBelow serial output from the device is monitored on PlatformIO CLI terminal window. This serial output represents receiving “alert level 1” notification and sending sound value and co2 value.
......
.?[0;32mI (81726) MAIN: ************* hvac_Callback?[0m
?[0;32mI (81726) MAIN: Delta - hvacStatus state changed to ALML1 ?[0m
?[0;32mI (81726) MAIN: setting side LEDs to red?[0m
?[0;32mI (81756) MAIN: *****************************************************************************?[0m
?[0;32mI (81756) MAIN: On Device: hvacStatus ALML1 ?[0m
?[0;32mI (81756) MAIN: On Device: sound 193?[0m
?[0;32mI (81766) MAIN: On Device: co2 4981?[0m
?[0;32mI (81776) MAIN: Update Shadow: {"state":{"reported":{"sound":193,"co2":4981,"hvacStatus":"ALML1"}}, "clientToken":"xxxxxxxx-13"}?[0m
.....
The device shadow of AWS IoT Core represents as follows. The notification shows “alert level 1.”
The log group of AWS CloudWatch about AWS Lambda represent as follows. The notification shows “alert level 1” from AWS Lambda and “normal” from M5Stack Core2 for AWS.
Comments