Skip to content

Instantly share code, notes, and snippets.

@allep
Created January 19, 2012 18:21
Show Gist options
  • Save allep/1641642 to your computer and use it in GitHub Desktop.
Save allep/1641642 to your computer and use it in GitHub Desktop.
Pir hub node
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Alessandro Paganelli <alessandro.paganelli@gmail.com>
*/
/*
* Script usato per comunicare con pachube, tramite ethernet shield.
*/
#include <SPI.h>
#include <Ethernet.h>
// Put your feed ID here
#define _FEED_ID 0000
// Put your pachube key here
#define _PACHUBE_KEY ABCD
#define _DEBUG 0
#define _UPDATE_INTERVAL 30000
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {0xAB, 0xCD, 0xEF, 0x00, 0x12, 0x34};
// choose your IP address
byte ip[] = {192, 168, 0, 20 };
// Setup the IP address of your gateway
byte gateway[] = {192, 168, 0, 254};
// Setup the netmask of your network
byte subnet[] = {255, 255, 255, 0};
// The address of the server you want to connect to (pachube.com):
byte server[] = { 173, 203, 98, 29 };
// initialize the library instance:
Client client(server, 80);
String stringa("1, yes");
char readCharacter;
unsigned long time;
void
setup()
{
// start the ethernet connection and serial port:
Ethernet.begin(mac, ip, gateway, subnet); // This should work with Arduino prior to 1.0
// give the ethernet module time to boot up:
delay(1000);
Serial.begin(9600);
time = millis();
}
void
loop()
{
if (Serial.available() > 0)
{
// I received something
readCharacter = Serial.read();
if (readCharacter == 'A')
{
// Read the whole message (starting with a "A")
for (int i = 0; i < 4; i++)
{
readCharacter = Serial.read();
}
#if _DEBUG
Serial.println("Message received!");
#endif
// I send a message over the Internet only if enough time has passed
// since last update
if (millis() - time >= _UPDATE_INTERVAL)
{
time = millis();
SendDataOverEthernet();
}
#if _DEBUG
else
{
Serial.println("Message not sent - too short update interval!");
}
#endif
}
}
}
// this method makes a HTTP connection to the server:
void
SendDataOverEthernet()
{
// Check the connection
if (!client.connected())
{
// I must connect
if(!client.connect())
{
#if _DEBUG
Serial.println("Connection failed");
#endif
delay(1000);
return;
}
}
// send the HTTP PUT request.
// fill in your feed address here:
client.print("PUT /v2/feeds/_FEED_ID.csv HTTP/1.1\n");
client.print("Host: api.pachube.com\n");
// fill in your Pachube API key here:
client.print("X-PachubeApiKey: _PACHUBE_KEY\n");
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
client.println(stringa.length(), DEC);
// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close\n");
// here's the actual content of the PUT request:
client.println(stringa);
client.println();
#if _DEBUG
while (client.available())
{
readCharacter = client.read();
Serial.print(readCharacter);
}
#endif
client.stop();
}
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Alessandro Paganelli <alessandro.paganelli@gmail.com>
*/
/*
* Questo script implementa un semplice ricevitore a 433 MHz, che ritrasmette ciò che ha
* ricevuto sulla seriale.
*/
#include <simple_wireless.h>
#include <VirtualWire.h>
#include <string.h>
#define _TIMEOUT 5000 // 5 seconds
/* declaration of the receiver object */
sw_receiver rx;
/* declaration of the data unit to be used for communications */
sw_dataUnit aDataUnit;
void
setup()
{
Serial.begin(9600);
rx.setSpeed(2000);
rx.setLocalAddress(0x02);
rx.setRxPin(8);
}
void
loop()
{
/* start reception */
if(rx.receive(aDataUnit, _TIMEOUT))
Serial.println("Alarm"); // Send an alarm message over the serial line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment