If software serial no longer available then.....

Hi,

I'm using the Software Serial library to link two mega's via wifi.

I need to implement an IF that basically monitors the availability of the wifi signal. If the wifi signal is lost, and so nothing is received for more than 1 second THEN do such and such.

if (mySerial.available() == 0) {
    digitalWrite(39, HIGH);
    digitalWrite(40, LOW);
   } else {
    digitalWrite(40, HIGH);
    digitalWrite(39, LOW);
   }

When running the above code both LED's (pins 39 & 40) flash. I know this is because there is a small amount of latency between each byte sent, therefore the mySerial.available() funct will sometimes read a 0.

Is there a way I can monitor mySerial.available(), and if it returns 0 consistently for > 1000ms then run my 'broken wifi link' code?

I did look at serial.setTimeout(). But I'm unsure as to how I can execute code IF this timeout value is reached.

Thanks,
Jim

Use an unsigned long variable to store the time (millis()) when the last data was available.

Next subtract it from millis() and compare to your 10 second limit.

Thanks for the reply Sterretje,

I don't suppose you have a basic example do you?

Thanks in advance.

void loop()
{
  // time when last byte was received
  static unsigned long lastDatatime;

  if (mySerial.available() == 0)
  {
    digitalWrite(39, HIGH);
    digitalWrite(40, LOW);

    // check how long ago a byte was received
    if (millis() - lastDatatime >= 10000)
    {
      // time out
    }
  }
  else
  {
    digitalWrite(40, HIGH);
    digitalWrite(39, LOW);

    // remember when last byte was received
    lastDatatime = millis();
  }
}

Don't use SoftwareSerial. The Mega has 3 extra hardware serial ports, Serial1, Serial2 and Serial3, on pins 14-19.

Thanks, I will try this tomorrow.

Dev, what would be the advantages of using the hardware serial over my current software serial configuration?

Jim_cliff:
Thanks, I will try this tomorrow.

Dev, what would be the advantages of using the hardware serial over my current software serial configuration?

The hardware is designed for the task. SoftwareSerial is a hack to make it work without dedicated hardware. It uses more CPU resources, is much more likely to fail if you use too many other interrupts, and it hogs every single one of your pin change interrupts for itself, no matter how many pins you actually end up using it on.

The Mega has 4 UARTs, while most other Arduinos have just one. For all the people that have an UNO, the only way to connect two serial devices is use Serial for one of them, and a software serial library for the other one.

Sending a byte requires "serializing" it into a stream of bits that can be received by another device. First, you have to send a START bit, then the 8 bits of the byte, and then finish with a STOP bit. The most common configuration is to send 10 bits for each byte you want to transfer.

A UART will take one byte and perform all the timing and shifting to send those bits at the desired baud rate (bits per second). The Arduino code basically writes one byte into a special register, and the UART "peripheral" takes care of all the rest. The UART will generate an interrupt when that byte has been completely sent, and another byte can be accepted. The CPU does very little work. The UART is independent from the CPU, and it is dedicated to toggling the TX pin at the right rate, and with the individual bits of your byte. The CPU is free to do other things while those 10 bits are being transmitted.

A software serial library uses the CPU to toggle the TX pin. It waits the appropriate amount of time, and shifts each bit of a byte as they go out. Instead of writing a byte with one CPU instruction, the library executes thousands of instructions to toggle the pin at the correct baud rate.

There are 3 main software serial libraries, and they put different loads on the CPU:

  • AltSoftSerial is the best, because it uses some additional timing peripherals (TIMER1, 2 or 3). When receiving or transmitting, it does not dedicate the CPU (i.e., disable interrupts) to waiting for each bit. It only works on two specific pins. It uses about 20x more CPU time than HardwareSerial.

  • NeoSWSerial is next best, because it re-uses a timing peripheral that is already being used (TIMER0, aka millis() clock). Although receiving does not disable interrupts, transmitting does. The CPU cannot do anything else while transmitting, except receiving at the same time. It works on any two pins, but only at baud rates 9600, 19200 and 39400. It uses 20x to 500x more CPU time than HardwareSerial.

  • SoftwareSerial is the worst, because it disables interrupts when receiving or transmitting, and it cannot transmit and receive at the same time. It uses about 500x more CPU time than HardwareSerial.

None of these libraries allow you to receive from more than one software serial port at a time. If you have two devices that can send a character at any time, plus using Serial for debug prints, these libraries cannot help you.

The main problem with software serial libraries is disabling interrupts. In order to meet the baud rate timing, NeoSWSerial and SoftwareSerial force the CPU to twiddle its thumbs for each bit. Other parts of your sketch cannot execute. Although AltSoftSerial does not affect other parts of your sketch, it can lose bits if some other part of your sketch disables interrupts longer than one bit time. AltSoftSerial has to check the RX pin frequently.

Another way to look at this is to compare the CPU clock to the baud rate. Most Arduinos are running at 16MHz or 8Mhz. At the most common baud, 9600 (i.e., 0.0096MHz), the CPU could have executed 10,000 instructions while just one character is being sent or received.

Ironically, one of the main reasons that many people buy a Mega instead of an UNO is to get the extra UARTs.