JonnyLit
Published © GPL3+

Agrumino Lemon-Temperature and Humidity on Cayenne Platform

Using Agrumino Lemon to read Humidity and Temperature on Cayenne myDevices platform

BeginnerFull instructions provided1 hour137
Agrumino Lemon-Temperature and Humidity on Cayenne Platform

Things used in this project

Hardware components

Lifely Agrumino Lemon
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Cayenne
myDevices Cayenne

Story

Read more

Code

Agrumino_Cayenne_humid_temp

C/C++
/*
Cayenne Agrumino Humidity-Temperature measure
*/


// WiFi network info
char SSID_NAME[] = "****"; //insert your wifi connection name
char SSID_PASSWORD[] = "****"; //insert your wifi password



#define CONNECTION_ATTEMPTS 30
#define MAX_NUM_NOTIFICATIONS_PER_DAY 90 //Cayenne limit is max 90 sms or email notifications per day

//#define SECONDS_BETWEEN_MEASURES 86400/(MAX_NUM_NOTIFICATIONS_PER_DAY/2) //seconds in a day divide the max number of Cayenne notifications per day, 
                                                                           //where the '/2' is because the 90 notifications could be at worst 45 for the humidity and 45 for the temperature.
#define SECONDS_BETWEEN_MEASURES 60  //if it is unlikely to receive 90 notifications per day, use this instead of the expression above, and change the value on your need.

#include <CayenneMQTTESP8266.h>
#include <Agrumino.h>

Agrumino agrumino; //Agrumino object

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard
char username[] = "****";
char password[] = "****";
char clientID[] = "****";

//Variables for humidity and temperature values
float humidity;
float temperature;

//Counter of wifi connection attempts
int wifiCount = 0;

void wifiSetup(){
  
  Serial.print("Connecting to ");
  Serial.println(SSID_NAME);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID_NAME, SSID_PASSWORD);

  while ((WiFi.status() != WL_CONNECTED) && (wifiCount < CONNECTION_ATTEMPTS)){
    delay(500);
    wifiCount++;
    Serial.print(".");
  }

  if (wifiCount >= CONNECTION_ATTEMPTS){
    Serial.println("Failed to connect,...."
                  "Please check your internet connection..."
                  "I'm rebooting now...");
    ESP.restart();
  }else if (WiFi.status() == WL_CONNECTED) {
    Serial.print("I'm Connected with:"  );
    Serial.println(SSID_NAME);
    delay(100);
  }
}


void setup(){
  
  Serial.begin(115200);
  Serial.println("Cayenne begin");
  Cayenne.begin(username, password, clientID, SSID_NAME, SSID_PASSWORD);
  Serial.println("Agrumino Setup");
  agrumino.setup();
  agrumino.turnBoardOn();
  long int agruminoChipId = ESP.getChipId();
  Serial.println("My ChipId is : " + String(agruminoChipId));
  wifiSetup();
  delay(500);
}

void loop(){
  
  //Run Cayenne Functions
  Cayenne.loop();
}

CAYENNE_OUT(V0){
  /*V0 is the output Channel #0 for sending data to Cayenne with MQTT, the first channel to be used */
  
  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    humidity = agrumino.readSoil(); //soilMoisture
  } while  (isnan(humidity));

  Serial.print("humidity: ");
  Serial.println(humidity);
  
  //Write to Cayenne Dashboard
  Cayenne.virtualWrite(V0, humidity);
}

CAYENNE_OUT(V1){
  /*V1 is the output Channel #1 for sending data to Cayenne with MQTT, the second channel to be used, so after V0 */
  
  //Check if read failed and try until success
  do {
    //Read temperature as Celsius
    temperature = agrumino.readTempC();
  } while  (isnan(temperature));

  Serial.print("temperature: ");
  Serial.println(temperature);

  //Write to Cayenne Dashboard
  Cayenne.virtualWrite(V1, temperature);
  delay(1000); //there must be a delay between the Cayenne.virtualWrite and the deepSleepSec functions, otherwise the measure doesn't reach cayenne platform (the dashboard would remain 'Offline').
  
  //The deepSleepSec function will be used in order to save battery when the Agrumino is unused for measures or connection.
  //The deepSleepSec time has been chosen according to the max possible number of Cayenne notifications per day(90), and also used as frequency measure.
  //Anyway looking at the Serial Monitor after activating the visible timings (checkbutton bottom-left), it is possible to see that there is a time shift 
  //of 11 seconds between two measures, due to some extra time for the connection, restart and other routines with delays. 
  //So 11 seconds will be subtracted ('-11' in the deepSleepSec argument) to calibrate and obtain the real frequency measure specified as ''SECONDS_BETWEEN_MEASURES''.
  //Anyway, you can change the frequency measure as needed:
  //infact it is very unlikely that there will be a notification for every measure, see the comment at the beginning of this file for the macro 'SECONDS_BETWEEN_MEASURES'.
  agrumino.deepSleepSec(SECONDS_BETWEEN_MEASURES-11); //remember that the max seconds of deepsleep are 4'294 seconds (see the function deepSleepSec in Agrumino.cpp)
}

Credits

JonnyLit
1 project • 0 followers
Contact
Thanks to P.Santillan.

Comments

Please log in or sign up to comment.