Author Topic: Rfm12b  (Read 5152 times)

JimJim

  • NewMember
  • *
  • Posts: 14
Rfm12b
« on: October 22, 2013, 06:49:53 PM »
Hi,  I would like some help,  I am trying to send some data.  What I don't know how to do is send that
data on the rfm12b.  I have two programs (Arduino) Tx and Rx.

What I am trying to do is, when A push button goes High,  it will go to the Moteino (TX) to Moteion (RX)to turn on led.  and  Then use A Potentiometer to Moteino (TX) to Moteion (RX) and then (pwm) led.  and
count

power3+3;
battery+4;
brian+1000;


Here is my code,  I am missing something one the tx and rx code.


TX
Code: [Select]
int val;
int new_val;
int power1;
int buttonState = 0;
const int buttonPin = 7;

#include <RFM12B.h>
#include <avr/sleep.h>


#define NODEID        2  //network ID used for this unit
#define NETWORKID    99  //the network ID we are on
#define GATEWAYID     1  //the node ID we're sending to
#define ACK_TIME     50  // # of ms to wait for an ack
#define SERIAL_BAUD  115200


uint8_t KEY[] = "ABCDABCDABCDABCD";


RFM12B radio;
typedef struct {int power1, power2, power3, battery,brian;}PayloadTX;
PayloadTX emontx;


void setup()
{
  Serial.begin(SERIAL_BAUD);
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);
  radio.Sleep(); //sleep right away to save power
  Serial.println("Transmitting...\n\n");
}

void loop()
{
   val = analogRead(0);

  new_val = map(val, 0, 1023, 253, 0);

  buttonState = digitalRead(buttonPin);

  emontx.power1=emontx.power1=new_val;
  emontx.power2=emontx.power2=buttonState;
  emontx.power3=emontx.power3+3;
  emontx.battery=emontx.battery+4;
  emontx.brian=emontx.brian+1000;
 
 radio.Wakeup();
 {
 radio.Send(0, &emontx, sizeof emontx);
 }
 

  Serial.print("power1: ");
  Serial.println(emontx.power1);
  Serial.print("power2: ");
  Serial.println(emontx.power2);
  Serial.print("power3: ");
  Serial.println(emontx.power3);
  Serial.print("battery: ");
  Serial.println(emontx.battery);
  Serial.print("brian: ");
  Serial.println(emontx.brian);
  Serial.print(new_val);
  Serial.println("  ");

  delay(1000);
}



RX

Code: [Select]
byte power1;
const int ledPin =  9;
const int ledPin2 =  7;
#include <RFM12B.h>


#define NODEID           1  //network ID used for this unit
#define NETWORKID       99  //the network ID we are on
#define SERIAL_BAUD 115200

typedef struct { int power1, power2, power3, battery,brian; } PayloadTX;
PayloadTX emontx; 

const int emonTx_NodeID=2;            //emonTx node ID

uint8_t KEY[] = "ABCDABCDABCDABCD";


RFM12B radio;
void setup()
{
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);      //comment this out to disable encryption
  Serial.begin(SERIAL_BAUD);
  Serial.println("Listening...");
  pinMode (7, OUTPUT);
}

void loop()
{
  if (radio.ReceiveComplete())
 
  {
    if (radio.CRCPass())
    {
 
   // if (NodeID == emonTx_NodeID)  {             
        emontx=*(PayloadTX*)radio.DataLen;
      }
       
       Serial.print("power1: "); Serial.println(emontx.power1);
       Serial.print("power2: "); Serial.println(emontx.power2);
       Serial.print("power3: "); Serial.println(emontx.power3);
       Serial.print("battery: "); Serial.println(emontx.battery);
       Serial.print("brian: "); Serial.println(emontx.brian);
       
       Serial.print(emontx.power1);
       Serial.println("  ");
       analogWrite (9, emontx.power1);
       
         if (emontx.power2 == HIGH)
                     { // turn LED on:   
              digitalWrite(ledPin2, HIGH); 
                    }
                else {
                            // turn LED off:
                   digitalWrite(ledPin2, LOW);
                 }
         }
  } 


 
« Last Edit: October 22, 2013, 06:52:09 PM by JimJim »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Rfm12b
« Reply #1 on: October 22, 2013, 10:17:00 PM »
Did you see the LED examples for blinking/PWMing LEDs remotely?

https://github.com/LowPowerLab/RFM12B/tree/master/Examples

You might have better luck by using the atmega built in pullups and drive the buttons low when they are pushed. Then you just have to read the buttons consistently and flip some LED state depending on the received packet.

JimJim

  • NewMember
  • *
  • Posts: 14
Re: Rfm12b
« Reply #2 on: October 23, 2013, 06:12:14 PM »
Here is all I want,  it like no one can help me. 

I want to use this code (Tx Rx) I found on the internet.  Want to add an (16-byte encryption KEY).  That is all I am looking for help for. 





TX
Code: [Select]
//Simple RFM12B wireless demo - Receiver - no ack
//Glyn Hudson openenergymonitor.org GNU GPL V3 12/4/12
//Credit to JCW from Jeelabs.org for RFM12

