Hi, i need help with my project, im trying to upload de latitude and longitude of my NodeMCU ESP8266 to a realtime database and i dont know why im not able to connect with firebase. Could someone help me please? i need to finish this project soon. Im gonna upload de code and the file also. Appreciate any help
FILe:
googleLocation.ino (4.0 KB)
CODE:
#include <Arduino.h>
#if defined ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#elif defined ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#error Wrong platform
#endif
#include <WifiLocation.h>
const char* googleApiKey = "AIzaSyDJm-EEtFWGceOAnj4L16uvfGQcLNLk7CM";
const char* ssid = "";
const char* passwd = "";
#define HOSTFIREBASE "" // Host o url of Firebase
#define LOC_PRECISION 7 // Precisión de latitud y longitud
WifiLocation location (googleApiKey);
location_t loc;
// Variables
byte mac[6];
String macStr = "";
String nombreComun = "NodeMCU";
// Cliente WiFi
WiFiClientSecure client;
// Set time via NTP, as required for x.509 validation
void setClock () {
configTime (0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print ("Waiting for NTP time sync: ");
time_t now = time (nullptr);
while (now < 8 * 3600 * 2) {
delay (500);
Serial.print (".");
now = time (nullptr);
}
struct tm timeinfo;
gmtime_r (&now, &timeinfo);
Serial.print ("\n");
Serial.print ("Current time: ");
Serial.print (asctime (&timeinfo));
}
void setup() {
Serial.begin(115200);
// Connect to WPA/WPA2 network
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// wait 5 seconds for connection:
Serial.print("Status = ");
Serial.println(WiFi.status());
delay(500);
}
Serial.println ("Connected");
setClock ();
macStr = obtenerMac();
Serial.print("MAC NodeMCU: ");
Serial.println(macStr);
loc = location.getGeoFromWiFi();
Serial.println("Location request data");
Serial.println(location.getSurroundingWiFiJson());
Serial.println ("Location: " + String (loc.lat, 7) + "," + String (loc.lon, 7));
//Serial.println("Longitude: " + String(loc.lon, 7));
Serial.println ("Accuracy: " + String (loc.accuracy));
Serial.println ("Result: " + location.wlStatusStr (location.getStatus ()));
}
void loop() {
// Hacemos la petición HTTP mediante el método PUT
peticionPut();
// Esperamos 15 segundos
delay(15000);
}
String obtenerMac()
{
// Obtenemos la MAC del dispositivo
WiFi.macAddress(mac);
// Convertimos la MAC a String
String keyMac = "";
for (int i = 0; i < 6; i++)
{
String pos = String((uint8_t)mac[i], HEX);
if (mac[i] <= 0xF)
pos = "0" + pos;
pos.toUpperCase();
keyMac += pos;
if (i < 5)
keyMac += ":";
}
// Devolvemos la MAC en String
return keyMac;
}
/********** FUNCIÓN QUE REALIZA LA PETICIÓN PUT **************/
void peticionPut()
{
// Cerramos cualquier conexión antes de enviar una nueva petición
client.stop();
client.flush();
// Enviamos una petición por SSL
if (client.connect(HOSTFIREBASE, 443)) {
// Petición PUT JSON
String toSend = "PUT /dispositivo/";
toSend += macStr;
toSend += ".json HTTP/1.1\r\n";
toSend += "Host:";
toSend += HOSTFIREBASE;
toSend += "\r\n" ;
toSend += "Content-Type: application/json\r\n";
String payload = "{"lat":";
payload += String(loc.lat, LOC_PRECISION);
payload += ",";
payload += ""lon":";
payload += String(loc.lon, LOC_PRECISION);
payload += ",";
payload += ""prec":";
payload += String(loc.accuracy);
payload += ",";
payload += ""nombre": "";
payload += nombreComun;
payload += ""}";
payload += "\r\n";
toSend += "Content-Length: " + String(payload.length()) + "\r\n";
toSend += "\r\n";
toSend += payload;
Serial.println(toSend);
client.println(toSend);
client.println();
client.flush();
client.stop();
Serial.println("Todo OK");
} else {
// Si no podemos conectar (if we cannot connect)
client.flush();
client.stop();
Serial.println("SOmething went wrong");
}
}