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:"
#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:"
#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!!