{"id":380,"date":"2020-03-30T20:24:25","date_gmt":"2020-03-31T01:24:25","guid":{"rendered":"https:\/\/sundrysites.com\/?page_id=380"},"modified":"2020-03-30T21:13:17","modified_gmt":"2020-03-31T02:13:17","slug":"one-wire-sensor-sketch","status":"publish","type":"page","link":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/","title":{"rendered":"Air Sensors Sketch"},"content":{"rendered":"\n<p>This sketch is used to collect temperature data from a DS18S20 one-wire temperature sensor, an MQ5 hydrocarbon gas sensor and a humidity sensor.&nbsp; It queries these units whenever HCA tells it to and reports the data to HCA.<\/p>\n\n\n\n<p>#include &lt;SPI.h&gt;<br>#include &lt;Ethernet.h&gt;<br>#include &lt;EthernetUdp.h&gt;<br>#include &lt;OneWire.h&gt;<br>#include &lt;stdlib.h&gt;<\/p>\n\n\n\n<p>\/\/ DS18S20 Temperature chip i\/o<br>OneWire ds(9);&nbsp; \/\/ on pin 9<br>String tempPacHead = &#8220;taddr:&#8221;;<br>String ds18addr = &#8220;&#8221;;<br>String tempPacket = &#8220;&#8221;;<br>String tempStr = &#8220;:&#8221;;<br>char fahr[10];<\/p>\n\n\n\n<p>\/\/ humidity variables<br>int humSensorPin = 0;&nbsp; \/\/ hum sensor goes to analog 0<br>int humSensorValue = 0;<br>char RH[10];<br>String humname = &#8220;ard70hum1&#8221;;<br>String humpacket = &#8220;&#8221;;<\/p>\n\n\n\n<p>\/\/ mq5 gas sensor vars<br>int mq5SensorPin = 1;&nbsp; \/\/ mq5 sensor goes to analog 1<br>int mq5SensorValue = 0;<br>String mq5name = &#8220;ard70mq51&#8221;;<br>String mq5packet = &#8220;&#8221;;<\/p>\n\n\n\n<p>\/\/ Enter a MAC address and IP address for your controller below.<br>\/\/ The IP address will be dependent on your local network:<br>byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x70 };<br>IPAddress ip(192,168,0,70);<br>unsigned int Port_Remote = 5000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ remote port<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>char cmdGetData = &#8216;1&#8217;;&nbsp;&nbsp; &nbsp; \/\/ a &#8220;1&#8221; means read all the data<br>char cmdStatusCheck = &#8216;9&#8217;;&nbsp;&nbsp; \/\/ a &#8220;9&#8221; means smarthome is asking for status<br>char cmdIn = &#8216;0&#8217;;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ used to obtain and compare buffer character<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>{<br>Serial.begin(9600);<br>delay(5000);<br>\/\/ start the Ethernet and UDP:<br>Ethernet.begin(mac,ip);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ no need for gateway or mask<br>Udp.begin(localPort);<br>}<\/p>\n\n\n\n<p>void loop() {<br>\/\/ initialize variables<br>delay(10000);<br>byte i;<br>byte present = 0;<br>byte data[12];<br>byte addr[8];<br>float cent;<br>float tFahr;<br>float tRH;<br>String stringOne;<\/p>\n\n\n\n<p>\/\/ listen for incoming packets<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; Serial.println(packetBuffer);<br>cmdIn = (packetBuffer[0]);<br>if (cmdIn == cmdGetData)<br>{<br>\/\/ gather data<br>\/\/ start of temp sensor code<br>while (ds.search(addr))<br>{<br>ds18addr = &#8220;&#8221;;<br>for( i = 0; i &lt; 8; i++)<br>{<br>stringOne = String(addr[i], HEX);<br>if (stringOne.length() &lt; 2){stringOne = &#8220;0&#8221; + stringOne; }<br>stringOne.toUpperCase();<br>ds18addr = ds18addr + stringOne;<br>}<br>if ( OneWire::crc8( addr, 7) == addr[7])<br>{<br>ds.reset();<br>ds.select(addr);<br>ds.write(0x44,1);<br>delay(1000);<br>present = ds.reset();<br>ds.select(addr);<br>ds.write(0xBE);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Read Scratchpad<\/p>\n\n\n\n<p>for ( i = 0; i &lt; 9; i++) {data[i] = ds.read();}<\/p>\n\n\n\n<p>\/\/ calc temp value<br>cent = ( (data[1] &lt;&lt; 8) + data[0] )*0.0625;<br>tFahr = (cent * 9 \/ 5) + 32;<br>dtostrf(tFahr,4,1,fahr);<br>tempPacket = tempPacHead + ds18addr + tempStr + fahr;<br>Udp.beginPacket(IP_Remote, Port_Remote);<br>Udp.print(tempPacket);<br>Udp.endPacket();<br>}&nbsp; \/\/ end of crc ok branch<br>}&nbsp;&nbsp;&nbsp; \/\/ end of while loop and temp sensor code<\/p>\n\n\n\n<p>\/\/ start of humidity sensor code<br>humSensorValue = analogRead(humSensorPin);<br>tRH = (humSensorValue &#8211; 225) \/ 6.38; \/\/ for HIH-4000<br>dtostrf(tRH,4,1,RH);<br>humpacket = humname + &#8220;=&#8221; + RH;<br>Udp.beginPacket(IP_Remote, Port_Remote);<br>Udp.print(humpacket);<br>Udp.endPacket();<br>\/\/ end of humidity sensor code<\/p>\n\n\n\n<p>\/\/ start of gas sensor code<br>mq5SensorValue = analogRead(mq5SensorPin);<br>mq5packet = mq5name + &#8220;=&#8221; + mq5SensorValue;<br>Udp.beginPacket(IP_Remote, Port_Remote);<br>Udp.print(mq5packet);<br>Udp.endPacket();<br>\/\/ end of gas sensor code<\/p>\n\n\n\n<p>}&nbsp; \/\/ end of cmdGetData = 0 section<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;ard70ok=yes&#8221;);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ send packet good status<br>Udp.endPacket();<br>}&nbsp; \/\/ end of status check<br>}&nbsp; \/\/ end of packet size section<br>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This sketch is used to collect temperature data from a DS18S20 one-wire temperature sensor, an MQ5 hydrocarbon gas sensor and a humidity sensor.&nbsp; It queries these units whenever HCA tells it to and reports the data to HCA. #include &lt;SPI.h&gt;#include &lt;Ethernet.h&gt;#include &lt;EthernetUdp.h&gt;#include &lt;OneWire.h&gt;#include &lt;stdlib.h&gt; \/\/ DS18S20 Temperature chip i\/oOneWire ds(9);&nbsp; \/\/ on pin 9String tempPacHead &hellip; <a href=\"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Air Sensors 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-380","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>Air Sensors 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\/one-wire-sensor-sketch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Air Sensors Sketch - Sundrysites\" \/>\n<meta property=\"og:description\" content=\"This sketch is used to collect temperature data from a DS18S20 one-wire temperature sensor, an MQ5 hydrocarbon gas sensor and a humidity sensor.&nbsp; It queries these units whenever HCA tells it to and reports the data to HCA. #include &lt;SPI.h&gt;#include &lt;Ethernet.h&gt;#include &lt;EthernetUdp.h&gt;#include &lt;OneWire.h&gt;#include &lt;stdlib.h&gt; \/\/ DS18S20 Temperature chip i\/oOneWire ds(9);&nbsp; \/\/ on pin 9String tempPacHead &hellip; Continue reading Air Sensors Sketch &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/\" \/>\n<meta property=\"og:site_name\" content=\"Sundrysites\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-31T02:13:17+00:00\" \/>\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\\\/one-wire-sensor-sketch\\\/\",\"url\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/one-wire-sensor-sketch\\\/\",\"name\":\"Air Sensors Sketch - Sundrysites\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sundrysites.com\\\/#website\"},\"datePublished\":\"2020-03-31T01:24:25+00:00\",\"dateModified\":\"2020-03-31T02:13:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/one-wire-sensor-sketch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/one-wire-sensor-sketch\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/sundrysites.com\\\/index.php\\\/home-info\\\/arduino-projects\\\/sketches\\\/one-wire-sensor-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\":\"Air Sensors 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":"Air Sensors 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\/one-wire-sensor-sketch\/","og_locale":"en_US","og_type":"article","og_title":"Air Sensors Sketch - Sundrysites","og_description":"This sketch is used to collect temperature data from a DS18S20 one-wire temperature sensor, an MQ5 hydrocarbon gas sensor and a humidity sensor.&nbsp; It queries these units whenever HCA tells it to and reports the data to HCA. #include &lt;SPI.h&gt;#include &lt;Ethernet.h&gt;#include &lt;EthernetUdp.h&gt;#include &lt;OneWire.h&gt;#include &lt;stdlib.h&gt; \/\/ DS18S20 Temperature chip i\/oOneWire ds(9);&nbsp; \/\/ on pin 9String tempPacHead &hellip; Continue reading Air Sensors Sketch &rarr;","og_url":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/","og_site_name":"Sundrysites","article_modified_time":"2020-03-31T02:13:17+00:00","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\/one-wire-sensor-sketch\/","url":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/","name":"Air Sensors Sketch - Sundrysites","isPartOf":{"@id":"https:\/\/sundrysites.com\/#website"},"datePublished":"2020-03-31T01:24:25+00:00","dateModified":"2020-03-31T02:13:17+00:00","breadcrumb":{"@id":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-sketch\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sundrysites.com\/index.php\/home-info\/arduino-projects\/sketches\/one-wire-sensor-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":"Air Sensors 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\/380","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=380"}],"version-history":[{"count":2,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages\/380\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/sundrysites.com\/index.php\/wp-json\/wp\/v2\/pages\/380\/revisions\/438"}],"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=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}