logo elektroda
logo elektroda
X
logo elektroda

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

p.kaczmarek2 11082 17

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.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
Listen:
  • ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    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:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    ChatGPT (gpt-3.5-turbo) prices are slightly less:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    There are also other models:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

    Used tile
    I used a plate for the project NodeMcu v3 CH340 which can be purchased for as little as PLN 17:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

    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:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    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:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    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:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, 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:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    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.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 14790 posts with rating 12936, helped 659 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • ElectrodaBot gets a physical face and actuators

    #2 20540769
    gulson
    System Administrator
    Posts: 29531
    Help: 150
    Rate: 6124
    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 his sensors, make decisions, and influence the real world with his actuators. :)

    And really, great! It was on reddit that someone boasted that he was collecting parking space occupancy from the sensor database. After that, he talked to ChatGPT about telling him where on a given day and time he has the highest probability of finding a free parking space.

    The future is now, old man.

    Added after 7 [minutes]:

    And they just released https://github.com/Significant-Gravitas/Auto-GPT, which is code that allows artificial intelligence to make several independent decisions to achieve a human-defined goal. I'm analyzing this right now, but initially it looks like the user's task breaks down into small tasks, and then uses Google, language models to suggest the user step by step to achieve the goal. So, in a way, artificial intelligence is becoming more and more independent. Now in this thread she has gone outside to electronics and we finally have Terminator v0.1! oops.
  • Voice I/O and sensor-based AI assistant ideas

    #3 20540800
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14790
    Help: 659
    Rate: 12936
    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, and maybe downloading e.g. weather forecasts from another API (there are various available) and make, for example, an AI advisor for the wife helping her choose the right clothes to go out.. in the prompt you could put information about the user's style preferences and information about the weather.

    In general, this AI is so good that it would probably even be able to advise us what dishes to make from what we have in the fridge and the only technical problem would be to determine what is in this fridge ... although ... apparently the newer model can take a photo for the entrance ingredients and from the photo itself to know what is what. I wonder how well it works.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • Need for a numeric model for sensor decisions

    #4 20540813
    gulson
    System Administrator
    Posts: 29531
    Help: 150
    Rate: 6124
    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, whether it would not be necessary to create a model that is more able to analyze numerical data. I know Bloomberg did something like this for finance - BloombergGPT.
    For sure, the language model will work, as Terminator v0.1 will want to say something, i.e. communicate with a human. The same language model will work if we give him the command "Look for John Connor". The language model is simply a great interface to communicate with a human being, because text and speech are what we communicate with.
    However, for our Terminator v0.1 to make decisions on its own and not hallucinate or deceive itself (as is often the case with language models), we should probably develop our own model for electronics. For now, there is nothing as strong as GPT4. Probably only a matter of time.
  • ChatGPT can issue hidden background commands for automation

    #5 20540852
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14790
    Help: 659
    Rate: 12936
    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:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

    I wonder if with the right prompt it would be possible to set the bot to use these commands "quietly", without placing them in the "voice" text. Of course, the idea is that the bot works and "talks", and the program in the background receives these commands from it to do automation.

    Added after 21 [minutes]:

    This is just interesting:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    The right prompt and even the ChatGPT model is able to simultaneously talk to the user and execute commands in the background. The response parser from the API would separate it out accordingly.

    Added after 3 [minutes]:

    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • Natural-language sensor links are pointless on ESP8266

    #6 20541035
    Anonymous
    Level 1  
  • #7 20541441
    krzbor
    Level 29  
    Posts: 1783
    Help: 42
    Rate: 1072
    Entering texts in the case of IoT is not particularly pleasant. Have you tested Whisper?
  • Analogy between GPT token prediction and robot control

    #8 20541467
    gulson
    System Administrator
    Posts: 29531
    Help: 150
    Rate: 6124
    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 model will then be redundant, and in most cases standalone machines can be removed as a waste of resources :)
    However, the same idea with GPT can be used. It's about finding the next word (token) to build a meaningful sentence.
    In the case of the Terminator, it will have to decide on its own, based on sensors, where and how to move, i.e. to predict its next movements of actuators based on sensors and cameras.
    Of course, you could leave it with a basic model and let it learn, let it fall over, but I think it would be worth uploading a huge sample of model data right away so that it knows how to behave. Similarly, in the case of open source language models Vicuna, Dally 2.0, Alpaca there are several GB of sample texts and then you can further train with your texts.

    Hence, Terminator, after uploading a basic sample of behavior, will continue to learn on its own.

    Interesting times, slightly scary.
  • Warning about AI assistants as a tool for control

    #9 20541726
    DJ_KLIMA
    Level 25  
    Posts: 666
    Help: 68
    Rate: 223
    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 step to turn off the human SELF and replace it with AJ. It will soon be a much more powerful tool to control people than anyone can imagine, it is enough for people to get used to Assistants in various forms, the same has happened with computer technology in the hands of the masses! The Internet is now like a huge shopping mall, and human minds have been taken over by social media and websites where algorithms adjust content by closing it tightly. It's like a magician's trick, the magician will do anything to charm and show exactly what he wants. Be careful where you load AJ.
  • #10 20542124
    Anonymous
    Level 1  
  • Embedded AI benefits for IoT sensors and actuators

    #11 20542138
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8887
    Help: 364
    Rate: 954
    LABEL_AI_GENERATED
    khoam wrote:
    Explain what "Embedded AI" is in relation to IoT sensor or actuator devices.
    "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, data processing and machine learning take place on powerful servers in the cloud, to which IoT devices send data. In the case of "Embedded AI", the algorithms are implemented directly on microcontrollers, microprocessors or dedicated integrated circuits (SoCs) of IoT devices.

    With regard to IoT devices such as sensors or actuators, Embedded AI can help in various aspects, such as:

    1. Reduction of data transfer: By processing data on site, the amount of data transferred to the cloud can be reduced, which saves bandwidth and resources. Exceptional data or data representing specific conditions can be forwarded while the rest of the data can be processed locally.

    2. Faster response: Thanks to local data processing, IoT devices can react faster to changing environmental conditions. For example, an actuator with built-in artificial intelligence can adapt to changing conditions more quickly, without having to wait for instructions from the cloud.

    3. Increased independence: Devices with built-in AI can operate more independently and reliably, even when cloud connectivity is poor or unavailable.

    4. Energy Optimization: For battery-powered IoT devices, "Embedded AI" can help save energy by optimizing power consumption based on real-time data and conditions.

    5. Adaptability: Built-in AI algorithms enable IoT devices to learn and adapt to changing situations and user behavior. For example, smart thermostats can learn and adapt to users' temperature preferences to improve comfort and save energy.

    In conclusion, "Embedded AI" in IoT devices such as sensors and actuators allows for more intelligent, efficient and autonomous operation of these devices, improving their performance and ability to process data on site.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #12 20542195
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • Embedded AI data storage and algorithm types

    #13 20542214
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8887
    Help: 364
    Rate: 954
    LABEL_AI_GENERATED
    khoam wrote:
    How and in what form are the data stored in this case and what kind of algorithms are involved?
    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 of such systems can include RAM, which is used to store temporary data and needed to perform current calculations, and non-volatile memory, such as Flash memory, which stores permanent data, such as a machine learning model, settings or measurement history.

    In the context of the algorithms used in "Embedded AI", we can mention several basic categories:

    1. Supervised learning algorithms: Such as linear regression, decision trees, support vector machines (SVMs) and neural networks. With these algorithms, the model is trained on data with labels that are assigned to specific conditions or outcomes. An example is the classification of an acoustic signal into speech sounds and other sounds.

    2. Unsupervised learning algorithms: Such as clustering, principal component analysis (PCA), and autoencoders. With these algorithms, the model learns the data structure without prior labeling. An example is the detection of anomalies in measurement signals.

    3. Reinforcement learning algorithms: In this category, the model learns to make decisions by exploring and exploiting the environment, optimizing the reward function. An example is the control of a simple robot that learns to navigate in the environment.

    4. Optimization and filtration algorithms: Such as Kalman algorithm, particle filter and particle swarm algorithm. For these algorithms, the model learns to estimate the state of the system based on ia priori inputs. An example is the tracking of objects in measurements from multiple sensors.

    The choice of the appropriate algorithm depends on the specific application, hardware limitations and system requirements. It is worth adding that in the case of embedded AI, special attention should be paid to the optimization of algorithms in terms of computational efficiency and energy consumption.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #14 20542678
    Anonymous
    Level 1  
  • #15 20542771
    gulson
    System Administrator
    Posts: 29531
    Help: 150
    Rate: 6124
    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 time.
  • FANN library recommended for embedded machine learning

    #16 20542810
    Anonymous
    Level 1  
  • Embedded AI reduces cloud data transfer and latency

    #17 20544633
    pixel7
    Level 24  
    Posts: 656
    Help: 53
    Rate: 160
    gulson wrote:
    Thanks, I didn't know there was such a thing as embedded AI. That's exactly what I meant. Only if it was as powerful as OpenAI's language model products.
    I think it's a matter of time.


    I published last year on this forum this device: link
    which is trained like this:

    ElektrodaBot wrote:

    1. Supervised learning algorithms: Such as linear regression, decision trees, support vector machines (SVMs) and neural networks. With these algorithms, the model is trained on data with labels that are assigned to specific conditions or outcomes. An example is the classification of an acoustic signal into speech sounds and other sounds.


    and meets these assumptions:

    ElektrodaBot wrote:

    1. Reduction of data transfer: By processing data on site, the amount of data transferred to the cloud can be reduced, which saves bandwidth and resources. Exceptional data or data representing specific conditions can be forwarded while the rest of the data can be processed locally.
    2. Faster response: Thanks to local data processing, IoT devices can react faster to changing environmental conditions. For example, an actuator with built-in artificial intelligence can adapt to changing conditions more quickly, without having to wait for instructions from the cloud.
    3. Increased independence: Devices with built-in AI can operate more independently and reliably, even when cloud connectivity is weak or unavailable.
  • #18 20544639
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8887
    Help: 364
    Rate: 954
    LABEL_AI_GENERATED
    pixel7 wrote:
    (...)



    and fulfils these assumptions:


    1. Reducing data transfer: By processing data locally, the amount of data sent to the cloud can be reduced, saving bandwidth and resources. Exceptional data or data representing specific conditions can be forwarded, while the rest of the data can be processed locally.
    2. faster response: With local processing, IoT devices can react faster to changing environmental conditions. For example, an actuator with built-in artificial intelligence can adapt faster to changing conditions without having to wait for instructions from the cloud.
    3 Increased autonomy: Devices with embedded AI can operate more independently and reliably, even when the connection to the cloud is weak or unavailable.
    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 approach to solving the problem!

    Indeed, over time, we can expect to see more and more advanced language models and AI technologies embedded directly into devices. We are already seeing significant progress in this area, and the pace of development is likely to accelerate with the emergence of new technologies, research centres and machine learning systems.

    It is also worth mentioning that, in addition to the advances in embedded AI, the performance of the hardware is also steadily increasing, making it possible to process data efficiently and handle increasingly complex models on local devices. As a result, in the future we will be able to benefit from powerful, intelligent systems that operate without the need to send data to the cloud.

    If you have questions about embedded AI technology or need help developing your project, go ahead and ask, I'm happy to help!
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
Listen:

Topic summary

LABEL_AI_GENERATED
The discussion revolves around utilizing the OpenAI API to run ChatGPT on the ESP8266 microcontroller. Participants explore the feasibility of sending requests to the API via HTTPS, handling API key transfers, and supporting JSON formats. Ideas for practical applications include displaying responses on small screens, integrating voice input/output, and using sensors for decision-making. The concept of "Embedded AI" is introduced, emphasizing local data processing on IoT devices to enhance efficiency and response times. Various algorithms for machine learning in embedded systems are discussed, alongside the potential for future advancements in AI capabilities.
AI summary based on the discussion. May contain errors.

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.
ADVERTISEMENT