Switched to server mode instead of client mode

This commit is contained in:
Will Bradley 2012-08-18 23:09:30 -07:00
parent 97d1534066
commit 84e9dd1a03
2 changed files with 113 additions and 57 deletions

View File

@ -45,8 +45,8 @@ byte DS1307::bcdToDec(byte val)
void DS1307::stopDs1307()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(0x80);
Wire.write(0);
Wire.write(0x80);
Wire.endTransmission();
}
*/
@ -65,15 +65,15 @@ void DS1307::setDateDs1307(byte second, // 0-59
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
@ -88,19 +88,19 @@ void DS1307::getDateDs1307(byte *second,
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
/*

View File

@ -60,8 +60,6 @@
#include <Ethernet.h> // Ethernet stuff
#include <SPI.h>
#include <Server.h>
#include <Client.h>
#include <DS1307.h> // DS1307 RTC Clock/Date/Time chip library
@ -160,17 +158,13 @@ boolean privmodeEnabled = false; // Switch for ena
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xAB, 0xBA, 0xDA, 0xDE, 0xFE, 0xED };
byte ip[] = { 192,168,1,199 };
byte server[] = { 192,168,1,200 }; // webserver for logging, access control, etc
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
// 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):
Client client(server, 80);
// strings for storing results from web server
String httpresponse = "";
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
/* Create an instance of the various C++ libraries we are using.
*/
@ -213,6 +207,9 @@ const prog_uchar statusMessage6[] PROGMEM = {"Door2UnlockedState_1_Lock
void setup(){ // Runs once at Arduino boot-up
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Wire.begin(); // start Wire library as I2C-Bus Master
@ -252,10 +249,6 @@ void setup(){ // Runs once at Arduino boot-up
Serial.begin(57600); // Set up Serial output at 8,N,1,57600bps
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// wait a bit
delay(10000);
logReboot();
chirpAlarm(1); // Chirp the alarm to show system ready.
@ -267,11 +260,74 @@ void setup(){ // Runs once at Arduino boot-up
}
void loop() // Main branch, runs over and over again
{
// if the server's disconnected, stop the client:
if (!client.connected()) {
// listen for incoming clients
EthernetClient client = server.available();
String readString = String(100); //string for fetching data from address
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
// 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();
if(readString.indexOf('?') > 0) {
client.println("hello!");
}
// output status
client.println("<pre>");
client.print("Alarm armed:");
client.println(alarmArmed,DEC);
client.print("Alarm activated:");
client.println(alarmActivated,DEC);
client.print("Alarm 3:");
client.println(pollAlarm(3),DEC);
client.print("Alarm 2:");
client.println(pollAlarm(2),DEC);
client.print("Door 1 locked:");
client.println(door1Locked);
client.print("Door 2 locked:");
client.println(door2Locked);
client.println("</pre>");
client.println("<a href=");
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();
}
readCommand(); // Check for commands entered at serial console
@ -945,18 +1001,18 @@ void PROGMEMprintln(const prog_uchar str[]) // Function to retrieve logging s
char c;
if(!str) return;
if(client.connect()) {
client.print("GET /~access/log/?device=door&data=");
//if(client.connect(server, 80) > 0) {
// client.print("GET /~access/log/?device=door&data=");
while((c = pgm_read_byte(str++))){
Serial.print(c,BYTE);
client.print(c,BYTE);
Serial.write(c);
// client.write(c);
}
client.println(" HTTP/1.0");
client.println();
client.stop();
//client.println(" HTTP/1.0");
//client.println();
//client.stop();
// reset values coming from http
httpresponse = "";
}
// httpresponse = "";
//}
Serial.println();
}
@ -965,18 +1021,18 @@ void PROGMEMprint(const prog_uchar str[]) // Function to retrieve logging str
char c;
if(!str) return;
if(client.connect()) {
client.print("GET /~access/log/?device=door&data=");
//if(client.connect(server, 80) > 0) {
// client.print("GET /~access/log/?device=door&data=");
while((c = pgm_read_byte(str++))){
Serial.print(c,BYTE);
client.print(c,BYTE);
Serial.write(c);
// client.write(c);
}
client.println(" HTTP/1.0");
client.println();
client.stop();
// client.println(" HTTP/1.0");
// client.println();
// client.stop();
// reset values coming from http
httpresponse = "";
}
// httpresponse = "";
//}
}