Compare commits

...

6 Commits

3 changed files with 157 additions and 172 deletions

View File

@ -0,0 +1 @@
BasedOnStyle: Chromium

View File

@ -1,6 +1,4 @@
// rename this file to "APIKey.h" and fill in a real key!
String thingtweetAPIKey = "myThingspeakKey";
String serverAPIKey = "myserverAPIKey";
String pingGetAPIKey ="pingGetApiKey";
#define UPDATE_API_KEY "updateApiKey"
#define PING_GET_API_KEY "pingGetApiKey"

View File

@ -12,21 +12,20 @@ Example sketches from Arduino team, Ethernet by Adrian McEwen
Based on the Ethernet to Thingspeak exaple by Hans Scharler
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SPI.h>
#include "APIKey.h"
#include "TimerOne.h"
byte mac[] = { 0xD4, 0xBA, 0xD9, 0x9A, 0x7C, 0x95 }; // Must be unique on local network
// Must be unique on local network
byte mac[] = {0xD4, 0xBA, 0xD9, 0x9A, 0x7C, 0x95};
// Initialize Arduino Ethernet Client
EthernetClient client;
// Twitter response variables
#define SERVER_DOMAIN "spaceapi.it-syndikat.org" // URL
String currentLine = ""; // string to hold the text from server
// Specific variables
// Status
@ -56,9 +55,11 @@ const int tclose = 3;
const int beeper = A5;
#define MODE_OPEN_STATE 0
#define MODE_PING_STATE 1
void setLeds() {
if (pingtimer>=0)
{
if (pingtimer >= 0) {
if (hsopen) { // If space is not closed, beep three times with
// beep
if ((pingtimer > 220) && ((pingtimer / 5) % 2 == 1)) {
@ -101,7 +102,8 @@ void setLeds(){
if (ledtimer < (ledspan / 2)) {
analogWrite(glight, (255 / (ledspan / 2) * ledtimer)); // fade up
} else {
analogWrite(glight, 255-(255/(ledspan/2)*(ledtimer-(ledspan/2))));//fade down
analogWrite(glight, 255 - (255 / (ledspan / 2) *
(ledtimer - (ledspan / 2)))); // fade down
}
analogWrite(rlight, 0);
break;
@ -128,7 +130,8 @@ void setLeds(){
analogWrite(rlight, tmp); // fade up
} else {
int tmp = 255-(255/(ledspan/2)*(ledtimer-(ledspan/2)));
int tmp =
255 - (255 / (ledspan / 2) * (ledtimer - (ledspan / 2)));
analogWrite(glight, tmp); // fade down
analogWrite(rlight, tmp); // fade down
}
@ -139,7 +142,9 @@ void setLeds(){
if (ledtimer < (ledspan / 2)) {
analogWrite(rlight, (255 / (ledspan / 2) * ledtimer)); // fade up
} else {
analogWrite(rlight, 255-(255/(ledspan/2)*(ledtimer-(ledspan/2))));//fade down
analogWrite(rlight,
255 - (255 / (ledspan / 2) *
(ledtimer - (ledspan / 2)))); // fade down
}
analogWrite(glight, 0);
break;
@ -178,8 +183,7 @@ void setRoom(int statuss){
setLeds();
}
void startEthernet()
{
void startEthernet() {
client.stop();
if (debug) {
Serial.println("Connecting Arduino to network...");
@ -189,16 +193,13 @@ void startEthernet()
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
if (Ethernet.begin(mac) == 0) {
if (debug) {
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
setEth(0);
}
else
{
} else {
if (debug) {
Serial.println("Arduino connected to network using DHCP");
Serial.println();
@ -217,6 +218,8 @@ void startEthernet()
void readServerStatus(int mode) {
char lastsign = '0';
boolean readStatus = false;
String currentLine = ""; // string to hold the text from server
while (client.connected()) {
if (client.available()) {
// read incoming bytes:
@ -225,26 +228,34 @@ void readServerStatus(int mode) {
// add incoming byte to end of line:
currentLine += inChar;
if(debug){Serial.print(inChar);}
if (debug) {
Serial.print(inChar);
}
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
if (lastsign == '\n') { // /r/n /r/n is the end of a header
readStatus = true; // start to parse the content of the line
if(debug){Serial.println("##END OF HEADER##");}
if (debug) {
Serial.println("##END OF HEADER##");
}
}
}
if (readStatus) {
if (currentLine.startsWith("true", 0)) {
if(debug){Serial.println("");}
if (debug) {
Serial.println("");
}
switch (mode) {
case 0:
case MODE_OPEN_STATE:
setRoom(1);
break;
case 1:
if(debug){Serial.println("Wink Wink");}
case MODE_PING_STATE:
if (debug) {
Serial.println("Wink Wink");
}
pingtimer = pingspan;
break;
}
@ -253,13 +264,17 @@ void readServerStatus(int mode) {
return;
}
if (currentLine.startsWith("false", 0)) {
if(debug){Serial.println("");}
if (debug) {
Serial.println("");
}
switch (mode) {
case 0:
case MODE_OPEN_STATE:
setRoom(0);
break;
case 1:
if(debug){Serial.println("No Wink");}
case MODE_PING_STATE:
if (debug) {
Serial.println("No Wink");
}
break;
}
// close the connection to the server:
@ -267,7 +282,8 @@ void readServerStatus(int mode) {
return;
}
}
if (inChar != '\r') {// removes /r so we dan test if the header end with two newlines, hacky but works.
if (inChar != '\r') { // removes /r so we dan test if the header end with
// two newlines, hacky but works.
lastsign = inChar;
}
}
@ -276,32 +292,36 @@ void readServerStatus(int mode) {
client.stop();
}
void TriggerServerReq(String s, int mode) {
void TriggerServerReq(String req, int mode) {
// attempt to connect, and wait a millisecond:
if(debug){Serial.println("connecting to server... Status req");}
if (debug) {
Serial.println("connecting to server... Status req");
}
if (client.connect(SERVER_DOMAIN, 80)) {
if(debug){Serial.println("making HTTP request...");}
// make HTTP GET request to server:
client.println("GET "+ s +" HTTP/1.1");
if (debug) {
Serial.println("making HTTP request: " + req);
}
// make HTTP request to server:
client.println(req + " HTTP/1.1");
client.println("HOST: " SERVER_DOMAIN);
client.println("Connection: close");
client.println();
readServerStatus(mode);
} else {
if(debug){Serial.println("Not connected...");}
if (debug) {
Serial.println("Not connected...");
}
}
// note the time of this connect attempt:
}
void RequestState() {
TriggerServerReq("/api/status-s.php",0);
TriggerServerReq("GET /api/status-s.php", MODE_OPEN_STATE);
}
// Status LED pins
void setup()
{
void setup() {
pinMode(glight, OUTPUT);
pinMode(rlight, OUTPUT);
pinMode(beeper, OUTPUT);
@ -332,39 +352,21 @@ void setup()
RequestState();
}
void TriggerServerUpdate(boolean stat) {
// attempt to connect, and wait a millisecond:
if(debug){Serial.println("connecting to server... Update Req");}
if (client.connect(SERVER_DOMAIN, 80)) {
if(debug){Serial.println("making HTTP request...");}
// make HTTP GET request to server:
String s =(stat?"true":"false");
client.println("GET /api/update.php?open=" + s + "&apikey="+serverAPIKey+" HTTP/1.1");
client.println("HOST: " SERVER_DOMAIN);
client.println("Connection: close");
client.println();
readServerStatus(0);
}else{
if(debug){Serial.println("Not connected...");}
}
// note the time of this connect attempt:
}
void readButtons() {
if ((digitalRead(topen) == LOW) && (hsopen != 1)) {
// startEthernet();
setRoom(2);
TriggerServerUpdate(true);
TriggerServerReq("POST /api/update.php?open=true&apikey=" UPDATE_API_KEY, MODE_OPEN_STATE);
}
if ((digitalRead(tclose) == LOW) && (hsopen != 0)) {
// startEthernet();
setRoom(2);
TriggerServerUpdate(false);
TriggerServerReq("POST /api/update.php?open=false&apikey=" UPDATE_API_KEY, MODE_OPEN_STATE);
}
}
void RequestPing() {
TriggerServerReq("/api/ping-get.php?apikey="+pingGetAPIKey,1);
TriggerServerReq("POST /api/ping-get.php?apikey=" PING_GET_API_KEY, MODE_PING_STATE);
}
void launchUpdate() {
@ -375,23 +377,7 @@ void launchUpdate(){
}
}
void loop()
{
void loop() {
readButtons();
launchUpdate();
}
//This just reads the server return and logs it to the serial
// @deprecated
void readServerReturn() {
//if(debug){Serial.println("readServerReturn ... ");}
while(client.connected()) {
if (client.available()) {
char c = client.read();
if(debug){Serial.print(c);}
}
}
if(debug){Serial.println();}
if(debug){Serial.println("disconnecting.");}
client.stop();
}