Hey everyone.
I have a project that's been going along quite nicely... until now. I have a clock hooked up to a motion sensor, which after 15 minutes will send motion data wirelessly to a hub, which I have hooked up via ethernet to a database. Unfortunately for me, I cannot get rid of that middle 'wireless gap' step, and my problem revolves around it. Basically I need to come up with a clearly deliniated series of 'states' for my two arduinos to go between, and I'm having trouble setting it up.
My setup:
Clock & Motion Sensor -> Arduino 1 ('Clock') <--> XBee 1 ----Wireless---- XBee 2 <--> Arduino 2 ('Hub') -> Ethernet -> Database.
Here's a basic diagram of the flow of what I'm trying to do:
(Sorry, huge picture, didn't want to embed it here.)
so basically:
1 - Both enter 'setup', and have to wait for a "ready" signal to continue.
2 - 'Hub' fetches current time, and sends it to 'Clock', which is waiting.
3 - Once the Clock's time is set, it watches for 15 minutes, and collects data.
4 - At the end of this period, it packages it up, and sends it to the 'Hub'
5 - 'Hub' sends the data on to the database, and returns to fetch the current time (just in case the clock is off.)
6 - Return to step #2.
And finally, here's my current 'outline' code for both.
Clock:
#define XBEE_ID 1
int check = 0;
void setup() {
Serial.begin(57600);
delay(1000);
Serial.print("READY!");
}
void loop() {
switch (check){
case 0:
{
//Wait for Timestamp
waitToRead("TIME");
Serial.println("Time now set");
check = 1;
break;
}
case 1:
{
//Collect Data
delay(3000);
check = 2;
break;
}
case 2:
{
//Send Data to Hub
delay(1000);
Serial.print("DATAHUB!");
check = 0;
}
}
}
void waitToRead(String wantString){
String readStr = "";
//looking for a string terminated by a ! - "hello!" "bye!" "66r3!", etc.
while(!wantString.equals(readStr)){
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == '!') {
break;
} //breaks out of capture loop to print readstring
readStr += c;
} //makes the string readString
}
}
and
Hub:
#define XBEE_ID 0
int check = 0;
void setup() {
Serial.begin(57600);
delay(100);
//FIRST CONNECTION
waitToRead("READY");
}
void loop() {
switch (check){
case 0:
{
delay(2000);
Serial.print("TIME!");
check = 1;
}
case 1:
{
waitToRead("DATAHUB");
check = 0;
}
}
}
void waitToRead(String wantString){
String readStr = "";
//looking for a string terminated by a ! - "hello!" "bye!" "66r3!", etc.
while(!wantString.equals(readStr)){
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == '!') {
break;
} //breaks out of capture loop to print readstring
readStr += c;
} //makes the string readString
}
}
Now obviously that isn't everything - I actually have the entirety of the "meat" of the programs written in another file, where everything's running simultaneously, but I want to put it into this framework and get more control over everything. Aaaand I'm stuck. Nothing really seems to work. I'm not coming up with a clean enough way to switch between 'reading' and 'sending', and the XBees are either getting nothing at all, or compounding the message with the last thing received and going into a sort of feedback loop.
Any advice at all as to how to control this system better would be wonderful. Unfortunately the obvious one (change the hardware!) isn't going to happen. Gotta work with what I've got. However, all my hardware is (at least at the moment!) set up correctly - my XBees are sending and receiving perfectly, if I load simple programs. The problem now is just the code.