Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Dilghas Ommer
Created September 19, 2021

Safe & Healthy Lounge Room

A device in which the people without a mask will be identified and alerted, also the entry to the lounge room will be restricted.

25
Safe & Healthy Lounge Room

Things used in this project

Hardware components

AWS IoT EduKit
Amazon Web Services AWS IoT EduKit
×1

Software apps and online services

AWS IoT
Amazon Web Services AWS IoT
AWS EC2
Amazon Web Services AWS EC2
EdgeLine

Story

Read more

Schematics

Door Closed

Door Opened

Code

main.c

C/C++
/*
 * AWS IoT EduKit - Core2 for AWS IoT EduKit
 * Cloud Connected Blinky v1.4.0
 * main.c
 * 
 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * Additions Copyright 2016 Espressif Systems (Shanghai) PTE LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
/**
 * @file main.c
 * @brief simple MQTT publish and subscribe for use with AWS IoT EduKit reference hardware.
 *
 * This example takes the parameters from the build configuration and establishes a connection to AWS IoT Core over MQTT.
 *
 * Some configuration is required. Visit https://edukit.workshop.aws
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "esp_log.h"

#include "aws_iot_config.h"
#include "aws_iot_log.h"
#include "aws_iot_version.h"
#include "aws_iot_mqtt_client_interface.h"

#include "core2forAWS.h"

#include "wifi.h"
#include "blink.h"
#include "ui.h"



TaskHandle_t mic_handle, speaker_handle, atecc_handle;

/* The time between each MQTT message publish in milliseconds */
#define PUBLISH_INTERVAL_MS 3000

/* The time prefix used by the logger. */
static const char *TAG = "MAIN";

/* The FreeRTOS task handler for the blink task that can be used to control the task later */
TaskHandle_t xBlink;
TaskHandle_t xGreen;

/* CA Root certificate */
extern const uint8_t aws_root_ca_pem_start[] asm("_binary_aws_root_ca_pem_start");
extern const uint8_t aws_root_ca_pem_end[] asm("_binary_aws_root_ca_pem_end");

/* Default MQTT HOST URL is pulled from the aws_iot_config.h */
char HostAddress[255] = AWS_IOT_MQTT_HOST;

/* Default MQTT port is pulled from the aws_iot_config.h */
uint32_t port = AWS_IOT_MQTT_PORT;

void speakerTask(void *arg);

