uncategorized

Week 19- Project development

Smart Switch - S²

Since week 1, the week when we pen downed the idea, we have learned a lot. We gained knowledge of almost all aspects of product development, from planning, designing, electronic production, mechanical design etc to its ownership and legal matters. The coming two weeks we will be indulged in applying all these to develop our project. You can see the plans, expected timeline here.

I started off with outlining the work and dividing it into small chunks. These are what I came up with

  • Learn about ESP8266.
  • Look how to control the ESP externally using a microcontroller
  • design the switch’s structure.

Rough Sketch

I will divide each of my development into different chapters for easy classification.

Chapter: ESP8266

ESP8266-01

I have heard from numerous people about he compact and affordable WiFi module but haven’t worked with it once. So i had to start from scratch. Their are a bunch of online forums discussing and developing on ESP but they all seemed to be ambiguous and incomplete. My idea is to

  • have the ESP just for accessing a WiFi
  • use an external mmicrocontroller to control the device

When you look into the details, you will see that in the short span after its first appearance itself it has iterated number of times to form new, better refined versions. The list begins with ESP 01 and goes on till ESP 14, totally 16 boards. You can also see a lit of other derivative development board based on ESP in the market. Please have look at the wiki page for finer details.

The general/minimal features of the board are

  • 32-bit RISC CPU
  • 64 KiB of instruction RAM, 96 KiB of data RAM
  • External QSPI flash - 512 KiB to 4 MiB* (up to 16MiB is supported)
  • IEEE 802.11 b/g/n Wi-Fi: most have integrated antenna
  • minimum 2 and max 20 GPIO pins
  • UART on dedicated pins, plus a transmit-only UART can be enabled on GPIO2

Yeah, thats right it packs a punch !!

As we don’t have any ESP in our lab, i ordered one ESP8266-01 and 12E for playing with. But you can see the problem here is it has a good processor within itself, and most of the online info is for making use of that to program and control the board. But that is not what I need. I need a separate controller to control the ESP.

To start with accessing the modules, we can use AT commands serially transmitted to the ESP. You can get the list of AT commands here. Neil have also listed them with some good examples in here too.

After I got the ESP I started to test it by trying to communicate with it. ESP works in 3.3v and draws huge amount of current, even up to 300mA. So first I have to make a stable power supply for ESP. If you are going to ignore this, you will find your ESP restarting randomly. I had a 3.3v,1A buck with me and I used it to power the ESP. The buck was given the power from a FTDI(5V) cable. The pin diagram of an ESP-01 is ESP-01
Here the you have to connect the VCC and GND to that of the buck and Rx and Tx to that of FTDI cable. Make sure both the FTDI and ESP have common ground. For enabling the ESP connect the chip power down (CH_PD) to 3.3v.

Now connect the FTDI cable to your PC and open a serial monitor for communicating directly with the module. The default baudrate was 115200 in my case. Check different values till you are connected. when initialised or switched own you will notice a few strings shows up in your serial monitor and finally declares initialized. If you see anything else chances for wrong baudrate and/or powers upply is not stable. Also make sure that CR,LF line ending are enabled while sending the commands.

Some of the handy AT commands are

  • AT which replys READY
  • AT+RST which restarts the module
  • AT+GMR which will return the firmware version
  • AT+CIFSR which gives the current IP address of the ESP
  • AT+CIOBAUD=<baud rate> which can set the baudrate. make it accurate enough for the interfacing board. I changed mine to 9600.
  • AT+RESTORE this will restore the factory settings. This could be very useful if you somehow end up not knowing what all changes you made and is messed up.

There are more commands which you can try out. Try to connect to your Wifi, try to setup TCP/UDP clients and server. After juggling with the commands you will become familiarized. Once all the errors where sorted out and the communication stabilized I moved to the next step. NOte that completing this chapter alone took me 4 days, so don’t take it lightly.

Chapter: Communication

I decided to use Atmega 328P as the controller. It has dedicated USART pins for serial communication. I will make use of it talk with ESP. After fixing on that I began searching a protocol to communicate over the internet. Since my project works over internet, i have to follow certain protocols for communication and it is not easy as transmitting and receiving serially. The protocol is called Internet protocol or simply IP. The IP layers

You can look here for explanation on basics of networking. This could be of help if you are unfamiliar to the domain.

Thinking in the line of an IoT device i thought why not try out a IoT protocol for communication? If I am aiming to have the project in the IoT category I should aply its protocols too right? So I decided to delve into that topic.

When I started to look around and I almost ended up with MQTT every time. It is can be described as

“MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimize network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.”

