I've started to prototype something using an Arduino Mega and Yún Shield.
It's simple. It reads an ADC at 50Hz and sends data to a server through a socket. But because of that combination, I need to use Bridge to send data through the socket. It's very slow.
Should I change the Arduino Mega for a Leonardo or a Due to get better performance?
Here is my code, maybe I did something wrong.
Arduino code:
#include <Bridge.h>
#include <Process.h>
#include <Time.h>
#include <TimeLib.h>
#include <stdlib.h>
boolean interruptFlag = 0;
int HD = A0;
int HG = A1;
int CD = A2;
int CG = A3;
int ECG = A5;
String analogSignals = "";
static char outstr[15];
int count = 0;
float x = 0.0;
void setup() {
// Bridge startup.
Bridge.begin();
// Initialize Bridge values.
Bridge.put("data", "");
noInterrupts(); // Stop interrupts.
TCCR1A = 0; // Set entire TCCR1A register to 0.
TCCR1B = 0; // Same for TCCR1B.
TCNT1 = 0; // Initialize counter value to 0.
// Set compare match register for 50Hz increments.
OCR1A = 39999; // = 16000000 / (8 * 50) - 1 (must be <65536)
// Turn on CTC mode.
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 64 prescaler.
TCCR1B |= (0 << CS12) | (1 << CS11) | (1 << CS10);
// Enable timer compare interrupt.
TIMSK1 |= (1 << OCIE1A);
interrupts(); // Allow interrupts.
// Launch Python script.
/*Process p;
p.begin("python");
p.addParameter("/root/test/send.py");
p.run();*/
}
ISR(TIMER1_COMPA_vect) {
if (!interruptFlag) {
interruptFlag = 1;
}
}
void loop() {
if (interruptFlag) {
writeAnalogSignals();
interruptFlag = 0;
}
}
void writeAnalogSignals() {
String HDReading = convertToVoltage(analogRead(HD));
String HGReading = convertToVoltage(analogRead(HG));
String CDReading = convertToVoltage(analogRead(CD));
String CGReading = convertToVoltage(analogRead(CG));
String ECGReading = convertToVoltage(analogRead(ECG));
if (analogSignals.length() == 0) {
analogSignals = String(x, 2) + ";" + HDReading + ";" + HGReading + ";" + CDReading + ";" + CGReading + ";0.0;0.0;" + ECGReading;
} else {
analogSignals += "|" + String(x, 2) + ";" + HDReading + ";" + HGReading + ";" + CDReading + ";" + CGReading + ";0.0;0.0;" + ECGReading;
}
x += 0.02;
count += 1;
if (count >= 50) {
Bridge.put("data", analogSignals);
analogSignals = "";
count = 0;
}
}
// Convert the analog reading to voltage.
String convertToVoltage(int analogSignal) {
dtostrf(analogSignal * (5.0 / 1023),8, 6, outstr);
return outstr;
}
Python code:
import socket
import sys
import getopt
import time
def main(argv):
try:
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
# Create a UDP socket.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('192.168.1.100', 9050)
bridge = bridgeclient()
previous = ""
# Send data
while 1:
data = bridge.get("data")
if data != previous:
sent = sock.sendto(data, server_address)
previous = data
time.sleep(0.5)
finally:
sock.close()
time.sleep(0.5)
that you put. Try lowering it to e.g. 0.01 to perform a check every 10 milliseconds... If this doesn't work, maybe the problem is the overhead added; to limit this store on the arduino or the Yùn some data (e.g. 50 acquisitions) and then send them all at once.time.sleep(0.01)
and I stored 50 acquisitions, but It's slow to get 1 sec of datas.