#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include "ThingsBoard.h"
#define led 25
#define ECHO_PIN 12
#define TRIG_PIN 13
#define ldr 34
#define ANALOG_THRESHOLD 500
#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 "dSjAURVzNkFSNslU9E9c"
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD 115200
// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
int led_delay=0;
// Main application loop delay
int quant = 20;
// 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;
float lampu;
char buffer[50];
PubSubClient client(espClient);
static char strDetection[10] = {0};
char clientId[50];
int value = 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() {
// initialize serial for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
pinMode(led, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
client.setServer("broker.emqx.io", 1883);
InitWiFi();
}
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() {
delay(quant);
// Led();
client.loop();
led_passed += quant;
send_passed += quant;
// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}
float jarak = readDistanceCM();
// 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(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
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.
Serial.println("Subscribe done");
subscribed = true;
}
int analogValue = analogRead(ldr);
if (analogValue < ANALOG_THRESHOLD){
digitalWrite(led, 1); // turn on LED
} else{
digitalWrite(led, 0); // turn off LED
}
/// sprintf(strDetection, "%d", value);
//client.publish("topicName/detection", strDetection);
Serial.print("Jarak: ");
Serial.print(jarak);
Serial.println(" cm ");
Serial.print("LDR: ");
Serial.println(analogValue);
delay(500);
// Check if it is a time to send DHT22 temperature and humidity
tb.sendTelemetryFloat("Jarak", jarak);
tb.sendTelemetryFloat("Ldr", analogValue);
send_passed = 0;
}
/*void Led(){
float jarak = readDistanceCM();
if(jarak > 0 && jarak < 100 ){
digitalWrite(led, HIGH);
lampu=1;
}else{
digitalWrite(led, LOW);
lampu=0;
}
} */