Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
monopatis committed Jul 15, 2013
1 parent 72849db commit ba66a9d
Show file tree
Hide file tree
Showing 923 changed files with 98,769 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Arduino/Garden_coap/CoAP_Garden_Alarms/CoAP_Garden_Alarms.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Arduino Coap Garden Alarms Programmer
* Monopatis Dimitris
*/


//Include XBEE Libraries
#include <XBee.h>
#include <XbeeRadio.h>
#include <Time.h>
#include <TimeAlarms.h>
//Include CoAP Libraries
#include <coap.h>
#include "myPOSTSensor.h"
#include "myPOSTprogram.h"
#include "myPOSTtime.h"

//Create the XbeeRadio object we'll be using
XBeeRadio xbee = XBeeRadio();
//Create a reusable response object for responses we expect to handle
XBeeRadioResponse response = XBeeRadioResponse();
//Create a reusable rx16 response object to get the address
Rx16Response rx = Rx16Response();

//CoAP object
Coap coap;

myPOSTSensor bSensor = myPOSTSensor("valve3" , 3);
myPOSTtime cSensor = myPOSTtime("time");
myPOSTprogram fSensor = myPOSTprogram("program", 3);

//Runs only once
void setup()
{
pinMode(3, OUTPUT);
// comment out for debuging
xbee.initialize_xbee_module();
//start our XbeeRadio object and set our baudrate to 38400.
xbee.begin( 38400 );
//Initialize our XBee module with the correct values (using the default channel, channel 12)h
xbee.init(12);

// init coap service
coap.init( &xbee, &response, &rx );

//add the resourse resGET
coap.add_resource(&bSensor);
coap.add_resource(&cSensor);
coap.add_resource(&fSensor);

}

void loop()
{
//run the handler on each loop to respond to incoming requests
coap.handler();
}




72 changes: 72 additions & 0 deletions Arduino/Garden_coap/CoAP_Garden_Alarms/coapSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/********************************************************************************
** The Arduino-CoAP is free software: you can redistribute it and/or modify **
** it under the terms of the GNU Lesser General Public License as **
** published by the Free Software Foundation, either version 3 of the **
** License, or (at your option) any later version. **
** **
** The Arduino-CoAP 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 Lesser General Public License for more details. **
** **
** You should have received a copy of the GNU Lesser General Public **
** License along with the Arduino-CoAP. **
** If not, see <http://www.gnu.org/licenses/>. **
*******************************************************************************/

#ifndef COAPSENSOR_H
#define COAPSENSOR_H

#include "packet.h"


class CoapSensor {
public:

CoapSensor() {
this->method = GET | POST;
this->name = "unknown";
this->fast = true;
this->notify_time = 20;
this->content_type = TEXT_PLAIN;
this->changed = false;
}

CoapSensor(String name) {
this->method = GET | POST;
this->name = name;
this->fast = true;
this->notify_time = 20;
this->content_type = TEXT_PLAIN;
this->changed = false;
}

coap_status_t callback(uint8_t method, uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_data_len, queries_t queries);
uint8_t get_method();
String get_name();
bool get_fast();
bool is_changed();
void mark_notified();
uint16_t get_notify_time();
uint8_t get_content_type();
uint8_t set_method(uint8_t method);
String set_name(String name);
bool set_fast(bool fast);
uint16_t set_notify_time(uint16_t notify_time);
uint8_t set_content_type(uint8_t content_type);
virtual void check(void);
virtual void get_value(uint8_t* output_data, size_t* output_data_len);
virtual void set_value(uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_data_len);
private:
String name;
bool fast;
uint16_t notify_time;
uint8_t content_type, method;
uint8_t enable_method(uint8_t method);
uint8_t disable_method(uint8_t method);
protected:
bool changed;
};

#endif

35 changes: 35 additions & 0 deletions Arduino/Garden_coap/CoAP_Garden_Alarms/myPOSTSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "coapSensor.h"

