This week, I built a small embedded program with ChatGPT at the proverbial coder’s keyboard. The only coding I did was to correct small mistakes and to integrate the chunks I asked ChatGPT to generate. I am still experimenting with the various ways I can communicate with ChatGPT, so bear with me as I try some new things.
For our project, we will use an ESP32 and an OLED screen to display famous quotes retrieved from the Internet. The design will be roughly like this:
We won’t concern ourselves with smaller details, like how the user might configure the ESP32 to connect to a wireless network. At this point, we are concerned with standing up the ‘core technology’ of our ‘product.’
ESP32 Quote Fetching (Written by ChatGPT)
First things first, we need to get our ESP32 to retrieve quotes from the Internet. I was originally dubious about ChatGPT’s ability to write an application that could pull these quotes, so I started by asking it to write a program in Python. With a Python script, I could verify functionality before adding steps like compilation and flashing to the mix.
After proving out its abilities in Python, I asked ChatGPT to write the same program in C. You can see our conversation below. Notice the back-and-forth nature of the conversation, including corrections and specifications I made to ChatGPT:
Travis: Can you write the same program in C for a WROOM ESP32? ChatGPT: Yes, I can! Here's an example of how you could implement the same program in C for an ESP32 module using the ESP-IDF framework: ...CODE BLOCK... Travis: This is for esp32 so needs a loop and setup ChatGPT: You're right, I apologize for that oversight. Here's an updated version of the program that includes a setup and loop function for the ESP32: ...CODE BLOCK... Travis: Can you replace the printf with a serial out ChatGPT: Sure, here's the modified program with serial output instead of printf: ...CODE BLOCK... Travis: Sorry I meant the arduino serial out ChatGPT: No problem! Here's the modified program with serial output using the Arduino Serial library:
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <stdlib.h>
#include <time.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void fetch_quote(char *quote, char *author) {
WiFiClient client;
HTTPClient http;
http.begin(client, "https://api.quotable.io/random");
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
Serial.printf("HTTP request failed: %s\n", http.errorToString(httpCode).c_str());
http.end();
return;
}
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
String content = doc["content"].as<String>();
String author_item = doc["author"].as<String>();
content.toCharArray(quote, 256);
author_item.toCharArray(author, 256);
http.end();
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Serial connected!");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
srand(time(NULL));
}
void loop() {
char quote[256];
char author[256];
fetch_quote(quote, author);
char output[512];
sprintf(output, "%s\n- %s\n", quote, author);
Serial.println(output);
delay(1000);
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Serial connected!");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
srand(time(NULL));
}
void loop() {
char quote[256];
char author[256];
fetch_quote(quote, author);
char output[512];
sprintf(output, "%s\n- %s\n", quote, author);
Serial.println(output);
delay(1000);
}
After a quick attempt at compiling, I see that I don’t have the ‘ArduinoJson.h’ that ChatGPT wants to use, so I ask how to attain that in the following snippet of the conversation:
Travis: How do I use the ArduinoJson.h? ChatGPT: ArduinoJson.h is a library that provides a simple and efficient way to parse and generate JSON data on Arduino and other embedded systems. To use it, you'll need to follow these steps: Download and install the library. You can do this by going to the "Sketch" menu in the Arduino IDE, selecting "Include Library", and then "Manage Libraries". In the Library Manager, search for "ArduinoJson" and click "Install" to install the latest version. ...MORE INSTRUCTIONS (NOT NEEDED FOR OUR CASE)...
Once I followed ChatGPT’s instruction, I found one final bug: There were two ‘setup’ and ‘loop’ blocks. Once I fixed this simple mistake, I then had a program that could be compiled. The next exciting step would be to flash it to the ESP32 and then see if the serial monitor would start displaying quotes!
Attempt #1 Output
Serial connected! Connecting to WiFi... Connecting to WiFi... Connecting to WiFi... Connecting to WiFi... Connecting to WiFi... WiFi connected! HTTP request failed: connection lost xV���k@T@0 - ��@�? HTTP request failed: connection lost xV���k@T@0 - ��@�? ...MORE OF THIS...
The ESP32 connected to WiFi. It looks like it attempted an HTTP request, but the connection was lost. Let’s ask ChatGPT what is up…
Travis: I am getting 'HTTP request failed: connection lost' ChatGPT: The error "HTTP request failed: connection lost" could indicate that the ESP32 is losing its Wi-Fi connection during the HTTP request. This could happen if the Wi-Fi signal is weak or unstable. To troubleshoot this issue, you could try the following steps: 1. Make sure that the ESP32 is connected to a stable Wi-Fi network with a strong signal. 2. Increase the timeout for the HTTP request by calling the setTimeout method on the HTTPClient object. For example: ...CODE BLOCK... This will give the ESP32 more time to complete the HTTP request before timing out. 3. Try using a different HTTP client library, such as the WiFiClient library, to make the HTTP request. For example: ...CODE BLOCK... This code uses the WiFiClient library to make an HTTP GET request to the API server. Note that this code does not use HTTPS, so it is less secure than the previous code that used WiFiClientSecure. However, it may be more reliable in some situations. I hope this helps! Let me know if you have any other questions.
ChatGPT gave me a list of debug options. Super helpful!
I took ChatGPT’s list out of order and honed in on a small detail in the ‘WiFiClientSecure’ debug option. I noticed in the original code that ChatGPT was accessing the quotable API using an HTTPS connection. I simply removed the ‘s’ from ‘https’ to access it without SSL security and… (drum-roll, continue onto the next section please!)…
Attempt #2 Output, Nicely Done ChatGPT!
Serial connected! Connecting to WiFi... Connecting to WiFi... Connecting to WiFi... WiFi connected! Never find fault with the absent. - Alexander Pope One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man. - Elbert Hubbard Real success is finding your lifework in the work that you love. - David McCullough A dream is your creative vision for your life in the future. You must break out of your current comfort zone and become comfortable with the unfamiliar and the unknown.
We have quotes!
ESP32 OLED Display Output
All right, now we need to set up the quotes we get from the Internet to display on our OLED screen.
Check out my continued conversation with ChatGPT as I ask for help on the display portion of the project.
Travis: I have a 0.96" OLED screen, are you familiar with them? ChatGPT: Yes, I'm familiar with 0.96" OLED screens! They are commonly used with microcontrollers and are great for displaying text and graphics in small projects. There are a variety of libraries available for interfacing with OLED screens, so depending on the specific screen you have, there may be different libraries that work best. If you have any questions about using an OLED screen with your project, feel free to ask! Travis: I would like to display the fetched quote on the OLED screen instead of serial out ChatGPT: Sure, here's an example code snippet that shows how you can display the fetched quote on a 0.96" OLED screen using the Adafruit_SSD1306 library: ...CODE BLOCK (See Below)... In this example code, we're using the Adafruit_SSD1306 library to initialize and interact with the OLED screen. The fetched quote is then displayed on the OLED screen using the display object's println() and setCursor() methods. Note that you'll need to modify the API_HOST, API_URL, your_SSID, and your_PASSWORD variables to match your specific requirements. Travis: What pins are used for the OLED screen on the ESP32? ChatGPT: The pins used for the OLED screen on the ESP32 depend on the specific breakout board you're using. However, many 0.96" OLED screens for the ESP32 use the I2C protocol to communicate with the microcontroller. Here's an example pinout for the popular ESP32 DevKitC board and an OLED screen using the I2C protocol: ...TABLE (See Below)... Note that the pin names on your OLED screen may be different depending on the manufacturer, so it's important to consult the documentation for your specific screen to ensure you're using the correct pins. Additionally, you may need to install additional libraries to interface with the OLED screen depending on the specific breakout board you're using.
To summarize our conversation, I asked if ChatGPT was familiar with the OLED screen I was using. Then, I asked if ChatGPT could generate the code to display the quote fetched on the OLED screen. Finally, I asked how I should wire my ESP32 to the OLED screen.
Below you will find the code and pin-out table that ChatGPT provided me. I noticed a couple of things, namely:
- ChatGPT has nearly zero deterministic behavior between code generations. Each one was noticeably unique.
- Anecdotally, ChatGPT seems to have organized the code better with the added complexity of displaying to the OLED screen.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address of the OLED screen
#define API_HOST "api.quotable.io"
#define API_URL "/random"
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("SSD1306 allocation failed");
while (1);
}
// Clear the display buffer
display.clearDisplay();
// Set text color to white
display.setTextColor(SSD1306_WHITE);
// Set font size
display.setTextSize(1);
// Connect to Wi-Fi network
WiFi.begin("your_SSID", "your_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Make HTTP request to API
HTTPClient http;
http.begin(API_HOST, 80, API_URL);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// Parse JSON response
const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 160;
DynamicJsonDocument doc(bufferSize);
deserializeJson(doc, payload);
const char* quote = doc["content"];
const char* author = doc["author"];
// Display quote on OLED screen
display.clearDisplay();
display.setCursor(0, 0);
display.println(quote);
display.setCursor(0, 20);
display.println("- " + String(author));
display.display();
} else {
Serial.println("HTTP request failed");
}
// Wait 10 seconds before making the next request
delay(10000);
}
ESP32 Pin | OLED Pin |
---|---|
GPIO21 | SDA |
GPIO22 | SCL |
3.3V | VCC |
GND | GND |
Display, Attempt #1 Output
I am blown away by the results of this. It worked! There was only one flaw: When a quote was too long, it wrapped around the screen over the top of the author’s name. This display problem was due to ChatGPT’s use of hard-coded location coordinates. I could see two solutions right out of the gate:
- Have the quote scroll across the screen instead of wrapping.
- Have the author’s name appended to the quote with a new line just before it.
Either of these would have worked, but both required too much coding for me. So, I asked ChatGPT to do it!
Travis: The text wraps and is colliding with the authors name, how can I resolve this? ChatGPT: To prevent text from colliding with other elements on your OLED screen, you may need to adjust the font size, spacing, or position of your text. Here are some suggestions for resolving the text wrapping issue: List Items 1..2 3. Move the author's name to a different location on the OLED screen, such as below the quote text or in a separate area of the screen. This will create more space for the quote text to be displayed without colliding with other elements. List Items 4..5 Travis: How would I implement option #3 ChatGPT: To move the author's name to a different location on the OLED screen, you can adjust the Y position of the text using the display.drawString() function. Here's an example of how to move the author's name to be displayed below the quote text: ...CODE BLOCK... In this example, we first display the quote text using the drawStringMaxWidth() function, which automatically wraps the text based on the width of the OLED screen. We then use the setCursor() function to move the cursor to a new line below the quote text, and display the author's name using the print() function. Note that you may need to adjust the Y position of the cursor depending on the size of your OLED screen and the amount of spacing you want between the quote text and the author's name. Travis: How would I dynamically change the authors line to be below the quote text ChatGPT: If you want to dynamically change the position of the author's name based on the length of the quote text, you can calculate the height of the quote text and adjust the Y position of the author's name accordingly. Here's an example of how to do this: ...MORE DEBUG (2 More Rounds of Questions)... Travis: Is there a way to simply get the cursors location after a println? ChatGPT: In Arduino, you can use the getTextBounds() function to get the size of the text that was just printed. You can then use this information to calculate the position of the next line of text. Here's an example: ...CODE BLOCK... In this example, we start by printing the first line of text. We then use the getTextBounds() function to get the size of the printed text. The function takes six parameters: the string of text to measure, the x and y coordinates of the cursor, and four output variables that will contain the position and size of the text. After getting the size of the printed text, we calculate the position of the next line of text by adding some padding between lines. Finally, we print the second line of text at the new cursor position.
ChatGPT’s Solution to the Text Collision
Basically, I asked ChatGPT how to resolve the text collision with the author’s name and it gave me a list of suggestions. I opted for moving the author’s name below the quote on the screen. I then asked ChatGPT to generate the code for me. The new code broke my compile by requiring things that were non-existent in the libraries I have. After a few more questions, I had an example of a way to do it without using the libraries it had suggested. Below is the example that got me “un-stuck”:
int x, y; // x and y coordinates of the cursor
// Print the first line of text
display.println("Hello, world!");
// Get the size of the printed text
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds("Hello, world!", x, y, &x1, &y1, &w, &h);
// Calculate the position of the next line of text
x = x + w + 5; // add some padding between lines
y = y + h;
// Print the second line of text
display.println("This is the second line.");
I couldn’t merely copy and paste the above snippet of ChatGPT’s code into the overall program. Instead, I had to use it as a general example to guide me as I implemented my own solution. As the architect rather than the technical developer, this was almost too much coding work, but it was still helpful for finishing out the quote display sketch. The final code is below:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address of the OLED screen
#define API_HOST "api.quotable.io"
#define API_URL "/random"
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("SSD1306 allocation failed");
while (1);
}
// Clear the display buffer
display.clearDisplay();
// Set text color to white
display.setTextColor(SSD1306_WHITE);
// Set font size
display.setTextSize(1);
// Connect to Wi-Fi network
WiFi.begin("your_SSID", "your_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Make HTTP request to API
HTTPClient http;
http.begin(API_HOST, 80, API_URL);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// Parse JSON response
const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 160;
DynamicJsonDocument doc(bufferSize);
deserializeJson(doc, payload);
const char* quote = doc["content"];
const char* author = doc["author"];
display.clearDisplay();
display.setCursor(0, 0);
display.println(quote);
// Get the size of the printed text
int x, y; // x and y coordinates of the cursor
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(quote, x, y, &x1, &y1, &w, &h);
// Calculate the position of the next line of text
x = x + w + 5; // add some padding between lines
y = y + h;
display.setCursor(x, y);
display.println("- " + String(author));
display.display();
} else {
Serial.println("HTTP request failed");
}
// Wait 10 seconds before making the next request
delay(10000);
}
Conclusion
I was able to get a functioning copy of the core technology of this project in under 2 hours. Granted, I did need some background knowledge in coding to be able to integrate and get required libraries. Keep in mind though that I still got stuck, but ChatGPT was able to guide me through each time.
So far, I am concerned about a few things after using ChatGPT:
- Does it actually make things faster?
- Blind acceptance is problematic.
- I’m not so keen to be reliant on something which exists exclusively online.
Let’s quickly address these three points.
Does it actually make things faster?
I have to question how much faster this actually makes an engineer. In the case of this morning, I think I completed the goal faster than I might have otherwise. However, there is a case to be made that the large amount of output from ChatGPT can easily make an engineer feel like they are making headway, when in fact they are not.
Blind acceptance is problematic (ChatGPT might have your back)
In the article today, ChatGPT offered a few different API’s for generating quotes from the Internet. This is a problem if ChatGPT’s training data happened to include malicious APIs and someone blindly accepted it as “working.” The same concern applies to libraries suggested by ChatGPT.
Reliance on something that exists only online
This concern is probably the least troublesome of all three, as most developers will presumably have an Internet connection. However, if you are relying on ChatGPT for the majority of your work, you may be lost if your Internet connection is compromised. In the future, it would be awesome to have an onsite version of the Large Language Model to ensure engineers have guaranteed access. A model developer firm could license local versions (based on a general set of training data) to individual corporations. The client companies could then continue to train their individually-licensed models on their own internal data. Intellectual property would be safeguarded and models would be more versatile and accurate when responding to user needs.
Wrapping Up
Thank you for reading this week’s post! It was really quite amazing working with ChatGPT. I learned some new and crazy things, like the Python “Easter egg” built-in library called this that contains a poem written by Tim Peters (?!).
As always, please feel free to leave a comment below!
-Travis