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.

Decorator pattern
Software DevelopmentTech Bites
January 23, 2023

Decorator pattern

Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code. A decorator pattern allows users to add new functionality to an existing object without altering its structure. This design pattern comes under…
JPA annotations in Hibernate
Software DevelopmentTech Bites
January 5, 2023

JPA annotations in Hibernate

Suppose you have wondered how we interact with relational databases without (directly) using SQL queries. In that case, the answer usually lies in Object-Relational Mapping (ORM), which will do the job of converting Java objects and statements to database tables and related queries. Hibernate comes into this story as the…
Dynamic programming
Software DevelopmentTech Bites
December 9, 2022

Dynamic Programming

Dynamic programming is both a mathematical optimization method and a computer programming method. By definition, it is a technique in computer programming that helps to efficiently solve a class of problems with overlapping subproblems and optimal substructure properties. It refers to simplifying a complicated problem by breaking it into simpler…

Leave a Reply