updated version. Now with there is an API key. And it works :D
This commit is contained in:
parent
35438eb87f
commit
9814908d7a
7 changed files with 122 additions and 140 deletions
5
ITS_Open_direct/APIKey_template.h
Normal file
5
ITS_Open_direct/APIKey_template.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
// rename this file to "APIKey.h" and fill in a real key!
|
||||
|
||||
String thingtweetAPIKey = "myThingspeakKey";
|
||||
String serverAPIKey = "myserverAPIKey";
|
||||
|
|
@ -26,32 +26,14 @@ Example sketches from Arduino team, Ethernet by Adrian McEwen
|
|||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
//#include <EthernetUdp.h>
|
||||
|
||||
#include "APIKey.h"
|
||||
|
||||
|
||||
char ITSAddress[] = "it-syndikat.org";
|
||||
|
||||
// Local Network Settings
|
||||
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
|
||||
//byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
|
||||
|
||||
unsigned int localPort = 8888; // local port to listen for UDP packets
|
||||
byte mac[] = { 0xD4, 0xBE, 0xD9, 0x9A, 0x7C, 0x95 }; // Must be unique on local network
|
||||
|
||||
// Variable Setup - check if these Variables are needed
|
||||
long lastConnectionTime = 0;
|
||||
boolean lastConnected = true;
|
||||
int failedCounter = 0;
|
||||
|
||||
// NTP stuff
|
||||
//IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server
|
||||
IPAddress timeServer(193,170,62,252); // ana austrian one
|
||||
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov NTP server
|
||||
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov NTP server
|
||||
|
||||
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
|
||||
|
||||
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
|
||||
|
||||
// Initialize Arduino Ethernet Client
|
||||
EthernetClient client;
|
||||
|
@ -59,23 +41,11 @@ EthernetClient client;
|
|||
// Twitter response variables
|
||||
char serverName[] = "it-syndikat.org"; // twitter URL
|
||||
String currentLine = ""; // string to hold the text from server
|
||||
String tweet = ""; // string to hold the tweet
|
||||
boolean readingTweet = false; // if you're currently reading the tweet
|
||||
|
||||
|
||||
// Specific variables
|
||||
// Status
|
||||
int hsopen =2;
|
||||
int ethernetstatus = -1;
|
||||
/*
|
||||
0=not set
|
||||
1=read thingspeak response
|
||||
2=read tweet
|
||||
*/
|
||||
int readStatus=0;
|
||||
|
||||
// Twitter message, intermediate part
|
||||
String tmsg = " Hackerspace at ";
|
||||
|
||||
//debug options
|
||||
boolean debug = true;
|
||||
|
@ -130,50 +100,22 @@ void setup()
|
|||
|
||||
void loop()
|
||||
{
|
||||
switch(readStatus){
|
||||
case 0:{
|
||||
readStatus = readButtons();
|
||||
break;
|
||||
}
|
||||
case 1:{
|
||||
readStatus = readServerReturn();
|
||||
if(debug){
|
||||
Serial.print("got it ... status: ");
|
||||
Serial.println(readStatus);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2:{
|
||||
readStatus = readServerStatus();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if Arduino Ethernet needs to be restarted
|
||||
|
||||
lastConnected = client.connected();
|
||||
readButtons();
|
||||
}
|
||||
|
||||
|
||||
int readButtons(){
|
||||
void readButtons(){
|
||||
int ret=0;
|
||||
if((digitalRead(topen)==LOW)&&(hsopen!=1)){
|
||||
startEthernet();
|
||||
//startEthernet();
|
||||
setRoom(2);
|
||||
TriggerServerUpdate(true);
|
||||
hsopen=1;
|
||||
ret=1;
|
||||
setRoom(hsopen);
|
||||
}
|
||||
if((digitalRead(tclose)==LOW)&&(hsopen!=0)){
|
||||
startEthernet();
|
||||
//startEthernet();
|
||||
setRoom(2);
|
||||
TriggerServerUpdate(false);
|
||||
hsopen=0;
|
||||
ret=1;
|
||||
setRoom(hsopen);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void startEthernet()
|
||||
|
@ -236,12 +178,14 @@ void TriggerServerReq() {
|
|||
// make HTTP GET request to server:
|
||||
client.println("GET /status-s.php HTTP/1.1");
|
||||
client.println("HOST: it-syndikat.org");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
readServerStatus();
|
||||
//readServerReturn();
|
||||
}else{
|
||||
if(debug){Serial.println("Not connected...");}
|
||||
}
|
||||
// note the time of this connect attempt:
|
||||
readStatus=2;
|
||||
}
|
||||
|
||||
void TriggerServerUpdate(boolean stat) {
|
||||
|
@ -251,19 +195,22 @@ void TriggerServerUpdate(boolean stat) {
|
|||
if(debug){Serial.println("making HTTP request...");}
|
||||
// make HTTP GET request to server:
|
||||
String s =(stat?"true":"false");
|
||||
client.println("GET /update.php?open=" + s + " HTTP/1.1");
|
||||
client.println("GET /update.php?open=" + s + "&apikey="+serverAPIKey+" HTTP/1.1");
|
||||
client.println("HOST: it-syndikat.org");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
readServerStatus();
|
||||
}else{
|
||||
if(debug){Serial.println("Not connected...");}
|
||||
}
|
||||
// note the time of this connect attempt:
|
||||
readStatus=1;
|
||||
}
|
||||
|
||||
//clean this mess up
|
||||
//reads out the status returned by the server and sets the LED's appropriately.
|
||||
int readServerStatus() {
|
||||
if (client.connected()) {
|
||||
char lastsign='0';
|
||||
boolean readStatus = false;
|
||||
while(client.connected()) {
|
||||
if (client.available()) {
|
||||
// read incoming bytes:
|
||||
char inChar = client.read();
|
||||
|
@ -271,62 +218,59 @@ int readServerStatus() {
|
|||
// add incoming byte to end of line:
|
||||
currentLine += 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 you're currently reading the bytes of a tweet,
|
||||
// add them to the tweet String:
|
||||
if (readingTweet) {
|
||||
if (inChar != '<') {
|
||||
tweet += inChar;
|
||||
}
|
||||
else {
|
||||
if (readStatus) {
|
||||
// if you got a "<" character,
|
||||
// you've reached the end of the tweet:
|
||||
readingTweet = false;
|
||||
if(debug){Serial.println("Message:");}
|
||||
if(debug){Serial.println(tweet);}
|
||||
if(debug){Serial.println(tweet.startsWith("true", 0));}
|
||||
if(debug){Serial.println(tweet.startsWith("false", 0));}
|
||||
if(tweet.startsWith("true", 0)){hsopen=1;}
|
||||
else {if(tweet.startsWith("false", 0)){hsopen=0;}
|
||||
else {hsopen=-1;}}
|
||||
if(currentLine.startsWith("true", 0)){
|
||||
if(debug){Serial.println("");}
|
||||
hsopen=1;
|
||||
setRoom(hsopen);
|
||||
// close the connection to the server:
|
||||
client.stop();
|
||||
return 0;
|
||||
}
|
||||
if(currentLine.startsWith("false", 0)){
|
||||
if(debug){Serial.println("");}
|
||||
hsopen=0;
|
||||
setRoom(hsopen);
|
||||
// close the connection to the server:
|
||||
client.stop();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// if the current line ends with <text>, it will
|
||||
// be followed by the tweet:
|
||||
if ( currentLine.endsWith("<status>")) {
|
||||
// tweet is beginning. Clear the tweet string:
|
||||
readingTweet = true;
|
||||
tweet = "";
|
||||
if (inChar != '\r') {// removes /r so we dan test if the header end with two newlines, hacky but works.
|
||||
lastsign = inChar;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 2;
|
||||
hsopen=-1;
|
||||
setRoom(hsopen);
|
||||
client.stop();
|
||||
}
|
||||
|
||||
|
||||
//clean this mess up
|
||||
int readServerReturn() {
|
||||
//This just reads the server return and logs it to the serial
|
||||
void readServerReturn() {
|
||||
//if(debug){Serial.println("readServerReturn ... ");}
|
||||
if (client.connected()) {
|
||||
while(client.connected()) {
|
||||
if (client.available()) {
|
||||
char c = client.read();
|
||||
Serial.print(c);
|
||||
return 1;
|
||||
if(debug){Serial.print(c);}
|
||||
}
|
||||
return 1;
|
||||
}else{
|
||||
Serial.println();
|
||||
Serial.println("disconnecting.");
|
||||
}
|
||||
if(debug){Serial.println();}
|
||||
if(debug){Serial.println("disconnecting.");}
|
||||
client.stop();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
<!--
|
||||
Copypasta:
|
||||
|
||||
char thingSpeakAddress[] = "api.thingspeak.com";
|
||||
|
||||
|
||||
-->
|
||||
<?php
|
||||
|
||||
|
||||
$username = ($_POST['t_user']);
|
||||
$password = ($_POST['t_pass']);
|
||||
$message = ($_POST['t_update']);
|
||||
|
||||
$url = 'http://twitter.com/statuses/update.xml';
|
||||
$curl_handle = curl_init();
|
||||
curl_setopt($curl_handle, CURLOPT_URL, "$url");
|
||||
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
|
||||
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($curl_handle, CURLOPT_POST, 1);
|
||||
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
|
||||
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
|
||||
$buffer = curl_exec($curl_handle);
|
||||
curl_close($curl_handle);
|
||||
if (empty($buffer)) {
|
||||
echo "<p align=\"center\" >".'Sorry, due to an error your Twitter status could not be updated! Please check your username/password!'."</p>";
|
||||
} else {
|
||||
echo "<p align=\"center\">".'Your Twitter status has successfully been updated!'."</p>";
|
||||
}
|
||||
?>
|
||||
|
||||
|
9
server/status-s.php
Normal file
9
server/status-s.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
header('Content-type: text/plain');
|
||||
$str=file_get_contents('status.txt');
|
||||
$arr=explode(',',$str);
|
||||
$open=$arr[0];
|
||||
$date=$arr[1]; //lastchange: upadate via file see explode/implode
|
||||
echo $open;
|
||||
?>
|
||||
|
33
server/status.php
Normal file
33
server/status.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
header('Content-type: application/json');
|
||||
$str=file_get_contents('status.txt');
|
||||
$arr=explode(',',$str);
|
||||
$open=$arr[0];
|
||||
$date=$arr[1]; //lastchange: upadate via file see explode/implode
|
||||
echo <<< eot
|
||||
{
|
||||
"api":"0.12",
|
||||
"space":"IT-Syndikat",
|
||||
"url":"http://it-syndikat.org",
|
||||
"icon":{
|
||||
"open":"http://it-syndikat.org/images/ITS_Open.png",
|
||||
"closed":"http://it-syndikat.org/images/ITS_Closed.png"
|
||||
},
|
||||
"address":"Tschamlerstrasse 3, 6020 Innsbruck, Austria",
|
||||
"contact":{
|
||||
"phone":"+43512563468",
|
||||
"twitter":"@ItSyndikat",
|
||||
"email":"wir@it-syndikat.org",
|
||||
"ml":"its-public@lists.catbull.com"
|
||||
},
|
||||
"open":$open,
|
||||
"lastchange":$date,
|
||||
"logo":"http://it-syndikat.org/images/its_l.png",
|
||||
"lat":47.2578,
|
||||
"lon":11.3961
|
||||
}
|
||||
|
||||
eot;
|
||||
|
||||
?>
|
||||
|
1
server/status.txt
Normal file
1
server/status.txt
Normal file
|
@ -0,0 +1 @@
|
|||
false,1361048493
|
22
server/update.php
Normal file
22
server/update.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
header('Content-type: text/plain');
|
||||
if(isset($_GET['apikey']) && ($_GET['apikey']=='apikey1' || $_GET['apikey']=='apikey2')){
|
||||
if(isset($_GET['open']) && $_GET['open']=='true')
|
||||
{
|
||||
file_put_contents ( 'status.txt' , 'true,'.time() );
|
||||
echo "true";
|
||||
}else if(isset($_GET['open']) && $_GET['open'] =='false')
|
||||
{
|
||||
file_put_contents ( 'status.txt' , 'false,'.time() );
|
||||
echo "false";
|
||||
}
|
||||
}else{
|
||||
$str=file_get_contents('status.txt');
|
||||
$arr=explode(',',$str);
|
||||
$open=$arr[0];
|
||||
$date=$arr[1]; //lastchange: upadate via file see explode/implode
|
||||
echo "API Key Missing!\n";
|
||||
echo $open;
|
||||
}
|
||||
?>
|
||||
|
Reference in a new issue