#include "thingProperties.h"
unsigned long lastMsg = 0;
const float GAMMA = 0.7;
const float RL10 = 85.0;
const int sensorPin = 15;
void setup() {
Serial.begin(9600);
// Inisialisasi pin yang digunakan
pinMode(sensorPin, INPUT);
// 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();
lastMsg = millis();
}
void loop() {
ArduinoCloud.update();
if ((millis() - lastMsg) > 1000) {
ReadSensor();
// Print data ke Serial Monitor
Serial.print("Intensity: ");
Serial.print(ldr);
Serial.println(" lux");
if (ArduinoCloud.connected()) {
Serial.println("Connected to Arduino Cloud");
} else {
Serial.println("Not connected to Arduino Cloud");
}
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;
}