Smart Helmet
Smart Helmet
connected to Easy IoT Cloud with four relays and one ESP8266. We will use the Arduino
IDE to program the ESP8266.
Devices can be controlled over the Easy IoT Cloud interface or the native Android
application.
If you don't have any experience and aren't qualified to work with MAINS, I wouldn't
encourage you to play around!
Do NOT use this without proper knowledge about MAIN circuits and without a proper
FUSE.
Max current for solid state in this tutorial is 2A — suitable for room light only.
Introduction
Materials
ESP8266 Wi-Fi module
Relay module
3.3V power supply
4.7K Resistor
NPN Transistor TO-92 2N2222
In the program, change the Easy IoT Cloud username and password:
If you use different DO pins than the default D0, D1, D2, D3, change those pins.
In the program, also change the module ID to correspond to those in the user interface:
#define MODULE_ID_1 1
#define MODULE_ID_2 2
#define MODULE_ID_3 3
#define MODULE_ID_4 4
#include <ESP8266WiFi.h>
#include <MQTT.h>
#define AP_SSID "xxx"
#define AP_PASSWORD "xxx"
#define EIOTCLOUD_USERNAME "xxx"
#define EIOTCLOUD_PASSWORD "xxx"
// create MQTT object
#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com"
#define DO_TOPIC "/Sensor.Parameter1"
#define PIN_DO_1 D0 // DO pin1
#define MODULE_ID_1 1
#define PIN_DO_2 D1 // DO pin2
#define MODULE_ID_2 2
#define PIN_DO_3 D2 // DO pin3
#define MODULE_ID_3 3
#define PIN_DO_4 D3 // DO pin4
#define MODULE_ID_4 4
MQTT myMqtt("", EIOT_CLOUD_ADDRESS, 1883);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(AP_SSID, AP_PASSWORD);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(AP_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
};
Serial.println("WiFi connected");
Serial.println("Connecting to MQTT server");
//set client id
// Generate client name based on MAC address and last 8 bits of
microsecond counter
String clientName;
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
myMqtt.setClientId((char*) clientName.c_str());
Serial.print("MQTT client id:");
Serial.println(clientName);
// setup callbacks
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);
//////Serial.println("connect mqtt...");
myMqtt.setUserPwd(EIOTCLOUD_USERNAME, EIOTCLOUD_PASSWORD);
myMqtt.connect();
delay(500);
pinMode(PIN_DO_1, OUTPUT);
pinMode(PIN_DO_2, OUTPUT);
pinMode(PIN_DO_3, OUTPUT);
pinMode(PIN_DO_4, OUTPUT);
subscribe();
}
void loop() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void subscribe()
{
myMqtt.subscribe("/" + String(MODULE_ID_1) + DO_TOPIC); //DO 1
myMqtt.subscribe("/" + String(MODULE_ID_2) + DO_TOPIC); //DO 2
myMqtt.subscribe("/" + String(MODULE_ID_3) + DO_TOPIC); //DO 3
myMqtt.subscribe("/" + String(MODULE_ID_4) + DO_TOPIC); //DO 4
}
void myConnectedCb() {
Serial.println("connected to MQTT server");
subscribe();
}
void myDisconnectedCb() {
Serial.println("disconnected. try to reconnect...");
delay(500);
myMqtt.connect();
}
void myPublishedCb() {
Serial.println("published.");
}
void myDataCb(String& topic, String& data) {
if (topic == String("/"+String(MODULE_ID_1)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_1, HIGH);
else
digitalWrite(PIN_DO_1, LOW);
Serial.print("Do 1:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_2)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_2, HIGH);
else
digitalWrite(PIN_DO_2, LOW);
Serial.print("Do 2:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_3)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_3, HIGH);
else
digitalWrite(PIN_DO_3, LOW);
Serial.print("Do 3:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_4)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_4, HIGH);
else
digitalWrite(PIN_DO_4, LOW);
Serial.print("Do 4:");
Serial.println(data);
}
}
The easiest way to test program is to use an ESP8266 LUA node MCU board. Just connect it
via USB port and you can test the program. If you use this board, you can also uncomment
#define DEBUG to see debug messages. The Flash button on the ESP8266 board will work
as a button to manually change switch state. The on board LED displays the switch state.
Hardware
In our case, we use NODEMCU ESP8266, but you can use any other type of ESP8266 with
all pins exposed. In our program, we connect D0, D1, D2, and D3 in a circuit, which controls
the relays. The relays are using 5V. ESP8266 is using 3.3V. To connect the 3.3V ESP8266 to
the 5V relay module, we use one NPN transistor 2N2222. The connection is shown below:
The GND is common, the ESP8266 is on 3.3V, and the relay module is on 5V. You can use a
relay module with more relays (2, 4, 8).
https://www.amazon.in/Headfox-Signal-Indicator-Light-
Helmet/dp/B07H1VN1CJ/ref=sr_1_5?ie=UTF8&qid=1541754594&sr=8-
5&keywords=headfox+helmet
https://www.amazon.in/Tradico%C2%AE-Bracketless-Windshield-Wiper-2012-
2014/dp/B07D94GWDP/ref=sr_1_fkmr0_4?ie=UTF8&qid=1541755307&sr=8-4-
fkmr0&keywords=wiper+for+helmet+with+motor
And there's your switch! As a note, modules can be manually renamed in Easy IoT Cloud
configuration to suit your needs!