Gabriele Foddis
Published © CC BY

Lifely Agrumino Lemon esp8266 with Tago. IO

Send data from your Lifely Agrumino Lemon in the Tago. io. Work with all esp8266 but change "dataToSend. cpp". www. lifely. cc and www. tago. io

IntermediateFull instructions provided18 minutes266
Lifely Agrumino Lemon esp8266 with Tago. IO

Things used in this project

Hardware components

Agrumino
Lifely Agrumino
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Arduino IDE 1.8 or above
×1
Visual Studio Code with Platformio
×1
Free account Tago.io Cloud IoT Platform
www.tago.io
×1

Story

Read more

Code

LIFELY_Agrumino_Lemon_with_TAGO_IO.ino

C/C++
/*LifelyAgruminoLemonForTAGO_IO- 
  Created by Gabriele Foddis on 12/2022, last update 03/2023
  gabriele.foddis@lifely.cc
  With this Sketch you can send data from your Fantastic Lifely Agrumino Lemon Device in the Tago.IO Cloud Platform
  For better experience use this with Visual Studio Code and Platformio. You can also use it with Arduino Ide.
  Important, edit data in datatosend.ccp. You can configure the sending timing and you can use it in always-on mode as well 
  Build your amazing dashboard in the Tago.IO Cloud
  This sketch need a internet connection and an account from www.tago.io
  Find Lifely Agrumino Lemon (REV4 AND REV5) in www.lifely.cc */

#include <Arduino.h>
//#define ARDUINOJSON_USE_LONG_LONG 0 //set 1 if necessary
//#define ARDUINOJSON_USE_DOUBLE 0 //set 1 if necessary
#include <ArduinoJson.h> 
#include "datatosend.h"  /// before burn and upload edit datatosend.cpp data
#include <Agrumino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#define SERIAL_BAUD 115200
#define MIN_TO_MS (1000000 * 60)
#define HTTPCLIENT_1_1_COMPATIBLE 1

Agrumino agrumino;

void readDataFromDevice(){
  isBatteryCharging = agrumino.isBatteryCharging();
  temperature = agrumino.readTempC();
  soilMoisture = agrumino.readSoil();
  lux = agrumino.readLux();
  batteryVoltage = agrumino.readBatteryVoltage();
  batteryLevel = agrumino.readBatteryLevel();
  delay(200);
}


void blinkAndSleep() {
  agrumino.turnLedOn();
  delay(200);
  agrumino.turnLedOff();
  if(alwaysON == false && SLEEP_TIME_MIN > 1 ){ 
  Serial.println("I sleep for: "+String(SLEEP_TIME_MIN) + " minutes...");
  ESP.deepSleep(MIN_TO_MS * SLEEP_TIME_MIN);
  }
  if(alwaysON == false && SLEEP_TIME_MIN == 1){ 
  Serial.println("I sleep for: "+String(SLEEP_TIME_MIN) + " minute...");
  ESP.deepSleep(MIN_TO_MS * SLEEP_TIME_MIN);
  }
  else if (alwaysON == true)
  Serial.println("I read and send data every : "+ String(alwaysOnTime) + "milliseconds"); 
  delay(alwaysOnTime);

}

void setup() {
  Serial.begin(SERIAL_BAUD);
  agrumino.setup();
  agrumino.turnBoardOn();
  WiFi.begin(networkName, networkPassword);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected to WiFi network ("+String(networkName)+") with IP Address: ");
  Serial.print(WiFi.localIP());   
  agrumino.turnLedOn();
  delay(200);
  agrumino.turnLedOff();
}

void loop() {   
  readDataFromDevice();
  dataToSend();
  WiFiClient tagoClient;
  delay(100);
  HTTPClient httpTagoClient; 
  delay(100);
  httpTagoClient.begin(tagoClient, serverName);
  httpTagoClient.addHeader("Content-Type", "application/json; charset=UTF-8");     
  httpTagoClient.addHeader("Device-Token", apiTagoDeviceToken);
  int httpCode = httpTagoClient.POST(outputData);
  delay(200);
  //Serial.println("Data Sent is: " + String(output)); //print JsonData

    if (httpCode > 0) {
      Serial.printf("[HTTP] POST... code: %d\n", httpCode);
      if (httpCode == HTTP_CODE_OK) {
        const String& payload = httpTagoClient.getString();
        Serial.println("I Received this payload:\n<<");
        Serial.println(payload);
        Serial.println(">>");
      }
    } else {
      Serial.printf("[HTTP] POST... failed, error: %s\n", httpTagoClient.errorToString(httpCode).c_str());
    }
      Serial.println("HTTP Response code: ");
      Serial.println(httpCode);    
    delay(100);      
    httpTagoClient.end();
    tagoClient.stopAll();
    Serial.println(ESP.getFreeHeap());
    blinkAndSleep(); 
  }

