#include <DHT.h>
#include <SPI.h>
#include <WiFi101.h>


#define pump 3
#define blower 4
#define uv 5
#define biopump 7


#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);




char ssid[] = "Plaas"; //  your network SSID (name)
char pass[] = "itfc4700";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
int deviceID=230;
int userID=23;

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.smart.afriponics-sa.com";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
float t = 0;
float h = 0;
String data = "";
long postTimer=(5*60*1000);
long readTimer=0;
long postInterval= 1500;



void setup() {
  //Initialize serial and wait for port to open:
  dht.begin();
  Serial.begin(9600);
 

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    //delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();


}

void postData(){
  getReadings();
    Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("POST /includes/switchTEST.php HTTP/1.1");
    client.println("Host: www.smart.afriponics-sa.com");
    //client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;: text/html");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    //client.println(data);
    client.println(data);
    client.println();
    client.println();
    delay(10);
    if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
    Serial.println(data);
  }
  postTimer=millis();

}

void getReadings(){
    h =  dht.readHumidity(); 
  t =  dht.readTemperature(); 
 data = "userID=" + String(userID) + "&deviceID=" + String(deviceID);
readTimer=millis();
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop() {


if ((millis()-postTimer)>postInterval){
  postData();
}
 if (client.available()) {
    char c = client.read();
    Serial.print(c);
   
  }
  
}





