SPI RFM12B

Hi I am trying to send data, two way’s on two Moteino. Just A simple push switch on pin 5 and the led on pin 9.
One (1 Moteino) pin 5 goes high and the other (2 Moteino) pin 9 goes high.
One (2 Moteino) pin 5 goes high and the other (1 Moteino) pin 9 goes high.
One (1 Moteino) pin 5 goes low and the other (2 Moteino) pin 9 goes low.
One (2 Moteino) pin 5 goes low and the other (1 Moteino) pin 9 goes low.
and so on.

As of now one led will light up as you push the switch on the other Moeino.

Here is my arduino Sketch, if you people can help great if not ok.

Moeino 1

int led = 9;
 const int ledPin =  9;
 
 int buttonState = 0;
const int buttonPin = 5;

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

// 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 GATEWAYID     2  //the node ID we're sending to
#define SERIAL_BAUD 115200


uint8_t KEY[] = "ABCDABCDABCDABCD";


RFM12B radio;

typedef struct { int power1; }PayloadRX;      // create structure - a neat way of packaging data for RF comms
PayloadRX emonrx;  

typedef struct {int  power2;} 
 PayloadTX; 
 PayloadTX emontx;  
 
 

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 (led, OUTPUT);
  int button = 5;
}

void loop()
{
  
  
  buttonState = digitalRead(buttonPin);
  

  emonrx.power1=emonrx.power1=buttonState;
  
  
  
  
  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {
       emontx=*(PayloadTX*) radio.Data; 
       
       
    radio.Wakeup();
    radio.Send(GATEWAYID, &emonrx, sizeof emonrx);
 

  Serial.print("power1: "); Serial.println(emonrx.power1); 
  
  Serial.print("power2: "); Serial.println(emontx.power2);
  
     
     
      
   
      if (emontx.power2 == HIGH) {     
        // turn LED on:    
        digitalWrite(ledPin, HIGH);  
      } 
   else 

        // turn LED off:
        digitalWrite(ledPin, LOW); 
        delay(100);
       {
       
       
       
       
  delay(1000);
    
       }
    }
  }
}

Moeino 2

int buttonState = 0;
const int buttonPin = 5;

int led = 9;
 const int ledPin =  9;
 
#include <RFM12B.h>
#include <avr/sleep.h>


// 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        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;


RFM12B radio;
byte sendSize=0;

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

typedef struct {int  power1;} 
 PayloadRX; 
 PayloadRX emonrx; 

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");
  pinMode (led, OUTPUT);
  int button1 = 5;
}

void loop()
{
  
  

  
 buttonState = digitalRead(buttonPin);
  

  emontx.power2=emontx.power2=buttonState;


 
 
  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");
    }
  }

  // emonrx=*(PayloadRX*) radio.Data; 
  
  radio.Wakeup();
  radio.Send(GATEWAYID, &emontx, sizeof emontx);
  

 

  Serial.print("power2: "); Serial.println(emontx.power2); 
  
  
    

  
   Serial.print("power1: "); Serial.println(emonrx.power1); 
 
  
  
   
      if (emonrx.power1 == HIGH) {     
        // turn LED on:    
        digitalWrite(ledPin, HIGH);  
      } 
   else 

        // turn LED off:
        digitalWrite(ledPin, LOW); 
        delay(100);
       {
       
       
         delay(1000);
       

           
      
    }
  }

if you people can help great if not ok.

We probably could, if you had a question. If not, OK.

Moeino 2 code isn't working right. There is something wrong with the code. The Led on Moeino 1 will not go high, when switch goes high on Moeino 2. How do you send data both way's????

why do you have 2 ints (led, and ledPin) on the same pin?

You’ve setup “led” as and output, but not “ledPin”. Then your code tells “ledPin” to do something, when it has not been defined as an input or output.

you also haven’t defined if the “buttonState” and “buttonPin” are inputs or outputs, either.

  int button = 5;
}

A local variable that immediately goes out of scope. How useful is that?

  emonrx.power1=emonrx.power1=buttonState;

Maybe you need a few more emonrx.power1= in there. You definitely need more spaces in there.

There is NOTHING magic about the name of the variable in the struct. power1 = buttonState makes no sense.

       emontx=*(PayloadTX*) radio.Data;

What is this for?

Is Moeino 1 sending data or is it receiving data? You seem to be mixing the two. If it is both a sender and a receiver, separate the sending and receiving portions.

You don't need to separate struct definitions for the sender and the receiver. The same struct definition should be used for sending and receiving. Different instances, of course, but not different definitions.

Your code is far more complicated than it needs to be to send the state of a switch from one device to another. Get THAT working before you try to get fancy.

