Sensors Sketch

This sketch is used to collect temperature data from a DS18S20 one-wire temperature sensor, an MQ5 hydrocarbon gas sensor and a humidity sensor.  It queries these units whenever HCA tells it to and reports the data to HCA.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OneWire.h>
#include <stdlib.h>

// DS18S20 Temperature chip i/o
OneWire ds(9);  // on pin 9
String tempPacHead = “taddr:”;
String ds18addr = “”;
String tempPacket = “”;
String tempStr = “:”;
char fahr[10];

// humidity variables
int humSensorPin = 0;  // hum sensor goes to analog 0
int humSensorValue = 0;
char RH[10];
String humname = “ard70hum1”;
String humpacket = “”;

// mq5 gas sensor vars
int mq5SensorPin = 1;  // mq5 sensor goes to analog 1
int mq5SensorValue = 0;
String mq5name = “ard70mq51”;
String mq5packet = “”;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x70 };
IPAddress ip(192,168,0,70);
unsigned int Port_Remote = 5000;      // remote port
IPAddress IP_Remote(192, 168, 0, 10);           // IP address for smarthome
unsigned int localPort = 5001;      // local port to listen on
char cmdGetData = ‘1’;     // a “1” means read all the data
char cmdStatusCheck = ‘9’;   // a “9” means smarthome is asking for status
char cmdIn = ‘0’;        // used to obtain and compare buffer character
// can not use dig pins 2, 4, 10, 11, 12, 13
// An EthernetUDP instance to let us receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup()
{
Serial.begin(9600);
delay(5000);
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);           // no need for gateway or mask
Udp.begin(localPort);
}

void loop() {
// initialize variables
delay(10000);
byte i;
byte present = 0;
byte data[12];
byte addr[8];
float cent;
float tFahr;
float tRH;
String stringOne;

// listen for incoming packets
int packetSize = Udp.parsePacket();
if(packetSize)
{
// Serial.print(“Received packet of size “);
// Serial.println(packetSize);
// Serial.print(“From “);
// IPAddress remote = Udp.remoteIP();
// for (int i =0; i < 4; i++)
//    {
//    Serial.print(remote[i], DEC);
//    if (i < 3)
//        {
//        Serial.print(“.”);
//        }
//    }
//
//  Serial.print(“, port “);
//  Serial.println(Udp.remotePort());

// read the packet into packetBufffer
Udp.read(packetBuffer,1);
//    Serial.print(“Contents:”);
//  Serial.println(packetBuffer);
cmdIn = (packetBuffer[0]);
if (cmdIn == cmdGetData)
{
// gather data
// start of temp sensor code
while (ds.search(addr))
{
ds18addr = “”;
for( i = 0; i < 8; i++)
{
stringOne = String(addr[i], HEX);
if (stringOne.length() < 2){stringOne = “0” + stringOne; }
stringOne.toUpperCase();
ds18addr = ds18addr + stringOne;
}
if ( OneWire::crc8( addr, 7) == addr[7])
{
ds.reset();
ds.select(addr);
ds.write(0x44,1);
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE);         // Read Scratchpad

for ( i = 0; i < 9; i++) {data[i] = ds.read();}

// calc temp value
cent = ( (data[1] << 8) + data[0] )*0.0625;
tFahr = (cent * 9 / 5) + 32;
dtostrf(tFahr,4,1,fahr);
tempPacket = tempPacHead + ds18addr + tempStr + fahr;
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(tempPacket);
Udp.endPacket();
}  // end of crc ok branch
}    // end of while loop and temp sensor code

// start of humidity sensor code
humSensorValue = analogRead(humSensorPin);
tRH = (humSensorValue – 225) / 6.38; // for HIH-4000
dtostrf(tRH,4,1,RH);
humpacket = humname + “=” + RH;
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(humpacket);
Udp.endPacket();
// end of humidity sensor code

// start of gas sensor code
mq5SensorValue = analogRead(mq5SensorPin);
mq5packet = mq5name + “=” + mq5SensorValue;
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(mq5packet);
Udp.endPacket();
// end of gas sensor code

}  // end of cmdGetData = 0 section
// respond to status check
if (cmdIn == cmdStatusCheck)
//  Serial.println(“matched9!”);
{
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ard70ok=yes”);        // send packet good status
Udp.endPacket();
}  // end of status check
}  // end of packet size section
}

Arduino, Venstar, Wemos, HCA, etc.