Sounds great right ? I also chose o try out the same for the project. For MQTT the work flow is described with Methods. MQTT defines methods (sometimes referred to as verbs) to indicate the desired action to be performed on the identified resource. What this resource represents, whether pre-existing data or data that is generated dynamically, depends on the implementation of the server. Often, the resource corresponds to a file or the output of an executable residing on the server.

  1. Connect:
    Waits for a connection to be established with the server.
  2. Disconnect:
    Waits for the MQTT client to finish any work it must do, and for the TCP/IP session to disconnect.
  3. Subscribe:
    Waits for completion of the Subscribe or UnSubscribe method.
  4. UnSubscribe:
    Requests the server unsubscribe the client from one or more topics.
  5. Publish:
    Returns immediately to the application thread after passing the request to the MQTT client.

MQTT Stack

My plan is to run a MQTT client in a local server setup in Raspberry pi and then access it through the Static IP of our internet connection. The microcontroller will listen (subscribe) to a topic run in the Rpi server. For this first i have to have the following Things

  1. ESP as wifi access device: For this, after hours of research came across the Wifiesp. It is an arduino library fo having ESP connection similar to connecting to a WiFi shield. This wss a crucial part in communication as I was left in the dark as most of the already existing help was for directly programming the ESP, but my need was different. Installing the library is quite simple, pen the manage libraries under sketch->include library.Search for Wifiesp and install what shows up first.
  2. MQTT library for programming the controller: I have to have my controller to follow the methods of MQTT for proper communication. For that I chose to have PubSubClient. Which says to have support for the ethernet shield and all. WiFiesp is similar to the ethernet shield, so it is a leap in the dark hoping that both are cross compatible. Download the library from the
  3. A message broker for MQTT protocol: I chose to have Mosquitto as my broker. For trying out it in m system, which is Arch based, i installer the AUR package. In Rpi i have raspbian running with LAMP server configured. Since it is based on debian add the repository and install using apt-get.
  4. Have 328P added to boards in arduino for programming: After trying many failed attmpts i came across this. This worked for me and i was able to program with the external 20mhz oscillator. For having these boards in your list simply copy https://raw.githubusercontent.com/sleemanj/optiboot/master/dists/package_gogo_diy_atmega8_series_index.json and paste it in the additional boards manager URL’s box in preferences of the arduino IDE. Restart the IDE to have the boards listed.

Chapter: The mock up

I connected the ESP my arduino mega board and programmed the mega to send and receive serially. This setup will help in debugging the connection and also mimic my final setup. I tried the AT commands directly and made sure the communication is happening properly. Serial communication code used was

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
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(5, 6); // RX, TX
#endif

void setup() {
// initialise serial ports
Serial.begin(115200);
Serial1.begin(9600);
}

void loop() {
// send data from wifi to board:
if (Serial.available())
{
int inByte = Serial.read();
Serial.write(inByte);
}

// send data vice-versa
if (Serial1.available())
{
int inByte = Serial1.read();
Serial.write(inByte);
}

}

After checking this I tried to write code for MQTT communication with the Rpi server. Started writing code combiing fuctions from Wifiesp and PubSubClient libraries. Here is what i came up with

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
#include "WiFiEsp.h"
#include <WiFiEspClient.h>
#include <PubSubClient.h>

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

IPAddress server(192, 168, 11, 200); // adress of my Rpi servere, give the dress of your server here
char ssid[] = "abcd"; // your network SSID (name)
char pass[] = "***"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

//print any message received for subscribed topic
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();
}

// Initialize the Ethernet client object
WiFiEspClient espClient;
PubSubClient client(server, 1883, callback, espClient); //1833 is the default port of MQTT

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect, just a name to identify the client
if (client.connect("smartswitch")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("/world","dinkan");
// ... and resubscribe
client.subscribe("/hello");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);

// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}

// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}

// you're connected now, so print out the data
Serial.println("You're connected to the network");

//connect to MQTT server
//client.setServer(server, 5000);
//client.setCallback(callback);
}


void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();
}

Successfully uploaded the code and opened the serial port to see the progress. But there were two errors firstly the connections terminates and reconnects periodically and secondly it is not able to listen to topics !. I tried to debug the problem but all were in vain. So I paused development of this chapter for the time being.

Chapter: UDP server

This is my next option for communicating over internet. We can setup a UDP server in the esp using AT commands. When the IP and Port is given it can receive and send data seamlessly.

After many tries I wrote the following code for setting up the UDP server and to take actions according to the received data.

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
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>

char ssid[] = "abcd"; // your network SSID (name)
char pass[] = "*****"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's statu

unsigned int localPort = 5000; // local port to listen on

char packetBuffer[255]; // buffer to hold incoming packet
char ReplyBuffer[] = "yuppee"; // a string to send back

WiFiEspUDP Udp;

void setup() {

Serial.begin(9600);
WiFi.init(&Serial);

while ( status != WL_CONNECTED) {
Serial.write("AT+CWMODE=1");
delay(500);
status = WiFi.begin(ssid, pass);
}

Udp.begin(localPort);
pinMode (13, OUTPUT);
}

