Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <ThingSpeak.h>
- const char *ssid = "";
- const char *password = "";
- const char *talkbackAPIKey = "";
- const int pinLED1 = 2; // Pin GPIO untuk LED 1
- const int pinLED2 = 4; // Pin GPIO untuk LED 2
- const int pinBuzzer = 5; // Pin GPIO untuk Buzzer
- WiFiClient klien;
- void setup() {
- Serial.begin(115200);
- pinMode(pinLED1, OUTPUT);
- pinMode(pinLED2, OUTPUT);
- pinMode(pinBuzzer, OUTPUT);
- // Terhubungkan ke WiFi
- WiFi.begin(ssid, password);
- Serial.print("Menghubungkan ke WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nTerhubung ke WiFi");
- // Inisialisasi ThingSpeak
- ThingSpeak.begin(klien);
- }
- void loop() {
- // Periksa perintah dari TalkBack
- ThingSpeak.writeField(talkbackAPIKey, 1, 0, "command", "check", "txt");
- // Tunggu respon
- delay(5000);
- // Periksa respon dari TalkBack
- String respon = ThingSpeak.readStringField(talkbackAPIKey, 2, "string");
- if (respon.length() > 0) {
- if (respon == "led1_on") {
- digitalWrite(pinLED1, HIGH); // Nyalakan LED 1
- Serial.println("Mendapatkan Respon : 200, LED 1 ON");
- } else if (respon == "led1_off") {
- digitalWrite(pinLED1, LOW); // Matikan LED 1
- Serial.println("Mendapatkan Respon : 200, LED 1 OFF");
- } else if (respon == "led2_on") {
- digitalWrite(pinLED2, HIGH); // Nyalakan LED 2
- Serial.println("Mendapatkan Respon : 200, LED 2 ON");
- } else if (respon == "led2_off") {
- digitalWrite(pinLED2, LOW); // Matikan LED 2
- Serial.println("Mendapatkan Respon : 200, LED 2 OFF");
- } else if (respon == "buzzer_on") {
- digitalWrite(pinBuzzer, HIGH); // Nyalakan Buzzer
- Serial.println("Mendapatkan Respon : 200, BUZZER ON");
- } else if (respon == "buzzer_off") {
- digitalWrite(pinBuzzer, LOW); // Matikan Buzzer
- Serial.println("Mendapatkan Respon : 200, BUZZER OFF");
- } else {
- Serial.println("Mendapatkan Respon : 404, Perintah tidak valid");
- }
- } else {
- Serial.println("Tidak ada respon dari TalkBack");
- }
- delay(10000); // Tunggu 10 detik sebelum memeriksa lagi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement