It is an upgrade to our previous article to control ESP32 Arduino LED from IBM Watson IoT. We already talked about Android Apps for Watson IoT for this purpose. We already have an article to use relay with Arduino. Our linked relay guide should be enough for anyone to understand how to use relay and AC connections. These relay modules will create mechanical clicking sound like switch. My relay works with 3.3v of ESP32 (it is rated for 5v) when controlled from internet/smartphone, however the pushbutton makes the thing puzzled. Using a logic level shifter will end the puzzling situation – it will convert 3.3V logic to that of 5V.

Here is the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #include <WiFi.h> #include <PubSubClient.h> #include <Ticker.h> #include <HTTPClient.h> #include <SPI.h> const char* ssid = "yourNetworkName"; const char* password = "yourNetworkPass"; //-------- Customise these values ----------- #define ORG "change" #define DEVICE_TYPE "change" #define DEVICE_ID "change" #define TOKEN "change" //-------- Customise the above values -------- #define DEVICE_BUTTON 0 #define DEVICE_RELAY 19 #define DEVICE_GREEN_LED 18 #define DEVICE_RED_LED 4 Ticker ledBlinker; char server[] = ORG ".messaging.internetofthings.ibmcloud.com"; char authMethod[] = "use-token-auth"; char token[] = TOKEN; char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID; #define CMD_STATE "/gpio/" // use the '+' wildcard so it subscribes to any command with any message format const char commandTopic[] = "iot-2/cmd/+/fmt/+"; void gotMsg(char* topic, byte* payload, unsigned int payloadLength); WiFiClient wifiClient; PubSubClient client(server, 1883, gotMsg, wifiClient); int buttonPressDuration; void setup() { Serial.begin(115200); Serial.println(); pinMode(DEVICE_RELAY, OUTPUT); pinMode(DEVICE_GREEN_LED, OUTPUT); pinMode(DEVICE_RED_LED, OUTPUT); ledBlinker.attach(0.1, ledBlink); // fast blink indicates Wifi connecting wifiConnect(); ledBlinker.attach(0.4, ledBlink); // slower blink indicates MQTT connecting mqttConnect(); ledBlinker.detach(); digitalWrite(DEVICE_GREEN_LED, LOW); // low is led on to show connected pinMode(DEVICE_BUTTON, INPUT); attachInterrupt(DEVICE_BUTTON, buttonPress, CHANGE); } int lastHeartBeat; void loop() { if (buttonPressDuration > 0) { doCommand(digitalRead(DEVICE_RELAY) ? "off" : "on"); buttonPressDuration = 0; } if (!client.loop()) { mqttConnect(); } if (millis()-lastHeartBeat > 10000) { Serial.print("loop: gpio "); Serial.print(DEVICE_RELAY); Serial.print(" current state "); Serial.println(digitalRead(DEVICE_RELAY) ? "On" : "Off"); digitalWrite(DEVICE_GREEN_LED, HIGH); // flicker LED to show its active delay(200); digitalWrite(DEVICE_GREEN_LED, LOW); lastHeartBeat = millis(); } } void gotMsg(char* topic, byte* payload, unsigned int payloadLength) { Serial.print("gotMsg: invoked for topic: "); Serial.println(topic); if (String(topic).indexOf(CMD_STATE) > 0) { String cmd = ""; for (int i=0; i<payloadLength; i++) { cmd += (char)payload[i]; } doCommand(cmd); } else { Serial.print("gotMsg: unexpected topic: "); Serial.println(topic); } } void doCommand(String cmd) { int currentState = digitalRead(DEVICE_RELAY); int newState = (cmd == "on"); digitalWrite(DEVICE_RELAY, newState); Serial.print("Relay switched from "); Serial.print(currentState ? "On" : "Off");Serial.print(" to "); Serial.println(newState ? "On" : "Off"); } unsigned long startPress = 0; void buttonPress() { int currentState = digitalRead(DEVICE_BUTTON); if (currentState == 0) { // 0 is pressed, 1 is released startPress = millis(); } else { int diff = millis() - startPress; if (diff > 100) { // debounce buttonPressDuration = diff; } } Serial.print("Button "); Serial.print(currentState ? "released" : "pressed"); Serial.print(" duration="); Serial.println(buttonPressDuration); } void ledBlink() { digitalWrite(DEVICE_GREEN_LED, ! digitalRead(DEVICE_GREEN_LED)); } void wifiConnect() { Serial.print("Connecting to "); Serial.print(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("\nWiFi connected, IP address: "); Serial.println(WiFi.localIP()); } void mqttConnect() { if (!!!client.connected()) { Serial.print("Reconnecting MQTT client to "); Serial.println(server); while (!!!client.connect(clientId, authMethod, token)) { Serial.print("."); delay(500); } Serial.println(); } subscribeTo(commandTopic); } void subscribeTo(const char* topic) { Serial.print("subscribe to "); Serial.print(topic); if (client.subscribe(topic)) { Serial.println(" OK"); } else { Serial.println(" FAILED"); } } |
Look at our repository for code, you will realize that we have two separate cURL commands to send the LED/Relay to on and off :
---
1 2 3 4 5 6 7 | # turns the device on curl -u <use-the-API-Key>:<use-auth-token> -H "Content-Type: text/plain" -v -X POST http://<your org>.messaging.internetofthings.ibmcloud.com:1883/api/v0002/application/types/<yourDeviceType>/devices/<yourDeviceId>/commands/gpio -d "on" # turn the device off curl -u <use-the-API-Key>:<use-auth-token> -H "Content-Type: text/plain" -v -X POST http://<your org>.messaging.internetofthings.ibmcloud.com:1883/api/v0002/application/types/<yourDeviceType>/devices/<yourDeviceId>/commands/gpio -d "off" |
There is a nice free application named “HTTP Request Shortcuts” :
1 2 3 | # https://play.google.com/store/apps/details?id=ch.rmy.android.http_shortcuts&hl=en_US # |
It has the option to import cURL commands.
Place shortcuts (widgets) on your home screen to submit HTTP requests to all your favourite RESTful APIs, web services and other URL resources. It has the option to add custom icons for shortcuts too. It is designed to control your home automation system and IoT devices using Raspberry Pi and Arduino. Most importantly, the app is open source, it is available on GitHub:
1 2 3 4 5 | # https://github.com/Waboodoo/HTTP-Shortcuts # |
You can fork the repo, customize the app on Android Studio and develop your custom version.
Tagged With AC IBM Watson , ac switch with esp32 , esp32 arduino wireless turning on appliances