{"id":372,"date":"2020-03-30T20:19:54","date_gmt":"2020-03-31T01:19:54","guid":{"rendered":"https:\/\/sundrysites.com\/?page_id=372"},"modified":"2020-03-30T20:19:54","modified_gmt":"2020-03-31T01:19:54","slug":"controlling-relays-sketch","status":"publish","type":"page","link":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/","title":{"rendered":"Controlling Relays Sketch"},"content":{"rendered":"\n<p>This sketch receives a command from HCA and triggers one of it relays accordingly.<\/p>\n\n\n\n<p>#include &lt;SPI.h&gt;<br>#include &lt;Ethernet.h&gt;<br>#include &lt;EthernetUdp.h&gt;<\/p>\n\n\n\n<p>\/\/ Enter a MAC address and IP address for your controller below.<br>\/\/ change these to appropriate values<br>byte mac[] = {&nbsp; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x68 };<br>IPAddress ip(192, 168, 0, 68);<br>unsigned int Port_Remote = 5000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ remote port for status check<br>IPAddress IP_Remote(192, 168, 0, 10);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ IP address for smarthome<br>unsigned int localPort = 5001;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ local port to listen on<br>\/\/ we don&#8217;t check on the sender&#8217;s port or addr for now<br>char cmdGDoper = &#8216;1&#8217;;&nbsp;&nbsp; &nbsp; \/\/ a &#8220;1&#8221; means flip the garage door opener<br>char cmdResetPC = &#8216;2&#8217;;&nbsp;&nbsp; \/\/ a &#8220;2&#8221; means turn the computer off for 60 sec to reboot it<br>char cmdBathBlowerOn = &#8216;3&#8217;;&nbsp;&nbsp; &nbsp;\/\/ a &#8220;3&#8221; means turn the bath room blower on<br>char cmdBathBlowerOff = &#8216;4&#8217;;&nbsp; \/\/ a &#8220;4&#8221; means turn the bath room blower off<br>char cmdStatusCheck = &#8216;9&#8217;;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ a &#8220;9&#8221; means reply with good status<br>char cmdIn = &#8216;0&#8217;;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ used to obtain and compare buffer character<br>int garageDoorOperPin = 9;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ digital pin to control garage door opener<br>int resetPCPin = 8;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ digital pin to control pc relay<br>int bathBlowerPin = 7;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ digital pin to control blower<br>\/\/ can not use dig pins 2, 4, 10, 11, 12, 13<br>\/\/ An EthernetUDP instance to let us receive packets over UDP<br>EthernetUDP Udp;<br>char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; \/\/buffer to hold incoming packet<\/p>\n\n\n\n<p>void setup() {<br>digitalWrite(garageDoorOperPin, HIGH);&nbsp; \/\/ high=off for this relay<br>digitalWrite(resetPCPin, HIGH);<br>digitalWrite(bathBlowerPin, LOW);&nbsp;&nbsp; &nbsp;\/\/ low=off for this relay<br>pinMode(garageDoorOperPin, OUTPUT);<br>pinMode(resetPCPin, OUTPUT);<br>pinMode(bathBlowerPin, OUTPUT);<br>\/\/ start the Ethernet and UDP:<br>Ethernet.begin(mac, ip);<br>Udp.begin(localPort);<br>delay(10000);<br>\/\/&nbsp;&nbsp; &nbsp;Serial.begin(9600);<br>\/\/ Serial.println(&#8220;Starting&#8221;);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>void loop() {<br>int packetSize = Udp.parsePacket();<br>if(packetSize)<br>{<br>\/\/ Serial.print(&#8220;Received packet of size &#8220;);<br>\/\/ Serial.println(packetSize);<br>\/\/ Serial.print(&#8220;From &#8220;);<br>\/\/ IPAddress remote = Udp.remoteIP();<br>\/\/ for (int i =0; i &lt; 4; i++)<br>\/\/&nbsp;&nbsp; &nbsp;{<br>\/\/&nbsp;&nbsp; &nbsp;Serial.print(remote[i], DEC);<br>\/\/&nbsp;&nbsp; &nbsp;if (i &lt; 3)<br>\/\/&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br>\/\/&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Serial.print(&#8220;.&#8221;);<br>\/\/&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>\/\/&nbsp;&nbsp; &nbsp;}<br>\/\/<br>\/\/&nbsp; Serial.print(&#8220;, port &#8220;);<br>\/\/&nbsp; Serial.println(Udp.remotePort());<\/p>\n\n\n\n<p>\/\/ read the packet into packetBufffer<br>Udp.read(packetBuffer,1);<br>\/\/&nbsp;&nbsp; &nbsp;Serial.print(&#8220;Contents:&#8221;);<br>\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(packetBuffer);<br>cmdIn = (packetBuffer[0]);<br>\/\/ press garage door opener<br>if (cmdIn == cmdGDoper)<br>{<br>\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(&#8220;matched1!&#8221;);<br>digitalWrite(garageDoorOperPin, LOW);&nbsp; \/\/ relay uses NO connections<br>delay (100);&nbsp;&nbsp; &nbsp;\/\/ oper relay for 0.1 sec<br>digitalWrite(garageDoorOperPin, HIGH);<br>}<br>\/\/ trip smarthome pc relay for a minute<br>if (cmdIn == cmdResetPC)<br>{<br>\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(&#8220;matched2!&#8221;);<br>digitalWrite(resetPCPin, LOW);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ relay uses NC connections<br>delay (60000);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ delay for 60 seconds<br>digitalWrite(resetPCPin, HIGH);<br>}<br>\/\/ turn bath blower on<br>if (cmdIn == cmdBathBlowerOn)<br>{<br>\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(&#8220;matched3!&#8221;);<br>digitalWrite(bathBlowerPin, HIGH);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ relay uses NO connections<br>}<br>\/\/ turn bath blower off<br>if (cmdIn == cmdBathBlowerOff)<br>{<br>\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(&#8220;matched4!&#8221;);<br>digitalWrite(bathBlowerPin, LOW);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ relay uses NO connections<br>}<br>\/\/ reply to status check<br>\/\/ respond to status check<br>if (cmdIn == cmdStatusCheck)<br>\/\/&nbsp; Serial.println(&#8220;matched9!&#8221;);<br>{<br>Udp.beginPacket(IP_Remote, Port_Remote);<br>Udp.write(&#8220;ard68ok=yes&#8221;);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ send packet good status<br>Udp.endPacket();<br>}&nbsp; \/\/ end of status check<\/p>\n\n\n\n<p>}&nbsp; \/\/ end of incoming packet handler<br>}&nbsp; \/\/ end of main loop<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This sketch receives a command from HCA and triggers one of it relays accordingly. #include &lt;SPI.h&gt;#include &lt;Ethernet.h&gt;#include &lt;EthernetUdp.h&gt; \/\/ Enter a MAC address and IP address for your controller below.\/\/ change these to appropriate valuesbyte mac[] = {&nbsp; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x68 };IPAddress ip(192, 168, 0, 68);unsigned int Port_Remote = 5000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ remote &hellip; <a href=\"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Controlling Relays Sketch<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":370,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-372","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Controlling Relays Sketch - Sundrysites<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Controlling Relays Sketch - Sundrysites\" \/>\n<meta property=\"og:description\" content=\"This sketch receives a command from HCA and triggers one of it relays accordingly. #include &lt;SPI.h&gt;#include &lt;Ethernet.h&gt;#include &lt;EthernetUdp.h&gt; \/\/ Enter a MAC address and IP address for your controller below.\/\/ change these to appropriate valuesbyte mac[] = {&nbsp; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x68 };IPAddress ip(192, 168, 0, 68);unsigned int Port_Remote = 5000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ remote &hellip; Continue reading Controlling Relays Sketch &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/\" \/>\n<meta property=\"og:site_name\" content=\"Sundrysites\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/controlling-relays-sketch\\\/\",\"url\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/controlling-relays-sketch\\\/\",\"name\":\"Controlling Relays Sketch - Sundrysites\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sundrysites.com\\\/#website\"},\"datePublished\":\"2020-03-31T01:19:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/controlling-relays-sketch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/controlling-relays-sketch\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/controlling-relays-sketch\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/sundrysites.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Home Info\",\"item\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Arduino Projects\",\"item\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Sketches\",\"item\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Controlling Relays Sketch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/sundrysites.com\\\/#website\",\"url\":\"https:\\\/\\\/sundrysites.com\\\/\",\"name\":\"Sundrysites\",\"description\":\"As In Various and Sundry\",\"publisher\":{\"@id\":\"https:\\\/\\\/sundrysites.com\\\/#\\\/schema\\\/person\\\/1e11ce6af1587a3431e57dfc0797ab61\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/sundrysites.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/sundrysites.com\\\/#\\\/schema\\\/person\\\/1e11ce6af1587a3431e57dfc0797ab61\",\"name\":\"Wayne Gulden\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g\",\"caption\":\"Wayne Gulden\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/sundrysites.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Controlling Relays Sketch - Sundrysites","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/","og_locale":"en_US","og_type":"article","og_title":"Controlling Relays Sketch - Sundrysites","og_description":"This sketch receives a command from HCA and triggers one of it relays accordingly. #include &lt;SPI.h&gt;#include &lt;Ethernet.h&gt;#include &lt;EthernetUdp.h&gt; \/\/ Enter a MAC address and IP address for your controller below.\/\/ change these to appropriate valuesbyte mac[] = {&nbsp; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x68 };IPAddress ip(192, 168, 0, 68);unsigned int Port_Remote = 5000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ remote &hellip; Continue reading Controlling Relays Sketch &rarr;","og_url":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/","og_site_name":"Sundrysites","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/","url":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/","name":"Controlling Relays Sketch - Sundrysites","isPartOf":{"@id":"https:\/\/sundrysites.com\/#website"},"datePublished":"2020-03-31T01:19:54+00:00","breadcrumb":{"@id":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/controlling-relays-sketch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sundrysites.com\/"},{"@type":"ListItem","position":2,"name":"Home Info","item":"https:\/\/sundrysites.com\/index.php\/home-info\/"},{"@type":"ListItem","position":3,"name":"Arduino Projects","item":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/"},{"@type":"ListItem","position":4,"name":"Sketches","item":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/"},{"@type":"ListItem","position":5,"name":"Controlling Relays Sketch"}]},{"@type":"WebSite","@id":"https:\/\/sundrysites.com\/#website","url":"https:\/\/sundrysites.com\/","name":"Sundrysites","description":"As In Various and Sundry","publisher":{"@id":"https:\/\/sundrysites.com\/#\/schema\/person\/1e11ce6af1587a3431e57dfc0797ab61"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sundrysites.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/sundrysites.com\/#\/schema\/person\/1e11ce6af1587a3431e57dfc0797ab61","name":"Wayne Gulden","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g","caption":"Wayne Gulden"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/c0392cdfb7826ed5407aad362998bc6dd03fe293560ce15ccc2211357e3cec6e?s=96&d=mm&r=g"},"sameAs":["https:\/\/sundrysites.com"]}]}},"_links":{"self":[{"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages\/372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":1,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages\/372\/revisions"}],"predecessor-version":[{"id":373,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages\/372\/revisions\/373"}],"up":[{"embeddable":true,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages\/370"}],"wp:attachment":[{"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/media?parent=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}