My Garden GBoard Code

#include "CumulocityPlatform.h"
#include "SIM900.h"
#include <SoftwareSerial.h>

#define moisture_input 0

CumulocityPlatform cPlatform("<<host>>", "<<tenantId>>", "<<user>>", "<<password>>", "<<applicationKey>>");
GSMModule* mod;

char id[8];
int result;

boolean reminder=false;

float moisture; 
//_________________________________________
void setup()
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println(F("GSM Shield testing."));

  mod = new GSMModule();
  Serial.println(F("Attaching GPRS..."));
  if(mod->attachGPRS("internet.telekom", "telekom", "tm")) {
    Serial.println(F("GPRS attached."));
    cPlatform.setGSM(mod);

    Serial.println(F("Registering a device..."));
    result = cPlatform.registerDevice("MyGarden", id, 8);

    if(result<0) {
      Serial.println(F("Registration error."));
      Serial.println(result);
      while(true);
    }
    else {
      Serial.print(F("Arduino registered with id: "));
      Serial.println(id);
    }
  }
  else {
    Serial.println(F("Could not attach GPRS."));
    while(true);
  }
};
//__________________________________________________
void loop()
{
  Serial.println(F("Test_Anfang"));

  int sensorValue = analogRead(1);
  float Rsensor;
  Rsensor=(float)(1023-sensorValue)*10/sensorValue;
  Serial.println(Rsensor);

  Serial.println(F("Sending measurement"));
  result = cPlatform.sendMeasurement("LightMeasurement", "Ev", Rsensor, "lx");
  if(result > 0) {
    Serial.println(F("Measurement sent successfully."));
  }
  else {
    Serial.println(result);
    Serial.println(F("Measurement sending failed."));
  }

  moisture=SoilMoisture(); 
  Serial.print(F("Soil moisture: "));
  Serial.println(moisture);
  float percent;
  percent=(float)(moisture/1023)*100;
  Serial.println(percent);
  Serial.println(F("Sending measurement"));
  result = cPlatform.sendMeasurement("MoistureMeasurement", "moisture", percent, "per");
  if(result > 0) {
    Serial.println(F("Measurement sent successfully."));
  }
  else {
    Serial.println(result);
    Serial.println(F("Measurement sending failed."));
  }

  if (moisture<200 && reminder==false){
    Serial.println(F("trocken"));
    //send alarm to the developer platform
    int result = cPlatform.raiseAlarm("alarm_water_low", "active", "major", "moisture to low");
    if(result < 0) {
      Serial.println(F("Alarm failed."));
    } else {
      Serial.println(F("Alarm sent."));
    }
      reminder=true;
  }
  else
    if (moisture>800 && reminder==true){
      reminder=false;
      Serial.println(F("gegossen"));
    }

  delay(300000); 
};
//___________________________________________________

float SoilMoisture(){
  float reading;

  pinMode(9,OUTPUT);
  pinMode(11,OUTPUT);

  digitalWrite(9,LOW);
  digitalWrite(11,HIGH);

  delay(2000);

  reading=analogRead(moisture_input);

  digitalWrite(9,HIGH);
  digitalWrite(11,LOW);

  delay(1000);

  digitalWrite(9,LOW);

  return reading;
};

Comments 1

  1. Pingback: Helping our Office Plant with an Arduino (Part 2) | Let Machines Talk Let Machines Talk

Leave a Reply

Your email address will not be published. Required fields are marked *