• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Controlling AC Powered Appliances With ESP32 and IBM Watson IoT

By Abhishek Ghosh June 9, 2019 8:53 pm Updated on June 30, 2019

Controlling AC Powered Appliances With ESP32 and IBM Watson IoT

Advertisement

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.

Controlling AC Powered Appliances with ESP32 and IBM Watson IoT

Here is the code :

Vim
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 :

Advertisement

---

Vim
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” :

Vim
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:

Vim
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

This Article Has Been Shared 734 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Orthopaedic Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Controlling AC Powered Appliances With ESP32 and IBM Watson IoT

  • Send Basic Push Message from Arduino ESP32 using Blynk

    How to Send Basic Push Message from Arduino ESP32 using Blynk? With Blynk like web service & library, it is easy to create such basic project.

  • Connecting ESP32 Arduino with DHT11 with IBM Watson IoT

    Earlier, we described how to create graph on IBM Watson IoT dashboard by using the default widgets. In previous guide, we described how to use ESP32 Arduino with DHT11 sensor. Here is the Code and Diagram to Connect ESP32 Arduino with DHT11 with IBM Watson IoT and Get Odometer Like Gauges on Dashboard. For this […]

  • Scope of ESP32 in Commercial IoT Products

    Can ESP32 Used in Commercial IoT Products? Yes, ESP32 has modules and SoC to fit your production need. However, Arduino IDE is not for production.

  • Control ESP32 Arduino LED from IBM Watson IoT

    Here is How to Turn On and Off an LED Connected With ESP32 Arduino From IBM Watson IoT Using Simple Bash Scripts. You Can Use Relay Instead of LED.

  • M5Stack (ESP32 Arduino) : Modular, Stackable, Wearable IoT Platform

    M5Stack is ESP32 Arduino by heart. It is a modular, stackable, wearable IoT platform for development which is obviously Open Source.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

You can subscribe to our Free Once a Day, Regular Newsletter by clicking the subscribe button below.

Click To Subscribe

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (21K Followers)
  • Twitter (5.3k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • How to Build a DIY Water Level Indicator? April 16, 2021
  • How Startups Can Convince the Investors April 14, 2021
  • What to Know About the Cloud Storage Services for Smartphones April 13, 2021
  • WonderFox HD Video Converter Factory Pro Review April 10, 2021
  • What is the Maximum Cable Length Between Arduino/ESP32 and a Sensor April 8, 2021

 

About This Article

Cite this article as: Abhishek Ghosh, "Controlling AC Powered Appliances With ESP32 and IBM Watson IoT," in The Customize Windows, June 9, 2019, April 17, 2021, https://thecustomizewindows.com/2019/06/controlling-ac-powered-appliances-with-esp32-and-ibm-watson-iot/.

Source:The Customize Windows, JiMA.in

 

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Cookie Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2021 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy