Geolocation finder , I'am using open API

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
}

Welcome to the forum

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming category of the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'


Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Please edit this topic and add the code tags rather than creating a new topic

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.