Raspberry Pi & Arduino serial communication

I'm building a cabinet for my home lab computers, which I would like to control power to each through the Arduino, relays, and MOSFETs. I want it all to be controllable via a website which will also be the main hub to control all systems in my house.

So I've hooked up an Arduino Mega to a Raspberry Pi via USB cable, the sketch on the Arduino monitors for serial input and takes action accordingly, the Raspberry is running a Flask web server hosting the website that sends serial commands to the Arduino when instructed.

The Arduino is really just a placeholder for now, I am planning on etching my own PCB with an ATMega328 microcontroller integrated into it with all the relays, MOSFETs and supporting circuitry once I know my full requirements and I have designed the circuit.
I plan on hooking my custom Arduino PCB to the Raspberry via the Raspberry's GPIO serial connection in the end and not having the serial to USB conversion going on.

At the moment all is working ok, but there is a ~10s delay from me sending the serial command from the Flask website, the page has fully reloaded then ~10s delay, to the Solid State Relays I'm controlling respond.

I had this exact setup working, exact equipment I used last time to control some LED light strips remotely, with no time delay, the only difference being is new software.

I have attached the sketch the Arduino is running incase there is a problem there.
But does anyone have any input as to why the delay might be happening??? USB communication, better off using GPIO??? Inherent speed of board?? Or bad board?? Just noticed today that this board doesn't like being powered via 12v through the jack, but will run all day long on USB???

Cheers
Iain

arduino.ino (1.46 KB)

  while(newData == true) {
    cmd = (recievedChar - '0');
    if (cmd == 1) {
      val = digitalRead(beastpin);
      pin = 51;
      }
    if (cmd == 2) {
      val = digitalRead(backuppin);
      pin = 53;
      }
    if (cmd == 3) {
      val = digitalRead(printerpin);
      pin = 49;
      }
    if (val == 0) {
      newVal = 1;
      }
    if (val == 1) {
      newVal = 0;
      }
    digitalWrite(pin, newVal);
    newData = false;
    }
  }

How many times will this while loop iterate? Why do you need a while loop to make something happen ONCE?

  Serial.begin(9600);

I'm almost certain that the Arduino can send data faster than that, and that the Pi can read data faster than that.

What appears to be the problem, though, is that on every pass through loop, you read the state of three pins AND send the state to the Pi. loop() gets called quite often, so you are spamming the Pi with information that hasn't changed.

Sending data only when the state of a pin has changed would GREATLY reduce the volume of data sent, making the Arduino wait for room in the outgoing serial buffer far less often, making it possible to read incoming data far more frequently.

PaulS:
How many times will this while loop iterate? Why do you need a while loop to make something happen ONCE?

  Serial.begin(9600);

I'm almost certain that the Arduino can send data faster than that, and that the Pi can read data faster than that.

......

Cheers bud, that's great. Will re-work it. Sometimes you just can't see the wood from the trees.

I can't see anything in your Arduino program that would account for the delay. A higher baud would be better, but the difference won't account for a 10 second delay.

So the problem must be in your Python code.

I use the simpler Bottle web framework for my Python GUIs for controlling my Arduinos and I am not conscious of any delay. That does NOT mean that I think Flask is the cause of the problem. AFAIK it is better than Bottle - but I do not need the extra complexity.

By the way, I would change this line

while(newData == true) {

to

if(newData == true) {

...R

Robin2:
...R

Cheers for the advice Robin2, before I read your response I have changed my Arduino sketch around and increased the baud rate and I am experiencing no delay what so ever now.

Thanks to both of you again...

arduino.ino (1.47 KB)