Here is some code that works. It will only send data in one way. (Tx to Rx).
I would like to know how to send it both ways. This is where I need help?

This code has one switch, pot, and led.
(1 Moteino) switch is pin5, pot is pin0.
(2 Moteino) Led is pin9, the Led is on the (Moteino).

This is how I want to new code to work. Send data both ways.
(1 Moteino) switch is pin5, pot is pin0 and Led is pin9, the Led is on the (Moteino).
(2 Moteino) switch is pin5, pot is pin0 and Led is pin9, the Led is on the (Moteino).

1 Moteino

//
TX
//LowPowerLab Led Pot

int val;
int new_val;
int power1;
int buttonState = 0;
const int buttonPin = 5;

#include <RFM12B.h>

#define NODEID        2  
#define NETWORKID    99  
#define GATEWAYID     1  
#define SERIAL_BAUD  115200
char input = 0;
int interPacketDelay = 1000;
uint8_t KEY[] = "ABCDABCDABCDABCD";

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

void setup()
{
  Serial.begin(SERIAL_BAUD);
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);
  Serial.println("Transmitting...\n\n");
  int button1 = 5;
}

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;

  if (Serial.available() > 0) {
    input = Serial.read();
    if (input >= 48 && input <= 57) 
    {
      interPacketDelay = 100 * (input-48);
      if (interPacketDelay == 0) interPacketDelay = 1000;
      Serial.print("\nChanging delay to ");
      Serial.print(interPacketDelay);
      Serial.println("ms\n");
    }
  }
  radio.Wakeup();
  radio.Send(GATEWAYID, &emontx, sizeof emontx);
  
 
  Serial.print("power1: "); Serial.println(emontx.power1); 
  Serial.print("power2: "); Serial.println(emontx.power2); 
 delay(100);
  }

2 Moteino

//RX
//LowPowerLab Pot and Led
 int led1 = 9;
 byte power1;
 const int ledPin =  9;
 const int MyData_NodeID=10;

 #include <RFM12B.h>

 #define NODEID           1  
 #define NETWORKID       99  
 #define SERIAL_BAUD 115200
 uint8_t KEY[] = "ABCDABCDABCDABCD";

 RFM12B radio;

 typedef struct {int power1, power2;} 
 PayloadTX; 
 PayloadTX emontx;  

void setup()
 {
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);     
  Serial.begin(SERIAL_BAUD);
  Serial.println("Listening...");
  pinMode (led1, OUTPUT);
 }

 void loop()
 {
  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {

      emontx=*(PayloadTX*) radio.Data; 
      Serial.print("power1: "); 
      Serial.println(emontx.power1); 
      Serial.print("power2: "); 
      Serial.println(emontx.power2); 
      
     if (emontx.power2 == HIGH) {     
        // turn LED on:    
        digitalWrite(ledPin, HIGH);  
      } 
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW); 
        delay(100);

      }
    }
  }
}

Is there any hope for me????????

Hi

Here is the simple way of sending tx to rx, if there is some one out there that can give me
some hints on how to send it from tx to rx and rx to tx, it would be great.

I would like help on sending buttonPin = 5 and val both ways?
Here is my simple code…

TX

//TX
//LowPowerLab Led Pot

int val;
int new_val;
//int power1;
int buttonState = 0;
const int buttonPin = 5;

#include <RFM12B.h>


#define NODEID        2  
#define NETWORKID    99  
#define GATEWAYID     1  
#define SERIAL_BAUD  115200
char input = 0;
int interPacketDelay = 1000;
uint8_t KEY[] = "ABCDABCDABCDABCD";

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


void setup()
{
  Serial.begin(SERIAL_BAUD);
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);
  Serial.println("Transmitting...\n\n");
}
void loop()
{
   val = analogRead(0);
  
  new_val = map(val, 0, 1023, 253, 0); 
  
 buttonState = digitalRead(buttonPin);
  
 emontx.power1=new_val;
 emontx.power2=buttonState;

   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("Sending");
 
  radio.Send(GATEWAYID, &emontx, sizeof emontx);
  
  Serial.print("power1: "); Serial.println(emontx.power1); 
  Serial.print("power2: "); Serial.println(emontx.power2); 
 
delay(1000);
  
}
//RX
//LowPowerLab Pot and Led

 const int ledPin =  9;

 #include <RFM12B.h>

 #define NODEID           1  
 #define NETWORKID       99  
 #define SERIAL_BAUD 115200
 uint8_t KEY[] = "ABCDABCDABCDABCD";

 RFM12B radio;

 typedef struct {int power1, power2;} 
 PayloadTX; 
 PayloadTX emontx;  

 void setup()
 {
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);     
  Serial.begin(SERIAL_BAUD);
  Serial.println("Listening...");
  pinMode (ledPin, OUTPUT);
 }

 void loop()
 {
  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {

      emontx=*(PayloadTX*) radio.Data; 

      Serial.print("power1: "); 
      Serial.println(emontx.power1); 
      Serial.print("power2: "); 
      Serial.println(emontx.power2); 
      
if (emontx.power2 == HIGH) {     
        // turn LED on:    
        digitalWrite(ledPin, HIGH);  
      } 
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW); 
        delay(1000);

      }
    }
  }
}

