#include <Arduino.h>
#include "thingProperties.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD(0x27, 16, 2);
unsigned long lastMsg = 0;
const float GAMMA = 0.7;
const float RL10 = 85.0;
const int sensorPin = 15;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Online");
LCD.setCursor(0, 1);
LCD.print("Updating time...");
lastMsg = millis();
}
void loop() {
ArduinoCloud.update();
if ((millis() - lastMsg) > 1000) {
ReadSensor();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Intensity: ");
LCD.print(ldr);
LCD.print(" lux");
LCD.setCursor(0, 1);
if (ArduinoCloud.connected()) {
LCD.print("connected");
} else {
LCD.print("not connected");
}
lastMsg = millis();
}
}
void ReadSensor() {
int analogValue = analogRead(sensorPin);
float voltage = analogValue / 4095.0 * 3.3; // Mengonversi nilai ADC ke tegangan (misalnya 3.3V untuk ESP32/ESP8266)
float resistance = 5000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if (isnan(lux)) {
Serial.println(F("data error!"));
return;
}
ldr = lux;
}