Table of contents:

The ChatGPT chatbot has been a breakthrough in artificial intelligence technology, and the potential of this product is amazing. Many experts from different fields share their experiences of using an AI-based chatbot in their work. The 2Smart team also decided to test whether ChatGPT can make life easier for an IoT device enthusiast.

Developing an IoT device with ChatGPT

We decided to give ChatGPT an easy task to see how it handles it. There is a “Get started” section on the 2Smart blog, where several articles describe the creation of a Wi-Fi relay, that is, an elementary IoT product with very simple functionality. Creating the same device will become a technical task for the chatbot.

IoT device using ChatGPT: writing ESPHome configuration with the help of AI

It was proposed to use ESPHome as a platform for writing device firmware. 2Smart Cloud supports low-code IoT development for smart innovators and allows them to create cloud devices with ESPHome-based firmware. For this, we use our unique bridge. This bridge transforms the ESPHome commands into messages that the MQTT broker understands (more about how to use MQTT Explorer to work with 2Smart broker). This way, developers can use the ESPHome syntax and create firmware without even knowing how to program. This is what enthusiasts who build their smart products with 2Smart Cloud use. The end users of devices created in this way have access to the Telegram bot for managing IoT devices and other platform features.

We decided to see if ChatGPT could make life easier for enthusiast developers and write ESPHome-based firmware for them. And to make the experiment as authentic as possible, we acted like a “dummy” at all stages of communication with the bot, accepting his answers uncritically.

The terms of reference for the device schematic and how ChatGPT coped with it

Focusing on the basic functionality of the Wi-Fi relay described in the “Get started” section, we set the following task for the chatbot:

“Please draw a schematic for an IoT device using the following requirements:

  • The device is controlled by the ESP32 microcontroller.
  • A 10A mini relay is connected to the microcontroller.
  • The LED that lights up when the relay is turned on is connected to the MCU.
  • The button, with which the user of the device can manually switch the state of the relay, is also connected to the microcontroller”.

After some thought, ChatGPT generated the following schematic:

IoT device using ChatGPT: writing ESPHome configuration with the help of AI

The schematic turned out to be incomprehensible to the “dummy,” and we did not use it. Instead, we decided to assemble the device based on the same schematic that has already been described in our blog. It is easy to find it on the Internet and repeat it from photos of finished devices on a breadboard. Once the device was built, we asked ChatGPT to write its firmware configuration.

Terms of reference for ESPHome relay firmware configuration and what happened as a result

Since we assembled the device according to our own schematic and connected the relay, button, and LED to specific pins of the ESP32 microcontroller, we changed the task for ChatGPT a little and made it more specific. It looked like this:

“Please help me to write ESPHome configuration for IoT device based on ESP32 microcontroller. This will be a Wi-Fi relay with remote management that I will connect to the 2Smart Cloud platform.

Here is the specification for the device:

  • The device is controlled by the ESP32 microcontroller.
  • A 10A mini relay is connected to the GPIO4 pin of the microcontroller.
  • The LED that lights up when the relay is turned on is connected to the GPIO2 pin.
  • The button, with which the user of the device can manually switch the state of the relay, is connected to the GPIO13 pin.
  • I want the microcontroller to report the current Wi-Fi signal level to the platform every 5 seconds. This also needs to be taken into account in the configuration”.

After a little thought, the chatbot wrote the following ESPHome configuration:

esphome:
  name: iot_relay
  platform: ESP32
  board: esp32dev

wifi:
  ssid: "your_wifi_ssid"
  password: "your_wifi_password"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Iot Relay Fallback Hotspot"
    password: "your_fallback_password"

  # Report WiFi signal level every 5 seconds
  # Use the 'mqtt' component to send this data to the 2Smart Cloud platform
  on_loop:
    - if:
        condition:
          wifi.connected:
        then:
          - mqtt.publish:
              topic: "iot_relay/wifi_signal"
              payload: !lambda 'return to_string(WiFi.RSSI());'
          - delay: 5s

mqtt:
  broker: "mqtt.2smart.com"
  username: "your_mqtt_username"
  password: "your_mqtt_password"

switch:
  - platform: gpio
    pin: GPIO4
    name: "Iot Relay"
    id: iot_relay_switch
    icon: "mdi:power-socket"
    restore_mode: ALWAYS_ON
    on_turn_on:
      - gpio.output: GPIO2, HIGH
    on_turn_off:
      - gpio.output: GPIO2, LOW

button:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    name: "Iot Relay Button"
    on_press:
      - switch.toggle: iot_relay_switch

We started creating a new product in the 2Smart Cloud platform, choosing ESP32 as the microcontroller and ESPHome as the firmware base. We inserted the resulting code into the editor, removing obviously unnecessary blocks from it.

When using ESPHome in the 2Smart Cloud platform, you only need to describe the logic of the operation of specific device sensors. Therefore, we removed everything related to Wi-Fi credentials, MQTT broker parameters, and service information.

As a result, we left the following part of the configuration, which describes only the logic of the relay, the buttons for its control, and the LED. It also solves the problem of transmitting information about the current Wi-Fi signal level:

  # Report WiFi signal level every 5 seconds
  # Use the 'mqtt' component to send this data to the 2Smart Cloud platform
  on_loop:
    - if:
        condition:
          wifi.connected:
        then:
          - mqtt.publish:
              topic: "iot_relay/wifi_signal"
              payload: !lambda 'return to_string(WiFi.RSSI());'
          - delay: 5s

