Advertisement
mudhita_triari

ESP32 menggunakan talkback pada thinkspeak iot platform v.1

Apr 2nd, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <ThingSpeak.h>
  3.  
  4. const char *ssid = "";
  5. const char *password = "";
  6. const char *talkbackAPIKey = "";
  7.  
  8. const int pinLED1 = 2;      // Pin GPIO untuk LED 1
  9. const int pinLED2 = 4;      // Pin GPIO untuk LED 2
  10. const int pinBuzzer = 5;    // Pin GPIO untuk Buzzer
  11.  
  12. WiFiClient klien;
  13.  
  14. void setup() {
  15.   Serial.begin(115200);
  16.   pinMode(pinLED1, OUTPUT);
  17.   pinMode(pinLED2, OUTPUT);
  18.   pinMode(pinBuzzer, OUTPUT);
  19.  
  20.   // Terhubungkan ke WiFi
  21.   WiFi.begin(ssid, password);
  22.   Serial.print("Menghubungkan ke WiFi");
  23.   while (WiFi.status() != WL_CONNECTED) {
  24.     delay(500);
  25.     Serial.print(".");
  26.   }
  27.   Serial.println("\nTerhubung ke WiFi");
  28.  
  29.   // Inisialisasi ThingSpeak
  30.   ThingSpeak.begin(klien);
  31. }
  32.  
  33. void loop() {
  34.   // Periksa perintah dari TalkBack
  35.   ThingSpeak.writeField(talkbackAPIKey, 1, 0, "command", "check", "txt");
  36.  
  37.   // Tunggu respon
  38.   delay(5000);
  39.  
  40.   // Periksa respon dari TalkBack
  41.   String respon = ThingSpeak.readStringField(talkbackAPIKey, 2, "string");
  42.  
  43.   if (respon.length() > 0) {
  44.     if (respon == "led1_on") {
  45.       digitalWrite(pinLED1, HIGH); // Nyalakan LED 1
  46.       Serial.println("Mendapatkan Respon : 200, LED 1 ON");
  47.     } else if (respon == "led1_off") {
  48.       digitalWrite(pinLED1, LOW); // Matikan LED 1
  49.       Serial.println("Mendapatkan Respon : 200, LED 1 OFF");
  50.     } else if (respon == "led2_on") {
  51.       digitalWrite(pinLED2, HIGH); // Nyalakan LED 2
  52.       Serial.println("Mendapatkan Respon : 200, LED 2 ON");
  53.     } else if (respon == "led2_off") {
  54.       digitalWrite(pinLED2, LOW); // Matikan LED 2
  55.       Serial.println("Mendapatkan Respon : 200, LED 2 OFF");
  56.     } else if (respon == "buzzer_on") {
  57.       digitalWrite(pinBuzzer, HIGH); // Nyalakan Buzzer
  58.       Serial.println("Mendapatkan Respon : 200, BUZZER ON");
  59.     } else if (respon == "buzzer_off") {
  60.       digitalWrite(pinBuzzer, LOW); // Matikan Buzzer
  61.       Serial.println("Mendapatkan Respon : 200, BUZZER OFF");
  62.     } else {
  63.       Serial.println("Mendapatkan Respon : 404, Perintah tidak valid");
  64.     }
  65.   } else {
  66.     Serial.println("Tidak ada respon dari TalkBack");
  67.   }
  68.  
  69.   delay(10000); // Tunggu 10 detik sebelum memeriksa lagi
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement