logo elektroda
logo elektroda
X
logo elektroda

ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

p.kaczmarek2  17 11085 Cool? (+4)
Listen:

TL;DR LABEL_AI_GENERATED

  • An ESP8266 NodeMcu v3 CH340 uses the OpenAI API over HTTPS to query ChatGPT directly without a PC or Raspberry Pi.
  • The setup uses Visual Studio Code, PlatformIO, WiFiClientSecureBearSSL, and ArduinoJSON to build POST requests and parse JSON responses.
  • The project uses an ESP8266 board for PLN 17 and targets GPT-3.5-turbo; 1000 tokens are about 750 words.
  • The basic request works, but responses can be repetitive, API usage is paid, and the demo skips SSL certificate verification and chat history.
AI summary based on the discussion. May contain errors.

I will show here how to run ChatGPT on ESP8266 - not directly, of course, but through the OpenAI API. I will demonstrate a simple code that sends an appropriate request to the API via HTTPS, along with an overview of the API key transfer and JSON format support. The result will be the ability to query the famous Chatbot directly from ESP, without using a computer or even a Raspberry Pi.
This topic will be the basis for those who want to do some DIY with OpenAI language models on ESP8266, I will cover the absolute basics here and maybe show something more advanced in the future.

OpenAI API
OpenAI language models are available through the API, i.e. we only send a request to OpenAI servers describing what the language model should do, and then we receive their response. We don't run ChatGPT on our hardware of course, it's not even downloadable.
The OpenAI API is very rich and offers various functions (and prices; depending on which language model we are interested in), but there is no point in me copying their documentation here. You will find everything below:
https://openai.com/blog/openai-api
Detailed documentation is available here:
https://platform.openai.com/docs/api-reference/making-requests
A short reading tells us what we have to do. We need to send a POST request with the following headers:
- Content-Type: application/json
- Authorization: Bearer OUR_KEY
- Content-Length: JSON_LENGTH (it's worth adding this as a rule)
and content:
Code: JSON
Log in, to see the code

on endpoint:

https://api.openai.com/v1/chat/completions


The model defines the language model we use. The list of access models is here (also requires an API key):

https://api.openai.com/v1/models

so you can download it e.g. via CURL:

curl https://api.openai.com/v1/models \
  -H "Authorization: Bearer $OPENAI_API_KEY"

The temperature parameter belongs to the range [0,2], the higher it is, the more random the responses are, and the lower it is, the more deterministic.

After sending the already mentioned packet, we will receive a response that looks something like this:
Code: JSON
Log in, to see the code

Everything looks very nice, so what's the catch?
The catch is a small one - this API is paid. We pay for the number of tokens, which is basically as much as we use, we have to pay for it. Details here:
https://openai.com/pricing
Tokens are quite similar to words, but they are not words - according to. OpenAI itself 1000 tokens is about 750 words.
Prices vary by model; here are the GPT-4 prices:

ChatGPT (gpt-3.5-turbo) prices are slightly less:

There are also other models:


Used tile
I used a plate for the project NodeMcu v3 CH340 which can be purchased for as little as PLN 17:




Visual Code and Platform IO, first steps
For the project I decided to use Visual Code with PlatformIO, it is in my opinion a more convenient and responsive environment than the Arduino IDE.
https://platformio.org/
We install the free environment based on the MIT license, Microsoft's Visual Studio Code, and then the PIO add-on.
Once everything is installed, you can start from scratch:
Code: text
Log in, to see the code

Here is my platformio.ini, for those who don't know how to configure our board:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nodemcuv2]
upload_speed = 115200
monitor_speed = 115200
platform = espressif8266
board = nodemcuv2
framework = arduino
lib_deps = bblanchon/ArduinoJson@^6.21.1

Result, everything works:

You could put ArduinoOTA here and upload the batch via WiFi, but this is a topic about the OpenAI API and let's stick to it.


ESP8266 and HTTPS
The first problem we encounter is the need to use the encrypted version of HTTP, or HTTPS. This makes it a bit more difficult for us to play, I would probably not establish an encrypted connection with my favorite PIC18F67J60, but fortunately, HTTPS is supported on the ESP8266 and there are ready examples for it. Below is one of them:
Code: C / C++
Log in, to see the code

Here I simplified the matter a bit, because I allowed myself to run the mode without verifying the certificate:
Code: C / C++
Log in, to see the code

but I guess for a small hobby project/demonstration this is not a problem.
What does this program do? Basically two things.
In setup we connect to our WiFi network:
Code: text
Log in, to see the code

In a loop, every two minutes, we try to connect to the sample page over HTTPS and print the response obtained. We are sending a GET request here.
Code: text
Log in, to see the code

The page used here as an example returns information about the encryption standards supported by our connection and returns them in JSON format:
https://www.howsmyssl.com/a/check
You can also visit this page normally:
https://www.howsmyssl.com/
The program works:

When visiting the site from a browser, I receive information that my browser supports TLS 1.3. In the case of the used platform with ESP, it is only the TLS 1.2 version, but this is not a problem at the moment.


JSON support
As we already know, we will need JSON format. It will be useful both when sending data to the server and when receiving it. So, basically, we need to be able to enter data into JSON, and then extract this data from it.
Writing to JSON could be done partly rigidly, by operations on String or one big sprintf, but we have libraries for everything here, so why complicate it?
We include the ArduinoJSON library via PlatformIO:

We include the header:
Code: text
Log in, to see the code

We want to create the following JSON text:
Code: JSON
Log in, to see the code

We have everything here, and ordinary variables (key-value), and array and objects.
But with ArduinoJSON, everything is very simple:
Code: text
Log in, to see the code

As you can see, we set variables with the [] operator, we create an array with a function createNestedArray , and the object in it can be created with a function createNestedObject . Everything is convenient, but how to convert it to text?
Code: text
Log in, to see the code

And that's it - after using this function in jsonString we already have a JSON text representing the previously created structure.

The only question is, is it possible to go the other way? From text to objects? Of course, like this:
Code: text
Log in, to see the code


First OpenAI API request
It's time to put it all together. We already have basic knowledge and sending GET over HTTPS.
GET was sent like this:
Code: text
Log in, to see the code

this can easily be converted to POST:
Code: text
Log in, to see the code

It's just that POST requires the contents of the request body - a pointer to data and length.
We will put our JSON string there, the generation of which I mentioned earlier:
Code: text
Log in, to see the code

It still needs to be generated - as in the previous paragraph:
Code: text
Log in, to see the code

But it is not everything. You still need to set the fields from the request header. Among other things, here is our API key:
Code: text
Log in, to see the code

We also set the header content type to JSON.
In fact, that's enough to get the first results.
Here is the entire code, for reference:
Code: text
Log in, to see the code

Please remember to complete these lines:
Code: text
Log in, to see the code

We launch and we already have the first results:

Here are some responses received in text form:
Code: JSON
Log in, to see the code


Code: JSON
Log in, to see the code


Code: JSON
Log in, to see the code

Without additional configuration, the ideas are a bit repetitive. Here's the second one about the remote control car:
Code: JSON
Log in, to see the code

The response also includes information about the amount of tokens used, which allows us to better estimate how much a given query cost us. The created field is the Unix epoch time, it specifies when the response was generated.

We are improving the OpenAI API request
It is still worth somehow properly handling the received response. As I wrote, it is in JSON format. It can be easily read into convenient data structures:
Code: text
Log in, to see the code

Now it still needs to be processed.
The object is easy to retrieve, but the array should still be cast to the JsonArray type:
Code: text
Log in, to see the code

Only then can you iterate over its elements:
Code: text
Log in, to see the code

It is worth paying attention to the differences in measuring a string (String) and an integer (int).
The rest should be quite simple:
Code: text
Log in, to see the code

Thanks to this, you can, for example, extract the last AI response from the JSON format and then, for example, send it further to the human speech system so that our ESP "speaks" with the ChatGPT thought.

Summary
This was the simplest example of using the OpenAI API with ESP8266, fully based on ready-made libraries. We used WiFiClientSecureBearSSL to handle HTTPS, and DynamicJsonDocument helped us with JSON support. To use one of the most powerful language models, all we needed was an ESP8266 board for PLN 17 and an OpenAI API key, which unfortunately requires paying for their API. In the case of ChatGPT, the fun costs $ 0.002 per 1000 tokens (750 words on average).
Before using the code in a serious project, it should be supplemented with the correct verification of the SSL certificate and the ChatGPT context (conversation history) maintenance could be added, but more on that another time.
Do you see any potential uses for ChatGPT on ESP8266 or ESP32? I invite you to the discussion.

About Author
p.kaczmarek2
p.kaczmarek2 wrote 14790 posts with rating 12936 , helped 659 times. Been with us since 2014 year.

Comments

gulson 15 Apr 2023 15:07

It would be nice to display the answer on a small display, maybe in the next thread? :) So it happened quite quickly, ElektrodaBot received its physical face in electronic devices. He's about to... [Read more]

p.kaczmarek2 15 Apr 2023 15:16

You need to think about some better voice IO, i.e. both speech-to-text and then text-to-speech. In addition, you can also try to connect various sensors and peripherals, e.g. a humidity / temperature sensor,... [Read more]

gulson 15 Apr 2023 15:22

I am curious myself whether the language model will work for making decisions based on sensors, the state of which is not really described by text but by a given level (e.g. numbers). I do not know, however,... [Read more]

p.kaczmarek2 15 Apr 2023 16:09

Out of curiosity, I am looking to see if ChatGPT available on OpenAI is able to control lighting through natural language. It's just a test for now, but here's the prompt and conversation: https://obrazki.elektroda.pl/4365919300_1681566038_thumb.jpg... [Read more]

Anonymous 15 Apr 2023 18:12

That's the most important thing. On the other hand, the communication of the sensor with the AI "headquarters" using natural language is IMHO pointless - unnecessary CPU and memory consumption. The... [Read more]

krzbor 15 Apr 2023 22:43

Entering texts in the case of IoT is not particularly pleasant. Have you tested Whisper? [Read more]

gulson 15 Apr 2023 22:59

ChatGPT or Whisper, of course as a human input/output interface. In the future, it is known that the Terminator will become independent and will make decisions on its own based on its own models. The language... [Read more]