#include <JeeLib.h>

#define myNodeID 30 //node ID of Rx (range 0-30)
#define network 210 //network group (can be in the range 1-250).
#define freq RF12_433MHZ //Freq of RF12B can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. Match freq to module

typedef struct { int power1, power2, power3, battery; } PayloadTX; // create structure - a neat way of packaging data for RF comms
PayloadTX emontx;

const int emonTx_NodeID=10; //emonTx node ID

void setup() {
 
  rf12_initialize(myNodeID,freq,network); //Initialize RFM12 with settings defined above
  Serial.begin(9600);
  Serial.println("RF12B demo Receiver - Simple demo");
 
 Serial.print("Node: ");
 Serial.print(myNodeID);
 Serial.print(" Freq: ");
 if (freq == RF12_433MHZ) Serial.print("433Mhz");
 if (freq == RF12_868MHZ) Serial.print("868Mhz");
 if (freq == RF12_915MHZ) Serial.print("915Mhz");
 Serial.print(" Network: ");
 Serial.println(network);
}

void loop() {
 
 if (rf12_recvDone()){
  if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0) {
   
    int node_id = (rf12_hdr & 0x1F);                 //extract nodeID from payload
       
    if (node_id == emonTx_NodeID) { //check data is coming from node with the corrct ID
        emontx=*(PayloadTX*) rf12_data; // Extract the data from the payload
       Serial.print("power1: "); Serial.println(emontx.power1);
       Serial.print("power2: "); Serial.println(emontx.power2);
       Serial.print("power3: "); Serial.println(emontx.power3);
       Serial.print("battery: "); Serial.println(emontx.battery);
       Serial.println(" ");
  }
 }
}
}



RX
Code: [Select]
//Simple RFM12B wireless demo - Receiver - no ack
//Glyn Hudson openenergymonitor.org GNU GPL V3 12/4/12
//Credit to JCW from Jeelabs.org for RFM12

#include <JeeLib.h>

#define myNodeID 30 //node ID of Rx (range 0-30)
#define network 210 //network group (can be in the range 1-250).
#define freq RF12_433MHZ //Freq of RF12B can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. Match freq to module

typedef struct { int power1, power2, power3, battery; } PayloadTX; // create structure - a neat way of packaging data for RF comms
PayloadTX emontx;

const int emonTx_NodeID=10; //emonTx node ID

void setup() {
 
  rf12_initialize(myNodeID,freq,network); //Initialize RFM12 with settings defined above
  Serial.begin(9600);
  Serial.println("RF12B demo Receiver - Simple demo");
 
 Serial.print("Node: ");
 Serial.print(myNodeID);
 Serial.print(" Freq: ");
 if (freq == RF12_433MHZ) Serial.print("433Mhz");
 if (freq == RF12_868MHZ) Serial.print("868Mhz");
 if (freq == RF12_915MHZ) Serial.print("915Mhz");
 Serial.print(" Network: ");
 Serial.println(network);
}

void loop() {
 
 if (rf12_recvDone()){
  if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0) {
   
    int node_id = (rf12_hdr & 0x1F);                 //extract nodeID from payload
       
    if (node_id == emonTx_NodeID) { //check data is coming from node with the corrct ID
        emontx=*(PayloadTX*) rf12_data; // Extract the data from the payload
       Serial.print("power1: "); Serial.println(emontx.power1);
       Serial.print("power2: "); Serial.println(emontx.power2);
       Serial.print("power3: "); Serial.println(emontx.power3);
       Serial.print("battery: "); Serial.println(emontx.battery);
       Serial.println(" ");
  }
 }
}
}
« Last Edit: October 23, 2013, 07:03:48 PM by JimJim »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Rfm12b
« Reply #3 on: October 23, 2013, 07:52:12 PM »
If you use Jeelib you will have to call this function after you initialize RFM12b:

Code: [Select]
rf12_encrypt((uint8_t*)"ABCDABCDABCDABCD");

Here's the reference for that: http://jeelabs.net/pub/docs/jeelib/RF12_8cpp.html#a3c062141ee4f93a63b156b3454d0e205

JimJim

  • NewMember
  • *
  • Posts: 14
Re: Rfm12b
« Reply #4 on: October 30, 2013, 07:40:29 PM »
Hi is there Anyone that can help me with sending (int) from (0 to 255) on the rfm12.  I have some code started but there are no examples
out there.  On how to do this. 




TX
Code: [Select]
int pot0;
int pot1;
int pot2;
int pot3;
int pot4;
int pot5;


#include <RFM12B.h>
#include <avr/sleep.h>

 
#define NODEID        2  //network ID used for this unit
#define NETWORKID    99  //the network ID we are on
#define GATEWAYID     1  //the node ID we're sending to
#define ACK_TIME     50  // # of ms to wait for an ack
#define SERIAL_BAUD  115200


uint8_t KEY[] = "ABCDABCDABCDABCD";

