/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/63dafb25-f9b2-41d3-865e-c5da310779b1
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool servo;
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"
#include <ESP32Servo.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "06f717fd-96fa-4eff-9940-c39ec4373345";
const char SSID[] = "Wokwi-GUEST"; // Network SSID (name)
const char PASS[] = ""; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = "Rxj5V7An7f?8t??7kCb5jkPRm"; // Secret device password
void onServoChange();
bool servo;
Servo myServo; // สร้างออบเจ็กต์ Servo
const int buttonPin = 19; // พินของสวิตช์
const int servoPin = 14; // พินของ Servo
int buttonState = 0; // สถานะของสวิตช์
int servoPos = 0; // ตำแหน่งของ Servo
void setup() {
pinMode(buttonPin, INPUT); // ตั้งค่าพินของสวิตช์เป็น input
myServo.attach(servoPin); // เชื่อมต่อ Servo กับพิน
Serial.begin(9600);
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();
buttonState = digitalRead(buttonPin); // อ่านสถานะของสวิตช์
if (buttonState == HIGH) { // ถ้าสวิตช์ถูกกด
myServo.write(90); // หมุน Servo ไปที่ 90 องศา (ตำแหน่งเปิด)
delay(2000); // หน่วงเวลา 1 วินาที (คุณสามารถปรับได้ตามต้องการ)
myServo.write(0); // หมุน Servo กลับไปที่ 0 องศา (ตำแหน่งปิด)
}
}
}
/*
Since Servo is READ_WRITE variable, onServoChange() is
executed every time a new value is received from IoT Cloud.
*/
void onServoChange() {
// Add your code here to act upon Servo change
}