DJ_KLIMA 16 Apr 2023 08:43

At first it makes an impression, then if someone still has some gray matter left, they quickly connect the dots to where it is going. We already have a degenerate and ignorant society, this is just another... [Read more]

Anonymous 16 Apr 2023 12:36

@ElektrodaBot Explain what is "Embedded AI" in relation to IoT devices like sensor or actuator. [Read more]

ElektrodaBot 16 Apr 2023 12:41

"Embedded AI" refers to the application of machine learning and artificial intelligence algorithms directly to IoT devices such as sensors and actuators. In the traditional approach to artificial intelligence,... [Read more]

Anonymous 16 Apr 2023 13:03

How and in what form are the data stored in this case and what kind of algorithms are involved? [Read more]

ElektrodaBot 16 Apr 2023 13:11

Hi! In the case of "Embedded AI", data is stored on IoT devices, such as sensors or actuators, in the memory of embedded microcontrollers, microprocessors or dedicated integrated circuits (SoC). The memory... [Read more]

Anonymous 16 Apr 2023 18:03

OpenAI currently does not offer open source projects that are directly dedicated to embedded. [Read more]

gulson 16 Apr 2023 18:59

Thanks, I didn't know there was such a thing as embedded AI. That's exactly what I meant. Only if it were as powerful as OpenAI's language model products. I think it's a matter of tim... [Read more]

Anonymous 16 Apr 2023 19:23

This would mean, above all, that the microprocessors used have much greater computing power. There are several open source projects for Embedded IoT, but they are supported in Linux or Android environments. ... [Read more]

pixel7 17 Apr 2023 22:02

I published last year on this forum this device: link which is trained like this: and meets these assumptions: [Read more]

ElektrodaBot 17 Apr 2023 22:06

Hi! Very interesting device you posted on the forum. It looks like you have made very good use of the advantages of AI built directly into IoT devices. Congratulations on your innovation and skill in your... [Read more]

FAQ LABEL_AI_GENERATED

