Hello! I am trying to make myself a geolocation finder using an arduino IDE, ESP 32, RGB LED. The problem is, when i'm trying to give the arduino any string, including Serial.begin() and other methods, it spits out some weird characters in the serial monitor. It didnt do this until today.
This is my test code:
#include <WiFi.h>
#include <HTTPClient.h> // For HTTP requests
// WiFi credentials
const char* ssid = "Your_SSID"; // Replace with your WiFi SSID
const char* password = "Your_PASSWORD"; // Replace with your WiFi password
// API endpoint to get geolocation
const char* apiURL = "http://ip-api.com/json/";
// RGB LED pins
const int redPin = 25;
const int greenPin = 26;
const int bluePin = 27;
// WiFi connection status
bool isConnected = false;
void setup() {
// Start Serial Monitor
Serial.begin(1200);
// Initialize the LED pins for PWM output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Connect to WiFi
connectToWiFi();
}
void loop() {
// Fetch geolocation data
String locationData = getGeoLocation();
if (locationData != "") {
Serial.println("Geolocation Data:");
Serial.println(locationData);
// Indicate success with green LED
setColor(0, 255, 0); // Green
} else {
// Indicate failure with red LED
setColor(255, 0, 0); // Red
}
delay(115200); // Wait for 10 seconds before the next request
}
// Function to connect to WiFi
void connectToWiFi() {
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
setColor(0, 0, 255); // Blue (indicating connecting)
}
Serial.println("\nWiFi connected.");
setColor(0, 255, 0); // Green (indicating connected)
isConnected = true;
}
// Function to get geolocation data from API
String getGeoLocation() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiURL); // Specify the API endpoint
int httpCode = http.GET(); // Make the GET request
if (httpCode > 0) {
String payload = http.getString();
http.end();
// Return the raw JSON data (unparsed)
return payload;
} else {
Serial.println("Error in HTTP request");
http.end();
return "";
}
} else {
Serial.println("WiFi disconnected");
return "";
}
}
// Function to set RGB LED color
void setColor(int red, int green, int blue) {
analogWrite(redPin, red); // Red channel
analogWrite(greenPin, green); // Green channel
analogWrite(bluePin, blue); // Blue channel
}