datatosend.cpp

C/C++
/*
  Created by gabriele.foddis@lifely.cc on December 2022.
  for LIFELY_Agrumino_Lemon_with_TAGO_IO.ino
  
  Latest changes and updates 03/2023 by:  
  Gabriele Foddis gabriele.foddis@lifely.cc
  
  for a better experience use Visual Studio Code and Platformio

*/

#include <Arduino.h>
//#define ARDUINOJSON_USE_LONG_LONG 0 //set 1 if necessary
//#define ARDUINOJSON_USE_DOUBLE 0 //set 1 if necessary
#include <ArduinoJson.h>
#include "C:\Users\gabri\Documents\Arduino\Lifely_Agrumino_Lemon_Tago_IO\datatosend.h" ///add your path for datasotosend.h
unsigned int SLEEP_TIME_MIN = 5;//Time in minute, max value is 60 minutes min value is 1
const char* networkName = "PlumCake"; // your SSID network
const char* networkPassword = "ImVeryHappy"; // SSID password 
const char* serverName = "http://api.tago.io/data"; //don't edit this
String apiTagoDeviceToken = "cd8ccc84-1b8a-4fe1-bb85-0terf14c2cc1"; //Your TAGO.IO Token
long int alwaysOnTime = 5000; ///Set this time in millisecond for your "always on delay" (default 5000 (5 seconds)) min value is 1
boolean isBatteryCharging;
boolean alwaysON = false; // Set true if you need real time monitor data, default is false.
double temperature;
double lux;
double batteryVoltage;
unsigned int soilMoisture;
unsigned int batteryLevel;
String outputData;

void dataToSend(){

StaticJsonDocument<1024> data;

JsonObject data_T = data.createNestedObject();
data_T["variable"] = "Temperature";
data_T["value"] = temperature;
data_T["group"] = "1";
data_T["unit"] = "°C";

JsonObject data_SM = data.createNestedObject();
data_SM["variable"] = "SoilMoisture";
data_SM["value"] = soilMoisture;
data_SM["group"] = "1";
data_SM["unit"] = "%";

JsonObject data_L = data.createNestedObject();
data_L["variable"] = "Lux";
data_L["value"] = lux;
data_L["group"] = "1";
data_L["unit"] = "Lux";

JsonObject data_BL = data.createNestedObject();
data_BL["variable"] = "BatteryLevel";
data_BL["value"] = batteryLevel;
data_BL["group"] = "1";
data_BL["unit"] = "%";

JsonObject data_BV = data.createNestedObject();
data_BV["variable"] = "BatteryVoltage";
data_BV["value"] = batteryVoltage;
data_BV["group"] = "1";
data_BV["unit"] = "V";

JsonObject data_BC = data.createNestedObject();
data_BC["variable"] = "BatteryCharging";
data_BC["value"] = isBatteryCharging;
data_BC["group"] = "1";
data_BC["unit"] = "T/F";

delay(300);
serializeJsonPretty(data, outputData);
//Serial.print("json is: " + outputData); ///print to debug json data
}

datatosend.h

C/C++
/*
  Created by gabriele.foddis@lifely.cc on December 2022.
  for LIFELY_Agrumino_Lemon_with_TAGO_IO.ino
  
  Latest changes and updates 03/2023 by:  
  Gabriele Foddis gabriele.foddis@lifely.cc
  
  for a better experience use Visual Studio Code and Platformio

*/

#ifndef DATATOSEND_H
#define DATATOSEND_H

#include <Arduino.h>

extern unsigned int SLEEP_TIME_MIN;
extern const char* networkName; 
extern const char* networkPassword;
extern const char* serverName;
extern String apiTagoDeviceToken; 
extern long int alwaysOnTime; 
extern boolean isBatteryCharging;
extern boolean alwaysON; 
extern long int alwaysOnTime; 
extern double temperature;
extern double lux;
extern double batteryVoltage;
extern unsigned int soilMoisture;
extern unsigned int batteryLevel;
extern String outputData;

void dataToSend();

#endif

LIFELY_Agrumino_Lemon_with_TAGO_IO

Credits

Gabriele Foddis
7 projects • 5 followers
Production Manager- IIoT Business Designer - R&D
Contact

Comments

Please log in or sign up to comment.