/* Microservices available at http://www.saltlake71.eu/urpc Author: massimo.sala.71 AT gmail.com */ #include #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) #include #elif defined(ARDUINO_SAMD_MKR1000) #include #elif defined(ARDUINO_ARCH_ESP8266) #include #elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA) #include #elif defined(ARDUINO_PORTENTA_C33) #include #elif defined(ARDUINO_UNOR4_WIFI) #include #endif #include // from uRPC 0.97, no authentication required //const uint8_t AUTH_TOKEN[] = "Basic ... insert here your access token ..."; const int PACKET_LEN = 12; const int RECORDS_MAX = 30; // fewer Access Points reduce memory consumption, but location will be less precise uint8_t BUFFER[RECORDS_MAX * PACKET_LEN]; void setup() { Serial.begin(115200); while (! Serial) // USB: wait for serial port to connect ; if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); while (true); // don't continue } } void loop() { int retries = 30; while (retries && WiFi.status() != WL_CONNECTED) { -- retries; delay(1000); Serial.print("."); } Serial.println(""); if (WiFi.status() == WL_CONNECTED) { int n_ap = get_access_points(BUFFER, RECORDS_MAX); Serial.print("Found "); Serial.print(n_ap); Serial.println(" access points"); if (n_ap) http_request_and_print("http://saltlake71.eu/urpc?function=geoloc", BUFFER, n_ap * PACKET_LEN); } else Serial.println("WiFi: not connected, will retry"); } int get_access_points(uint8_t * ptr, int limit) { int n_total = WiFi.scanNetworks(); if (n_total <= 0) { Serial.println("WiFi: could not list Access Points, will retry"); return 0; } int n = 0; for (int i = 0; i < n_total; i ++) { uint8_t * mac = WiFi.BSSID(i); int signal = WiFi.RSSI(i); // dBm if (signal < 0 && signal > -150) // sanity checks { snprintf((char *) ptr, 2 + 4 + 1, "%02d%04d", n, signal); memcpy(ptr + 6, mac, 6); ptr += 12; // fixed records of 12 bytes n += 1; if (n == 100 || n == limit) break; } } return n; } void http_request_and_print(const char * url, uint8_t * data, int dataLength) { HTTPClient http; http.begin(url); // from uRPC 0.97, no authentication required // http.addHeader("Authorization", AUTH_TOKEN); int httpResponseCode = http.POST(data, dataLength); if (httpResponseCode == 200) Serial.print("Location: accuracy lat long = "); else Serial.print("Error: "); String response = http.getString(); Serial.println(response); http.end(); }