My first experience with IoT was during my undergraduate studies while working on my bachelor thesis. This is what motivated me to further explore IoT and the ABH Internship in IoT seemed like the best opportunity to gain practical experience in the field. Unlike the other ABH Internship projects, which have been offered before, the IoT internship was new so there was very little information and I had no idea what to expect. Needless to say, I was excited and nervous at the same time.

The goal of the project I worked on during the IoT internship was the implementation of firmware for the ESP32 microcontroller, which would support the following tasks: Wi-Fi connectivity, Configurability, read data from sensors and send data to MQTT server. I used 3 different sensors: temperature and humidity sensor, light sensor and sound sensor. I used the official Espressif IoT Development Framework for programming.

I took information from the sensors via analog inputs and I continued to distribute them via wi-fi using the MQTT communication protocol. The MQTT protocol works on the publish/subscribe principle. I used the ESP32 microcontroller as a publisher who published information collected from a sensor on a MQTT server on a defined topic. We could access this information from another client by connecting to the MQTT server and subscribing to the same topic.

For the purpose of this blog, I decided to only show the code for the temperature sensor.

In addition to the functions necessary for wi-fi connection and configuration of the MQTT server, it is necessary to implement functions for reading data from the sensor and sending it to the MQTT server. “sensor_task” serves to read data from the sensor, while “publish_task” serves to send the data to the MQTT server as shown in the code below:

void sensor_task(void *pvParameter)
{

    int send_message = CONFIG_SAMPLING_TIME - 2000;
    ESP_LOGI(TAG, "sensor_task started");

    //set gpio for temperature sensor
    setDHTgpio(26);
    float temp = 0;

    while (1)
    {

        int ret = readDHT();
        errorHandler(ret);

	//function getTemperature(); read data from sensor
        temp = getTemperature();

        if (!xQueueSend(queueTemperature, &temp, send_message))
        {
            printf("Faild to send temperature to queue\n");
        }

        vTaskDelay(pdMS_TO_TICKS(CONFIG_SAMPLING_TIME));
    }
}

void publish_task(void *pvParameter)
{
    ESP_LOGI(TAG, "publish_task started");
    float temp1 = 0;
    int receive_message = CONFIG_SAMPLING_TIME + 100;

    xEventGroupWaitBits(bit_status, START_BIT, false, true, portMAX_DELAY);
    ESP_LOGI(TAG, "publish_task xEventGroupWaitBits done");

    char *create_cJSON_temperature(void)
    {

        time(&now);
        localtime_r(&now, &timeinfo);
        strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);

        char *string = NULL;

        cJSON *root_temp = cJSON_CreateObject();
        cJSON *fmt_temp = NULL;

        if (cJSON_AddStringToObject(root_temp, "version", "1.0") == NULL)
        {
            goto end;
        }

        if (cJSON_AddStringToObject(root_temp, "timestamp", strftime_buf) == NULL)
        {
            goto end;
        }

        if (cJSON_AddStringToObject(root_temp, "type", "metrics") == NULL)
        {
            goto end;
        }

        cJSON_AddItemToObject(root_temp, "payload", fmt_temp = cJSON_CreateObject());

        if (cJSON_AddStringToObject(fmt_temp, "name", "temperature") == NULL)
        {
            goto end;
        }

        if (cJSON_AddNumberToObject(fmt_temp, "value", temp1) == NULL)
        {
            goto end;
        }
        if (cJSON_AddStringToObject(fmt_temp, "timestamp", strftime_buf) == NULL)
        {
            goto end;
        }

        string = cJSON_Print(root_temp);
        if (string == NULL)
        {
            fprintf(stderr, "Failed to print temperature.\n");
        }

    end:
        cJSON_Delete(root_temp);
        return string;
    }

    while (1)
    {

        if (xQueueReceive(queueTemperature, &temp1, receive_message))
        {

            char *json_temp = NULL;
            json_temp = create_cJSON_temperature();
            esp_mqtt_client_publish(mqtt_client, "abh-iot/telemetry/intern-device/metrics/temperature", json_temp, 0, 0, 0);
        }

        else
        {
            printf("Faild to recive temperature to queue!\n");
        }
    }
}

 

The most difficult period was the first month of the Internship when I spent a lot of time doing research because I was introduced to new technologies. The second month was much more productive because I focused on specific tasks, and since I managed to finish the project, I had more time to realize some of the ideas that I thought were interesting. This Internship helped me learn many new things related to IoT, with which I have been able to expand my horizons in this field, and all thanks to my mentor Mirnes who gave me quality guidance and advice.

Crafting Collaboration in Elixir: A Case Study of Success in Software Development
Product ManagementSoftware DevelopmentTop
August 8, 2023

Crafting Collaboration in Elixir: A Case Study of Success in Software Development

Uniting different skill sets, experiences, and perspectives through engineering collaboration is a process that brings complex challenges, but also can forge innovation through synergy if done correctly. Atlantbh had a chance to work on such a project where close day-to-day collaboration with an external development team was needed for successful…

Leave a Reply