Hi I have this all most working. But I don’t get why it isn’t receiving data on one 1 Moteino.

1 Moteino

const int ledPin =  9;

int val;
int new_val;

int power11 = 0;
int power22 = 0;

int buttonState = 0;
const int buttonPin = 5;

#include <RFM12B.h>
#define NODEID        2  
#define NETWORKID    99  
#define GATEWAYID     1  
#define SERIAL_BAUD  115200
char input = 0;
int interPacketDelay = 1000;
uint8_t KEY[] = "ABCDABCDABCDABCD";
RFM12B radio;

typedef struct  { int power1, power2;}
PayloadTX;
PayloadTX emontx; 

typedef struct {int power11, power22;} 
PayloadRX; 
PayloadRX emonrx;  

void setup()
{
  Serial.begin(SERIAL_BAUD);
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);
  Serial.println("Transmitting...\n\n");
  pinMode (ledPin, OUTPUT);
}
void loop()
{
  val = analogRead(0);
  new_val = map(val, 0, 1023, 253, 0); 
  buttonState = digitalRead(buttonPin);
  emontx.power1=new_val;
  emontx.power2=buttonState;

  if (Serial.available() > 0) {
    input = Serial.read();
    if (input >= 48 && input <= 57) 
    {
      interPacketDelay = 100 * (input-48);
      if (interPacketDelay == 0) interPacketDelay = 1000;
      Serial.print("\nChanging delay to ");
      Serial.print(interPacketDelay);
      Serial.println("ms\n");
    }
  }
  radio.Send(GATEWAYID, &emontx, sizeof emontx);
  Serial.print("power1: "); 
  Serial.println(emontx.power1); 
  Serial.print("power2: "); 
  Serial.println(emontx.power2); 
  delay(1000);     

  if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {
      emonrx=*(PayloadRX*) radio.Data; 
      Serial.print("power11: "); 
      Serial.println(emonrx.power11); 
      Serial.print("power22: "); 
      Serial.println(emonrx.power22); 
    }
  } 
  if (emonrx.power22 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    delay(1000);
  }
}

2 Moteino

const int ledPin1 =  9;

int val1;
int new_val1;

int buttonState1 = 0;
const int buttonPin1 = 5;

#include <RFM12B.h>
#define GATEWAYID        2
#define NODEID           1  
#define NETWORKID       99 

#define SERIAL_BAUD 115200
char input = 0;
int interPacketDelay = 1000;
uint8_t KEY[] = "ABCDABCDABCDABCD";
RFM12B radio;

typedef struct  {int power11, power22;}
PayloadRX;
PayloadRX emonrx; 

typedef struct {int power1, power2;} 
PayloadTX; 
PayloadTX emontx;  

void setup()
{
  radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
  radio.Encrypt(KEY);     
  Serial.begin(SERIAL_BAUD);
  Serial.println("Listening...");
  pinMode (ledPin1, OUTPUT);
  radio.Send(GATEWAYID, &emonrx, sizeof emonrx);
}

void loop()
{
  val1 = analogRead(0);
  new_val1 = map(val1, 0, 1023, 253, 0); 
  buttonState1 = digitalRead(buttonPin1);
  emonrx.power11=new_val1;
  emonrx.power22=buttonState1;

if (Serial.available() > 0) {
    input = Serial.read();
    if (input >= 48 && input <= 57) 
    {
      interPacketDelay = 100 * (input-48);
      if (interPacketDelay == 0) interPacketDelay = 1000;
      Serial.print("\nChanging delay to ");
      Serial.print(interPacketDelay);
      Serial.println("ms\n");
    }
}
  //radio.Send(GATEWAYID, &emonrx, sizeof emonrx);
  Serial.print("power11: "); 
  Serial.println(emonrx.power11); 
  Serial.print("power22: "); 
  Serial.println(emonrx.power22); 
  delay(1000);

if (radio.ReceiveComplete())
  {
    if (radio.CRCPass())
    {
      emontx=*(PayloadTX*) radio.Data; 
      Serial.print("power1: "); 
      Serial.println(emontx.power1); 
      Serial.print("power2: "); 
      Serial.println(emontx.power2); 
    }
  }
if (emontx.power2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW); 
    delay(1000);
  }
}