#include <WiFi.h> // WiFi control for ESP32
//#include <ThingsBoard.h> // ThingsBoard SDK
#include "ThingsBoard.h"
#include <ESP32Time.h>
//ESP32Time rtc;
ESP32Time rtc(0); // offset in seconds GMT+1
#include "Servo.h"
Servo wadahPakan;
// #define ECHO_PIN 12
// #define TRIG_PIN 13
const int TRIG_PIN = 13;
const int ECHO_PIN = 12;
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define WIFI_AP_NAME "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define THINGSBOARD_ACCESSTOKEN "ttewGYbNi0RVYfD7r3By"
#define SERIAL_DEBUG_BAUD 115200
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
// Array with LEDs that should be lit up one by one
uint8_t leds_cycling[] = { 5, 19, 18 };
// Array with LEDs that should be controlled from ThingsBoard, one by one
uint8_t leds_control[] = { 26, 33, 25 };
// Main application loop delay
int quant = 20;
// Initial period of LED cycling.
int led_delay = 1000;
// Period of sending a temperature/humidity data.
int send_delay = 2000;
// Time passed after LED was turned ON, milliseconds.
int led_passed = 0;
// Time passed after temperature/humidity data was sent, milliseconds.
int send_passed = 0;
// Set to true if application is subscribed for the RPC messages.
bool subscribed = false;
// LED number that is currenlty ON.
int current_led = 0;
RPC_Response processDelayChange(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
// Process data
led_delay = data;
Serial.print("Set new delay: ");
Serial.println(led_delay);
return String(led_delay);
}
RPC_Response processGetDelay(const RPC_Data &data)
{
Serial.println("Received the get value method");
return String(led_delay);
}
RPC_Response processSetGpioState(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
int pin = data["pin"];
bool enabled = data["enabled"];
if (pin < COUNT_OF(leds_control)) {
Serial.print("Setting LED ");
Serial.print(pin);
Serial.print(" to state ");
Serial.println(enabled);
digitalWrite(leds_control[pin], enabled);
}
return String("{\"" + String(pin) + "\": " + String(enabled ? "true" : "false") + "}");
}
RPC_Response processGetGpioState(const RPC_Data &data)
{
Serial.println("Received the get GPIO RPC method");
String respStr = "{";
for (size_t i = 0; i < COUNT_OF(leds_control); ++i) {
int pin = leds_control[i];
Serial.print("Getting LED ");
Serial.print(pin);
Serial.print(" state ");
bool ledState = digitalRead(pin);
Serial.println(ledState);
respStr += String("\"" + String(i) + "\": " + String(ledState ? "true" : "false") + ", ");
}
respStr = respStr.substring(0, respStr.length() - 2);
respStr += "}";
return respStr;
}
// RPC handlers
RPC_Callback callbacks[] = {
{ "setValue", processDelayChange },
{ "getValue", processGetDelay },
{ "setGpioStatus", processSetGpioState },
{ "getGpioStatus", processGetGpioState },
};
void setup() {
Serial.begin(115200);
rtc.setTime(30, 46, 9, 29, 8, 2022); //set waktu 29 Agustus 2022 09:46:30
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
wadahPakan.attach(23);
wadahPakan.write(0);
// Pinconfig
for (size_t i = 0; i < COUNT_OF(leds_cycling); ++i) {
pinMode(leds_cycling[i], OUTPUT);
}
for (size_t i = 0; i < COUNT_OF(leds_control); ++i) {
pinMode(leds_control[i], OUTPUT);
}
}
void loop()
{
(pakan);
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
delay(quant);
led_passed += quant;
send_passed += quant;
// Check if next LED should be lit up
if (led_passed > led_delay) {
// Turn off current LED
digitalWrite(leds_cycling[current_led], LOW);
led_passed = 0;
current_led = current_led >= 2 ? 0 : (current_led + 1);
// Turn on next LED in a row
digitalWrite(leds_cycling[current_led], HIGH);
}
// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}
// Reconnect to ThingsBoard, if needed
if (!tb.connected()) {
subscribed = false;
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(THINGSBOARD_ACCESSTOKEN);
if (!tb.connect(THINGSBOARD_SERVER, THINGSBOARD_ACCESSTOKEN)) {
Serial.println("Failed to connect");
return;
}
}
// Subscribe for RPC, if needed
if (!subscribed) {
Serial.println("Subscribing for RPC... ");
// Perform a subscription. All consequent data processing will happen in
// callbacks as denoted by callbacks[] array.
if (!tb.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
Serial.println("Failed to subscribe for RPC");
return;
}
Serial.println("Subscribe done");
subscribed = true;
}
if (send_passed > send_delay) {
Serial.println("Sending data...");
tb.sendTelemetryFloat("distance", distance);
send_passed = 0;
}
tb.loop();
if (distance > 20) {
digitalWrite(leds_control[0], HIGH);
digitalWrite(leds_control[1], LOW);
digitalWrite(leds_control[2], LOW);
}
if (distance >= 2 && distance <= 20) {
digitalWrite(leds_control[0], LOW);
digitalWrite(leds_control[1], HIGH);
digitalWrite(leds_control[2], LOW);
}
else if (distance < 2) {
digitalWrite(leds_control[0], LOW);
digitalWrite(leds_control[1], LOW);
digitalWrite(leds_control[2], HIGH);
}
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP_NAME, 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_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
void pakan() {
if(rtc.getTime("%H:%M:%S");
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S"));
struct tm timeinfo = rtc.getTimeStruct();
// servo sebagai buka tutup wadah pakan
// delay(2000);
// wadahPakan.write(45);
// delay(2000);
// wadahPakan.write(0);
}