I have been able to reduce the power from ~20mA to ~4mA by using Sleep_n0m1.h and RFM69Ext.h. When I switch from using the RFM69 to RFM69Ext library I start getting errors when looking at the packets that the node is getting from the gateway. I need the node to be able to receive from the gateway every 1s, the power on the gateway doesn't matter. Could someone shine some light on this problem?
On the node I see:
Transmitting at 433 Mhz...
*4*8**
3*46*unlocked:ok!
*4*8**
3*46*locked:ok!
*0**test
3*46**test
3*46**test
3*46**test
3*46**0**test
It should say:
Transmitting at 433 Mhz...
unlocked:ok!
locked:ok!
test
test
Here is the code:
#include <RFM69.h>
#include <RFM69Ext.h>
#include <SPI.h>
#include <Sleep_n0m1.h>
#define GATEWAYID 1
#define NODEID 2 //unique for each node on same network
#define NETWORKID 100 //the same on all nodes that talk to each other
#define FREQUENCY RF69_433MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ACK_TIME 30 // max # of ms to wait for an ack
#define SERIAL_BAUD 115200
#define LED 9 // Moteinos have LEDs on D9
#define LOCKSENSOR 7
#define MOTOR_EN 4
#define MOTOR_1 3
#define MOTOR_2 5
char message[] = "a";
char buff[20];
long lastPeriod = -1;
int unlock_delay = 100;
int lock_delay = 100;
bool requestACK = true;
bool LOCKED = false;
bool UNLOCKED = false;
bool LOCKING = false;
bool UNLOCKING = false;
bool OLD_STATE = false;
bool promiscuousMode = true;
byte ackCount=0;
//RFM69 radio;
RFM69Ext radio;
Sleep sleep;
void setup()
{
Serial.begin(SERIAL_BAUD);
char buff[50];
sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(buff);
pinMode(LOCKSENSOR, INPUT);
pinMode(MOTOR_1, OUTPUT);
pinMode(MOTOR_2, OUTPUT);
pinMode(MOTOR_EN, OUTPUT);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
radio.setHighPower();
radio.encrypt(ENCRYPTKEY);
}
void loop()
{
delay(200);
radio.listen(1);
radio.sleep();
sleep.pwrDownMode();
sleep.sleepDelay(300);
radio.listen(0);
if (OLD_STATE!=digitalRead(LOCKSENSOR))
{
if (digitalRead(LOCKSENSOR))
{
if (radio.sendWithRetry(GATEWAYID, "u", 1))
Serial.println("unlocked:ok!");
else Serial.println("unlock:nothing...");
}
else
{
if (radio.sendWithRetry(GATEWAYID, "l", 1))
Serial.println("locked:ok!");
else Serial.println("lock:nothing...");
}
OLD_STATE = digitalRead(LOCKSENSOR);
}
while (LOCKING)
{
if (digitalRead(LOCKSENSOR))
{
digitalWrite(MOTOR_EN, HIGH);
digitalWrite(MOTOR_1, HIGH);
digitalWrite(MOTOR_2, LOW);
}
else
{
delay(lock_delay);
digitalWrite(MOTOR_1, LOW);
digitalWrite(MOTOR_2, LOW);
digitalWrite(MOTOR_EN, LOW);
LOCKING = false;
}
}
while (UNLOCKING)
{
if (!digitalRead(LOCKSENSOR))
{
digitalWrite(MOTOR_EN, HIGH);
digitalWrite(MOTOR_1, LOW);
digitalWrite(MOTOR_2, HIGH);
}
else
{
delay(unlock_delay);
digitalWrite(MOTOR_1, LOW);
digitalWrite(MOTOR_2, LOW);
digitalWrite(MOTOR_EN, LOW);
UNLOCKING = false;
}
}
if (radio.receiveDone())
{
for (byte i = 0; i < radio.DATALEN; i++)
message = (char)radio.DATA;
Serial.println(message);
if (message[0] == 'u')
{
UNLOCKING = true;
LOCKING = false;
Serial.println("unlock message received!");
}
if (message[0] == 'l')
{
LOCKING = true;
UNLOCKING = false;
Serial.println("lock message received!");
}
if (radio.ACK_REQUESTED)
{
byte theNodeID = radio.SENDERID;
radio.sendACK();
if (ackCount++%3==0) delay(3);
}
Blink(LED,1000);
}
if (Serial.available() > 0)
{
char input = Serial.read();
if (input == 's')
{
digitalWrite(MOTOR_1, LOW);
digitalWrite(MOTOR_2, LOW);
digitalWrite(MOTOR_EN, LOW);
}
if (input == 'l')
{
UNLOCKING = false;
LOCKING = true;
}
if (input == 'u')
{
UNLOCKING = true;
LOCKING = false;
}
}
}
void Blink(byte PIN, int DELAY_MS)
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
}