i have already made this project previuosly using arduino uno, wiznet w5100 ethernet shield and 4 channel relay shield. But using Arduino nano + ENC29J60 lot cheaper. not mention it has smaller size. Once you get this badboy online on your network, you can control every device in your house using your smartphone or any gadget you have(that rely on your coding skill, ofcourse).
let’s the fun begin
wait, as usual i don’t do lots of talk so here’s the schematic and arduino code.
1 | #include <UIPEthernet.h> |
2 |
3 | EthernetServer server = EthernetServer(23); |
4 | boolean alreadyConnected = false ; // whether or not the client was connected previously |
5 | String commandString; |
6 | String commandStringTmp; |
7 |
8 | int ledPinOne = 3; |
9 | int ledPinTwo = 4; |
10 | int ledPinThree = 5; |
11 | int ledPinFour = 6; |
12 | int val; |
13 |
14 | void setup() |
15 | { |
16 | pinMode(ledPinOne, OUTPUT); // sets the digital pin 1 as output |
17 | pinMode(ledPinTwo, OUTPUT); // sets the digital pin 1 as output |
18 | pinMode(ledPinThree, OUTPUT); // sets the digital pin 1 as output |
19 | pinMode(ledPinFour, OUTPUT); // sets the digital pin 1 as output |
20 | digitalWrite(ledPinOne, HIGH); |
21 | digitalWrite(ledPinTwo, HIGH); |
22 | digitalWrite(ledPinThree, HIGH); |
23 | digitalWrite(ledPinFour, HIGH); |
24 | |
25 | //Serial.begin(9600); |
26 |
27 | uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; |
28 | IPAddress myIP(192,168,200,36); |
29 | IPAddress gateway(192,168,200,18); |
30 |
31 | Ethernet.begin(mac,myIP,gateway); |
32 |
33 | server.begin(); |
34 | } |
35 |
36 | void loop() { |
37 | // wait for a new client: |
38 | EthernetClient client = server.available(); |
39 |
40 | // when the client sends the first byte, say hello: |
41 | if (client) { |
42 | if (!alreadyConnected) { |
43 | // clear out the input buffer: |
44 | client.flush(); |
45 | commandString = "" ; //clear the commandString variable |
46 |
47 | //server.println("--> Please type your command and hit Return..."); |
48 | alreadyConnected = true ; |
49 | } |
50 |
51 | while (client.available()) { |
52 | // read the bytes incoming from the client: |
53 | char newChar = client.read(); |
54 |
55 | if (newChar == 0x0D) //If a 0x0D is received, a Carriage Return, then evaluate the command |
56 | { |
57 | //server.print("Received this command: "); |
58 | //server.println(commandString); |
59 | //if (commandString.indexOf("quit") > -1){ |
60 | commandStringTmp = commandString; |
61 | commandStringTmp.replace( "\n" , "" ); |
62 | commandStringTmp.replace( "\r" , "" ); |
63 | if (commandStringTmp.equals( "quit" )) { |
64 | server.println(); |
65 | server.print( "disconnecting\r\n" ); |
66 | client.stop(); |
67 | } else { |
68 | processCommand(commandString); |
69 | } |
70 | } else |
71 | { |
72 | //Serial.println(newChar); |
73 | commandString += newChar; |
74 | } |
75 |
76 | } |
77 | } |
78 | } |
79 |
80 | void processCommand(String command) |
81 | { |
82 | command.replace( "" , "" ); |
83 | command.replace( "\r" , "" ); |
84 | //server.println("Processing command "); |
85 | //server.println(command); |
86 | |
87 | // ONE |
88 | if (command.equals( "ledoneon" )) { |
89 | //server.println("LED 1 On command received"); |
90 | digitalWrite(ledPinOne, LOW); // sets the LED one on |
91 | //server.println("LED 1 was turned on"); |
92 | commandString = "" ; |
93 | return ; |
94 | } |
95 | |
96 | |
97 | if (command.equals( "ledoneoff" )) { |
98 | //server.println("LED 1 Off command received"); |
99 | digitalWrite(ledPinOne, HIGH); // sets the LED one off |
100 | //server.println("LED 1 was turned off"); |
101 | commandString = "" ; |
102 | return ; |
103 | } |
104 |
105 | // TWO |
106 | if (command.equals( "ledtwoon" )) { |
107 | //server.println("LED 2 On command received"); |
108 | digitalWrite(ledPinTwo, LOW); // sets the LED one on |
109 | //server.println("LED 2 was turned on"); |
110 | commandString = "" ; |
111 | return ; |
112 | } |
113 | |
114 | if (command.equals( "ledtwooff" )) { |
115 | //server.println("LED 2 Off command received"); |
116 | digitalWrite(ledPinTwo, HIGH); // sets the LED one off |
117 | //server.println("LED 2 was turned off"); |
118 | commandString = "" ; |
119 | return ; |
120 | } |
121 |
122 | // THREE |
123 | if (command.equals( "ledthreeon" )) { |
124 | //server.println("LED 3 On command received"); |
125 | digitalWrite(ledPinThree, LOW); // sets the LED one on |
126 | //server.println("LED 3 was turned on"); |
127 | commandString = "" ; |
128 | return ; |
129 | } |
130 | |
131 | if (command.equals( "ledthreeoff" )) { |
132 | //server.println("LED 3 Off command received"); |
133 | digitalWrite(ledPinThree, HIGH); // sets the LED one off |
134 | //server.println("LED 3 was turned off"); |
135 | commandString = "" ; |
136 | return ; |
137 | } |
138 |
139 | // FOUR |
140 | if (command.equals( "ledfouron" )) { |
141 | //server.println("LED 4 On command received"); |
142 | digitalWrite(ledPinFour, LOW); // sets the LED one on |
143 | //server.println("LED 4 was turned on"); |
144 | commandString = "" ; |
145 | return ; |
146 | } |
147 | |
148 | if (command.equals( "ledfouroff" )) { |
149 | //server.println("LED 4 Off command received"); |
150 | digitalWrite(ledPinFour, HIGH); // sets the LED one off |
151 | //server.println("LED 4 was turned off"); |
152 | commandString = "" ; |
153 | return ; |
154 | } |
155 |
156 | // STATUS |
157 | // ONE |
158 | if (command.equals( "ledonestatus" )) { |
159 | //server.println("LED One status command received"); |
160 | val = digitalRead(ledPinOne); // sets the LED two on |
161 | server.print( "LED 1 was turned " ); |
162 | if (val == 0) { |
163 | server.println( "on" ); |
164 | } else { |
165 | server.println( "off" ); |
166 | } |
167 | commandString = "" ; |
168 | return ; |
169 | } |
170 | |
171 | // TWO |
172 | if (command.equals( "ledtwostatus" )) { |
173 | //server.println("LED Two status command received"); |
174 | val = digitalRead(ledPinTwo); // sets the LED two on |
175 | server.print( "LED 2 was turned " ); |
176 | if (val == 0) { |
177 | server.println( "on" ); |
178 | } else { |
179 | server.println( "off" ); |
180 | } |
181 | commandString = "" ; |
182 | return ; |
183 | } |
184 |
185 | // THREE |
186 | if (command.equals( "ledthreestatus" )) { |
187 | //server.println("LED Three status command received"); |
188 | val = digitalRead(ledPinThree); // sets the LED two on |
189 | server.print( "LED 3 was turned " ); |
190 | if (val == 0) { |
191 | server.println( "on" ); |
192 | } else { |
193 | server.println( "off" ); |
194 | } |
195 | commandString = "" ; |
196 | return ; |
197 | } |
198 |
199 | // FOUR |
200 | if (command.equals( "ledfourstatus" )) { |
201 | //server.println("LED Four status command received"); |
202 | val = digitalRead(ledPinFour); // sets the LED two on |
203 | server.print( "LED 4 was turned " ); |
204 | if (val == 0) { |
205 | server.println( "on" ); |
206 | } else { |
207 | server.println( "off" ); |
208 | } |
209 | commandString = "" ; |
210 | return ; |
211 | } |
212 | |
213 | commandString = "" ; |
214 | } |
you can test it, using telnet command to ip address assigned to ethernet shield.
example
1 | telnet 192.168.200.36 23 |
2 | ledoneon <- enter |
3 | ledoneoff <- enter |
4 | ledonestatus <- enter |
commands are:
1 | To turn relays on and off |
2 | - ledoneon |
3 | turn relay no 1 on |
4 | - ledoneoff |
5 | turn relay no 1 off |
6 | - ledtwoon |
7 | turn relay no 2 on |
8 | - ledtwooff |
9 | turn relay no 2 off |
10 | - ledthreeon |
11 | turn relay no 3 on |
12 | - ledthreeoff |
13 | turn relay no 3 off |
14 | - ledfouron |
15 | turn relay no 4 on |
16 | - ledfouroff |
17 | turn relay no 4 off |
To view the state of relays
1 | - leonestatus |
2 | - ledtwostatus |
3 | - ledthreestatus |
4 | - ledfourstatus |
have fun