Hi all,
So I'm just writing my first test programs to try and get my regular Moteino (remote) and my Moteino MEGA (receiver) to send / receive a simple message.
My regular Moteino has two push buttons that can single click and double click. For a total of four inputs, call them "1", "2", "3", "4". It also has an LED to be flashed quickly if the ACK was received after the message was sent. Slow single flash for a fail.
My Moteino MEGA is hooked up to a 16x2 LCD screen and I am looking to just display the result of the push button input. It also has some code to flash the onboard LED to show a message has been received.
Here is the code for the remote:
#include <OneButton.h>
#include <RFM69.h>
#include <SPI.h>
// Setup a new OneButton on pin A1.
OneButton button1(3, true);
// Setup a new OneButton on pin A2.
OneButton button2(5, true);
// Addresses for this node. CHANGE THESE FOR EACH NODE!
#define NETWORKID 0 // Must be the same for all nodes
#define MYNODEID 2 // My node ID
#define TONODEID 1 // Destination node ID
// RFM69 frequency
#define FREQUENCY RF69_915MHZ
// AES encryption (or not):
#define ENCRYPT true // Set to "true" to use encryption
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes
// Use ACKnowledge when sending messages (or not):
#define USEACK true // Request ACKs or not
// Packet sent/received indicator LED (optional):
#define LED 4 // LED positive pin
//#define GND 8 // LED ground pin
// Create a library object for our RFM69HCW module:
RFM69 radio;
int sendlength = 0;
char message;
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
void setup() {
pinMode(LED,OUTPUT);
digitalWrite(LED,LOW);
// link the button 1 functions.
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
// link the button 2 functions.
button2.attachClick(click2);
button2.attachDoubleClick(doubleclick2);
//Initialize the RFM69HCW:
radio.initialize(FREQUENCY, MYNODEID, NETWORKID);
// Turn on encryption if set to true above
if (ENCRYPT)
radio.encrypt(ENCRYPTKEY);
}
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// main code here, to run repeatedly:
void loop() {
// keep watching the push buttons:
button1.tick();
button2.tick();
// You can implement other code in here or just wait a while
delay(10);
} // loop
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// ----- button 1 callback functions
// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
void click1() {
message = "1";
if (radio.sendWithRetry(TONODEID, message, sendlength)){
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
delay(150);
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
}
else{
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
}
}
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
message = "2";
if (radio.sendWithRetry(TONODEID, message, sendlength)){
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
delay(150);
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
}
else{
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
}
}
// doubleclick1
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
void click2() {
message = "3";
if (radio.sendWithRetry(TONODEID, message, sendlength)){
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
delay(150);
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
}
else{
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
}
}
// click2
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
void doubleclick2() {
message = "4";
if (radio.sendWithRetry(TONODEID, message, sendlength)){
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
delay(150);
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
}
else{
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
}
}
// doubleclick2
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
And here is the code for the receiver:
#include <LiquidCrystal.h>
#include <RFM69.h>
#include <SPI.h>
// These constants won't change. They're used to give names to the pins used:
//const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
//int sensorValue = 0;
const int limitswitch = 25;
// initialize the library with the numbers of the interface pins
//Pin D16 is connected and was intended to somehow control back lighting but does not seem to be used.
LiquidCrystal lcd(18, 17, 22, 21, 20, 19); /*LCD connections*/
// Addresses for this node. CHANGE THESE FOR EACH NODE!
#define NETWORKID 0 // Must be the same for all nodes
#define MYNODEID 1 // My node ID
#define TONODEID 2 // Destination node ID
// RFM69 frequency, uncomment the frequency of your module:
//#define FREQUENCY RF69_433MHZ
#define FREQUENCY RF69_915MHZ
// AES encryption (or not):
#define ENCRYPT true // Set to "true" to use encryption
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes
// Use ACKnowledge when sending messages (or not):
#define USEACK true // Request ACKs or not
// Packet sent/received indicator LED (optional):
#define LED 15 // LED positive pin
// Create a library object for our RFM69HCW module:
RFM69 radio;
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("START"); // Intro Display
delay(1000);
pinMode(limitswitch, INPUT);
pinMode(LED,OUTPUT);
digitalWrite(LED,LOW);
radio.initialize(FREQUENCY, MYNODEID, NETWORKID);
// Turn on encryption if set to true above
if (ENCRYPT)
radio.encrypt(ENCRYPTKEY);
}
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------*/
void loop() {
// read the analog in value:
//sensorValue = analogRead(analogInPin);
if (radio.receiveDone()) // Got one!
{
// Print out the information:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("node:");
lcd.print(radio.SENDERID, DEC);
lcd.print(", val:");
lcd.print((char)radio.DATA[0], DEC);
// Send an ACK if requested.
if (radio.ACKRequested())
{
radio.sendACK();
//Serial.println("ACK sent");
}
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
delay(150);
digitalWrite(LED,HIGH);
delay(250);
digitalWrite(LED,LOW);
}
if (digitalRead(limitswitch) == 1){
lcd.setCursor(0,1);
lcd.print("UP");
}
else{
lcd.setCursor(0,1);
lcd.print("DOWN");
}
delay(100);
}
The flashing LEDs on the remote and the receiver would indicate that the message has been successfully sent and received... but the print out on the LCD screen is just showing the value to be '0' every time. So the LCD output looks like "node: 2, val:0"
I think I am somehow not using the right data type, or something is going on when I try to read/write the char values.
Any guidance on how to properly put messages into the right data type before sending and also processing them after receiving would be really helpful!
The basis of my code was found here:
https://learn.sparkfun.com/tutorials/rfm69hcw-hookup-guideThanks!