Skip to content

switching locations #10

Description

@Tennisee-data

unsigned long lastSwitchTime = 0;
const unsigned long switchInterval = 120000; // 2 minutes in milliseconds

// Define your cities
String towns[] = { "Zagreb", "Paris" };
int currentTownIndex = 0;

String myAPI = "d0d0bf1bb46822e5dce67c95f4fd0800";
String units = "metric"; // metric or imperial

// Helper function to build the server URL based on the current town
String getServerURL() {
return "https://api.openweathermap.org/data/2.5/weather?q=" +
towns[currentTownIndex] +
"&appid=" + myAPI +
"&units=" + units;
}

void getData() {
String server = getServerURL();
HTTPClient http;
http.begin(server);
int httpResponseCode = http.GET();

if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Server response:");
Serial.println(payload);
// Process the JSON payload, update display, etc.
} else {
Serial.print("HTTP ERROR ");
Serial.println(httpResponseCode);
}
http.end();
}

void updateData() {
// Check if it's time to switch location (every 2 minutes)
if (millis() - lastSwitchTime >= switchInterval) {
lastSwitchTime = millis();
currentTownIndex = (currentTownIndex + 1) % (sizeof(towns) / sizeof(towns[0]));
Serial.print("Switching to: ");
Serial.println(towns[currentTownIndex]);
getData(); // Get data for the new location right away
}

// ... your other update tasks
}

void loop() {
updateData();
draw();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions