Hi there. I am working on a sensor network, which reads different analog sensors on a Arduino, then sends the data via Xbees to a RaspberryPi dataserver, who is connected to an PC with LabView via Ethernet.
(I am a mechanical engineer doing my first steps programming outside of Matlab and i am totally new to microcontrollers, networks and so on. But it's fun to learn all this for my Master Thesis.)
My problem is the transmitting speed. Right now i get around 100 Hz maximum. Because i want to measure unsteady flow, 1000 Hz or higher would be nice. I tried my best to find the bottleneck in my network, but i am still not sure. So i want to show you the steps and write down my thoughts on it, so you could maybe help me.
1. Arduino (Uno at the moment, later i want to use the Micro)
void setup() {
Serial.begin(115200);
}
char Buffer[38];
void loop() {
int sensor0 = analogRead(0);
int sensor1 = analogRead(0);
int sensor2 = analogRead(0);
int sensor3 = analogRead(0);
int sensor4 = analogRead(0);
int sensor5 = analogRead(0);
unsigned long zeit = millis(); //zeit is the german word for time ;)
sprintf(Buffer,"%04i%04i%04i%04i%04i%04i%08i",sensor0,sensor1,sensor2,sensor3,sensor4,sensor5,zeit);
Serial.print ("s");
Serial.print (buffer);
delay (8);
}
What i do here is to read the analog sensors, which transforms them to digital signals. (Should be fast enough for 100 Hz as far as i read) Then i want to have all sensor data at the same length, so i fill it up to 4 characters with sprintf(). Then the buffer of sprintf() is send with Serial.print.
Why strings? I know this produces unnecessary data, but Serial.print uses strings anyway. And it seemd the easiest way to produce a dataset of a constant lenght. I could use Serial.write to send bytes, but then i would need a whole new idea to read the data back in.
Maybe sprintf() is slow. This was my first thought, so i replaced the function by various "if" and "else if", but this doesn't help. Do you want to see it? Just aks, it is a bit longer so i decided not to show it.
When i want to get faster and use a shorter delay, i sometimes don't get the whole buffer. it just starts new before all 32 characters are send. This gets worse the less delay i use. The first sensor signal is oft not harmed, but the last characters, which represent the time often get lost.
2. Xbees
Series 2, Pro version
115 200 bps
AT Mode
The router is connected to the Arduino. I know, that the arduino provides 50 mA and the XBee Pro needs 150 mA, but i worked fine before i read this (...ooops). Could this be a problem? The coordinator is attached to a USB dongle and connected to the RaspberryPi. The Xbees worked fine with different baudrates, so i got up to 115 200 bps. At the moment they are 20 cm away of each other on my desk, so there should be no connection problems.
3. Python script on RaspberryPi
Wrong forum, i know, but it might help you to undestand.
...
while 1:
aktuellerwert = ser.read(1)
if aktuellerwert == "s": #wait for startsign to avoid overlap
sensor0 = ser.read(4) #read the first 4 characters after "s"
sensor1 = ser.read(4)
.
.
sensor5 = ser.read(4)
stringtosend = str(sensor0 + " " + sensor1 + " " + ... + sensor5)
data = conn.recv(BUFFER_SIZE)
try:
conn.send(stringtosend)
...
It reads one character until it gets "s", then it reads the sensor data and the time, which are always the same length. If i try to go faster, the last characters are somtimes just "0", or the first characters of the next sample including the "s".
Then it sends the data via ethernet to a PC (or Internet in the future), where a LabView Script displays the data and saves it. The whole ethernet connection and LabView loop should be fast enought for far more than 1000 Hz samplerate.
Do you need any more information to help me?, or want to know more about what i am doing? Just ask for it.
Thank you very much for taking time for my problem.