daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <DHTesp.h>
  2. #include <WiFiClientSecure.h>
  3. #include <ThingerESP32.h>
  4.  
  5. #define TO_SECONDS 1000 * 1000
  6. #define USERNAME "usuario"
  7. #define DEVICE_ID "dispositivo"
  8. #define DEVICE_CREDENTIAL "contrasenia"
  9. #define DEEPSLEEP_SECONDS 30
  10. #define SENSOR_PIN 19
  11.  
  12. const char * ssids[] = { "Foo1", "Casa", "Patio" };
  13. const char * passwds[] = { "BAZAr", "fooo", "conta" };
  14.  
  15. float humidity = -1 ;
  16. float temperature = -1;
  17. bool write_bucket_status = false;
  18.  
  19. ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
  20. DHTesp dht;
  21.  
  22. void setup() {
  23.   Serial.begin(115200);
  24.   dht.setup(SENSOR_PIN, DHTesp::DHT11);
  25.   pinMode(LED_BUILTIN, OUTPUT);
  26.   WiFi.mode(WIFI_STA);
  27.   WiFi.disconnect();
  28.   delay(1*1000);
  29. }
  30.  
  31. void loop() {
  32.   int ssid_index = get_best_ssid();
  33.   if(ssid_index < 0){
  34.     Serial.println("OOOPS! no se encontro ssid elegido");
  35.     delay(1000);
  36.     return;
  37.   }
  38.   thing.add_wifi(ssids[ssid_index], passwds[ssid_index]);
  39.  
  40.   humidity = dht.getHumidity();
  41.   humidity = isnan(humidity) ? -1: humidity;
  42.   temperature = dht.getTemperature();
  43.   temperature = isnan(temperature) ? -1: temperature;
  44.  
  45.   pson data;
  46.   data["temperatura"] = temperature;
  47.   data["humedad"] = humidity;
  48.   thing.handle();
  49.   write_bucket_status = thing.write_bucket("LBucket", data);
  50.   Serial.println(isnan(temperature) ? -1: temperature );
  51.   ESP.deepSleep( (uint64_t)(DEEPSLEEP_SECONDS) * TO_SECONDS);
  52. }
  53.  
  54. int get_best_ssid(){
  55.   // errores deben ser retornos de numeros negativos
  56.   int n = WiFi.scanNetworks();
  57.   switch(n){
  58.     case -2:
  59.       Serial.println("Error -2 escaneando redes");
  60.       return -1;
  61.     case 0:
  62.       Serial.println("No hay redes");
  63.       return -2;
  64.   }
  65.  
  66.   for(int i=0;i<n;i++){
  67.     // iteramos los ssid encontrados
  68.     for(int j=0; j<sizeof(ssids)/sizeof(char*);j++){
  69.       // iteramos los ssid encontrados
  70.       if(WiFi.SSID(i) == ssids[j]){
  71.         Serial.print("MEJOR SSID: ");
  72.         Serial.println(ssids[j]);
  73.         return j;
  74.       }
  75.     }
  76.   }
  77.   return -3;
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top