// IoT Barometer


// Based on:
// Adafruit IO Analog In Example
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-analog-input
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"             // include config.h. it contains wifi and Adafruit credentials
#include <Wire.h>               // I2C library
#include <Adafruit_BMP280.h>    // BMP280 library

/************************ Example Starts Here *******************************/

// Variable declarations
float current_temp = 0;
float last_temp = -1;

float current_pres = 0.0;
float last_pres = -1;

Adafruit_BMP280 bmp; // sensor object

// set up the feeds

AdafruitIO_Feed *tempe_feed = io.feed("tempe_feed");
AdafruitIO_Feed *pres_feed = io.feed("pres_feed");


void setup() {

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  
}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  // read presure value
  float pres = bmp.readPressure();
  
  // read temperature
  float temp = bmp.readTemperature();
 

  // wait 10 seconds
  delay(10000);
 

  // Check for reading errors
  if (isnan(pres) || isnan(temp)){
    Serial.println("Error en obtener los datos ");
    return;
  }

  // Save currrent values
  current_temp = temp;
  current_pres = pres / 100;
  
  // return if the value hasn't changed
  if((current_temp == last_temp) && (current_pres == last_pres))
    return;

  // save the current state to the presure and temperature feeds
  Serial.println("sending -> ");
  Serial.println(current_temp);
  tempe_feed->save(current_temp);
  Serial.println(current_pres);
  pres_feed->save(current_pres);
  

  // Store last values
  last_temp = current_temp;
  last_pres = current_pres;

  // wait for 50 seconds to complete 1 minute.
  delay(50000);

   
  
}
