Go Down

Topic: How to stop process on receiver HC-12 when the sender HC-12's power go offline ? (Read 56 times) previous topic - next topic

Herryben

I want the led to turn off on the receiver when the power goes offline on the sender HC-12.

Code for the sender :

Code: [Select]

void loop{
    String st = "hello";
    HC12.print(st + 'A');  // send st to the receiver
    delay(100);
}


Code for the receiver :
Code: [Select]

void loop{
    while(HC12.available()){
        digitalWrite(led, 1);
        String st = HC12.readStringUntil('A');
        Serial.println("receive = " + st);
        delay(10);
   }
}


Could anyone help me, please?

RudolfAtRTC

I recommend 2 extensions:
  • At the receiver a timeout has to be coded, which recognises after about 200 ms, that the transmitter does not do any more.
  • If the transmitter is interrupted during a message, the command "readStringUntil (...)" must be terminated by timeout too. For this there is the command "Serial.setTimeout (...).


Code: [Select]
...
long lTimeout; // Holds the millis() for timeout calc.

void setup() {
  ...
HC12.setTimeout(200); // Timeout for active readStringUntil(...)
...
lTimeout = millis(); // Initial value for timeout in while(...)
}

void loop{

  while(HC12.available()){
    digitalWrite(led, 1);
    String st = HC12.readStringUntil('A');
    Serial.println("receive = " + st);
    delay(10);
lTimeout = millis(); // Timeout stating new
  }

if ((millis() - lTimeout) > 200) // Set timeout to 200 ms
{
digitalWrite(led,0); // Timeout detected -> LED off
}

}

Herryben

Thank you so much, man. Why the Tx led on receiver keep blinking? Why not Rx led?

Robin2

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Serial.readstring() is a blocking function. Have a look at the examples in Serial Input Basics - simple reliable, non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


Everytime that a new message is received save the value of millis() - something like
Code: [Select]
lastMessageTime = millis();
and then your code can have a check like this to see if the time between messages was too long
Code: [Select]
if (millis() - lastMessageTime >= allowedTime) {
   // message has failed, do something
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up