Neuraplug – Use our smart assistant in order to manage a traditional plug

Welcome to the first article Neurafy laboratory Post. With this article you will use Neurafy in daily situations. Specially during this first post we will learn how to control a plug with our “virtual assistant”.

Firstly, we must have material required, with the aim of becames traditional plug to smart:

  • Arduino module with wifi with 1 GPIO and  ADC (NODEMCU V3 ESP8266 or similar)

  • Relay module (KY-019 or similar)

  • AC / DC power source –> Vout = 5V Iout = 500mA (any mobile charger)

  • Hall sensor module (ACS711LC or similar)

On the other hand, you can use our demo button (AÑADIR LINK), or if you prefer using a telegram looking for the nfylab user.

Having said that, we will proceed to assemble the HW. Herebelow, you will see a block diagram to have a global vision of what we are going to do:

Once the hardware is connected, we must load the following program in the arduino module:

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

const char* ssid = “SSID de tu wifi“;

const char* password = “PASS de tu wifi“;

const char* mqtt_server = “mqtt.neurafy.com”;

const char* mqttUser = “nfylab”;

const char* mqttPassword = “nfylab”;

const int mqttPort = 1883;

void setup_wifi() {

delay(10);

// We start by connecting to a WiFi network

Serial.println();

Serial.print(“Connecting to “);

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(“.”);

}

randomSeed(micros());

Serial.println(“”);

Serial.println(“WiFi connected”);

Serial.println(“IP address: “);

Serial.println(WiFi.localIP());

}

void callback(char* topic, byte* payload, unsigned int length) {

Serial.print(“Message arrived [“);

Serial.print(topic);

Serial.print(“] “);

for (int i = 0; i < length; i++) {

Serial.print((char)payload[i]);

}

Serial.println();

if ((char)payload[0] == ‘0’) {

digitalWrite(5, LOW);

} else {

digitalWrite(5, HIGH); // Turn on relay

}

}

void reconnect() {

// Loop until we’re reconnected

while (!client.connected()) {

Serial.print(“Attempting MQTT connection…”);

// Create a random client ID

String clientId = “NFY-“;

clientId += String(random(0xffff), HEX);

// Attempt to connect

if (client.connect(clientId.c_str(), mqttUser, mqttPassword )) {

Serial.println(“connected”);

// Once connected, publish an announcement…

client.publish(“/nfylab/youremail“, “plug activado”);

// … and resubscribe

client.subscribe(“/nfylab/youremail “);

} else {

Serial.print(“failed, rc=”);

Serial.print(client.state());

Serial.println(” try again in 5 seconds”);

// Wait 5 seconds before retrying

delay(8000);

}

}

}

void setup() {

pinMode(5, OUTPUT);

digitalWrite(5, 0);

Serial.begin(115200);

setup_wifi();

client.setServer(mqtt_server, mqttPort);

client.setCallback(callback);

}

void loop() {

if (!client.connected()) {

reconnect();

}

client.loop();

long now = millis();

if (now – lastMsg > 2000) {

lastMsg = now;

++value;

}

delay(1000);

}

 

YOU ARE READY!, Now you can talk to your plug 24 hours a day, 7 days a week. Thanks to the NLP (Natural Language processing) your plug can understand the natural language of people.

Neurafy is happy to help you!