void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen,
                                    IoT_Publish_Message_Params *params, void *pData) {
    ESP_LOGI(TAG, "Subscribe callback");
    ESP_LOGI(TAG, "%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *)params->payload);
    if (strstr(topicName, "/blink") != NULL) {
        // Get state of the FreeRTOS task, "blinkTask", using it's task handle.
        // Suspend or resume the task depending on the returned task state
        eTaskState blinkState = eTaskGetState(xBlink);
        if (blinkState == eSuspended){
            vTaskResume(xBlink);
        } else{
            vTaskSuspend(xBlink);
        }
    }
    if (strstr(topicName, "/Green") != NULL) {
        // Get state of the FreeRTOS task, "blinkTask", using it's task handle.
        // Suspend or resume the task depending on the returned task state
        eTaskState blinkState = eTaskGetState(xGreen);
        if (blinkState == eSuspended){
            vTaskResume(xGreen);
        } else{
            vTaskSuspend(xGreen);
        }
    }
}

void disconnect_callback_handler(AWS_IoT_Client *pClient, void *data) {
    ESP_LOGW(TAG, "MQTT Disconnect");
    ui_textarea_add("Disconnected from AWS IoT Core...", NULL, 0);
    IoT_Error_t rc = FAILURE;

    if(pClient == NULL) {
        return;
    }

    if(aws_iot_is_autoreconnect_enabled(pClient)) {
        ESP_LOGI(TAG, "Auto Reconnect is enabled, Reconnecting attempt will start now");
    } else {
        ESP_LOGW(TAG, "Auto Reconnect not enabled. Starting manual reconnect...");
        rc = aws_iot_mqtt_attempt_reconnect(pClient);
        if(NETWORK_RECONNECTED == rc) {
            ESP_LOGW(TAG, "Manual Reconnect Successful");
        } else {
            ESP_LOGW(TAG, "Manual Reconnect Failed - %d", rc);
        }
    }
}

static void publisher(AWS_IoT_Client *client, char *base_topic, uint16_t base_topic_len){
    char cPayload[100];
    int32_t i = 0;

    IoT_Publish_Message_Params paramsQOS0;
    IoT_Publish_Message_Params paramsQOS1;

    paramsQOS0.qos = QOS0;
    paramsQOS0.payload = (void *) cPayload;
    paramsQOS0.isRetained = 0;

    // Publish and ignore if "ack" was received or  from AWS IoT Core
    sprintf(cPayload, "%s : %d ", "Hello from AWS IoT EduKit (QOS0)", i++);
    paramsQOS0.payloadLen = strlen(cPayload);
    IoT_Error_t rc = aws_iot_mqtt_publish(client, base_topic, base_topic_len, &paramsQOS0);
    if (rc != SUCCESS){
        ESP_LOGE(TAG, "Publish QOS0 error %i", rc);
        rc = SUCCESS;
    }

    paramsQOS1.qos = QOS1;
    paramsQOS1.payload = (void *) cPayload;
    paramsQOS1.isRetained = 0;
    // Publish and check if "ack" was sent from AWS IoT Core
    sprintf(cPayload, "%s : %d ", "Hello from AWS IoT EduKit (QOS1)", i++);
    paramsQOS1.payloadLen = strlen(cPayload);
    rc = aws_iot_mqtt_publish(client, base_topic, base_topic_len, &paramsQOS1);
    if (rc == MQTT_REQUEST_TIMEOUT_ERROR) {
        ESP_LOGW(TAG, "QOS1 publish ack not received.");
        rc = SUCCESS;
    }
}

void aws_iot_task(void *param) {
    IoT_Error_t rc = FAILURE;

    AWS_IoT_Client client;
    IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault;
    IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault;

    ESP_LOGI(TAG, "AWS IoT SDK Version %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);

    mqttInitParams.enableAutoReconnect = false; // We enable this later below
    mqttInitParams.pHostURL = HostAddress;
    mqttInitParams.port = port;    
    mqttInitParams.pRootCALocation = (const char *)aws_root_ca_pem_start;
    mqttInitParams.pDeviceCertLocation = "#";
    mqttInitParams.pDevicePrivateKeyLocation = "#0";
    
#define CLIENT_ID_LEN (ATCA_SERIAL_NUM_SIZE * 2)
#define SUBSCRIBE_TOPIC_LEN (CLIENT_ID_LEN + 3)
#define BASE_PUBLISH_TOPIC_LEN (CLIENT_ID_LEN + 2)

    char *client_id = malloc(CLIENT_ID_LEN + 1);
    ATCA_STATUS ret = Atecc608_GetSerialString(client_id);
    if (ret != ATCA_SUCCESS)
    {
        printf("Failed to get device serial from secure element. Error: %i", ret);
        abort();
    }

    char subscribe_topic[SUBSCRIBE_TOPIC_LEN];
    char base_publish_topic[BASE_PUBLISH_TOPIC_LEN];
    snprintf(subscribe_topic, SUBSCRIBE_TOPIC_LEN, "%s/#", client_id);
    snprintf(base_publish_topic, BASE_PUBLISH_TOPIC_LEN, "%s/", client_id);

    mqttInitParams.mqttCommandTimeout_ms = 20000;
    mqttInitParams.tlsHandshakeTimeout_ms = 5000;
    mqttInitParams.isSSLHostnameVerify = true;
    mqttInitParams.disconnectHandler = disconnect_callback_handler;
    mqttInitParams.disconnectHandlerData = NULL;

    rc = aws_iot_mqtt_init(&client, &mqttInitParams);
    if(SUCCESS != rc) {
        ESP_LOGE(TAG, "aws_iot_mqtt_init returned error : %d ", rc);
        abort();
    }

    /* Wait for WiFI to show as connected */
    xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                        false, true, portMAX_DELAY);    

    connectParams.keepAliveIntervalInSec = 10;
    connectParams.isCleanSession = true;
    connectParams.MQTTVersion = MQTT_3_1_1;

    connectParams.pClientID = client_id;
    connectParams.clientIDLen = CLIENT_ID_LEN;
    connectParams.isWillMsgPresent = false;
    ui_textarea_add("Connecting to AWS IoT Core...\n", NULL, 0);
    ESP_LOGI(TAG, "Connecting to AWS IoT Core at %s:%d", mqttInitParams.pHostURL, mqttInitParams.port);
    do {
        rc = aws_iot_mqtt_connect(&client, &connectParams);
        if(SUCCESS != rc) {
            ESP_LOGE(TAG, "Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL, mqttInitParams.port);
            vTaskDelay(pdMS_TO_TICKS(1000));
        }
    } while(SUCCESS != rc);
    ui_textarea_add("Successfully connected!\n", NULL, 0);
    ESP_LOGI(TAG, "Successfully connected to AWS IoT Core!");

    /*
     * Enable Auto Reconnect functionality. Minimum and Maximum time for exponential backoff for retries.
     *  #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
     *  #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
     */
    rc = aws_iot_mqtt_autoreconnect_set_status(&client, true);
    if(SUCCESS != rc) {
        ui_textarea_add("Unable to set Auto Reconnect to true\n", NULL, 0);
        ESP_LOGE(TAG, "Unable to set Auto Reconnect to true - %d", rc);
        abort();
    }

    ESP_LOGI(TAG, "Subscribing to '%s'", subscribe_topic);
    rc = aws_iot_mqtt_subscribe(&client, subscribe_topic, strlen(subscribe_topic), QOS0, iot_subscribe_callback_handler, NULL);
    if(SUCCESS != rc) {
        ui_textarea_add("Error subscribing\n", NULL, 0);
        ESP_LOGE(TAG, "Error subscribing : %d ", rc);
        abort();
    } else{
        ui_textarea_add("Subscribed to topic: %s\n\n", subscribe_topic, SUBSCRIBE_TOPIC_LEN) ;
        ESP_LOGI(TAG, "Subscribed to topic '%s'", subscribe_topic);
    }
    
    ESP_LOGI(TAG, "\n****************************************\n*  AWS client Id - %s  *\n****************************************\n\n",
             client_id);
    
    ui_textarea_add("Attempting publish to: %s\n", base_publish_topic, BASE_PUBLISH_TOPIC_LEN) ;
    while((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc)) {

        //Max time the yield function will wait for read messages
        rc = aws_iot_mqtt_yield(&client, 100);
        if(NETWORK_ATTEMPTING_RECONNECT == rc) {
            // If the client is attempting to reconnect we will skip the rest of the loop.
            continue;
        }

        ESP_LOGD(TAG, "Stack remaining for task '%s' is %d bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL));
        vTaskDelay(pdMS_TO_TICKS(PUBLISH_INTERVAL_MS));
        
        publisher(&client, base_publish_topic, BASE_PUBLISH_TOPIC_LEN);
    }

    ESP_LOGE(TAG, "An error occurred in the main loop.");
    abort();
}

void app_main()
{
    Core2ForAWS_Init();
    Core2ForAWS_Display_SetBrightness(100);
    
    ui_init();
    initialise_wifi();


    xTaskCreatePinnedToCore(&aws_iot_task, "aws_iot_task", 4096 * 2, NULL, 5, NULL, 1);
    xTaskCreatePinnedToCore(&blink_task, "blink_task", 4096 * 1, NULL, 2, &xBlink, 1);
    xTaskCreatePinnedToCore(&green_task, "green_task", 4096 * 1, NULL, 2, &xGreen, 1);
}

ui.c

C/C++
/*
 * AWS IoT EduKit - Core2 for AWS IoT EduKit
 * Smart Thermostat v1.3.0
 * ui.c
 * 
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"

#include "esp_log.h"
#include "core2forAWS.h"
#include "ui.h"

#define MAX_TEXTAREA_LENGTH 1024

static lv_obj_t *active_screen;
static lv_obj_t *out_txtarea;
static lv_obj_t *wifi_label;
static lv_obj_t *red_label;
static lv_obj_t* red_lmeter;
static lv_obj_t* door_label;
static lv_obj_t* mqtt_btn;
static lv_obj_t* mqtt_label;

static char *TAG = "UI";

static void mqtt_event_handler(lv_obj_t* button, lv_event_t event);

static void ui_textarea_prune(size_t new_text_length){
    const char * current_text = lv_textarea_get_text(out_txtarea);
    size_t current_text_len = strlen(current_text);
    if(current_text_len + new_text_length >= MAX_TEXTAREA_LENGTH){
        for(int i = 0; i < new_text_length; i++){
            lv_textarea_set_cursor_pos(out_txtarea, 0);
            lv_textarea_del_char_forward(out_txtarea);
        }
        lv_textarea_set_cursor_pos(out_txtarea, LV_TEXTAREA_CURSOR_LAST);
    }
}

void ui_textarea_add(char *baseTxt, char *param, size_t paramLen) {
    if( baseTxt != NULL ){
        xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
        if (param != NULL && paramLen != 0){
            size_t baseTxtLen = strlen(baseTxt);
            ui_textarea_prune(paramLen);
            size_t bufLen = baseTxtLen + paramLen;
            char buf[(int) bufLen];
            sprintf(buf, baseTxt, param);
            lv_textarea_add_text(out_txtarea, buf);
        } 
        else{
            lv_textarea_add_text(out_txtarea, baseTxt); 
        }
        xSemaphoreGive(xGuiSemaphore);
    } 
    else{
        ESP_LOGE(TAG, "Textarea baseTxt is NULL!");
    }
}

void ui_wifi_label_update(bool state){
    xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
    if (state == false) {
        lv_label_set_text(wifi_label, LV_SYMBOL_WIFI);
    } 
    else{
        char buffer[25];
        sprintf (buffer, "#0000ff %s #", LV_SYMBOL_WIFI);
        lv_label_set_text(wifi_label, buffer);
    }
    xSemaphoreGive(xGuiSemaphore);
}

void ui_meter_update(uint32_t value){
    xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
    lv_linemeter_set_value(red_lmeter, value);
    lv_label_set_text_fmt(red_label, "CO2: %d ppm", value);
    xSemaphoreGive(xGuiSemaphore);
}

void ui_door_label_update(char *txt) {
    xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
    lv_label_set_static_text(door_label, txt);
    xSemaphoreGive(xGuiSemaphore);
}

uint8_t ui_mqtt_get_value() {
    return lv_switch_get_state(mqtt_btn);
}

static void mqtt_event_handler(lv_obj_t* obj, lv_event_t event){
    if(event == LV_EVENT_VALUE_CHANGED) {
        uint8_t value = lv_switch_get_state(obj);

        if(value == 1) {
            lv_label_set_static_text(mqtt_label, "DOOR OPEN");
        } else {
            lv_label_set_static_text(mqtt_label, "DOOR CLOSED");
        }
        ESP_LOGI(TAG, "Door state: %x", value);
    }
}

void ui_init() {
    xSemaphoreTake(xGuiSemaphore, portMAX_DELAY);
    active_screen = lv_scr_act();
    wifi_label = lv_label_create(active_screen, NULL);
    lv_obj_align(wifi_label,NULL,LV_ALIGN_IN_TOP_RIGHT, 0, 6);
    lv_label_set_text(wifi_label, LV_SYMBOL_WIFI);
    lv_label_set_recolor(wifi_label, true);

    out_txtarea = lv_textarea_create(active_screen, NULL);
    lv_obj_set_size(out_txtarea, 300, 50);
    lv_obj_align(out_txtarea, NULL, LV_ALIGN_IN_TOP_MID, 0, 30);
    lv_textarea_set_max_length(out_txtarea, MAX_TEXTAREA_LENGTH);
    lv_textarea_set_text_sel(out_txtarea, false);
    lv_textarea_set_cursor_hidden(out_txtarea, true);
    lv_textarea_set_text(out_txtarea, "Welcome to\nSafe & Healthy Lounge Room\n");

    red_lmeter = lv_linemeter_create(active_screen, NULL);
    red_label = lv_label_create(active_screen, NULL);
    lv_label_set_static_text(red_label, "TEMP");
    lv_obj_align(red_label, active_screen, LV_ALIGN_IN_BOTTOM_LEFT, 20, -60);

    static lv_style_t red_lmeter_style;
    lv_style_init(&red_lmeter_style);
    lv_style_set_line_color(&red_lmeter_style, LV_STATE_DEFAULT, LV_COLOR_GREEN);
    lv_style_set_scale_grad_color(&red_lmeter_style, 40, LV_COLOR_RED);
    lv_style_set_border_opa(&red_lmeter_style, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_style_set_bg_opa(&red_lmeter_style, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_style_set_scale_end_color(&red_lmeter_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);


    lv_linemeter_set_range(red_lmeter, 0, 3000);                   //Set the range
    lv_linemeter_set_value(red_lmeter, 3000);                       //*Set the current value
    lv_linemeter_set_scale(red_lmeter, 240, 41);                  //Set the angle and number of lines
    lv_obj_add_style(red_lmeter, LV_LINEMETER_PART_MAIN, &red_lmeter_style);
    lv_obj_set_size(red_lmeter, 120, 120);
    lv_obj_align(red_lmeter, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 75);

    // door_label = lv_label_create(active_screen, NULL);
    // lv_label_set_static_text(door_label, "Door Closed");
    // lv_obj_align(door_label, active_screen, LV_ALIGN_IN_BOTTOM_RIGHT, 0, -45);

    mqtt_label = lv_label_create(active_screen, NULL);
    lv_label_set_static_text(mqtt_label, "SWIPE TO OPEN DOOR");
    lv_obj_align(mqtt_label, active_screen, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -65);
    mqtt_btn = lv_switch_create(active_screen, NULL);
    lv_obj_set_size(mqtt_btn, 300, 35);
    lv_obj_set_event_cb(mqtt_btn, mqtt_event_handler);
    lv_switch_off(mqtt_btn, LV_ANIM_OFF);
    lv_obj_align(mqtt_btn, active_screen, LV_ALIGN_CENTER, 0, 90);


    xSemaphoreGive(xGuiSemaphore);
}

blink.c

C/C++
/*
 * AWS IoT EduKit - Core2 for AWS IoT EduKit
 * Cloud Connected Blinky v1.4.0
 * blink.c
 * 
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "core2forAWS.h"
#include "blink.h"

static const char *TAG = "Blink";

void blink_task(void *arg) {
    vTaskSuspend( NULL );
    while (1) {
        Speaker_Init();
        Core2ForAWS_Speaker_Enable(1);
        Core2ForAWS_Sk6812_Clear();
        Core2ForAWS_Sk6812_Show();

        while (1) {     
            // Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_LEFT, 0xff0000);
            // Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_RIGHT, 0xff0000);
            // Core2ForAWS_Sk6812_Show();
            // Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_LEFT, 0x00ff00);
            // Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_RIGHT, 0x00ff00);
            // Core2ForAWS_Sk6812_Show();

            extern const unsigned char music[120264];
            Speaker_WriteBuff((uint8_t *)music, 120264, portMAX_DELAY);


            for (uint8_t i = 0; i < 5; i++) {
                Core2ForAWS_Sk6812_SetColor(i, 0xff0000);
                Core2ForAWS_Sk6812_Show();

                vTaskDelay(100 / portTICK_PERIOD_MS);
            }

            for (uint8_t i = 10; i > 4 ; i--) {
                Core2ForAWS_Sk6812_SetColor(i, 0xff0000);
                Core2ForAWS_Sk6812_Show();

                vTaskDelay(100 / portTICK_PERIOD_MS);
            }
            
            for (uint8_t i = 0; i < 5; i++) {
                Core2ForAWS_Sk6812_SetColor(i, 0x000000);
                Core2ForAWS_Sk6812_Show();
                vTaskDelay(100 / portTICK_PERIOD_MS);
            }
            
            for (uint8_t i = 10; i > 4; i--) {
                Core2ForAWS_Sk6812_SetColor(i, 0x000000);
                Core2ForAWS_Sk6812_Show();
                vTaskDelay(100 / portTICK_PERIOD_MS);
            }

            Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_LEFT, 0xff0000);
            Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_RIGHT, 0xff0000);
            Core2ForAWS_Sk6812_Show();

            extern const unsigned char music[120264];
            Speaker_WriteBuff((uint8_t *)music, 120264, portMAX_DELAY);

            for (uint8_t i = 40; i > 0; i--) {
                Core2ForAWS_Sk6812_SetBrightness(i);
                Core2ForAWS_Sk6812_Show();
                vTaskDelay(25 / portTICK_PERIOD_MS);
            }

            Core2ForAWS_Sk6812_SetBrightness(20);

        }
    }
    // Should never get here. FreeRTOS tasks loop forever.
    ESP_LOGE(TAG, "Error in blink task. Out of loop.");

    Core2ForAWS_Speaker_Enable(0);
    Speaker_Deinit();

    abort();
}


void green_task(void *arg) {
    vTaskSuspend( NULL );
    while (1) {
        Speaker_Init();
        Core2ForAWS_Speaker_Enable(1);
        Core2ForAWS_Sk6812_Clear();
        Core2ForAWS_Sk6812_Show();

        while (1) {     
            extern const unsigned char music[120264];
            Speaker_WriteBuff((uint8_t *)music, 120264, portMAX_DELAY);
            
            for (uint8_t i = 0; i < 5; i++) {
                Core2ForAWS_Sk6812_SetColor(i, 0x00ff00);
                Core2ForAWS_Sk6812_Show();

                vTaskDelay(100 / portTICK_PERIOD_MS);
            }

            for (uint8_t i = 10; i > 4 ; i--) {
                Core2ForAWS_Sk6812_SetColor(i, 0x00ff00);
                Core2ForAWS_Sk6812_Show();

                vTaskDelay(100 / portTICK_PERIOD_MS);
            }
           
            for (uint8_t i = 0; i < 5; i++) {
                Core2ForAWS_Sk6812_SetColor(i, 0x000000);
                Core2ForAWS_Sk6812_Show();
                vTaskDelay(100 / portTICK_PERIOD_MS);
            }
            
            for (uint8_t i = 10; i > 4; i--) {
                Core2ForAWS_Sk6812_SetColor(i, 0x000000);
                Core2ForAWS_Sk6812_Show();
                vTaskDelay(100 / portTICK_PERIOD_MS);
            }

            Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_LEFT, 0x00ff00);
            Core2ForAWS_Sk6812_SetSideColor(SK6812_SIDE_RIGHT, 0x00ff00);
            Core2ForAWS_Sk6812_Show();

            for (uint8_t i = 40; i > 0; i--) {
                Core2ForAWS_Sk6812_SetBrightness(i);
                Core2ForAWS_Sk6812_Show();
                vTaskDelay(25 / portTICK_PERIOD_MS);
            }

            Core2ForAWS_Sk6812_SetBrightness(20);
        }
    }
    // Should never get here. FreeRTOS tasks loop forever.
    ESP_LOGE(TAG, "Error in blink task. Out of loop.");

    Core2ForAWS_Speaker_Enable(0);
    Speaker_Deinit();

    abort();
}

blink.h

C Header File
/*
 * AWS IoT EduKit - Core2 for AWS IoT EduKit
 * Cloud Connected Blinky v1.4.0
 * blink.h
 * 
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

TaskHandle_t xBlink;

void blink_task(void *arg);
void green_task(void *arg);

Credits

Dilghas Ommer

Dilghas Ommer

4 projects • 2 followers
Experienced Marketing with demonstrated history of working in the research industry. Strong professional with a B.Tech focused in E&I Engg.

Comments