Code: Select all
/*
FromArduino
Get x,y and z aceleration values and write them to the serial port
where they will be received by the serial monitor and written to disk.
Given this nodes limted processing power and memory the data
will not be processed here.
Message format:
#<type of message>,<source node>,<sequence>,<Milliseconds>,<YYMMDD>,<HHMMSS.ssss>,<TimeZone>,<data>,<data>,...
type of message:
start: first output when a node starts (#start,<node>,<sequence>,<milliseconds>,,
raw: raw data eg: #raw,<node>,<sequence>,<Miliiseconds>,,,,MeasureID,<data>,<data>,...
event: event data eg: #event,<node>,<sequence>,<Miliiseconds>,,,,From Row, To Row,MeasureID,<data>,<data>,...
The event can reference other rows and in this example program the events refer to the raw row that follows.
it also has the same data as the raw row but that is not the requirement. The from Row and To Row can be empty.
(so you can see that a node can have multiple type of measures and muultiple types of events.
these each can have multiple measures. Later those that have multiple measures will be
stored as multiple rows in the db to make access to the data very direct in a SQL way)
delay: from a server the delay to be used eg: #delay,500
delayis: from a client in response to a #delay eg. #delayis,01,<sequence>,<Miliseconds>,,,,500
comment: for notes eg: #comment,01,<sequence>,<milliseconds>,,,,"I am an accelerometer node"
nodeexception: exceptions detected eg. #nodeexception,01,<sequence>,<Mlliseconds>,,,,"java stack captured by catch"
value: set the value in the client
valueis: from the client in response to a #value eg. #valueis,01,<sequence>,<Miliseconds>,,,,name,value
source node: the sending nodes id
sequence: start at 0 and incremented when row is sent
Milliseconds: since start of node
YYMMDD: when available otherwise 0 length string (just ending comma)
HHMMSS.ssss: when available otherwise 0 length string (just ending comma)
TimeZone: for the data time given
data: quoted text or numbers
examples: #raw,01,1,3000,,,,111,222,333
#comment,01,2,3100,,,,"This is a comment"
*/
#define NODENAME "01" // Identify the name of the node where the data came from
int sensorXPin = A0;
int sensorYPin = A1;
int sensorZPin = A2;
int sensorXValue = 0; //Acceleration values [0 .. 1023]
int sensorYValue = 0;
int sensorZValue = 0;
unsigned long rowCount = 0;
char request;
String requestString = "";
int delayMS = 0;
String valueName;
String valueValue;
String outputString;
int CRC;
String CRCString;
char CRCChar[12];
int seismicEventThreshold = 170;
/*********************************************************
Send a start row sart rows contain simply NodeName,"#start, node name"
*********************************************************/
void setup() {
Serial.begin(9600);
outputString = "#start,";
outputString += NODENAME;
outputString += ",";
outputString += rowCount;
outputString += ",";
outputString += millis();
outputString += ",,,,"; //This only looks like it should be ",,,,,"
itoa(int(CRC8(outputString)),CRCChar,10);
CRCString = CRCChar;
CRCString = "000" + CRCString;
outputString += CRCString.substring(CRCString.length() - 4);
outputString += "\n";
Serial.print(outputString);
rowCount += 1;
outputString = "#comment,";
outputString += (NODENAME);
outputString += ",";
outputString += rowCount;
outputString += ",";
outputString += millis();
outputString += ",,,,";
outputString += "I am an accelerometer node,";
itoa(int(CRC8(outputString)),CRCChar,10);
CRCString = CRCChar;
CRCString = "000" + CRCString;
outputString += CRCString.substring(CRCString.length() - 4);
outputString += "\n";
Serial.print(outputString);
rowCount += 1;
}
/*********************************************************
Start immediately then between sending values measured
look fort a #delay #\n command on the serial port
to set the delay interval the default interval is 0;
echo the command so it can be logged at the server
*********************************************************/
void loop() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if ((requestString.length() == 0) & (inChar == '#')) {
requestString = "#";
} else if (requestString.length() > 0 and inChar == '\n') {
if (requestString.length() > 7) {
if (requestString.substring(0,7) == "#delay,") {
delayMS = requestString.substring(7).toInt();
writeDelayIs();
} else if (requestString.substring(0,7) == "#value,") {
valueName = "";
valueValue = "";
int valueNameStart;
int valueNameEnd;
int valueValueStart;
if (requestString.indexOf(",") > 1) {
if (requestString.indexOf(",",requestString.indexOf(",")) > 1) {
valueNameStart = requestString.indexOf(",") + 1;
valueNameEnd = requestString.indexOf(",", (valueNameStart + 1 ));
if (valueNameEnd > valueNameStart) {
valueName = requestString.substring(valueNameStart, valueNameEnd);
valueValueStart = requestString.indexOf(",",valueNameEnd) + 1;
valueValue = requestString.substring(valueValueStart);
writeValueIs(valueName, valueValue);
}
}
}
}
requestString = "";
}
} else if (requestString.length() > 0) {
requestString = requestString + inChar;
if (requestString.length() > 100) {
requestString = "";
}
}
}
delay(delayMS);
sensorXValue = analogRead(sensorXPin);
sensorYValue = analogRead(sensorYPin);
sensorZValue = analogRead(sensorZPin);
if (sensorXValue > (511 + seismicEventThreshold)
or sensorYValue > (511 + seismicEventThreshold)
or sensorZValue > (511 + seismicEventThreshold)
or sensorXValue < (511 - seismicEventThreshold)
or sensorYValue < (511 - seismicEventThreshold)
or sensorZValue < (511 - seismicEventThreshold)
) {
writeEvent();
}
writeRawValues();
}
/*
//CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the the terms of the GNU GPL 3.0 license
//this is the original function that is adapted for this sketch
byte CRC8Original(const byte *data, byte len) {
byte crc = 0x00;
while (len--) {
byte extract = *data++;
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
*/
//CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the the terms of the GNU GPL 3.0 license
byte CRC8(String stringData) {
int len = stringData.length();
int i = 0;
byte crc = 0x00;
//Serial.print("(0:" + stringData + ")\n");
while (len--) {
byte extract;
extract = (byte) stringData.charAt(i++);
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
/*********************************************************
Format a raw row and send it to the serial port
raw rows with more one or more measures contains "#raws", node name, sample count since program start,
milli seconds since program start,MeasureID, x value, y value, z value
terminate the line with a 4 digit CRC8 and a line feed (no comma)
*********************************************************/
void writeRawValues() {
outputString = "#raw,";
outputString += NODENAME;
outputString += ",";
outputString += rowCount;
outputString += ",";
outputString += millis();
outputString += ",,,," ;
outputString += "Accel";
outputString += ",";
outputString += sensorXValue;
outputString += ",";
outputString += sensorYValue;
outputString += ",";
outputString += sensorZValue;
outputString += ",";
itoa(int(CRC8(outputString)),CRCChar,10);
CRCString = CRCChar;
CRCString = "000" + CRCString;
outputString += CRCString.substring(CRCString.length() - 4);
outputString += "\n";
Serial.print(outputString);
rowCount += 1;
}
/*********************************************************
Format an event row and send it to the serial port
event rows with one or more measures contains: "#event", node name, sample count since program start,
milli seconds since program start,Date, Time, TZ,Event Type,MeasureID, x value, y value, z value
terminate the line with a 4 digit CRC8 and a line feed (no comma)
*********************************************************/
void writeEvent() {
outputString = "#event,";
outputString += NODENAME;
outputString += ",";
outputString += rowCount;
outputString += ",";
outputString += millis();
outputString += ",,,," ;
outputString += "AccEvt";
outputString += ",";
outputString += "Accel";
outputString += ",";
outputString += sensorXValue;
outputString += ",";
outputString += sensorYValue;
outputString += ",";
outputString += sensorZValue;
outputString += ",";
itoa(int(CRC8(outputString)),CRCChar,10);
CRCString = CRCChar;
CRCString = "000" + CRCString;
outputString += CRCString.substring(CRCString.length() - 4);
outputString += "\n";
Serial.print(outputString);
rowCount += 1;
}
void writeDelayIs() {
outputString = "#delayis,";
outputString += NODENAME;
outputString += ",";
outputString += rowCount;
outputString += ",";
outputString += millis();
outputString += ",,,,";
outputString += delayMS;
outputString += ",";
itoa(int(CRC8(outputString)),CRCChar,10);
CRCString = CRCChar;
CRCString = "000" + CRCString;
outputString += CRCString.substring(CRCString.length() - 4);
outputString += "\n";
Serial.print(outputString);
rowCount += 1;
}
void writeValueIs(String valueName, String valueValue) {
outputString = "#valueis,";
outputString += NODENAME;
outputString += ",";
outputString += rowCount;
outputString += ",";
outputString += millis();
outputString += ",,,,";
outputString += valueName;
outputString += ",";
outputString += valueValue;
outputString += ",";
itoa(int(CRC8(outputString)),CRCChar,10);
CRCString = CRCChar;
CRCString = "000" + CRCString;
outputString += CRCString.substring(CRCString.length() - 4);
outputString += "\n";
Serial.print(outputString);
rowCount += 1;
}