switch:
  - platform: gpio
    pin: GPIO4
    name: "Iot Relay"
    id: iot_relay_switch
    icon: "mdi:power-socket"
    restore_mode: ALWAYS_ON
    on_turn_on:
      - gpio.output: GPIO2, HIGH
    on_turn_off:
      - gpio.output: GPIO2, LOW

button:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    name: "Iot Relay Button"
    on_press:
      - switch.toggle: iot_relay_switch

We got an error message when we started building test firmware for a device with this configuration. Transferring information about the Wi-Fi signal level in this form to 2Smart Cloud will not work. In principle, this was immediately clear since ChatGPT proposed the implementation of this feature using a command for the MQTT broker. In our platform, everything is simpler, and for this functionality, you need to use the sensor mechanism. But we promised to be as non-critical as possible, so we left this block. We obliterated it and tried again.

IoT device using ChatGPT: writing ESPHome configuration with the help of AI

On the next attempt to build the test firmware, the platform could not process the strings responsible for the LED’s operation. After removing them, we got the following short configuration:

switch:
  - platform: gpio
    pin: GPIO4
    name: "Iot Relay"
    id: iot_relay_switch
    icon: "mdi:power-socket"
    restore_mode: ALWAYS_ON

button:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    name: "Iot Relay Button"
    on_press:
      - switch.toggle: iot_relay_switch

Re-running the firmware build, we got a new error on the platform: “Component not found: button”. Referring to the official documentation of ESPHome, we read that to connect a physical button, you need to use another component – “binary_sensor”. Fixing the config manually:

switch:
  - platform: gpio
    pin: GPIO4
    name: "Iot Relay"
    id: iot_relay_switch
    icon: "mdi:power-socket"
    restore_mode: ALWAYS_ON

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    name: "Iot Relay Button"
    on_press:
      - switch.toggle: iot_relay_switch

Test firmware build is successful! After it is installed on the device, the relay is connected to the platform. Using an IoT app builder with no-code, we created a mobile app layout for device control and used it to switch the relay from the browser. The device works correctly, and even the LED lights up when the relay is turned on and goes out when it is turned off. This feature works even though we removed the mention of the connected diode from the ESPHome configuration.

IoT device using ChatGPT: writing ESPHome configuration with the help of AI

However, we are missing a helpful feature that was in the original terms of reference and which had to be abandoned. Namely, transmitting information about the current Wi-Fi signal level. We ask ChatGPT to implement this functionality again, but we already give it some suggestive information:

“Let’s get back to transmitting information about the current Wi-Fi signal level and add this block to the configuration. Let this information be transmitted through the text sensor mechanism”.

ChatGPT rewrites the ESPHome configuration again and offers the following solution to implement the feature we need:

text_sensor:
  - platform: wifi_signal
    name: "Iot Relay Wi-Fi Signal Level"
    update_interval: 60s

We added these lines to the configuration and started building the firmware again. We got an error – the platform does not accept the value “text_sensor”. We clarify according to the ESPHome documentation – to get the “wifi_signal” parameter, the value “sensor” should be used. We realized that as much as we are trying to be non-critical to the chatbot, it is so non-critical to our requests – if we asked for a text sensor, he specified a text sensor, although this contradicts the documentation.

By correcting “text_sensor” to “sensor”, we got the ESPHome configuration without errors, built a new version of the test firmware, and installed it on the device. In the mobile app interface editor, it became possible to bind the “Signal lvl” widget to a new device sensor that transmits information about the Wi-Fi signal level. All in all, we got the device we wanted in about an hour.

IoT device using ChatGPT: writing ESPHome configuration with the help of AI

Conclusions and thoughts

After trying to use a popular chatbot to create a simple IoT device, we had mixed feelings. On the one hand, it’s amazing to watch the magic of how ChatGPT generates responses to requests, draws device schematics, writes configuration in a code editor, and fixes errors that the user points out to it.

However, mistakes do happen, and from the point of view of ESPHome standards, they are gross. Yes, 2Smart Cloud does not require you to specify Wi-Fi credentials, MQTT broker settings, and other service information in the ESPHome configuration, which enthusiast developers usually have to specify. Our bridge takes care of this, and we took this into account by removing the “extra” lines beforehand. ChatGPT is not required to know about these nuances, although we told him we would use the 2Smart platform to connect the device.

The problem is that even for such an elementary device as a relay, the chatbot made mistakes, which the enthusiast must refer to the ESPHome documentation to correct. First, the bot created the proper configuration for connecting the button but did not take into account that we needed a physical button, not a logical one. Then he easily succumbed to our provocation and described “wifi_signal” as a text sensor, not a regular one.

Thus, ChatGPT can assist IoT enthusiasts in creating an ESPHome-based device, but there is one paradox. An enthusiast must have the necessary knowledge or at least be able to use the documentation for ESPHome to correct the chatbot results.

This AI-powered bot can already drastically reduce device configuration time, and we believe it can handle much more complex tasks than Wi-Fi relay configuration. However, the user must set the task as completely and accurately as possible; otherwise, mistakes are inevitable. It is too early to shift all your tasks to artificial intelligence in 2023.

Rate this article:

4.7 / 5. 10

( 10 votes )

Don't forget to share this post!

Let’s dive into your case

Share with us your business idea and expectations about the software or additional services.

    Thanks for contacting us!

    We will get in touch soon.

    This site is protected by Google reCAPTCHA and the following Privacy policy and Terms of use apply

    Send us a message

    This site is protected by Google reCAPTCHA and the following Privacy policy and Terms of use apply