// #if defined(ESP8266)
// #include <ESP8266WiFi.h>
// #elif defined(ESP32)
#include <WiFi.h>
// #endif
#include "ThingsBoard.h"
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Preferences.h>
#include "RTClib.h"
const int I2C_ADDRESS = 0x68;
LiquidCrystal_I2C lcd(0x27,20,4);
Preferences preferences;
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Servo myservo1;
Servo myservo2;
#define CURRENT_FIRMWARE_TITLE "TEST"
#define CURRENT_FIRMWARE_VERSION "1.0.0"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
#define TOKEN "9PqPQJ8PzmaCHq5wsqJK"
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define ECHO_PIN 2
#define TRIG_PIN 4
#define light 36
// #define push 13
const int relayPinUV = 25;
const float GAMMA = 0.7;
const float RL10 = 50;
int a;
int b;
// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
// int keadaan = 0;
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
InitWiFi();
lcd.init();
lcd.backlight();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(relayPinUV, OUTPUT);
// pinMode(push, INPUT);
myservo1.attach(19);
myservo2.attach(18);
// preferences.begin("Simpanan", false);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
float lux(){
int val = analogRead(light);
float voltage = val / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
return pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
}
float readDistanceCM(){
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
// if(digitalRead (push) == HIGH){
// keadaan++;
// delay(100);
// }
// if(keadaan==1){
// lcd.setCursor(0, 0);
// lcd.print("Mode Manual");
// lcd.setCursor(0, 1);
// lcd.print("Roof Open");
// myservo1.write(0);
// digitalWrite( relayPinUV, LOW);
// } else if(keadaan==2){
// lcd.setCursor(0, 0);
// lcd.print("Mode Manual");
// lcd.setCursor(0, 1);
// lcd.print("Roof Close");
// myservo1.write(90);
// digitalWrite( relayPinUV, HIGH);
// } else if(keadaan==3){
// keadaan=0;
// } else {
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
DateTime now = rtc.now();
preferences.begin("Simpanan", false);
lcd.setCursor(0, 0);
lcd.print("Smart Roof Tomatos");
lcd.setCursor(0, 1);
lcd.print("lux = ");
lcd.print(lux());
if (now.second()==5){
myservo2.write(0);
}
if (now.second()==10){
if(a==0){
preferences.putFloat("jarak1", readDistanceCM());
a=1;
b=0;
}
}
if (now.second()==30){
if(b==0){
preferences.putFloat("jarak2", readDistanceCM());
a=0;
b=1;
}
}
if (now.second()==32){
float curahhujan = (100*(preferences.getFloat("jarak2", 0)-preferences.getFloat("jarak1", 0))/20);
lcd.setCursor(0, 2);
lcd.print("i=");
lcd.print(curahhujan);
lcd.print("mm");
Serial.print("lux = ");
Serial.println(lux());
Serial.println("Sending data lux dan curah hujan...");
tb.sendTelemetryInt("curahhujan", curahhujan);
tb.sendTelemetryInt("intensitas cahaya", lux());
if(lux()>=1157 & lux()<=3800 & curahhujan<=200 & curahhujan>=100){
myservo1.write(0);
digitalWrite( relayPinUV, LOW);
lcd.setCursor(0, 3);
lcd.print("Roof Open");
} else {
myservo1.write(90);
digitalWrite( relayPinUV, HIGH);
lcd.setCursor(0, 3);
lcd.print("Roof Close");
}
}
if (now.second()==34){
myservo2.write(90);
}
Serial.println("--------");
Serial.println(now.second());
Serial.print("jarak 1 = ");
Serial.println(preferences.getFloat("jarak1", 0));
Serial.print("jarak 2 = ");
Serial.println(preferences.getFloat("jarak2", 0));
Serial.print("lux = ");
Serial.println(lux());
// Serial.println();
// Serial.println("Sending data lux dan curah hujan...");
preferences.end();
delay(1000);
// }
}