Posted below is my code, I was wondering how i could implement the haversine formula to calculate the distance between two close coordinates. data is coming in from an array from another device and i was going to use that for one of the coordinates and then the other coordinates i would insert myself.
Please Help!
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
// Init ESP Now with fallback
void InitESPNow() {
WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
// config AP SSID
void configDeviceAP() {
char* SSID = "Slave_1";
bool result = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0);
if (!result) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
}
}
void setup() {
Serial.begin(115200);
Serial.println("ESPNow/Basic/Slave Example");
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP);
configDeviceAP();
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
InitESPNow();
esp_now_register_recv_cb(OnDataRecv);
}
void OnDataRecv(const uint8_t *mac_addr,const uint8_t* data, int data_len) {
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
if(data_len != 2*sizeof(float))
{
Serial.println("I haven't received 8 bytes of gps data.");
return;
}
const float* gpsData = reinterpret_cast<const float*>(data);
Serial.print("Latitude: ");
Serial.println(gpsData[0],8);
Serial.print("Longitude: ");
Serial.println(gpsData[1],8);
Serial.println("Distance: ");
}
void loop() {
}