Go Down

Topic: HC 12 transreceiver Loosing Transfer Data (Read 150 times) previous topic - next topic

Hello everyone,


I have difficulties programming an Arduino and HC-12 433 MHz module to transmit and (more importantly) receive a readable and useable text string. In short, I need to transmit a string from one HC-12 to the other, and display it on a P10 DMD LED DISPLAY. 

So when i upload the following code at both Arduino's (Nano and Uno) everything works fine. Sending and receiving messages through serial monitors works both sides.

Code: [Select]

#include <SoftwareSerial.h>

const byte HC12RxdPin = 3;                  // Recieve Pin on HC12
const byte HC12TxdPin = 2;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port

void setup() {
  Serial.begin(9600);                       // Open serial port to computer
  HC12.begin(9600);                         // Open serial port to HC12
}

void loop() {
  if(HC12.available()){                     // If Arduino's HC12 rx buffer has data
    Serial.write(HC12.read());              // Send the data to the computer
    }
  if(Serial.available()){                   // If Arduino's computer rx buffer has data
    HC12.write(Serial.read());
   
  }
}


 

The problem appears when im trying make it work with the following code on the Nano's side where the DMD Ledboard is connected.

Code: [Select]
#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>        //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h>        //
#include <TimerOne.h>  //


const byte HC12RxdPin = 10;                  // Recieve Pin on HC12
const byte HC12TxdPin = 12;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port


char stringa[60];
int i = 0;
int maxnum = 10;
int count = 0;

#define DISPLAYS_ACROSS 6
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);


void ScanDMD()
{
  dmd.scanDisplayBySPI();
}


void setup()
{

   //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
   Timer1.initialize(4800 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
   Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()

   //clear/init the DMD pixels held in RAM
  dmd.clearScreen( true );   //true is normal (all pixels off), false is negative (all pixels on)
  dmd.selectFont(System5x7);
  Serial.begin(9600);
  HC12.begin(9600); 
 
}


void loop(void)
{

     if (HC12.available() > 0) {
     
    int inbyte = HC12.read();
    switch (inbyte) {



case 'w' :

  dmd.clearScreen(true);
  dmd.drawString(  14,  0, "WELCOME", 7, GRAPHICS_NORMAL );
  delay(2000);
  dmd.clearScreen(true);

 
 break;


case'a':

     
    memset(stringa, 0, sizeof(stringa)); // set string contents to zero
    Serial.setTimeout(50); // second input timeout
    i = HC12.readBytesUntil('\n', stringa, sizeof(stringa)-1); // Read line of input
 dmd.drawString( 0, 0,stringa, strlen(stringa), GRAPHICS_NORMAL );
   
 break;



    }

}

}




While sending strings from Uno's Serial monitor like "aTest" what it happens is sometimes works and some others not. Sometimes string Test appears but some others without change anything i m getting strings like "te))" , "@k1)" etc.. with confused or losted characters...


srnet

#1
Jan 14, 2018, 06:28 pm Last Edit: Jan 14, 2018, 06:29 pm by srnet
So, what happens when in the second program you the SMD stuff, just disable the interrupt ?
$50SAT is now Silent (but probably still running)
http://www.50dollarsat.info/
http://www.loratracker.uk/

Take a look at the following code which works absolutely perfect when i m conncted to the arduino directly through my vb net application.Through my app i send every 1 second a Textbox.text string to the arduino. This text appears on my P10 Display without any problem. What i m typing in the vb net textbox appears on my Led display. But when I m trying to do the same throug the HC12 Transreceiver nothing works normaly. Some times i get the text that i wrote some others noting and the most of them i get unexpected characters like ";[p@$#... or letters i have never wrote in the textbox. So below is the working code

Code: [Select]

#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>        //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h>        //
#include <TimerOne.h>  //
#include "SystemFont5x7.h"

const byte HC12RxdPin = 10;                  // Recieve Pin on HC12
const byte HC12TxdPin = 12;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;



#define DISPLAYS_ACROSS 6
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);


void ScanDMD()
{
  dmd.scanDisplayBySPI();
}


void setup()
{

   //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
   Timer1.initialize(4800 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
   Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()

   //clear/init the DMD pixels held in RAM
  dmd.clearScreen( true );   //true is normal (all pixels off), false is negative (all pixels on)
  dmd.selectFont(System5x7);
  Serial.begin(9600);
  HC12.begin(9600); 
 
}

void loop() {
 recvBytesWithStartEndMarkers();
    showNewData();

 
}


void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '[';
    char endMarker = ']';
    char rc;
 
    while (Serial[/color].available() > 1 && newData == false) {
        rc = Serial[/color].read();
       
        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

 void showNewData() {
 
 if (newData == true) {
  Serial.println(receivedChars);
    dmd.clearScreen( true );
 dmd.drawString( 0, 0,receivedChars, strlen(receivedChars), GRAPHICS_NORMAL );
 newData = false;
 }
 
 }     




So In case of wireless communication i m using an arduino uno at the transimmitng side and a nano at the receiving. They both have their HC 12 .

Transmitting CODE :

Code: [Select]

#include <SoftwareSerial.h>

const byte HC12RxdPin = 3;                  // Recieve Pin on HC12
const byte HC12TxdPin = 2;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port

void setup() {
  Serial.begin(9600);                       // Open serial port to computer
  HC12.begin(9600);                         // Open serial port to HC12
}

void loop() {
  if(HC12.available()){                     // If Arduino's HC12 rx buffer has data
    Serial.write(HC12.read());              // Send the data to the computer
    }
  if(Serial.available()){                   // If Arduino's computer rx buffer has data
    HC12.write(Serial.read());
   
  }
}






Receiver CODE:

Code: [Select]
#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>        //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h>        //
#include <TimerOne.h>  //
#include "SystemFont5x7.h"

const byte HC12RxdPin = 10;                  // Recieve Pin on HC12
const byte HC12TxdPin = 12;                  // Transmit Pin on HC12

SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;



#define DISPLAYS_ACROSS 6
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);


void ScanDMD()
{
  dmd.scanDisplayBySPI();
}


void setup()
{

   //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
   Timer1.initialize(4800 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
   Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()

   //clear/init the DMD pixels held in RAM
  dmd.clearScreen( true );   //true is normal (all pixels off), false is negative (all pixels on)
  dmd.selectFont(System5x7);
  Serial.begin(9600);
  HC12.begin(9600); 
 
}

void loop() {
 recvBytesWithStartEndMarkers();
    showNewData();

 
}


void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '[';
    char endMarker = ']';
    char rc;
 
    while (HC12.available() > 1 && newData == false) {
        rc = HC12.read();
       
        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

 void showNewData() {
 
 if (newData == true) {
  Serial.println(receivedChars);
    dmd.clearScreen( true );
 dmd.drawString( 0, 0,receivedChars, strlen(receivedChars), GRAPHICS_NORMAL );
 newData = false;
 }
 
 }     




Its excactly the same with the working code at the begining the only thing that i have cahange is these two lines

Code: [Select]
while (HC12.available() > 1 && newData == false) {
        rc = HC12.read();


where the Serial changed to HC12

Go Up