Author Topic: ChatGPT for code generation: a marvel to watch!  (Read 1546 times)

0 Members and 1 Guest are viewing this topic.

Offline etiTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1801
  • Country: gb
  • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
ChatGPT for code generation: a marvel to watch!
« on: March 23, 2023, 01:03:22 am »
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: [Select]
#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 4

void 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: [Select]
#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 4

bool tvBgoneEnabled = false; //initially disabled

void 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!!
 

Offline etiTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1801
  • Country: gb
  • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
Re: ChatGPT for code generation: a marvel to watch!
« Reply #1 on: March 23, 2023, 01:09:37 am »
I've asked it to add the database of global IR codes for TV "power off" commands, as it lacked them...
 

Offline Infraviolet

  • Super Contributor
  • ***
  • Posts: 1014
  • Country: gb
Re: ChatGPT for code generation: a marvel to watch!
« Reply #2 on: March 23, 2023, 08:26:28 pm »
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.
 

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 6349
  • Country: ca
  • Non-expert
Re: ChatGPT for code generation: a marvel to watch!
« Reply #3 on: March 23, 2023, 09:31:11 pm »
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.

IRSend seems to set the pin as an output: https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRsend.cpp

Even if it didn't, usually arduino functions will do it for you.
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 

Offline etiTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1801
  • Country: gb
  • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
Re: ChatGPT for code generation: a marvel to watch!
« Reply #4 on: March 25, 2023, 09:45:34 am »
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
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: gb
Re: ChatGPT for code generation: a marvel to watch!
« Reply #5 on: March 25, 2023, 10:52:01 am »
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

Yeah.  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.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Online tom66

  • Super Contributor
  • ***
  • Posts: 6694
  • Country: gb
  • Electronics Hobbyist & FPGA/Embedded Systems EE
Re: ChatGPT for code generation: a marvel to watch!
« Reply #6 on: March 25, 2023, 12:54:59 pm »
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.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14445
  • Country: fr
Re: ChatGPT for code generation: a marvel to watch!
« Reply #7 on: March 26, 2023, 12:12:24 am »
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

That is sad.
 

Offline Smotritsky78

  • Newbie
  • Posts: 1
  • Country: by
    • my-diy
Re: ChatGPT for code generation: a marvel to watch!
« Reply #8 on: March 28, 2023, 07:20:16 pm »
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

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

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: gb
Re: ChatGPT for code generation: a marvel to watch!
« Reply #9 on: March 30, 2023, 09:28:57 am »
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.
« Last Edit: March 30, 2023, 09:30:47 am by paulca »
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Online tszaboo

  • Super Contributor
  • ***
  • Posts: 7369
  • Country: nl
  • Current job: ATEX product design
Re: ChatGPT for code generation: a marvel to watch!
« Reply #10 on: March 30, 2023, 09:47:34 am »
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
I 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.
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: gb
Re: ChatGPT for code generation: a marvel to watch!
« Reply #11 on: March 30, 2023, 09:53:11 am »
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.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Offline mfro

  • Regular Contributor
  • *
  • Posts: 210
  • Country: de
Re: ChatGPT for code generation: a marvel to watch!
« Reply #12 on: March 30, 2023, 09:59:04 am »
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  8).

Beethoven wrote his first symphony in C.
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: gb
Re: ChatGPT for code generation: a marvel to watch!
« Reply #13 on: March 30, 2023, 10:56:02 am »
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  8).

True, 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.
« Last Edit: March 30, 2023, 10:58:07 am by paulca »
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf