#include "thingProperties.h"
#include <EduIntro.h>
#include <NewPing.h>
#include <Wire.h>
#include <SeeedOLED.h>
#define LEVELFULL 32
#define LEVELEMPTY 32
#define VOLUME 25
#define trigPin 6
#define echoPin 7
#define MAX_DISTANCE 100
float duration;
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
int moist1Pin = A1;
int moist2Pin = A2;
int moist3Pin = A3;
int moist4Pin = A4;
int taster = 4;
int tasterstatus = 0;
float M1;
float M2;
float M3;
float M4;
float HUM;
float TMP;
DHT11 dht11(D1);
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
//Start I2C Communication for OLED Display
Wire.begin();
SeeedOled.init(); //initialze SEEED OLED display
SeeedOled.clearDisplay(); //clear the screen and set start position to top left corner
SeeedOled.setNormalDisplay(); //Set display to normal mode
SeeedOled.setPageMode(); //Set addressing mode to Page Mode
//Set digital pin D2 as an output
pinMode(2, OUTPUT);
//Set digital pin D4 as an input
pinMode(taster, INPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
//Get Cloud Info/errors , 0 (only errors) up to 4
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
//Wait to get cloud connection
while (ArduinoCloud.connected() != 1) {
ArduinoCloud.update();
delay(500);
}
}
void loop() {
//Update the Cloud
ArduinoCloud.update();
//read raw moisture1 value
float raw_moisture1 = analogRead(moist1Pin);
//map raw moisture1 to a scale of 0 - 100
moisture1 = map(raw_moisture1, 205, 471, 100, 0);
//read raw moisture2 value
float raw_moisture2 = analogRead(moist2Pin);
//map raw moisture2 to a scale of 0 - 100
moisture2 = map(raw_moisture2, 215, 480, 100, 0);
//read raw moisture3 value
float raw_moisture3 = analogRead(moist3Pin);
//map raw moisture3 to a scale of 0 - 100
moisture3 = map(raw_moisture3, 220, 505, 100, 0);
//read raw moisture4 value
float raw_moisture4 = analogRead(moist4Pin);
//map raw moisture4 to a scale of 0 - 100
moisture4 = map(raw_moisture4, 200, 456, 100, 0);
//read temperature and humidity
dht11.update();
temperature = dht11.readCelsius();
humidity = dht11.readHumidity();
// Level monitoring with ultrasonic sensor
// Waiting time between pings (about 20 pings/sec). not less than 29ms!
delay(50);
//Measurement with averaging
int iterations = 5;
//duration = sonar.ping() without averaging
duration = sonar.ping_median(iterations);
//Distance in cm
int distance = int((duration / 2) * 0.0343);
//Calculate level
int levelact = (LEVELEMPTY - distance);
//Calculate level in Percent
levelpercent = (levelact * 100 / LEVELFULL);
//Button configuration to switch on the Oled display
tasterstatus = digitalRead(taster);
M1 = map(raw_moisture1, 205, 471, 100, 0);
M2 = map(raw_moisture2, 215, 480, 100, 0);
M3 = map(raw_moisture3, 220, 505, 100, 0);
M4 = map(raw_moisture4, 200, 456, 100, 0);
HUM = dht11.readHumidity();
TMP = dht11.readCelsius();
if (tasterstatus == HIGH)//Button is activated - Oled outputs sensor values
{
SeeedOled.setTextXY(0,11); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("Box1"); //Print the string
SeeedOled.setTextXY(1,0); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("TMP "); //Print the string
SeeedOled.putFloat(TMP); //Print the value
SeeedOled.putString(" C"); //Print the string
SeeedOled.setTextXY(2,0); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("RLH "); //Print the string
SeeedOled.putFloat(HUM); //Print the value
SeeedOled.putString(" % "); //Print the string
SeeedOled.setTextXY(4,0); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("M_1 "); //Print the string
SeeedOled.putFloat(M1); //Print the value
SeeedOled.putString(" % "); //Print the string
SeeedOled.setTextXY(5,0); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("M_2 "); //Print the string
SeeedOled.putFloat(M2); //Print the value
SeeedOled.putString(" % "); //Print the string
SeeedOled.setTextXY(6,0); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("M_3 "); //Print the string
SeeedOled.putFloat(M3); //Print the value
SeeedOled.putString(" % "); //Print the string
SeeedOled.setTextXY(7,0); //Set the cursor to Xth Page, Yth Column
SeeedOled.putString("M_4 "); //Print the string
SeeedOled.putFloat(M4); //Print the value
SeeedOled.putString(" % "); //Print the string
delay(30000); //Oled stays activated for 30 seconds
SeeedOled.clearDisplay(); //Oled at rest
}
else
{
SeeedOled.clearDisplay();//Oled at rest
}
}
/*
Since Pumpe is READ_WRITE variable, onPumpeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPumpeChange() {
//Turn pump on or off
if(pumpe){
digitalWrite(2, HIGH);
}
else{
digitalWrite(2, LOW);
}
}
Comments
Please log in or sign up to comment.