class myPOSTSensor :
public CoapSensor
{
public:
int pin, status;
myPOSTSensor(String name, int pin):
CoapSensor(name)
{
this->pin = pin;
this->status = 0;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void get_value( uint8_t* output_data, size_t* output_data_len)
{
this->status = digitalRead(this->pin);
output_data[0] = 0x30 + status;
*output_data_len = 1;
}
void set_value(uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_data_len)
{
this->set(*input_data-0x30);
output_data[0] = 0x30 + status;
*output_data_len = 1;
this->changed = true;
}
void set(uint8_t value)
{
this->status = value;
digitalWrite(pin, status);
}
};

89 changes: 89 additions & 0 deletions Arduino/Garden_coap/CoAP_Garden_Alarms/myPOSTprogram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "coapSensor.h"
#include <Time.h>
#include <TimeAlarms.h>

class myPOSTprogram :
public CoapSensor
{
public:
int pin;
myPOSTprogram(String name, int pin):
CoapSensor(name)
{
this->pin = pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void get_value( uint8_t* output_data, size_t* output_data_len)
{
*output_data_len = sprintf( (char*)output_data, "%d", Alarm.count() );
}
void set_value(uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_data_len)
{
String s((char*) input_data);
int start = s.indexOf("H") + 1;
int end = s.lastIndexOf("H");
String sub = s.substring(start, end);
int hours = sub.toInt();
start = s.indexOf("M") + 1;
end = s.lastIndexOf("M");
sub = s.substring(start, end);
int minutes = sub.toInt();
start = s.indexOf("S") + 1;
end = s.lastIndexOf("S");
sub = s.substring(start, end);
int seconds = sub.toInt();
//Duration
start = s.indexOf("D") + 1;
end = s.lastIndexOf("D");
sub = s.substring(start, end);
int duration = sub.toInt();
//Weekday
start = s.indexOf("W") + 1;
end = s.lastIndexOf("W");
sub = s.substring(start, end);
timeDayOfWeek_t weekDay;
weekDay = (timeDayOfWeek_t) sub.toInt();
int ID1,ID2;
if (weekDay > 0 && weekDay<8) {
ID1 = Alarm.alarmRepeat(weekDay, hours, minutes, seconds, onOnceOnly);
seconds = seconds + duration;
minutes = minutes + seconds/60;
hours = hours + minutes/60;
hours = hours%24;
minutes = minutes%60;
seconds = seconds%60;
ID2 = Alarm.alarmRepeat(weekDay, hours, minutes, seconds, offOnceOnly);
}
else if (weekDay == 0)
{
ID1 = Alarm.alarmRepeat(hours, minutes, seconds, onOnceOnly);
seconds = seconds + duration;
minutes = minutes + seconds/60;
hours = hours + minutes/60;
hours = hours%24;
minutes = minutes%60;
seconds = seconds%60;
ID2 = Alarm.alarmRepeat(hours, minutes, seconds, offOnceOnly);
}
else if (weekDay == 10)
{
ID1 = Alarm.alarmOnce(hours, minutes, seconds, onOnceOnly);
seconds = seconds + duration;
minutes = minutes + seconds/60;
hours = hours + minutes/60;
hours = hours%24;
minutes = minutes%60;
seconds = seconds%60;
ID2 = Alarm.alarmOnce(hours, minutes, seconds, offOnceOnly);
}
*output_data_len = sprintf( (char*)output_data, "Day %d %d:%d:%d for %d ID1 %d id2 %d", weekDay, hours, minutes, seconds-duration, duration, ID1, ID2 );
this->changed = true;
}
void check(void)
{
Alarm.delay(100); // wait one second between clock display
}
};


29 changes: 29 additions & 0 deletions Arduino/Garden_coap/CoAP_Garden_Alarms/myPOSTtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "coapSensor.h"

class myPOSTtime :
public CoapSensor
{
public:
myPOSTtime(String name):
CoapSensor(name)
{
setTime(12,0,0,1,6,13);
}
void get_value( uint8_t* output_data, size_t* output_data_len)
{
*output_data_len = sprintf( (char*)output_data, "%lu", now() );
}
void set_value(uint8_t* input_data, size_t input_data_len, uint8_t* output_data, size_t* output_data_len)
{
long pctime = atol((const char*)input_data);
// check the integer is a valid time (greater than Jan 1 2013)
if( pctime >= 1357041600) // Jan 1 2013
{
// Sync Arduino clock to the time received on the serial port
setTime(pctime);
*output_data_len = sprintf( (char*)output_data, "New time is: %ld", pctime );
}
}
};


61 changes: 61 additions & 0 deletions Arduino/Garden_coap/CoAP_Garden_Sensors/CoAP_Garden_Sensors.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Arduino Coap Garden Sensors
* Monopatis Dimitris
*/


//Include XBEE Libraries
#include <XBee.h>
#include <XbeeRadio.h>
//Include CoAP Libraries
#include <coap.h>
#include "myGETSensor.h"
#include "myGETtemp.h"
#include "myGETmoist.h"

//Create the XbeeRadio object we'll be using
XBeeRadio xbee = XBeeRadio();
//Create a reusable response object for responses we expect to handle
XBeeRadioResponse response = XBeeRadioResponse();
//Create a reusable rx16 response object to get the address
Rx16Response rx = Rx16Response();

//CoAP object
Coap coap;

myGETSensor aSensor = myGETSensor("brightness" , A2);
myGETtemp dSensor = myGETtemp("temperature" , A1);
myGETmoist eSensor = myGETmoist("moisture" , A3);


//Runs only once
void setup()
{
pinMode(7, OUTPUT);

// comment out for debuging
xbee.initialize_xbee_module();
//start our XbeeRadio object and set our baudrate to 38400.
xbee.begin( 38400 );
//Initialize our XBee module with the correct values (using the default channel, channel 12)h
xbee.init(12);

// init coap service
coap.init( &xbee, &response, &rx );

//add the resourse resGET
coap.add_resource(&aSensor);
coap.add_resource(&dSensor);
coap.add_resource(&eSensor);

}

void loop()
{
//run the handler on each loop to respond to incoming requests
coap.handler();
}




Loading

0 comments on commit ba66a9d

Please sign in to comment.