Commit e78d9065 authored by PhiBo's avatar PhiBo 🚀

Initial commit

parents
#
# Project Configuration File
#
# A detailed documentation with the EXAMPLES is located here:
# http://docs.platformio.org/en/latest/projectconf.html
#
# A sign `#` at the beginning of the line indicates a comment
# Comment lines are ignored.
# Simple and base environment
# [env:mybaseenv]
# platform = %INSTALLED_PLATFORM_NAME_HERE%
# framework =
# board =
#
# Automatic targets - enable auto-uploading
# targets = upload
[env:uno]
platform = atmelavr
framework = arduino
board = diecimilaatmega328
#board = uno
# targets = upload
#include <ArduRPC.h>
#include <SoftwareSerial.h>
#include "sensor_node.h"
#include "DHT.h"
#define RPC_SERIAL_PORT Serial
#define RPC_SERIAL_BAUD 9600
#define RPC_NUM_HANDLERS 20
#define RPC_NUM_FUNCTIONS 0
SoftwareSerial mySerial(10, 11);
//ArduRPC rpc = ArduRPC(RPC_NUM_HANDLERS, RPC_NUM_FUNCTIONS);
//ArduRPC_Serial rpc_serial = ArduRPC_Serial(RPC_SERIAL_PORT, rpc);
//ArduRPC_SensorNode sensor_node = ArduRPC_SensorNode();
ArduRPCRequest rpc_request = ArduRPCRequest();
ArduRPCRequest_Serial h = ArduRPCRequest_Serial(rpc_request, RPC_SERIAL_PORT);
SensorWifiModuleRemote sensor_remote = SensorWifiModuleRemote(rpc_request, 0x10);
SensorNode sensor_node = SensorNode();
void setup() {
RPC_SERIAL_PORT.begin(RPC_SERIAL_BAUD);
/*sensor_dht = new DHT(6, DHT22);
sensor_dht->begin();*/
//mySerial.begin(9600);
//rpc.connectHandler(&sensor_node, 0x10);
// put your setup code here, to run once:
sensor_node.loadConfig();
Serial.println("foo");
}
void loop() {
//a->writeValue();
//rpc_serial.readData();
//sensor_dht->readTemperature();
sensor_node.run();
sensor_node.submitValues(&sensor_remote);
delay(5000);
}
#include "sensor_node.h"
uint8_t EEPROM_read(int pos)
{
pos = pos - NODE_EEPROM_BASIC_SENSOR_OFFSET;
if (pos == 0)
return 0;
if (pos == 1)
return 1;
if (pos == 2)
return 6;
return 0;
}
SensorNode::SensorNode()
{
}
BaseSensor *SensorNode::setupSensor(uint16_t type, uint8_t *options)
{
if(type == 0x01) {
return (BaseSensor *)new SensorDHT(options);
}
return NULL;
}
void SensorNode::loadConfig()
{
uint8_t i, j;
uint16_t sensor_type;
int pos = NODE_EEPROM_BASIC_SENSOR_OFFSET;
BaseSensor *sensor = NULL;
uint8_t sensor_options[8];
for(i = 0; i < NODE_MAX_SENSOR_COUNT; i++) {
sensor_type = 0;
sensor_type = (uint8_t)EEPROM_read(pos);
pos++;
sensor_type <<= 8;
sensor_type |= (uint8_t)EEPROM_read(pos);
pos++;
if(sensor_type == 0) {
pos += 8;
continue;
}
for(j = 0; j < 8; j++) {
sensor_options[j] = EEPROM_read(pos);
pos++;
}
sensor = this->setupSensor(sensor_type, sensor_options);
if (sensor != NULL) {
this->sensors[i] = sensor;
}
}
}
void SensorNode::resetValues()
{
this->value_count = 0;
}
void SensorNode::run()
{
uint8_t i;
uint16_t sensor_type;
BaseSensor *sensor;
for(i = 0; i < NODE_MAX_SENSOR_COUNT; i++) {
sensor = this->sensors[i];
if (sensor != NULL) {
sensor->start(this);
}
}
for(i = 0; i < NODE_MAX_SENSOR_COUNT; i++) {
sensor = this->sensors[i];
if (sensor != NULL) {
sensor->run(this);
}
}
int pos = NODE_EEPROM_BASIC_SENSOR_OFFSET;
this->resetValues();
for(i = 0; i < NODE_MAX_SENSOR_COUNT; i++) {
sensor = this->sensors[i];
if (sensor == NULL) {
pos += 10;
} else {
sensor_type = 0;
sensor_type = (uint8_t)EEPROM_read(pos);
pos++;
sensor_type <<= 8;
sensor_type |= (uint8_t)EEPROM_read(pos);
pos += 9;
this->current_sensor_id = i;
this->current_sensor_type = sensor_type;
sensor->finish(this);
}
}
}
void SensorNode::submitValues(SensorWifiModuleRemote *sensor_remote)
{
uint8_t i;
Serial.println("start");
sensor_remote->start();
Serial.println("getStatus");
sensor_remote->getStatus();
for(i = 0; i < this->value_count; i++) {
Serial.print("submitValue");
Serial.println(i);
sensor_remote->submitValue(&this->values[i * NODE_CACHE_VALUE_SIZE], NODE_CACHE_VALUE_SIZE);
}
Serial.println("finish");
sensor_remote->finish();
}
uint8_t *SensorNode::writeValuePrefix(uint8_t type)
{
uint8_t *data = &this->values[this->value_count * NODE_CACHE_VALUE_SIZE];
data[0] = this->current_sensor_id;
data[1] = (((this->current_sensor_type) >> 8) & 0xff);
data[2] = ((this->current_sensor_type) & 0xff);
data[3] = type;
data = data + 4;
return data;
}
void SensorNode::writeValue(uint8_t type, float value)
{
uint8_t *data;
uint8_t *v = (uint8_t *)&value;
data = this->writeValuePrefix(type);
data[0] = RPC_FLOAT;
data[1] = v[3];
data[2] = v[2];
data[3] = v[1];
data[4] = v[0];
this->value_count++;
}
BaseSensor::BaseSensor()
{
}
SensorDHT::SensorDHT(uint8_t *options)
{
this->dht = new DHT(options[0], DHT22);
}
void SensorDHT::start(SensorNode *sensor)
{
this->dht->readTemperature();
this->dht->readHumidity();
}
void SensorDHT::run(SensorNode *sensor)
{
}
void SensorDHT::finish(SensorNode *sensor)
{
float value;
value = this->dht->readTemperature();
sensor->writeValue(
SENSOR_VALUE_TEMPERATURE,
value
);
value = this->dht->readHumidity();
sensor->writeValue(
SENSOR_VALUE_HUMIDITY,
value
);
}
SensorWifiModuleRemote::SensorWifiModuleRemote(ArduRPCRequest &rpc, uint8_t handler_id) : ArduRPCRequestHandler()
{
this->_rpc = &rpc;
this->_handler_id = handler_id;
}
uint8_t SensorWifiModuleRemote::getStatus()
{
this->_rpc->reset();
Serial.println("call");
//this->_rpc->writeRequest_uint8(i);
this->_rpc->call(this->_handler_id, 0x12);
Serial.println("end");
return 0;
}
uint8_t SensorWifiModuleRemote::finish()
{
this->_rpc->reset();
Serial.println("call");
//this->_rpc->writeRequest_uint8(i);
this->_rpc->call(this->_handler_id, 0x11);
Serial.println("end");
return 0;
}
uint8_t SensorWifiModuleRemote::start()
{
this->_rpc->reset();
Serial.println("call");
//this->_rpc->writeRequest_uint8(i);
this->_rpc->call(this->_handler_id, 0x10);
Serial.println("end");
return 0;
}
uint8_t SensorWifiModuleRemote::submitValue(uint8_t *data, uint8_t length)
{
uint8_t i;
this->_rpc->reset();
Serial.println("call");
for(i = 0; i < length; i++) {
this->_rpc->writeRequest_uint8(data[i]);
}
this->_rpc->call(this->_handler_id, 0x13);
Serial.println("end");
return 0;
}
#ifndef _SENSOR_NODE_H
#define _SENSOR_NODE_H
#include <avr/pgmspace.h>
#include "ArduRPC.h"
#include "DHT.h"
#if (ARDUINO >= 100)
#include <Arduino.h>
#include <EEPROM.h>
#else
#endif
#define NODE_EEPROM_UUID_OFFSET 0
#define NODE_EEPROM_API_KEY_OFFSET 32
#define NODE_EEPROM_BASIC_SENSOR_OFFSET 64
#define NODE_MAX_SENSOR_COUNT 32
#define NODE_CACHE_VALUE_SIZE 9
#define SENSOR_VALUE_TEMPERATURE 1
#define SENSOR_VALUE_HUMIDITY 2
class SensorNode;
class BaseSensor
{
public:
BaseSensor();
virtual void
finish(SensorNode *),
run(SensorNode *),
start(SensorNode *);
private:
};
class SensorWifiModuleRemote : public ArduRPCRequestHandler
{
public:
SensorWifiModuleRemote(ArduRPCRequest &rpc, uint8_t handler_id);
uint8_t
finish(),
submitValue(uint8_t *, uint8_t),
start();
uint8_t
getStatus();
private:
};
class SensorNode
{
public:
SensorNode();
void
loadConfig(),
run(),
submitValues(SensorWifiModuleRemote *),
writeValue(uint8_t, float);
BaseSensor
*setupSensor(uint16_t, uint8_t *);
private:
uint8_t *writeValuePrefix(uint8_t);
void resetValues();
BaseSensor *sensors[NODE_MAX_SENSOR_COUNT];
uint16_t current_sensor_type;
uint8_t current_sensor_id;
uint8_t values[NODE_MAX_SENSOR_COUNT * 9];
uint8_t value_count;
};
class SensorDHT : public BaseSensor
{
public:
SensorDHT(uint8_t *);
void
finish(SensorNode *),
run(SensorNode *),
start(SensorNode *);
private:
DHT *dht;
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment