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 = "";
- enum DevicePin {
- LED1 = 2,
- LED2 = 4,
- BUZZER = 5
- };
- WiFiClient client;
- void setupDevice(DevicePin pin) {
- pinMode(pin, OUTPUT);
- }
- void connectToWiFi() {
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nConnected to WiFi");
- }
- void Setup_ThingSpeak() {
- ThingSpeak.begin(client);
- }
- void Logika_LED_dan_Buzzer(String command, DevicePin pin, const char* deviceName) {
- if (command == (String(deviceName) + "_on")) {
- digitalWrite(pin, HIGH);
- Serial.println("Response: 200, " + String(deviceName) + " ON");
- } else if (command == (String(deviceName) + "_off")) {
- digitalWrite(pin, LOW);
- Serial.println("Response: 200, " + String(deviceName) + " OFF");
- }
- }
- void setup() {
- Serial.begin(115200);
- setupDevice(LED1);
- setupDevice(LED2);
- setupDevice(BUZZER);
- connectToWiFi();
- Setup_ThingSpeak();
- }
- void loop() {
- ThingSpeak.writeField(talkbackAPIKey, 1, 0, "command", "check", "txt");
- delay(5000);
- String response = ThingSpeak.readStringField(talkbackAPIKey, 2, "string");
- if (response.length() > 0) {
- Logika_LED_dan_Buzzer(response, LED1, "LED1");
- Logika_LED_dan_Buzzer(response, LED2, "LED2");
- Logika_LED_dan_Buzzer(response, BUZZER, "BUZZER");
- } else {
- Serial.println("No response from TalkBack");
- }
- delay(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement