ChatGPT for code generation: a marvel to watch! - Page 1
Forum section: electronics / general

author: eti
author_rank: Super Contributor
timestamp: March 23, 2023, 01:03:22 am
content: Have been using "ChatGPT" (and also "DALL-E" - the AI artist) for nigh on a week now... and WOW!!I just made this request of "ChatGPT", having put it through its paces for various languages, and this is what it spat out:ME:"I need arduino code for an esp32 which will provide "TV-b-gone" functionality. The functionality will be triggered by a POST request made to a simple http server on the esp32"ChatGPT:"Here's an Arduino code for an ESP32 which will provide "TV-B-Gone" functionality triggered by a POST request made to a simple HTTP server on the ESP32:"Code:#include <WiFi.h>#include <WebServer.h>#include <IRremoteESP8266.h>#include <IRsend.h>const char* ssid = "your_SSID";const char* password = "your_PASSWORD";WebServer server(80);IRsend irsend(4); //an IR LED is connected to GPIO 4void handleRoot() {server.send(200, "text/plain", "TV-B-Gone activated");irsend.sendNEC(0xFFFFFFFF, 32); //send the "power off" code to the TV}void setup() {Serial.begin(115200);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(1000);Serial.println("Connecting to WiFi...");}Serial.println("WiFi connected");Serial.println("IP address: ");Serial.println(WiFi.localIP());server.on("/", handleRoot);server.begin();Serial.println("HTTP server started");}void loop() {server.handleClient();}ChatGPT Explains:"To use this code, make sure you have the following libraries installed in your Arduino IDE: WiFi, WebServer, and IRremoteESP8266. Then, modify the ssid and password variables to match your WiFi network credentials. Connect an IR LED to GPIO 4 on the ESP32.Upload the code to the ESP32, open the Serial Monitor, and wait for the ESP32 to connect to the WiFi network. Once connected, it will print out its IP address. Use a web browser or a tool like curl to make a POST request to the ESP32's IP address. The TV should turn off if it receives the correct IR code."Okay, so it only provided ONE TV brand code for power off, but this is AMAZING.Following on, I said:Me:"the web server must have a basic html page with a button titled "Enable/Disable" which must trigger the post request, and toggle the function on and off"ChatGPT:"Here's an updated version of the Arduino code for an ESP32 which includes a basic HTML page with a button titled "Enable/Disable". Clicking the button will trigger a POST request, which will toggle the TV-B-Gone functionality on and off:"Code:#include <WiFi.h>#include <WebServer.h>#include <IRremoteESP8266.h>#include <IRsend.h>const char* ssid = "your_SSID";const char* password = "your_PASSWORD";WebServer server(80);IRsend irsend(4); //an IR LED is connected to GPIO 4bool tvBgoneEnabled = false; //initially disabledvoid handleRoot() {String html = "<html><body><h1>TV-B-Gone</h1>";html += "<form method='post' action='/toggle'>";html += "<input type='submit' value='";if (tvBgoneEnabled) {html += "Disable";} else {html += "Enable";}html += "'></form></body></html>";server.send(200, "text/html", html);}void handleToggle() {tvBgoneEnabled = !tvBgoneEnabled;String message = tvBgoneEnabled ? "TV-B-Gone enabled" : "TV-B-Gone disabled";server.send(200, "text/plain", message);}void loop() {server.handleClient();if (tvBgoneEnabled) {irsend.sendNEC(0xFFFFFFFF, 32); //send the "power off" code to the TV}}void setup() {Serial.begin(115200);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(1000);Serial.println("Connecting to WiFi...");}Serial.println("WiFi connected");Serial.println("IP address: ");Serial.println(WiFi.localIP());server.on("/", handleRoot);server.on("/toggle", HTTP_POST, handleToggle);server.begin();Serial.println("HTTP server started");}ChatGPT explains:"In this updated code, the handleRoot() function returns a basic HTML page with a form that has a single button. The button's label is "Enable" if tvBgoneEnabled is false (meaning the TV-B-Gone functionality is currently disabled), and "Disable" if tvBgoneEnabled is true (meaning the TV-B-Gone functionality is currently enabled).When the user clicks the button, the form is submitted as a POST request to the /toggle endpoint, which is handled by the handleToggle() function. This function toggles the value of tvBgoneEnabled, and sends a response back to the client indicating whether TV-B-Gone is now enabled or disabled.The loop() function includes a check to see if tvBgoneEnabled is true. If it is, the code sends the "power off" code to the TV."Now THAT is clever! It has yet to be asked to update its TV database to cover most brands, as it seems to only have provided a single NEC code, but wow!!

author: eti
author_rank: Super Contributor
timestamp: March 23, 2023, 01:09:37 am
content: I've asked it to add the database of global IR codes for TV "power off" commands, as it lacked them...

author: Infraviolet
author_rank: Super Contributor
timestamp: March 23, 2023, 08:26:28 pm
content: Does it actually work? Because I don't see a line anywhere to initialise the pin in question as an output pin, you'd only get very faint short ranged light if you tried to power an IR LED using a pin which is probably conigured default as an input with pullups or pull downs.