int interPacketDelay = 1000; //wait this many ms between sending packets
char input = 0;

// Need an instance of the Radio Module
RFM12B radio;
byte sendSize=0;
char payload[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*(){}[]`|<>?+=:;,.";


void setup()
{
  Serial.begin(SERIAL_BAUD);
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);
  radio.Sleep(); //sleep right away to save power
  Serial.println("Transmitting...\n\n");
}

void loop()
{
         pot0 = analogRead(0);
         pot1 = analogRead(1);
         pot2 = analogRead(2);
         pot3 = analogRead(3);
         pot4 = analogRead(4);
         pot5 = analogRead(5);
         
         
  if (Serial.available() > 0) {
    input = Serial.read();
    if (input >= 48 && input <= 57) //[1..9] = {100..900}ms; [0]=1000ms
    {
      interPacketDelay = 100 * (input-48);
      if (interPacketDelay == 0) interPacketDelay = 1000;
      Serial.print("\nChanging delay to ");
      Serial.print(interPacketDelay);
      Serial.println("ms\n");
     
     
       
     
     
     
     
    }
  }
  Serial.println(pot0);
  Serial.println(pot1);
  Serial.println(pot2);
  Serial.println(pot3);
  Serial.println(pot4);
  Serial.println(pot5);
 
 
 
 
 
  Serial.print("Sending[");
  Serial.print(sendSize+1);
  Serial.print("]:");
  for(byte i = 0; i < sendSize+1; i++)
  Serial.print((char)payload[i]);
 

  radio.Send(GATEWAYID, payload, sendSize+1);
 
 


  sendSize = (sendSize + 1) % 88;
  Serial.println();
  delay(interPacketDelay);
}





RX
Code: [Select]


#include <RFM12B.h>

// You will need to initialize the radio by telling it what ID it has and what network it's on
// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
// The Network ID takes values from 0-255
// By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
#define NODEID           1  //network ID used for this unit
#define NETWORKID       99  //the network ID we are on
#define SERIAL_BAUD 115200

//encryption is OPTIONAL
//to enable encryption you will need to:
// - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
// - to call .Encrypt(KEY) to start encrypting
// - to stop encrypting call .Encrypt(NULL)
uint8_t KEY[] = "ABCDABCDABCDABCD";

// Need an instance of the Radio Module
RFM12B radio;
void setup()
{
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);      //comment this out to disable encryption
  Serial.begin(SERIAL_BAUD);
  Serial.println("Listening...");
}

void loop()
{
  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {
      Serial.print('[');Serial.print(radio.GetSender());Serial.print("] ");
      for (byte i = 0; i < *radio.DataLen; i++) //can also use radio.GetDataLen() if you don't like pointers
        Serial.print((char)radio.Data[i]);

      if (radio.ACKRequested())
      {
        radio.SendACK();
        Serial.print(" - ACK sent");
      }
    }
    else
      Serial.print("BAD-CRC");
   
    Serial.println();
  }
}
« Last Edit: October 30, 2013, 07:42:22 PM by JimJim »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Rfm12b
« Reply #5 on: October 30, 2013, 07:44:31 PM »
If all you need is a byte, tou can do something like this:

Code: [Select]
  payload[0] = 123;
  radio.Send(GATEWAYID, payload, 1);

Also see this example for RFM69 which uses structs, you can easily adapt that code for RFM12B or extract the useful part (sending a struct):
https://github.com/LowPowerLab/RFM69/tree/master/Examples/Struct_send
https://github.com/LowPowerLab/RFM69/tree/master/Examples/Struct_receive

JimJim

  • NewMember
  • *
  • Posts: 14
Re: Rfm12b
« Reply #6 on: October 30, 2013, 10:36:03 PM »
I wish people would make very simple  example's of code.  Because most people on here are not pro's at programing arduino code.
But I guess that's life........  The people that are good at making code,  think all people are as good as them...........I think like this, start
out simple and then become pro......... Make simple for all to catch on to..........But that's not the way it works...........

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Rfm12b
« Reply #7 on: October 30, 2013, 11:42:39 PM »
Jim,
C++ is a bit difficult to grasp compared to other languages, mainly because sooner or later you have to deal with understanding memory, pointers, bytes, bit logic etc. This is especially true for embedded systems and Arduino (pretty much anything other than blinking LEDs...). The examples I posted along with the RFM12B and RFM69 libraries are meant to cover the most general of cases where you might want to send data as clear ASCII text or as structs, or just send some signals back and forth. They are basic examples of how to initialize and send data with the radios. These examples will of course not cover every possible scenario everyone needs. I will add more examples as they become available.
Basically the 'payload' is an array of bytes (really just a pointer to an array). You can dump ANYTHING in that array.

If you want a single byte value (0-255) then just dump that value in the first byte of the array:

Code: [Select]
payload[0]=123;

If you want characters then do something like:

Code: [Select]
payload[0]='a';
payload[1]='b';
payload[2]='c';

and so forth.
Unfortunately developing any type of code will require a little more than copy pasting. But the good news is we're all in the same boat ;)