Components and supplies
Arduino UNO
Arduino Ethernet Shield 2
Relay (generic)
Apps and platforms
Arduino IDE
Project description
Code
Arduino sketch code
c_cpp
1//Sketch for Homotica Android App 2#include <Homotica.h> 3#include <Ethernet.h> 4#include <SPI.h> 5#include <RFID.h> 6#include <EthernetUdp.h> 7 8#define DEBUG_MODE 9#define SKETCH_NAME "HomoticaSketch" 10#define SKETCH_VERSION "1.0" 11#define ACTIVE_LOW //delete the line if you're using active_high setup 12 13#ifdef ACTIVE_LOW 14#define MHIGH 0x0 15#define MLOW 0x1 16#else 17#define MHIGH 0x1 18#define MLOW 0x0 19#endif 20 21#ifdef DEBUG_MODE 22#define DEBUG_PRINT(x) Serial.print(x) 23#define DEBUG_PRINTLN(x) Serial.println(x) 24#else 25#define DEBUG_PRINT(x) 26#define DEBUG_PRINTLN(x) 27#endif 28 29#define SDA_PIN 9 30#define RESET_PIN 8 31 32IPAddress ip(192, 168, 1, 20); 33byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0xBD, 0x21}; 34char Data_RX; 35int PIN, del, charsIndex[4], relayMode; 36static int startingPIN = 4, finishPIN = 7; 37unsigned const int localPort = 80; 38String msg, request, recivedCode; 39static String code = "abcdefgh"; 40 41char packetBuffer[200]; 42const char wrongAuth[] = "2"; 43const char positiveResponse[] = "1"; 44const char negativeResponse[] = "0"; 45EthernetUDP Udp; 46 47Homotica homotica; 48 49RFID mRFID(SDA_PIN, RESET_PIN); 50int lastRFIDAction = 0; 51static String allowedCards[] = {"6C69A3CU89", "77698D5R23"}; 52 53static String openDoor = "!7!2!1000!"; 54static String closeDoor = "!6!2!1000!"; 55 56void setup() { 57 Ethernet.begin(mac, ip); 58 Udp.begin(localPort); 59 SPI.begin(); 60 mRFID.init(); 61 62 Serial.begin(9600); 63 DEBUG_PRINT("UDP is at "); 64 DEBUG_PRINTLN(Ethernet.localIP()); 65 DEBUG_PRINTLN(""); 66 67 for (int i = startingPIN; i < finishPIN + 1; i++) { 68 pinMode(i, OUTPUT); 69 digitalWrite(i, MLOW); 70 } 71} 72 73void loop() { 74 checkUDP(); 75 checkRFID(); 76 homotica.refresh(); 77} 78 79void checkUDP() { 80 msg = ""; 81 int packetSize = Udp.parsePacket(); 82 if (packetSize) { 83 84 DEBUG_PRINTLN(); 85 DEBUG_PRINTLN("********************NEW INCOMING PACKET**************************"); 86 87 Udp.read(packetBuffer, 200); 88 msg = packetBuffer; 89 90 recivedCode = msg.substring(msg.indexOf("=") + 1 , msg.indexOf("!")); 91 request = msg.substring(msg.indexOf("?") + 1 , msg.indexOf("=")); 92 93 if (recivedCode == code) { 94 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 95 String myString = msg.substring(msg.indexOf("!")); 96 97 DEBUG_PRINT("Recived: "); 98 DEBUG_PRINTLN(msg); 99 DEBUG_PRINT("Substring: "); 100 DEBUG_PRINTLN(myString); 101 102 if (request == "QU") { 103 charsIndex[0] = msg.indexOf("!"); 104 charsIndex[1] = msg.indexOf("!", charsIndex[0] + 1); 105 PIN = msg.substring(charsIndex[0] + 1 , charsIndex[1]).toInt(); 106 String sensorReading = String(analogRead(PIN)); 107 char output[sensorReading.length() + 1]; 108 sensorReading.toCharArray(output, sensorReading.length() + 1); 109 Udp.write(output); 110 } 111 else if (request == "ST") { 112 Udp.write(positiveResponse); 113 Udp.endPacket(); 114 processSingleRunnable(myString); 115 } 116 else if (request == "MT") { 117 Udp.write(positiveResponse); 118 Udp.endPacket(); 119 processMultipleRunnable(myString); 120 } 121 else if (request == "CH") { 122 Udp.write(positiveResponse); 123 Udp.endPacket(); 124 } 125 else { 126 Udp.write(negativeResponse); 127 Udp.endPacket(); 128 } 129 } 130 else{ 131 Udp.write(wrongAuth); 132 Udp.endPacket(); 133 } 134 for ( int i = 0; i < sizeof(packetBuffer); ++i ) { 135 packetBuffer[i] = (char)0; 136 } 137 } 138 delay(10); 139} 140 141void checkRFID() { 142 if (mRFID.isCard()) { 143 mRFID.readCardSerial(); 144 String code = ""; 145 for (byte i = 0; i <= 4; i++) 146 { 147 code += String (mRFID.serNum[i], HEX); 148 code.toUpperCase(); 149 } 150 DEBUG_PRINTLN("Read code:"); 151 DEBUG_PRINTLN(code); 152 DEBUG_PRINTLN(); 153 154 for (String allowedCode : allowedCards) { 155 if (allowedCode == code) { 156 if (lastRFIDAction == 0) { //checks if the door has been closed or opened the last time 157 processSingleRunnable(openDoor); 158 lastRFIDAction = 1; 159 } else if (lastRFIDAction == 1) { 160 processSingleRunnable(closeDoor); 161 lastRFIDAction = 0; 162 } 163 } 164 } 165 delay(2000); 166 } 167} 168 169void processSingleRunnable(String msg) { 170 charsIndex[0] = msg.indexOf("!"); 171 charsIndex[1] = msg.indexOf("!", charsIndex[0] + 1); 172 charsIndex[2] = msg.indexOf("!", charsIndex[1] + 1); 173 charsIndex[3] = msg.indexOf("!", charsIndex[2] + 1); 174 175 PIN = msg.substring(charsIndex[0] + 1 , charsIndex[1]).toInt(); 176 relayMode = msg.substring(charsIndex[1] + 1, charsIndex[2]).toInt(); 177 del = msg.substring(charsIndex[2] + 1, charsIndex[3]).toInt(); 178 179 DEBUG_PRINTLN(); 180 DEBUG_PRINTLN("- OPENING -"); 181 DEBUG_PRINT("Pin: "); 182 DEBUG_PRINT(String(PIN)); 183 DEBUG_PRINT("; Mode: "); 184 DEBUG_PRINT(String(relayMode)); 185 DEBUG_PRINT("; Delay: "); 186 DEBUG_PRINTLN(msg.substring(charsIndex[2] + 1, charsIndex[3])); 187 DEBUG_PRINTLN("(0 --> ON, 1 --> OFF, 2 --> PUSH, 3 --> TOGGLE)"); 188 189 if (0 <= relayMode <= 2 && startingPIN <= PIN <= finishPIN) 190 { 191 if (relayMode == 0) { 192 digitalWrite(PIN, MHIGH); //relay on 193 } 194 else if (relayMode == 1) { 195 digitalWrite(PIN, MLOW); 196 } 197 else if (relayMode == 2) { 198 homotica.pushPin(PIN, del, digitalRead(PIN)); 199 } 200 else if (relayMode == 3) { 201 digitalWrite(PIN, !digitalRead(PIN)); 202 } 203 } 204} 205 206void processMultipleRunnable(String msg) { 207 char input[msg.length() + 1]; 208 msg.toCharArray(input, msg.length() + 1); 209 const char* divider = ("+"); 210 int lastIndex = 0; 211 for (int k = 0; k < sizeof(input); k++) { 212 if (String(input[k]) == "+") { 213 processSingleRunnable(msg.substring(lastIndex, k)); 214 k += 1; 215 lastIndex = k; 216 } 217 } 218} 219
Homotica library
Arduino sketch code
c_cpp
1//Sketch for Homotica Android App 2#include <Homotica.h> 3#include 4 <Ethernet.h> 5#include <SPI.h> 6#include <RFID.h> 7#include <EthernetUdp.h> 8 9#define 10 DEBUG_MODE 11#define SKETCH_NAME "HomoticaSketch" 12#define SKETCH_VERSION "1.0" 13#define 14 ACTIVE_LOW //delete the line if you're using active_high setup 15 16#ifdef 17 ACTIVE_LOW 18#define MHIGH 0x0 19#define MLOW 0x1 20#else 21#define MHIGH 0x1 22#define 23 MLOW 0x0 24#endif 25 26#ifdef DEBUG_MODE 27#define DEBUG_PRINT(x) Serial.print(x) 28#define 29 DEBUG_PRINTLN(x) Serial.println(x) 30#else 31#define DEBUG_PRINT(x) 32#define 33 DEBUG_PRINTLN(x) 34#endif 35 36#define SDA_PIN 9 37#define RESET_PIN 8 38 39IPAddress 40 ip(192, 168, 1, 20); 41byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0xBD, 0x21}; 42char 43 Data_RX; 44int PIN, del, charsIndex[4], relayMode; 45static int startingPIN = 46 4, finishPIN = 7; 47unsigned const int localPort = 80; 48String msg, request, 49 recivedCode; 50static String code = "abcdefgh"; 51 52char packetBuffer[200]; 53const 54 char wrongAuth[] = "2"; 55const char positiveResponse[] = "1"; 56const char 57 negativeResponse[] = "0"; 58EthernetUDP Udp; 59 60Homotica homotica; 61 62RFID 63 mRFID(SDA_PIN, RESET_PIN); 64int lastRFIDAction = 0; 65static String allowedCards[] 66 = {"6C69A3CU89", "77698D5R23"}; 67 68static String openDoor = "!7!2!1000!"; 69static 70 String closeDoor = "!6!2!1000!"; 71 72void setup() { 73 Ethernet.begin(mac, 74 ip); 75 Udp.begin(localPort); 76 SPI.begin(); 77 mRFID.init(); 78 79 Serial.begin(9600); 80 81 DEBUG_PRINT("UDP is at "); 82 DEBUG_PRINTLN(Ethernet.localIP()); 83 DEBUG_PRINTLN(""); 84 85 86 for (int i = startingPIN; i < finishPIN + 1; i++) { 87 pinMode(i, OUTPUT); 88 89 digitalWrite(i, MLOW); 90 } 91} 92 93void loop() { 94 checkUDP(); 95 96 checkRFID(); 97 homotica.refresh(); 98} 99 100void checkUDP() { 101 msg = 102 ""; 103 int packetSize = Udp.parsePacket(); 104 if (packetSize) { 105 106 107 DEBUG_PRINTLN(); 108 DEBUG_PRINTLN("********************NEW INCOMING PACKET**************************"); 109 110 111 Udp.read(packetBuffer, 200); 112 msg = packetBuffer; 113 114 recivedCode 115 = msg.substring(msg.indexOf("=") + 1 , msg.indexOf("!")); 116 request = msg.substring(msg.indexOf("?") 117 + 1 , msg.indexOf("=")); 118 119 if (recivedCode == code) { 120 Udp.beginPacket(Udp.remoteIP(), 121 Udp.remotePort()); 122 String myString = msg.substring(msg.indexOf("!")); 123 124 125 DEBUG_PRINT("Recived: "); 126 DEBUG_PRINTLN(msg); 127 DEBUG_PRINT("Substring: 128 "); 129 DEBUG_PRINTLN(myString); 130 131 if (request == "QU") { 132 133 charsIndex[0] = msg.indexOf("!"); 134 charsIndex[1] = msg.indexOf("!", 135 charsIndex[0] + 1); 136 PIN = msg.substring(charsIndex[0] + 1 , charsIndex[1]).toInt(); 137 138 String sensorReading = String(analogRead(PIN)); 139 char output[sensorReading.length() 140 + 1]; 141 sensorReading.toCharArray(output, sensorReading.length() + 1); 142 143 Udp.write(output); 144 } 145 else if (request == "ST") { 146 147 Udp.write(positiveResponse); 148 Udp.endPacket(); 149 processSingleRunnable(myString); 150 151 } 152 else if (request == "MT") { 153 Udp.write(positiveResponse); 154 155 Udp.endPacket(); 156 processMultipleRunnable(myString); 157 } 158 159 else if (request == "CH") { 160 Udp.write(positiveResponse); 161 162 Udp.endPacket(); 163 } 164 else { 165 Udp.write(negativeResponse); 166 167 Udp.endPacket(); 168 } 169 } 170 else{ 171 Udp.write(wrongAuth); 172 173 Udp.endPacket(); 174 } 175 for ( int i = 0; i < sizeof(packetBuffer); 176 ++i ) { 177 packetBuffer[i] = (char)0; 178 } 179 } 180 delay(10); 181} 182 183void 184 checkRFID() { 185 if (mRFID.isCard()) { 186 mRFID.readCardSerial(); 187 String 188 code = ""; 189 for (byte i = 0; i <= 4; i++) 190 { 191 code += String 192 (mRFID.serNum[i], HEX); 193 code.toUpperCase(); 194 } 195 DEBUG_PRINTLN("Read 196 code:"); 197 DEBUG_PRINTLN(code); 198 DEBUG_PRINTLN(); 199 200 for 201 (String allowedCode : allowedCards) { 202 if (allowedCode == code) { 203 if 204 (lastRFIDAction == 0) { //checks if the door has been closed or opened the last 205 time 206 processSingleRunnable(openDoor); 207 lastRFIDAction = 208 1; 209 } else if (lastRFIDAction == 1) { 210 processSingleRunnable(closeDoor); 211 212 lastRFIDAction = 0; 213 } 214 } 215 } 216 delay(2000); 217 218 } 219} 220 221void processSingleRunnable(String msg) { 222 charsIndex[0] = msg.indexOf("!"); 223 224 charsIndex[1] = msg.indexOf("!", charsIndex[0] + 1); 225 charsIndex[2] = msg.indexOf("!", 226 charsIndex[1] + 1); 227 charsIndex[3] = msg.indexOf("!", charsIndex[2] + 1); 228 229 230 PIN = msg.substring(charsIndex[0] + 1 , charsIndex[1]).toInt(); 231 relayMode 232 = msg.substring(charsIndex[1] + 1, charsIndex[2]).toInt(); 233 del = msg.substring(charsIndex[2] 234 + 1, charsIndex[3]).toInt(); 235 236 DEBUG_PRINTLN(); 237 DEBUG_PRINTLN("- OPENING 238 -"); 239 DEBUG_PRINT("Pin: "); 240 DEBUG_PRINT(String(PIN)); 241 DEBUG_PRINT("; 242 Mode: "); 243 DEBUG_PRINT(String(relayMode)); 244 DEBUG_PRINT("; Delay: "); 245 246 DEBUG_PRINTLN(msg.substring(charsIndex[2] + 1, charsIndex[3])); 247 DEBUG_PRINTLN("(0 248 --> ON, 1 --> OFF, 2 --> PUSH, 3 --> TOGGLE)"); 249 250 if (0 <= relayMode <= 251 2 && startingPIN <= PIN <= finishPIN) 252 { 253 if (relayMode == 0) { 254 digitalWrite(PIN, 255 MHIGH); //relay on 256 } 257 else if (relayMode == 1) { 258 digitalWrite(PIN, 259 MLOW); 260 } 261 else if (relayMode == 2) { 262 homotica.pushPin(PIN, 263 del, digitalRead(PIN)); 264 } 265 else if (relayMode == 3) { 266 digitalWrite(PIN, 267 !digitalRead(PIN)); 268 } 269 } 270} 271 272void processMultipleRunnable(String 273 msg) { 274 char input[msg.length() + 1]; 275 msg.toCharArray(input, msg.length() 276 + 1); 277 const char* divider = ("+"); 278 int lastIndex = 0; 279 for (int 280 k = 0; k < sizeof(input); k++) { 281 if (String(input[k]) == "+") { 282 processSingleRunnable(msg.substring(lastIndex, 283 k)); 284 k += 1; 285 lastIndex = k; 286 } 287 } 288} 289
Homotica library
Downloadable files
Wiring diagram
Wiring diagram
Comments
Only logged in users can leave comments