/*
Sketch generated by the Arduino IoT Cloud Thing "NTC Sensor Cloud"
https://create.arduino.cc/cloud/things/0bed33da-ac4f-4aee-84b0-ccc9460dcfab
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudTemperatureSensor suhu;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
//Pin yang dipakai di ESP32
#define NTC_ANALOG_PIN 34 //ke pin input sensor ntc
//============================================================================================
//============================================================================================
//Konfigurasi sensor NTC
const float BETA = 3950;
// float suhu = 0.0;
//shortcut kode
#define DEBUG
#ifdef DEBUG
#define SERIAL_BEGIN Serial.begin(115200)
#define debug(p) Serial.print(p)
#define debugln(p) Serial.println(p)
#else
#define SERIAL_BEGIN
#define debug(p)
#define debugln(p)
#endif
//1. Implementasi interface sensor NTC
//Fungsi untuk membaca nilai sensor NTC
float bacaSensorNTC(){
//Baca nilai sensor menggunakan input analog
int analogValue = analogRead(NTC_ANALOG_PIN);
//Kalibrasi untuk mengkonversi nilai analog menjadi nilai suhu celsius
float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
// delay(1500);
// 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();
}
void loop() {
ArduinoCloud.update();
// Your code here
//Klik sensor NTC, geser slide untuk mengatur suhu
//Panggil fungsi 'bacaSensorNTC()' untuk membaca Suhu NTC
suhu = bacaSensorNTC();
//Tampilkan ke Serial monitor
debugln("[NTC] Temperature: " + String(suhu) + " ℃");
delay(10); // this speeds up the simulation
}
/*
Since Suhu is READ_WRITE variable, onSuhuChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSuhuChange() {
// Add your code here to act upon Suhu change
}
// /*
// * Interface sensor NTC
// */
// //============================================================================================
// //shortcut kode
// #define DEBUG
// #ifdef DEBUG
// #define SERIAL_BEGIN Serial.begin(115200)
// #define debug(p) Serial.print(p)
// #define debugln(p) Serial.println(p)
// #else
// #define SERIAL_BEGIN
// #define debug(p)
// #define debugln(p)
// #endif
// //============================================================================================
// //============================================================================================
// //Pin yang dipakai di ESP32
// #define NTC_ANALOG_PIN 34 //ke pin input sensor ntc
// //============================================================================================
// //============================================================================================
// //Konfigurasi sensor NTC
// const float BETA = 3950;
// float suhu = 0.0;
// //============================================================================================
// //1. Implementasi interface sensor NTC
// //Fungsi untuk membaca nilai sensor NTC
// float bacaSensorNTC(){
// //Baca nilai sensor menggunakan input analog
// int analogValue = analogRead(NTC_ANALOG_PIN);
// //Kalibrasi untuk mengkonversi nilai analog menjadi nilai suhu celsius
// float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// return celsius;
// }
// //============================================================================================
// void setup() {
// //Inisalisasi komunikasi serial
// SERIAL_BEGIN;
// }
// void loop() {
// //Klik sensor NTC, geser slide untuk mengatur suhu
// //Panggil fungsi 'bacaSensorNTC()' untuk membaca Suhu NTC
// suhu = bacaSensorNTC();
// //Tampilkan ke Serial monitor
// debugln("[NTC] Temperature: " + String(suhu) + " ℃");
// delay(10); // this speeds up the simulation
// }