Created
September 14, 2018 15:23
-
-
Save grizmio/ca8ceb1df9ab8b0ab574c70810313dbb to your computer and use it in GitHub Desktop.
ESP32: Subir datos a thinger.io usando mejor ssid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DHTesp.h> | |
#include <WiFiClientSecure.h> | |
#include <ThingerESP32.h> | |
#define TO_SECONDS 1000 * 1000 | |
#define USERNAME "usuario" | |
#define DEVICE_ID "dispositivo" | |
#define DEVICE_CREDENTIAL "contrasenia" | |
#define DEEPSLEEP_SECONDS 30 | |
#define SENSOR_PIN 19 | |
const char * ssids[] = { "Foo1", "Casa", "Patio" }; | |
const char * passwds[] = { "BAZAr", "fooo", "conta" }; | |
float humidity = -1 ; | |
float temperature = -1; | |
bool write_bucket_status = false; | |
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL); | |
DHTesp dht; | |
void setup() { | |
Serial.begin(115200); | |
dht.setup(SENSOR_PIN, DHTesp::DHT11); | |
pinMode(LED_BUILTIN, OUTPUT); | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
delay(1*1000); | |
} | |
void loop() { | |
int ssid_index = get_best_ssid(); | |
if(ssid_index < 0){ | |
Serial.println("OOOPS! no se encontro ssid elegido"); | |
delay(1000); | |
return; | |
} | |
thing.add_wifi(ssids[ssid_index], passwds[ssid_index]); | |
humidity = dht.getHumidity(); | |
humidity = isnan(humidity) ? -1: humidity; | |
temperature = dht.getTemperature(); | |
temperature = isnan(temperature) ? -1: temperature; | |
pson data; | |
data["temperatura"] = temperature; | |
data["humedad"] = humidity; | |
thing.handle(); | |
write_bucket_status = thing.write_bucket("LBucket", data); | |
Serial.println(isnan(temperature) ? -1: temperature ); | |
ESP.deepSleep( (uint64_t)(DEEPSLEEP_SECONDS) * TO_SECONDS); | |
} | |
int get_best_ssid(){ | |
// errores deben ser retornos de numeros negativos | |
int n = WiFi.scanNetworks(); | |
switch(n){ | |
case -2: | |
Serial.println("Error -2 escaneando redes"); | |
return -1; | |
case 0: | |
Serial.println("No hay redes"); | |
return -2; | |
} | |
for(int i=0;i<n;i++){ | |
// iteramos los ssid encontrados | |
for(int j=0; j<sizeof(ssids)/sizeof(char*);j++){ | |
// iteramos los ssid encontrados | |
if(WiFi.SSID(i) == ssids[j]){ | |
Serial.print("MEJOR SSID: "); | |
Serial.println(ssids[j]); | |
return j; | |
} | |
} | |
} | |
return -3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment