Ethernet Shield to PHP and MySQL

Hello, I am constructing a project that consists of a larger project box that houses 8 duplex outlets, that each connects to a channel of an 8 channel relay module. Also in the circuit is a DHT22 temp sensor and a DS3231 Chronodot RTC. I have a working sketch to trigger relays based on time or atmospheric conditions as measured by the DHT. Everything is handled in “if” tests.

I am wanting to get my data posted to my PHP web site. I paid for the domain name through HostGator and built the PHP website with WordPress. My site is slowly becoming what I want it to look like, and I would like to get all my data collected and posted to my site. Is this even possible? Does it matter if the PHP exists outside of the microSD card? Or do I need some type of file on the card that will direct my project to my site? Man I hope this is possible.

Also, I am completely illiterate when it comes to servers in general, and from the exploring I’ve done so far, I can see that there are many variations, each I assume with their own pros and cons, so some guidance or suggestions are greatly needed. And Since I am posting my code, I am also open to suggestions to clean it up. I know I should use arrays in some parts instead of what I have, but I am still learning arrays and did not want to botch my code as it works good now as is. Plus there are a few delays being used that might work best if another method was used.

#include "DHT.h" // Temp/Humidity Sensor Library
#include <Wire.h> // i2c communications Library
#include "RTClib.h" // RealTimeClock Library for DS1307 and DS3231

#define RELAY_ON 1 // RELAY_ON and RELAY_OFF may need to swich values to work properly
#define RELAY_OFF 0 // Used to switch relay states for on/off of AC devices

#define Relay_A  30  // relay 1 - 
#define Relay_B  31  // relay 2 - 
#define Relay_C  32  // relay 3 - 
#define Relay_D  33  // relay 4 -
#define Relay_E  34  // relay 5 -
#define Relay_F  35  // relay 6 -
#define Relay_G  36  // relay 7 - 
#define Relay_H  37  // relay 8 - 

// Sensor Declaration
#define DHTPIN A2 
#define DHTTYPE DHT22

//  Sensor Reference Values
int hiTempDHT = 85; // Max Fahrenheit Value -- Change these around to find what works best for you
int lowTempDHT = 60;  // Min Fahrenheit Value -- Change these around to find what works best for you
int hiHumDHT = 60; // Max Humidity Value -- Change these around to find what works best for you

int hourNow = 0; // Variable to indicate PM hours in 12 hour format

RTC_DS1307 RTC;

//  declare DHT variables
float h;
float f;

float UTCOffset = -5.0;    // Your timezone relative to UTC (http://en.wikipedia.org/wiki/UTC_offset)


DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial1.begin(9600);
  //Serial2.begin() // For other baud rates
  //Serial3.begin() // For other baud rates

  //---(Set pins 30-37 as outputs )----  
  pinMode(Relay_A, OUTPUT);
  pinMode(Relay_B, OUTPUT);  
  pinMode(Relay_C, OUTPUT);  
  pinMode(Relay_D, OUTPUT);  
  pinMode(Relay_E, OUTPUT);
  pinMode(Relay_F, OUTPUT);
  pinMode(Relay_G, OUTPUT);
  pinMode(Relay_H, OUTPUT);  
  delay(1000);

  dht.begin();
  Wire.begin();
  RTC.begin();
 
  RTC.adjust(DateTime(__DATE__, __TIME__));  // this captures the time from the computer that is uploading sketch to Arduino
                                             // so ensure that the uploading computer has the correct time.
  delay(1000);
}