author: thm_w
author_rank: Super Contributor
timestamp: March 23, 2023, 09:31:11 pm
content: Quote from: Infraviolet on March 23, 2023, 08:26:28 pmIRSend seems to set the pin as an output:https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRsend.cppEven if it didn't, usually arduino functions will do it for you.

author: eti
author_rank: Super Contributor
timestamp: March 25, 2023, 09:45:34 am
content: It didn’t work.I keep getting going in circles for an hour, and it kept issuing an incomplete code… And every time I asked it to regenerate it generated a different program to do the same thing. I gave up

author: paulca
author_rank: Super Contributor
timestamp: March 25, 2023, 10:52:01 am
content: Quote from: eti on March 25, 2023, 09:45:34 amYeah. The way you can go back and correct it only works so far. I believe it is not just reading your latest message, but re-reading the entire conversation each time it responds. So it gets confused.The main issue is, the code is produces is almost always wrong in some way.It's still useful. If you were going to google and search manually though the responses on Stackoverflow knowing you will get half a dozen partial answers... you might as well ask CGPT.

author: tom66
author_rank: Super Contributor
timestamp: March 25, 2023, 12:54:59 pm
content: My most recent use cases:-How do I use grep to find a binary pattern in many files?It gave me an example in just 10 seconds, faster than Googling and reading stackoverflow or whatever.-Write a simple Pygame application to open a 640x480 window and fill it black.I know how to do this, but quicker to get the boilerplate from ChatGPT instead of writing it.-Write a gstreamer pipeline to decode an AVI MJPEG file and stream over the network using RTP.Again, something I've done many times before, but very quick to ask instead of looking it up.Very powerful, but not replacing programmers any time soon.

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 26, 2023, 12:12:24 am
content: Quote from: eti on March 25, 2023, 09:45:34 amThat is sad.

author: Smotritsky78
author_rank: Newbie
timestamp: March 28, 2023, 07:20:16 pm
content: Quote from: eti on March 25, 2023, 09:45:34 amWhen you realize that ChatGPT stopped before finishing the code, then just write him "Continue" and he will continue to write code, and not generate it again.P.S. I apologize for my English.

author: paulca
author_rank: Super Contributor
timestamp: March 30, 2023, 09:28:57 am
content: I tried to get it to pick me some electronics parts. Values maybe. Parts I thought should be easy for it.I asked it if it was aware of any DC-DC converters for driving mosfet gates. I was creating a circuit to drive a 10V mosfet gate from a 3.3V circuit.It gave me a speel about how to linearly lower voltage to get 10V.When I stopped it and said, "Don't I need to step up in voltage from 3.3V to 10V"So it gave me a speel about making a boost converter, but from 5V to 12V.I corrected it and it gave me the buck example again.I tried to tie it back to the beginning and asking it if any monolithic ICs existed to do the boost version.It gave me 4 examples of boost/buck ICs...Trying to steer it back to DC to DC converters and it still didn't get it.I gave up. I'm sure you could get it to work, but to be honest, it was faster to search myself on Farnell.

author: tszaboo
author_rank: Super Contributor
timestamp: March 30, 2023, 09:47:34 am
content: Quote from: eti on March 25, 2023, 09:45:34 amI was able to make it write functioning code, but it took a lot of trial and error.I had to write prompts that corrected the previous issues with the code, or instructed it not to use certain libraries.My prompt ended up 6-7 lines long, if I went above that, it ignored the last instructions.It was a windows shell script to scan a folder and call FFMPEG to convert something, so not trivial, plus I didn't want to figure out all the syntax for everything.

author: paulca
author_rank: Super Contributor
timestamp: March 30, 2023, 09:53:11 am
content: Good point and not something I have tried. Instead of correcting it "only". Correct it by restating your problem with that particular point clarified. Continue restarting the whole problem statement with each prompt.Nice. Thanks, I might try that.

author: mfro
author_rank: Regular Contributor
timestamp: March 30, 2023, 09:59:04 am
content: That's what I've seen as well. You need to be very specific on what you want and in most cases the outcome is "just about" right. And you still must be an expert in the topic to be able to judge about the result.That's even more frustrating than teaching the new hire that knows a lot but doesn't bring any common sense and experience.At least you can yell at the latter.

author: paulca
author_rank: Super Contributor
timestamp: March 30, 2023, 10:56:02 am
content: Quote from: mfro on March 30, 2023, 09:59:04 amTrue, true. However I always question common sense. If you are inside a ping pong ball it's common sense that the world is white. Someone with a golf upbringing would be awfully confused by your "common" sense."Common sense" says that crows are black.There is actually a really good book on "common sense" and how it's actually factually wrong. I'll dig out the name if I come across it again. It had pedantic crap like "How many nostrils do you have?", the answer is apparently 4, because they evolved from the in and out opennings of the gills. But it covers the crows "strawman" and many others. Written in a fairly funny and sarcastic style.