line 73... cheers
////
//
// General code from http://www.pushingbox.com for Arduino + Ethernet Shield (official) v1.2
//
////
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN A1 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
/////////////////
// MODIFY HERE //
/////////////////
byte mac[] = {
0x00, 0x13, 0x02, 0xCC, 0xD1, 0x19 }; // Be sure this address is unique in your network
byte ip[] = { 192,168,0, 101 };
//Your secret DevID from PushingBox.com. You can use multiple DevID on multiple Pin if you want
char DEVID1[] = "v1CC65E36Dxxxx00"; //Scenario : 1
char DEVID2[] = "v9565F5FAAxxxx41"; //Scenario : 2
//Numeric Pin where you connect your switch
#define pinDevid1 8// mm
#define pinDevid2 9// mm
/// 4 6 11 12 13 14
// rs, en, D4, D5 D6 D7
LiquidCrystal lcd(A4, A5 , 3, 2, A3, A2);
int sensorPin = A0; // select the input pin for the detector
//int countMAILW=0;
int count = 0;
int FED = 0;
int countMAILF = 0;
int sensorValue = 0; // variable to store the value coming from the detector
byte latch = 0; //initial variable
const int pump = 4;
const int fan = 5;
// Debug mode
#define DEBUG true
//////////////
// End //
//////////////
char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false;
boolean pinDevid2State = false;
boolean lastConnected = false; // state of the connection last time through the main loop
// 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):
EthernetClient client;
EthernetServer server(80);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print("W-");
Serial.begin(9600);
pinMode(pinDevid1, OUTPUT);// pin 8
//pinMode(pinDevid2, INPUT);// pin 9
pinMode(fan, OUTPUT);//
pinMode(pump, OUTPUT);//
// pinMode(relay, OUTPUT);
dht.begin();
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
lcd.setCursor(0,0);
lcd.print("Disconnected ");
//while(true);// no point in carrying on, so do nothing forevermore:
}
else{
Serial.println("Ethernet ready");
lcd.setCursor(0,0);
lcd.print("Net OK ");
delay(2000);
lcd.clear();
Serial.print("My IP address: ");// print the Ethernet board/shield's IP address:
Serial.println(Ethernet.localIP());
lcd.print(Ethernet.localIP());
delay(2000);
lcd.clear();
FED=0;
lcd.setCursor(10,0);
lcd.print("F");
lcd.print(FED);
}
delay(500);// give the Ethernet shield a second to initialize:
}}
void loop()
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(analogChannel));
client.println("
");
}
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();
}
}
{
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % ");
Serial.print("Temp: ");
Serial.print(t);
Serial.println("C");
}
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(8, 1);
lcd.print("H:");
lcd.print(h);
lcd.print("%");
//delay(2000);
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(t);
//lcd.print("C");
//delay(2000);
if(t>=26)
{ digitalWrite(fan, HIGH);
lcd.setCursor(15, 0);
lcd.print("F");}
if(t<=24)
{ digitalWrite(fan, LOW);
lcd.setCursor(15, 0);
lcd.print("_");}
lcd.setCursor(0,0);
lcd.print("W-");
lcd.setCursor(2,0);// (note: line 1 is the second row, since counting begins with 0):
// print the number of seconds since reset:
lcd.print(sensorValue);
sensorValue = analogRead(sensorPin); // read the value from the soil detector:
Serial.print("moisture sensor reads ");
Serial.println( sensorValue );
delay(100);
if(sensorValue<750 && count==0)
{
//countMAILF=0;
delay(2000);
if(sensorValue<750 && count==0)
delay(2000);
digitalWrite(pinDevid1, HIGH);
digitalWrite(pump, LOW);
lcd.setCursor(14, 0);
lcd.print("P");
delay(10000);
digitalWrite(pump, HIGH);
lcd.setCursor(14, 0);
lcd.print("R");
//digitalWrite(pinDevid1, HIGH);
}
count++;// this increments the count by 1
if(sensorValue>800)
//if(countMAILF ==0)
{digitalWrite(pinDevid1, LOW);
digitalWrite(pump, LOW);
lcd.setCursor(14, 0);
lcd.print("_");
count=0;}
if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false )// switch on pinDevid1 is ON
{
if(DEBUG){
Serial.println("pinDevid1 is HIGH");
}
pinDevid1State = true;
sendToPushingBox(DEVID1);//Sending request to PushingBox when the pin is HIGHT
delay(500);
}
delay(1000);
if (digitalRead(pinDevid1) == LOW && pinDevid1State == true) // switch on pinDevid1 is OFF
{
if(DEBUG){
Serial.println("pinDevid1 is LOW");
}
pinDevid1State = false;
sendToPushingBox(DEVID2);
// digitalWrite(9, HIGH);
}
//digitalWrite(pump, LOW);
delay(500);
// countMAILF = val;
//DEBUG part
// this write the respons from PushingBox Server.
// You should see a "200 OK"
if (client.available()) {
char c = client.read();
if(DEBUG){
Serial.print(c);
}
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
if(DEBUG){
Serial.println();
}
if(DEBUG){
Serial.println("disconnecting.");
}
client.stop();
}
lastConnected = client.connected();
}
//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]){
client.stop();
if(DEBUG){
Serial.println("connecting...");
lcd.setCursor(0,0);
lcd.print("connecting.... ");
delay(1000);
}
if (client.connect(serverName, 80)) {
if(DEBUG){
Serial.println("connected ");
lcd.setCursor(0,0);
lcd.print("connected ");
delay(1000);
}
if(DEBUG){
Serial.println("sendind request");
}
client.print("GET /pushingbox?devid=");
client.print(devid);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
lcd.setCursor(0,0);
lcd.print(" sent! ");
delay(5000);
lcd.clear();
}
else {
if(DEBUG){
Serial.println("connection failed");
}
}}}