Wireless serial communication doesn't pick up signals after reset or power cut of receiver side

Hi,
I am using wireless hc12 module, for transmitting distance data from ultrasonic sensor and to receive it on the receiver side. All said and done, project works extremely well, and now problem arises if there is any power cut or power glitch in receiver side, and receiver when retrieving the power doesn't pick up signals from transmitter as it was doing just before the power cut condition. Tried a lot of way somewhat similar question was asked before went through it and nothing worked in my favour. If anyone could please help me in this regard, it would be of great help

Components used

  1. Arduino Uno for Rx and Tx( two arduino)
  2. 2 hc 12 module
  3. ultrasonic module hcsr 04

Here is my transmitter code

#define max_distance 200
//These are the two libraries that are needed
#include <avr/interrupt.h>
#include <avr/sleep.h>

/* Here we set up our inputs and outputs.  LEDs connected to pins 10 and 13 and pushbuttons attached to 2 and 12 */ //interrupt pin is attached to digital pin 3.
#define ledPin 13// pb5
#define sleepPin 12 //pb4
#define interruptPin 11 // pb3
#define wakePin 2 // pd2
//sleepStatus is set up to keep track of the button input on pin 12.
int sleepStatus = 0;



#define trigPin 9 //pb1
#define echoPin 10//pb2
long duration;
unsigned long distance;
unsigned long length_of_interval = 1000;
unsigned long timer1;

void setup()
{
  Serial.begin(9600);

  //pinMode(trigPin, OUTPUT);// DDR MEANS PIN MODE, PORTB = DIGITALWRITE, PIN = DIGITAL READ
  //pinMode(echoPin, INPUT);
  DDRB |= B00101010;
  DDRD |= B00000000;
  PORTB |= B00010000;
  PORTD |= B00000100;

  
  timer1 = millis();

}

void loop()
{

  timer1 = millis();
  // Write a pulse to the HC-SR04 Trigger Pin
  PORTB |= B00000000;//digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  PORTB |= B00000010;//digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  PORTB &= B11111101;//digitalWrite(trigPin, LOW);

  // Measure the response from the HC-SR04 Echo Pin
  duration = pulseIn(echoPin, HIGH);

  
  distance = (duration * 0.036) / 2;
  //Serial.println(password);
  Serial.print(distance);
  Serial.print("$");
  //Serial.println();

  while ( millis() - timer1 < length_of_interval )
  {
    cli();
    sleep_enable();
    sei();
    sleep_cpu();
    sleep_disable();
  }

}

while my receiver code

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;
unsigned int a, b;



void setup() {
  Serial.begin(9600);


  Serial.println("<Arduino is ready>");

}


void loop() {
  recvWithEndMarker();
  showNewData();
  //Serial.println("entered main loop");
  if (a > 120)
  {
    
    Serial.println("entered");

  }
  else
  {
    Serial.println("exit");
  }
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '$';
  char rc;

  // if (Serial.available() > 0) {
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
      a = atoi(receivedChars);

    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);

    Serial.print("checked for long, it works dude ");
    Serial.println(a);

    //chkfn();
    newData = false;
  }
}

everything is working fine, until receiver side arduino, resets or switches off,

and if that happens serial receiver doesn't pick up signals and display the numbers as it was before

Please don't bump your thread.

I presume you have bod enabled. So you really should have code to clear any buffers with corrupted data, when there is a brown out detected.

Basically you reinitialise all variables to known states and clear any buffers, given you have no idea what the brown out did to any variable or buffer.

thanks for that, any code snippet to clear any buffer?, I guess it should be Serial.flush(), but where do I use it, tx side or receiver side also on which part of the program?

You could write a function that is called on a bod. Although, as far as I am aware, usually the brown out just resets the arduino.

What was stated was it is the receiver which may brown out, so the code should be placed in there.
As far as the transmitter is concerned, if there is no hand shaking going on, it will never know if a brown out has occurred so it is just a dumb transmitter.

I suppose a more intelligent receiver would check for a preamble code, when a transmitter starts to send data, which could be something as simple as an address code at the start of transmission. A simple conditional statement could do this.

I notice you have done this : ```
unsigned int a, b;
Which is incorrect and should never be done.
You always initialise any variable, never assume the variable is 0
The same goes for variable rc which could be anything.

This goes for any variable at any point at initialisation.
With a brown out, you have to make sure that all variables are intialised back to known states.
So that you can start afresh

These may help you work out what to do.

https://microchipdeveloper.com/8avr:avrreset
https://microchipdeveloper.com/8avr:bod

Note this states that the POR flag is never reset, unless you do it manually

yes, here the issue is with the receiver only, because if the transmitter resets itself or power shutdown happens, once the power retrieved for transmitter the wireless communication is established just like that, but if the receiver resets or power shutdown happens, the communication is not established after the power is back,

even if I use BOD, that should be used on transmitter side only which might not solve the purpose, I guess, please correct me if I am wrong

You should be using it on the receiver, given you have stated the receiver goes into brown out.

You can test for the brown out flag by reading the MCUSR, then create a function that is called in that eventuality, not forgetting to put MCUSR = 0; at the end of the function.

To be perfectly honest I would safe guard both transmitter and receiver, if either is likely to go into brown out.
It is the variables in the receiver which are more than likely becoming corrupted due to the brown out and it is these that need to be reset to known states in the event of a brown out.

Either way all variables in any program/sketch you write, should be initialised.

For more info on proper coding practices you could read the MISRA standards
Suffice to say just take my advice and not suffer that :slight_smile:

Hi,

I was using ATmega 8 ic on uno board, actually changed atmega 8 to atmega 328 ic just on receiver side and the problem seems to be solved, Thanks a ton for all your time,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.