#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDHCP.h>
// MAC Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const char* ip_to_str(const uint8_t*);
const char* hostName();
int setHostName(const char* newHostName);
// Initialize the Ethernet server library
//Server server(8080);
int localstatus = 7; //status
int gatestatus = 4; //old status
int s;
byte serverip[] = { 142, 55, 49, 114 };
Client client(serverip, 80); //apache web server running on port 80
void setup()
{
Serial.begin(9600);
pinMode (2,INPUT); //test pin
Serial.println("Attempting to obtain a DHCP lease...");
EthernetDHCP.begin(mac);
// Since we're here, it means that we now have a DHCP lease, so we print
// out some information.
const byte* ipAddr = EthernetDHCP.ipAddress();
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
Serial.println("A DHCP lease has been obtained.");
Serial.print("My IP address is ");
Serial.println(ip_to_str(ipAddr));
Serial.print("Gateway IP address is ");
Serial.println(ip_to_str(gatewayAddr));
Serial.print("DNS IP address is ");
Serial.println(ip_to_str(dnsAddr));
// Start the server
Serial.println("Starting Server.");
//server.begin();
}
void loop()
{
EthernetDHCP.maintain();
Serial.println("Comparing Server Status");
gatestatus = readvalues();
Serial.println();
Serial.println();
Serial.println();
Serial.println("Gatestatus:");
Serial.println(gatestatus);
Serial.println();
Serial.println();
Serial.println("Localstatus:");
Serial.println(localstatus);
Serial.println();
Serial.println();
if (localstatus!= gatestatus){
Serial.println("Statuses are not equal");
send_to_server();
}
Serial.println("Gate Status is");
Serial.println(gatestatus);
if (digitalRead(2) == HIGH)
{
// Serial.println("status 1");
localstatus = 1;
}
if (digitalRead(2) == LOW)
{
//Serial.println("Status 0");
localstatus = 0;
}
//)
// give the web browser time to receive the data
delay(500);
// close the connection:
client.stop();
}
// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
static char buf[16];
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
return buf;
}
int readvalues()
{
Serial.println("In Read Values");
while(1)
if (client.connect()) {
Serial.println("Readvalues client connected");
client.println("GET /headgateget.php");
client.print( millis() ); // prevent caching, like justjed said a post before
client.println("HTTP/1.0"); // no need for 1.1
client.println("Host: cs14.sheridanc.on.ca");
// client.println("Connection: close");
client.println();
//delay(7);
while (client.connected()){
if (client.available()) {
Serial.println("client is avaliable printing page");
char c = client.read();
Serial.println("parsed page, C=");
Serial.println(c);
delay(100);
s = c - '0';
Serial.println("S is equal to:");
Serial.println(s);
return s;
}
}
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
void send_to_server()
{
while(1){
Serial.println("sending to server function");
if (client.connect()) {
Serial.println("Set Server connected");
client.print("GET /headgatetestset.php?Status="); // Path only, not the complete URI
client.print(localstatus);
client.print("&");
client.print( millis() ); // prevent caching, like justjed said a post before
client.println(" HTTP/1.0"); // no need for 1.1
client.println("Host: cs14.sheridanc.on.ca");
client.println("Connection: close");
client.println();
Serial.println("posted to db");
return;
}
}
}
I have a question about using client.connected in two different functions. I have one function "readvalues()" and that connects and pulls the data from the server just fine, and soon after the program is going to run another function "send_to_server()" when it runs this the statement "if client.connect()" is never true, did I break something to do with connecting in the last function?