#include<WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
unsigned const long interval = 1000; //por simulacion 1 seg
unsigned long lastTime = 0;
const char* latitud;
const char* longitud;
const char* serverName1 = "http://api.open-notify.org/iss-now.json";
const char* serverName2 = "http://api.thingspeak.com/update";
const char* serverName3 = "http://168.227.96.230:1880/iss";
const char* client = "mwa0000032116199";
// Service API Key
String apiKey = "S4ZQESV7ZXLYD716";
String httpRequestData;
int httpResponseCode;
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected!");
Serial.println(WiFi.localIP());
}
void loop(){
if(millis()-lastTime > interval){
JSONVar iss;
HTTPClient http;
http.begin(serverName1);
int httpResponCode = http.GET();
Serial.println(httpResponCode);
if(httpResponCode > 0){
String payload = http.getString();
Serial.println(payload);
iss = JSON.parse(payload);
longitud = iss["iss_position"]["longitude"];
latitud = iss["iss_position"]["latitude"];
}
else{
Serial.print("error ");
Serial.println(httpResponCode);
}
http.begin(serverName2);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
Serial.println(longitud);
httpRequestData = "api_key=" + apiKey + "&field1=" + longitud + "&field2=" + latitud;
// Send HTTP POST request
httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
http.begin(serverName3);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
Serial.println(longitud);
httpRequestData = "api_key=" + apiKey + "&field1=" + longitud + "&field2=" + latitud;
// Send HTTP POST request
httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
/*
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
// JSON data to send with HTTP POST
String httpRequestData = "{\"api_key\":\"" + apiKey + "\",\"longitude\":\"" + longitud + "\"}";
Serial.print(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
*/
/*
// If you need an HTTP request with a content type: text/plain, use the following:
http.addHeader("Content-Type", "text/plain");
// Text data to send with HTTP POST
String httpRequestData = "String";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
*/
lastTime = millis();
}
}