Hey. I'm trying to upload this ChatGPT code (hope it's correct) to my ESP8266 NodeMCU v3 CH340 using Arduino IDE 2.1.0.
It's stuck on
"Leaving...
Hard resetting via RTS pin..."
Maybe that's because display pins are not soldered but connected via a breadboard to hold them in place.
Here's the code:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Encoder.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
// Wi-Fi credentials
const char* ssid = "MyWiFi";
const char* password = "FUN0PASS4U";
// WebSDR URL
const char* websdrUrl = "http://websdr.ewi.utwente.nl:8901/";
// Display pin connections
#define TFT_CS D8
#define TFT_RST D3
#define TFT_DC D4
#define TFT_SDA D7
#define TFT_SCL D5
#define TFT_BL -1 // No pin assigned for backlight control
// Speaker pin connections
#define SPEAKER_POSITIVE D2
#define SPEAKER_GROUND GND
// Encoder pin connections
#define ENCODER_CLK D1
#define ENCODER_DT D0
#define ENCODER_SW D8
// TFT display object
Adafruit_ST7735 display = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST, TFT_SDA, TFT_SCL);
// Encoder object
Encoder encoder(ENCODER_CLK, ENCODER_DT);
// Variables
int mode = 0;
int frequency = 4625;
void setup() {
// Initialize Serial communication
Serial.begin(115200);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wi-Fi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize the TFT display
display.initR(INITR_BLACKTAB);
display.setRotation(3);
display.fillScreen(ST7735_BLACK);
display.setTextSize(2);
display.setTextColor(ST7735_WHITE);
display.setCursor(0, 0);
display.println("Mode: WebSDR");
display.println("Frequency:");
display.println(frequency);
display.setRotation(0); // Reset rotation
display.begin();
}
void loop() {
// Check if the encoder has been turned
int16_t delta = encoder.read() * 10; // Increase sensitivity to 10 Hz per step
if (delta != 0) {
frequency += delta;
// Limit the frequency range
if (frequency < 0) {
frequency = 0;
}
if (frequency > 30000) {
frequency = 30000;
}
// Update the TFT display
display.fillScreen(ST7735_BLACK);
display.setCursor(0, 0);
display.println("Mode: WebSDR");
display.println("Frequency:");
display.println(frequency);
display.setRotation(0); // Reset rotation
display.begin();
// Delay to debounce the encoder
delay(100);
}
// Check if the encoder switch is pressed to toggle mode
if (digitalRead(ENCODER_SW) == LOW) {
toggleMode();
// Delay to debounce the switch
delay(200);
}
// WebSDR mode
if (mode == 0) {
// Connect to the WebSDR server
WiFiClient client;
if (client.connect("websdr.ewi.utwente.nl", 8901)) {
// Send HTTP GET request to the WebSDR server
client.print("GET /?tune=0");
client.print(frequency);
client.print("z HTTP/1.1\r\n");
client.print("Host: websdr.ewi.utwente.nl\r\n");
client.print("Connection: close\r\n");
client.print("\r\n");
// Read and discard the HTTP response headers
while (client.connected() && !client.available());
while (client.available()) {
client.read();
}
// Read and print the WebSDR audio data
while (client.connected() && client.available()) {
// Read audio data and play it through the speaker
byte audioData = client.read();
analogWrite(SPEAKER_POSITIVE, audioData);
}
} else {
// Connection failed
Serial.println("Connection to WebSDR server failed");
}
}
}
void toggleMode() {
mode = (mode + 1) % 2;
if (mode == 0) {
display.fillScreen(ST7735_BLACK);
display.setCursor(0, 0);
display.println("Mode: WebSDR");
display.println("Frequency:");
display.println(frequency);
display.setRotation(0); // Reset rotation
display.begin();
} else {
display.fillScreen(ST7735_BLACK);
display.setCursor(0, 0);
display.println("Mode: Encoder");
display.println("Adjust frequency");
display.setRotation(0); // Reset rotation
display.begin();
}
}
The sketch is created to listen to the WebSDR, change frequencies, and modes using the encoder, and show everything on the TFT display. And I don't know if ChatGPT used in the code how the pins are connected:
Speaker > NodeMCU
- Positive > D2
- Negative > G
Display > NodeMCU
- GND > G
- VCC > 3V
- SCL > SC
- SDA > D2
- RES > D3
- DC > D4
- CS > SK
- BLK > D8
Encoder > NodeMCU
- CLK > D6
- DT > D7
- SW > D0
- VCC > 3V
- GND > G