void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {

// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
if (strcmp (packetBuffer, "ON") == 0){
digitalWrite(13, HIGH);
}
else if (strcmp (packetBuffer, "OFF") == 0){
digitalWrite(13, LOW);
}

// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}

The above code will start a UDP server with the IP of the ESP listening to port 5000 and switch ON the LED connected to pin 13 when the string ON is received and turns it OFF when the string OFF is received. Now we can discuss how to connect to the ESP and send ON & OFF. We have to start a UDP client for establishing communication. I used python to have a UPD client which can do these tasks. 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
import socket

UDP_IP = "00.000.00.00" #your servers IP
UDP_PORT = "5000" #your servers Port
MESSAGE = raw_input('ON or OFF?\r\n')
try:

#Send Data
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE

sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

#recieve data
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data

finally:
sock.close()

Compile and run this code. It will ask for entering ON or OFF, enter the necessary command. Vola! and that’s it. I was successful in making the LED on and off.

Chapter: Electronics production

I started off by having the controller + esp in one board and the AC load switching as another board.

Controller + ESP8266

Even though I tried out with ESP8266-01 i designed for model 12E. the board comprises of

  • Atmega 328P
  • ESP8266-12E
  • Control lines to AC switching circuit.
  • some pinouts for debugging
  • the 3.3v buck
  • Vreg for converting 9V from battery to 5V for Controller and Buck

First Board layout
Board

After soldering on the components and i was not able to get proper output. While trying to desolder the ESP8266 it got damaged and I had to keep aside this board. Instead i decided to have the controller, esp and the AC switching circuit separately.

Controller alone

It has just the 328p and output lines to ESP board and control lines to AC switching circuit.
Controller alone

I plan to remove the ISP programming header after its use.

ESP8266 alone

After i damaged the first ESP 12E ESP 01 is what is left. Do i designed a board to accomodate the buck and ESP.
ESP alone

This will be connected to the controller board.

The controller and ESP board in their final look.

AC switching

The design of this itself took great time than anticipated. First i thought about using MOSFET for controlling the AC which failed miserably, then i tried TRIAC which also failed. The failure of MOSFET I believe is because i had P-MOSFET and I designed for N-MOSFET. While with triac I believe it is due to the lack of load between input and output connections. Finally I decided to have the mechanical relay for switching the circuit. The design process of the same and details are in week 13 . The MOSFET and TRIAC boards where actually blown up causing the CB’s to trip !! :D

MOSFET board
The blown board, note the capacitors and the connectors are removed

TRIAC Board
Burned triac

Relay Board

Chapter: Programming

After getting the board ready for programming, modify the written code for the new controller upload it. Here is the final arduino code of the controller

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
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>

char ssid[] = "abcd"; // your network SSID (name)
char pass[] = "*****"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's statu

unsigned int localPort = 5000; // local port to listen on

char packetBuffer[255]; // buffer to hold incoming packet
char ReplyBuffer[] = "yuppee"; // a string to send back

WiFiEspUDP Udp;

void setup() {

Serial.begin(9600);
WiFi.init(&Serial);

while ( status != WL_CONNECTED) {
Serial.write("AT+CWMODE=1");
delay(500);
status = WiFi.begin(ssid, pass);
}

Udp.begin(localPort);
pinMode (7, OUTPUT); //relay
pinMode (8, OUTPUT); //LED
}

void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {

// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
if (strcmp (packetBuffer, "ON") == 0){
digitalWrite(7, HIGH); //to relay
digitalWrite(8, HIGH); //to LED
}
else if (strcmp (packetBuffer, "OFF") == 0){
digitalWrite(7, LOW); //to relay
digitalWrite(8, LOW); //to LED
}

// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}

Completed boards along with connections

Chapter: Switch design

I am almost exhausted of time before final presentation. I hastily designed an enclosure which was laser cut and press fitted. It has a lid which can be opened. Also design the adapter which holds the leads for plugging into the plug point.

Design Version 1


Part of the socket-version1

The work in pipe line

Since the presentation, I have been working on a new socket design. It is a work in progress and not yet finished. I hope to complete it in the near future so as to have a visually appealing compact design.

Learning outcome

The two week I spend on the project taught me many valuable leas sons, not just technical but managerial too. Had to manage the time and resources so as to finish the project. The only board I did before these weeks is the relay board which I made for output device week. Everything else had been made in the last and present week. The time calculated by me for interfacing ESP and making the AC switch was very inaccurate. Debugging took up most of my time. This further resulted in the shortcomings of the physical design of the socket. For planning the Academy i have been using Trello which is a great tool for keeping track of your activities. I am a person who procastinates very much but it helped me to reach here.

License

The MIT License (MIT)

Copyright (c) 2016 Vishnu Easwaran E

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

All my design files

link to - Presentation Slide

link to - Presentation Video

Share