void loop()
{
  DateTime now = RTC.now();   

    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.println();

//Coded alarm triggers for Relays A-D  
 if (now.hour() == 23 && now.minute() == 29){   // Turn on time (hours and minutes) for Relay_A
   digitalWrite(Relay_A, RELAY_ON);
  }
 if (now.hour() == 23 && now.minute() == 30){   // Turn off time (hours and minutes) for Relay_A
   digitalWrite(Relay_A, RELAY_OFF);
  }
 if (now.hour() == 23 && now.minute() == 29){   // Turn on time (hours and minutes) for Relay_B
   digitalWrite(Relay_B, RELAY_ON);
  }
 if (now.hour() == 23 && now.minute() == 30){   // Turn off time (hours and minutes) for Relay_B
   digitalWrite(Relay_B, RELAY_OFF);
  }
// if (now.hour() == 21 && now.minute() == 05){   // Turn on time (hours and minutes) for Relay_C
//   digitalWrite(Relay_C, RELAY_ON);}
// if (now.hour() == 21 && now.minute() == 05){   // Turn off time (hours and minutes) for Relay_C
//   digitalWrite(Relay_C, RELAY_OFF);
// if (now.hour() == 21 && now.minute() == 05){   // Turn on time (hours and minutes) for Relay_D
//   digitalWrite(Relay_D, RELAY_ON);}
// if (now.hour() == 21 && now.minute() == 05){   // Turn off time (hours and minutes) for Relay_D
//   digitalWrite(Relay_D, RELAY_OFF);}

// calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
   
    Serial.println();
    
    Serial.println();
    delay(3000);
    
  h = dht.readHumidity();  // assigns humidity reading as "h"
  f = dht.readTemperature(true);  // assigns fahrenheit reading as "f"
  
  //   Tests temp & humidity to see if preset values are exceed.  
  //   If exceeded, trigger a relay high to power an exhaust fan or heater.
  
  if ((f) >= (hiTempDHT))  //  "if" current temp in fahrenheit is greater than or equal to preset value
  {
    digitalWrite(Relay_E, RELAY_ON);  // powers on Relay E (exhaust fan).
    Serial.print("\t");
    Serial.print(F("Exhausting the heat!")); //  Text printed to serial monitor
    Serial.print("\t");
  }
  else{
  digitalWrite(Relay_E, RELAY_OFF); //  Otherwise relay E is off.
  }
  
  if ((f) <= (lowTempDHT))   //  "if" current temp in fahrenheit is less than or equal to preset value
  {
  digitalWrite(Relay_F, RELAY_ON);  // powers on Relay F (heater).
  Serial.print("\t");
  Serial.print(F("Warming the room!"));  //  Text printed to serial monitor
  Serial.print("\t");
  }
  else{
  digitalWrite(Relay_F, RELAY_OFF);  //  Otherwise Relay F is off.
  }
  
  if ((h) >= (hiHumDHT))  //  "if" current humidity is greater than or equal to preset value
  {
  digitalWrite(Relay_E, RELAY_ON);  //  powers on Relay E (inlet fan).
  Serial.print("\t");
  Serial.print(F("Drying the air!"));  //  Text printed to serial monitor
  Serial.print("\t");
  }
  else{
  digitalWrite(Relay_E, RELAY_OFF);  //  Otherwise Relay E is off.
  }


  if (isnan(f) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } 
  
  else 
  {
    float hi = dht.computeHeatIndex(f, h);
    Serial.print(" Humidity: - "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print(" Fahrenheit: "); 
    Serial.print(f);
    Serial.println(" \t");
    Serial.print("Heat index: ");
    Serial.print(hi);
    Serial.println(" *F");
  }
  delay(1000);
}

As I will one day want to share my project and code and everything with others, I would also like to find how to get one of these boxes grafted into my PHP page. I really like the scrollability and the way it condenses the code to a small portion within the post/comment. Any leads on how I can get that added?

To recap, I seek advice on which type of “server” I should use, and how to get the data to my PHP and MySQL. I already created the table needed to catch the sensor data, but I am still learning how to tie all that together over at hostgator and wordpress.

Thanks a bunch for any consideration, and thanks in advance for any suggestions or good study leads.

hi i tried it with the uno-ethernet, and it works fine.

i used a Raspberry pi as the server. with MySQL.

and this page shows how to create the PHP side of it.

click here

Thanks spirit for the link. I spent most of yesterday reading it, and other projects on that T4A site, but unfortunately, at least as far as I can see, those projects are for interfacing with the Arduino from within the LAN. My goal is to get my data outside of the LAN and still be able to access it.

I currently have my own website that I’d like to have the data sent to. It is a PHP, and though I am wanting to use this site per say, I’m not dead locked into that being my only option. If it turns out that I can’t get the functionality I want, I have no problem going another route, so long as I can connect to my project over the internet. I can find many other uses from my site anyways.

On another note, I made some changes to the above sketch due to an LED not lighting up when I expected it to. As far as I can tell, the code now works, but my subconscious is telling me there are still errors in the code. Can someone take a peak and share your thoughts?

// Coded alarm triggers for Relays A-D.  Adjust for hours and minutes in accordance with 24 hour time format.
//
//****************** 18/6 Light Cycle - RELAY_A ****************************//
 if (now.hour() >= 6 && now.minute() >= 0){   // Turn on time for Veg Lights, ON at 6am
   digitalWrite(Relay_A, RELAY_ON);
  }
 if (now.hour() <= 0 && now.minute() <= 0){   // Turn off time for Veg Lights, OFF at 12am
   digitalWrite(Relay_A, RELAY_OFF);   
  }
  delay(500);
  //******************** FEED TIMES - RELAY_B ****************************************//
 if (now.hour() == 9 && now.minute() == 0){   // Turn on time for FeedPump1, ON 9am
   digitalWrite(Relay_B, RELAY_ON);
  }
 if (now.hour() == 9 && now.minute() == 10){   // Turn off time for FeedPump1, OFF 9:10am
   digitalWrite(Relay_B, RELAY_OFF);
  }
  if (now.hour() == 18 && now.minute() == 0){   // Turn on time for FeedPump1, ON 6pm
   digitalWrite(Relay_B, RELAY_ON);
  }
 if (now.hour() == 18 && now.minute() == 10){   // Turn off time for FeedPump1, OFF 6:10pm
   digitalWrite(Relay_B, RELAY_OFF);
  }
  delay(500);
  //********************** Heat Mat for Clones - RELAY_C *******************************//
 if (now.hour() == 2 && now.minute() == 0){   // Turn on time for HeatMat, ON at 2am
   digitalWrite(Relay_C, RELAY_ON);
  }
 if (now.hour() == 2 && now.minute() == 50){   // Turn off time for HeatMat, OFF at 2:50am, will soon be reassigned
   digitalWrite(Relay_C, RELAY_OFF);           // trigger based on DHT values
  }
  delay(500);

I noticed the mistake when I powered up the Arduino “during” one of the events and the LED never turned on. This I believe was because the sketch was not uploaded “prior” to that event, so I adjusted the test to be >= to turn things on, and <= to turn things off. I have the feeling that this is still not good enough, but I lack the experience to troubleshoot or foresee shortcomings of this method. Can someone please educate me a bit more?

TYIA