#include <ESP32Servo.h>
#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
WiFiClient client;
unsigned long myChannelNumber = 1;
const char* myWriteAPIKey = "EDZF03MUM71S4SMJ";
const char* server = "api.thingspeak.com";
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const int pinR = 15; //lampu merah
const int pinG = 2; //lampu hijau
const int pinB = 4; //lampu biru
int inputPin = 32; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
int pos = 0;
#define SERVO 25
Servo servo;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED){
delay(100);
Serial.print(".");
}
Serial.println("WiFi Connected !");
Serial.println(WiFi.localIP());
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
//NTC
analogReadResolution(10);
//LED
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
//PIR
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(SERVO, OUTPUT);
servo.attach(SERVO);
servo.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
int analogValue = analogRead(35);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
delay(1000);
ThingSpeak.setField(1, celsius);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel sudah ter-update !");
}else {
Serial.println("Terjadi Masalah" + String(x));
}
val = digitalRead(inputPin); // read input value
if (val == HIGH) {
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
servo.write(120);
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
servo.write(0);
// We only want to print on the output change, not state
pirState = LOW;
}
}
if (celsius < 0){
analogWrite(pinR, LOW);
analogWrite(pinG, LOW);
analogWrite(pinB, HIGH);
delay(500);
}
else if (celsius == 0; celsius <= 38 ) {
analogWrite(pinR, LOW);
analogWrite(pinG, HIGH);
analogWrite(pinB, LOW);
delay(500);
}
else if (celsius > 38) {
analogWrite(pinR, HIGH);
analogWrite(pinG, LOW);
analogWrite(pinB, LOW);
delay(500);
}
}