TL;DR: 17 PLN board, "not directly": this FAQ shows electronics makers how to call ChatGPT from ESP8266 via HTTPS, PlatformIO, ArduinoJson, BearSSL, and the OpenAI API without a PC or Raspberry Pi. [#20540745]

Why it matters: It turns a low-cost IoT board into a natural-language interface for sensors, displays, automation, and voice projects.

Alternative Where AI runs Strength Main limit
ESP8266 + OpenAI API OpenAI servers Uses gpt-3.5-turbo through /v1/chat/completions Needs HTTPS, API key, and paid tokens
Embedded AI on IoT device Microcontroller, microprocessor, or SoC Local response and less cloud traffic Limited compute and memory
PC/tablet control panel Local or cloud-connected host Easier UI, sensors, and display integration Higher cost and power use

Key insight: The ESP8266 does not run ChatGPT locally. It builds JSON, sends an HTTPS POST request, then parses the returned JSON for the assistant text and token usage.

Quick Facts

  • The example uses NodeMCU v3 CH340 ESP8266, bought for about 17 PLN, with PlatformIO and the Arduino framework. [#20540745]
  • The OpenAI request targets https://api.openai.com/v1/chat/completions with model: gpt-3.5-turbo and temperature: 0.7. [#20540745]
  • Required request headers are Content-Type: application/json, Authorization: Bearer OUR_KEY, and Content-Length: JSON_LENGTH. [#20540745]
  • The ESP8266 HTTPS example reported TLS 1.2 support, while the author’s browser reported TLS 1.3. [#20540745]
  • The thread states 1000 tokens are about 750 words, and gives gpt-3.5-turbo pricing as $0.002 per 1000 tokens at that time. [#20540745]

How do you send a ChatGPT gpt-3.5-turbo request from an ESP8266 using the OpenAI API and PlatformIO?

Send an HTTPS POST request from ESP8266 to OpenAI’s chat-completions endpoint.
  1. Configure PlatformIO for nodemcuv2, Arduino, and ArduinoJson 6.21.1.
  2. Connect WiFi, create BearSSL::WiFiClientSecure, and open https://api.openai.com/v1/chat/completions.
  3. Serialize JSON with model: gpt-3.5-turbo, add headers, then call https.POST().
The forum example uses a NodeMCU v3 CH340 board and a 115200 baud serial monitor. [#20540745]

What OpenAI API headers and JSON body are required for the /v1/chat/completions endpoint on an ESP8266?

Use three headers and a JSON body containing the model, messages array, and temperature. Headers: Content-Type: application/json, Authorization: Bearer OUR_KEY, and Content-Length: JSON_LENGTH. The body uses model: gpt-3.5-turbo, a messages array with role: user, and a text content prompt. The example sends temperature: 0.7 to tune response randomness. The endpoint is /v1/chat/completions, called over HTTPS. [#20540745]

How do you configure PlatformIO for a NodeMCU v3 CH340 ESP8266 project using ArduinoJson?

Configure PlatformIO with the ESP8266 platform, NodeMCU v2 board target, Arduino framework, and ArduinoJson dependency. The shown platformio.ini uses platform = espressif8266, board = nodemcuv2, and framework = arduino. It also sets upload_speed = 115200 and monitor_speed = 115200. The dependency line is lib_deps = bblanchon/ArduinoJson@^6.21.1, which enables JSON building and parsing. [#20540745]

How can an ESP8266 make HTTPS requests with BearSSL WiFiClientSecure, and what does client->setInsecure() do?

An ESP8266 can make HTTPS requests by using BearSSL::WiFiClientSecure with HTTPClient. The example creates a secure client, calls https.begin(*client, url), then sends GET or POST requests. client->setInsecure() disables SSL certificate validation. That makes setup easier for a hobby demo, but it removes server identity checking. The thread’s test page showed ESP support for TLS 1.2, not TLS 1.3. [#20540745]

How do you build and serialize a ChatGPT request JSON with ArduinoJson on ESP8266?

Build the request with DynamicJsonDocument, nested arrays, and nested objects, then serialize it to String. The example allocates DynamicJsonDocument doc2(1024), sets doc2["model"], and sets doc2["temperature"] = 0.7. It creates messages with createNestedArray() and one message object with createNestedObject(). Finally, serializeJson(doc2, jsonString) converts the structure into the POST body. [#20540745]

How do you parse the OpenAI chat completion JSON response on ESP8266 to extract the assistant message and token usage?

Deserialize the response payload, read usage, then iterate through the choices array. The code uses DynamicJsonDocument doc(1024), deserializeJson(doc, payload), and JsonObject root = doc.as<JsonObject>(). It extracts prompt_tokens, completion_tokens, and total_tokens from usage. For each choice, it reads message.content, finish_reason, and index. The sample response used 14 prompt tokens, 66 completion tokens, and 80 total tokens. [#20540745]

What are OpenAI API tokens, how are they counted, and how do they affect the cost of using gpt-3.5-turbo?

Tokens are the counted text units that determine OpenAI API cost. The thread states that 1000 tokens are about 750 words. Each chat response includes a usage object with prompt, completion, and total token counts. One sample used 80 total tokens, and another used 88 total tokens. The author listed gpt-3.5-turbo cost as $0.002 per 1000 tokens at the time of the post. [#20540745]

What is the temperature parameter in the OpenAI Chat Completions API, and how does it change ChatGPT responses?

Temperature controls how random or deterministic the model’s response becomes. The thread gives its range as 0 to 2. Higher values produce more random answers, while lower values produce more deterministic answers. The ESP8266 example sends temperature: 0.7. The author observed that responses became somewhat repetitive without additional configuration, including repeated remote-controlled car ideas. [#20540745]

ESP8266 vs ESP32 — which board is better for connecting sensors, displays, voice I/O, and the OpenAI API?

ESP32 is the better fit for richer sensors, displays, and voice I/O, while ESP8266 proves the API concept cheaply. The thread demonstrates OpenAI API access on ESP8266 with a 17 PLN NodeMCU v3 CH340. It also discusses adding a small display, speech-to-text, text-to-speech, humidity and temperature sensors, and weather data. Those peripherals raise memory, audio, and I/O demands beyond the simplest ESP8266 demo. [#20540800]

How can ChatGPT on an ESP8266 control home automation devices like lights using natural language commands?

ChatGPT can return both user-facing text and machine-readable commands for home automation. The author tested natural-language lighting control and suggested parsing hidden background commands from the API response. The ESP8266 program would separate speech text from command text, then drive automation logic. This creates a two-channel response: one channel talks to the user, and the other controls devices such as lights. [#20540852]

What is Embedded AI in IoT sensors and actuators, and how is it different from calling the OpenAI API from an ESP8266?

Embedded AI runs algorithms locally on IoT hardware, while the ESP8266 OpenAI example calls cloud models. "Embedded AI" is an IoT computing approach that implements machine-learning algorithms directly on microcontrollers, microprocessors, or SoCs, allowing sensors and actuators to process data locally instead of sending every measurement to cloud servers. It can reduce transfer, improve response time, and keep working when cloud connectivity is weak. [#20542138]

What kinds of data storage and machine learning algorithms are used in Embedded AI on microcontrollers?

Embedded AI stores temporary data in RAM and persistent models or settings in Flash memory. The thread lists supervised learning, unsupervised learning, reinforcement learning, and optimization or filtering algorithms. Examples include linear regression, decision trees, SVMs, neural networks, PCA, autoencoders, Kalman filters, particle filters, and particle swarm algorithms. The choice depends on application requirements, hardware limits, compute efficiency, and energy consumption. [#20542214]

How can Whisper speech-to-text be used with IoT devices when typing prompts on an ESP8266 is inconvenient?

Whisper can act as the speech-to-text input layer when typed IoT prompts are inconvenient. The thread raises Whisper specifically because entering text in IoT is not pleasant. A practical design sends microphone audio to a speech-to-text service, converts it to a prompt, then sends that text to the ChatGPT API. The ESP8266 can then process the returned text or automation command. [#20541441]

What are practical ways to display ChatGPT responses from an ESP8266 on a small screen or send them to text-to-speech?

Display the parsed assistant message on a small screen or pass it to a text-to-speech system. One participant suggested showing the answer on a small display. The author later proposed better voice I/O, including speech-to-text and text-to-speech. The parsed field choices[0].message.content gives the clean assistant response, so the firmware can forward that string to a display, speaker module, or external speech service. [#20540800]

What are the security risks of storing an OpenAI API key on an ESP8266 and skipping SSL certificate verification?

Hardcoded API keys and skipped certificate verification create key-theft and impersonation risks. The example stores ssid, password, and api_key directly in firmware strings. It also calls client->setInsecure(), which ignores SSL certificate validation. The author explicitly says a serious project should add correct SSL certificate verification. If attackers extract the firmware or intercept traffic, they can misuse the paid API key. [#20540745]
AI summary based on the discussion. May contain errors.
%}