I have Arduino Mega with Ethernet shield mounted on top running as a web server. It reads temperature, humidity, rain or not and pot soil moisture and then displays on web page. Can you please review my Arduino code?
#include <SPI.h>
#include <Ethernet.h>
#include "Dewpnt_heatIndx.h"
#include "util.h"
// 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, 0xED
};
IPAddress ip(192, 168, 1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 8090 is default for HTTP):
EthernetServer server(8090);
String rainMsg;
// if val of soil moisture is greater than 30 turn on solenoid
const int8_t DRY_SOIL_DEFAULT = 30;
// ///////////// PIN Setup /////////////////
const int solenoidPin = 6; // D6 : This is the output pin on the Arduino we are using
#define DHT11_PIN 8 // D8 to 2nd pin on DHT, leave 3rd pin unconnected 4th is gnd
const int8_t rainsense = 7; // analog sensor input pin A7
const int buzzerout = 2; // digital output pin D2 - buzzer output
//int countval = 0; // counter value starting from 0 and goes up by 1 every second
int SENSE = 1; // Soil Sensor input at Analog PIN A1
int val = 0;
double tempInC;
int humidity;
float dP; // dew point
float dPF; // dew point in fahrenheit
float tF; // temperature in fahrenheit
double hIinFah;
double hIinCel;
void setup() {
pinMode(solenoidPin, OUTPUT); // Sets the pin as an output
pinMode(buzzerout, OUTPUT);
pinMode(rainsense, INPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}
Serial.print(F("server is at "));
Serial.println(Ethernet.localIP());
}
void loop() {
val = analogRead(SENSE);
val = val / 10;
int rainSenseReading = analogRead(rainsense);
Serial.println(rainSenseReading); // serial monitoring message
// from heavy rain - no rain.
if (rainSenseReading < 300) {
Serial.print(F("Heavy rain: "));
Serial.println(rainSenseReading);
}
else if (rainSenseReading < 500) {
Serial.print(F("Moderately Raining: "));
Serial.println(rainSenseReading);
}
else if (rainSenseReading > 500) Serial.println(F("No Rain"));
tempInC = util::getTempHumdata(DHT11_PIN)[0];
humidity = util::getTempHumdata(DHT11_PIN)[1];
dP = (Dewpnt_heatIndx::dewPointFast(tempInC, humidity));
dPF = ((dP * 9) / 5) + 32;
tF = ((tempInC * 9) / 5) + 32;
Serial.print(F(" Soil Moisture: "));
Serial.println(val);
// Turn on Solenoid Valve if soil moisture value greater than 30
if (val > DRY_SOIL_DEFAULT) {
digitalWrite(solenoidPin, HIGH);
digitalWrite(buzzerout, HIGH);
}
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
if (c == '\n' && currentLineIsBlank) {
if (!sentHeader) {
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close")); // the connection will be closed after completion of the response
client.println(F("Refresh: 10")); // refresh the page automatically every 5 sec
client.println();
sentHeader = true;
}
client.println(F("<!DOCTYPE HTML>"));
client.println(F("<html><head><title>"));
client.println(F("Welcome to Arduino WebServer</title>"));
client.println(F("<body style='background-color:grey'>"));
client.println(c);
client.println(F("<p style='color:red';style='font-family: Arial'>LIVE</p>"));
if (rainSenseReading < 300) {
rainMsg = F("It is raining heavily !");
}
if (rainSenseReading < 500) {
rainMsg = F("Moderate rain.");
}
if (rainSenseReading < 500) {
client.print(rainMsg);
client.print(F(" Rain Sensor reads: "));
client.println(rainSenseReading);
}
else client.println(F("No Rain today !"));
client.print(F("<br>Temperature: <u>+</u>"));
client.print(tempInC, 1);
client.print(F("℃ / "));
client.print(tF);
client.println(F(" ℉, Humidity: "));
client.print(humidity, 1);
client.print(F("%<br>Dew Point: "));
client.print(dP);
client.print(F("℃ Heat Index: "));
if (tF < 70 || humidity < 30) {
client.print(tempInC);
client.print(F(" ℃ / "));
client.print(tF);
client.println(F(" ℉"));
}
else {
hIinFah = Dewpnt_heatIndx::heatIndex(tF, humidity);
hIinCel = (hIinFah + 40) / 1.8 - 40;
client.print(hIinCel);
client.print(F(" ℃/ "));
client.print(hIinFah);
client.println(F(" ℉ <br>"));
}
client.print(F("<br>Pot Soil Moisture: "));
client.print(val);
if (val < 50 && val > DRY_SOIL_DEFAULT) {
client.println(F(", Soil is getting dry.</body></html>"));
}
else if (val < 50)
{
client.println (F(" Soil is damp.</body></html>"));
}
else
{
if (val > DRY_SOIL_DEFAULT) {
client.println (F(" Soil is dry.</body></html>"));
}
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
Dewpnt_heatIndx.h
namespace Dewpnt_heatIndx
{
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity * 0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
double heatIndex(double tempF, double humidity)
{
double c1 = -42.38, c2 = 2.049, c3 = 10.14, c4 = -0.2248, c5 = -6.838e-3, c6 = -5.482e-2, c7 = 1.228e-3, c8 = 8.528e-4, c9 = -1.99e-6 ;
double T = tempF;
double R = humidity;
double A = (( c5 * T) + c2) * T + c1;
double B = ((c7 * T) + c4) * T + c3;
double C = ((c9 * T) + c8) * T + c6;
double rv = (C * R + B) * R + A;
return rv;
}
}
util.h
#include <dht.h>
namespace util
{
double* getTempHumdata(const int sensorPin)
{
double old_data [2];
dht DHT;
double nums [2];
int chk = DHT.read11(sensorPin);
int hum;
double temp;
Serial.print("Sensor ");
Serial.print(sensorPin);
Serial.print(" ");
switch (chk)
{
case 0:
hum = DHT.humidity;
temp = DHT.temperature;
nums[0] = temp;
nums[1] = hum;
Serial.print("Temperature: ");
Serial.print(nums[0]);
Serial.print(" Humidity: ");
Serial.println(nums[1]);
Serial.print(hum);
Serial.print(" % ");
Serial.print(temp);
Serial.println(" ℃");
break;
case -1: Serial.println(" Checksum error");break;
case -2: Serial.println(" Time out error"); break;
default: Serial.println(" Unknown error"); break;
return nums;
}
}
}