Difficulty getting two moteinos to drive a servo remotely [SOLVED]

Started by Mr. Bubbles, July 22, 2014, 10:15:31 PM

Mr. Bubbles

Hello,

I'm working on a project that uses one moteino to communicate with another via RF in order to turn on/off a servo. The gateway moteino will be wired to a switch that, when thrown, transmits a 1 instead of 0, instructing a node moteino to turn on or off a servo, accordingly. I am using code posted here from another thread as reference.

Gateway code:
/* Wirelessly transmit
 */

#include <RFM69.h>
#include <RFM69registers.h>
#include <SPI.h>
#include <SPIFlash.h>

#define NODEID      99
#define NETWORKID   100
#define GATEWAYID   1
#define FREQUENCY   RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 9600
//#define ACK_TIME    30  // # of ms to wait for an ack ---- Not Currently Used.

// Ack will not be used. This is a streaming application. If a packet is lost the next servo position data
// will be accurate enough to update and move the servo.

SPIFlash flash(8, 0xEF30);                           //EF40 for 16mbit windbond chip
RFM69 radio;                                         // initiate radio object
    

 typedef struct 
 {		
   int           feederstate;                              //create transmit variable & store data potentiometer 0 data
 } Payload;
 Payload theData;                                    // create transmission package 'theData'.


// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status


void setup() 
{
  pinMode(buttonPin, INPUT);     
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower();                              //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);                             // Output to serial operating frequency of transceiver       
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");          // Output to serial regarding status if Flash memory exists.
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

void loop() 
{

    Blink(LED, 1000);
    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH) {     
      // turn feeder on:
      Feed(1);
    } 
    else {
      // turn feeder off:
      Feed(0);
    }
    delay(1000);
}


void Feed(int feed_input)
{
    //initiate start feed command to second moteino
     // Output to serial some communications information - Data length, RSSI
    for (byte i = 0; i < radio.DATALEN; i++)
    Serial.print((char)radio.DATA[i]);
    Serial.print("   [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
    Serial.println();

 // translate Pot rotation to Servo position.    
    theData.feederstate = feed_input;
  
 // Transmit payload - theData
    radio.send(GATEWAYID, (const void*)(&theData), sizeof(theData));   // transmit data to other end
       
 // Output to serial information of # bytes sent.
    Serial.print("Sending struct (");
    Serial.print(sizeof(theData));
    Serial.println(" bytes) ");
    Serial.println();
    Serial.print(theData.feederstate);
    Serial.println();
}

//void Feed(int feed_time_ms)


 void Blink(byte PIN, int DELAY_MS)
  {
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
  }


Node code:
/* Wirelessly receive

 #include <RFM69.h>
 #include <SPI.h>
 #include <SPIFlash.h>
 #include <Servo.h>

 #define NODEID      1
 #define NETWORKID   100
 #define FREQUENCY   RF69_915MHZ                      //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
 #define KEY         "thisIsEncryptKey"               //has to be same 16 characters/bytes on all nodes, not more not less!
 #define LED         9
 #define SERIAL_BAUD 9600
 #define ACK_TIME    30                               // # of ms to wait for an ack --- Not currently used.

// Ack will not be used. This is a streaming application. If a packet is lost the next servo position data
// will be accurate enough to update and move the servo.

 RFM69 radio;                                         // initiate radio object
 SPIFlash flash(8, 0xEF30);                           //EF40 for 16mbit windbond chip
 bool promiscuousMode = false;                        //set to 'true' to sniff all packets on the same network

     //  Setup Servo Objects
 Servo Servo0;
 #define servopin         5
 typedef struct 
 {		
  int           feederstate;                                 //designate transmitted data for this potentiometer 0 value
 } Payload;
 Payload theData;

 void setup() 
 {

 // Attach Servo Objects to respective PWM pins


 // Radio initialization
  Serial.begin(SERIAL_BAUD);
  delay(100);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower(); //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  radio.promiscuous(promiscuousMode);
  char buff[50];
  sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);                                // Output operting frequency of transceiver
  
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");              // Print status of Flash memory
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
 }

 byte ackCount=0;

 void loop() 
 { 
  theData.feederstate = 2;
   //process any serial input
  if (Serial.available() > 0) {
    char input = Serial.read();
    }

  if (radio.receiveDone()) 
  {  
  //Output to serial information of: NodeID & RSSI  
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.println("]");
    if (promiscuousMode) 
    {
      Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
    }
	
    if (radio.DATALEN != sizeof(Payload))
      Serial.print("Invalid payload received, not matching Payload struct!");
    else
    {
      theData = *(Payload*)radio.DATA;                       //assume radio.DATA actually contains our struct and not something else

   // Print to Serial Terminal to verify RF data and Servo degree conversions.
      Serial.print(" Data_Servo0 ");
      Serial.println(theData.feederstate);
      Serial.println();
      if (theData.feederstate == 1) {
        Serial.print(" Starting to feed ");
        Serial.println();
        Servo0.attach(servopin); 
        Servo0.write(0);                           // Write position to servo0
      }
      else if (theData.feederstate == 0) {
        Serial.print(" Stopping feed ");
        Serial.println();
        //turn feeder off
        Servo0.detach();
      }
      else {
        Serial.print(" invalid ");
        Serial.println();
      }
    }
    }
    
  Blink(LED,3);                                             // Blink indicates transceiver operation.
  }




 void Blink(byte PIN, int DELAY_MS)
  {
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
  }


When I compile and upload the programs, it seems to work initially. The node moteino indicates that it is receiving a '0' packet of data from the gateway, but when the switch is thrown, the serial output stops. It does not give an error; just stops. It then resumes with outputting the "stopping feed" once the switch is back in a neutral position and the gateway is sending a 0. I've confirmed the output of the gateway device by monitoring serial output, but I can't find where the issue is.

Any ideas?

Felix

Without the hardware and knowing what your libraries do I cannot guesstimate where the issue is but I can throw a few ideas:

- check the way you debounce your button
- check that your Servo0 class is not spending a LOT of time doing its thing. You want to call radio.receiveDone() as often as possible. This is one of the most common mistakes, calling receiveDone once then going and going something for the next few hundred ms. You will miss packets.

Mr. Bubbles

I've updated the scripts some and changed the two numbers being sent to 2 and 3, and commented out the servo lines (for troubleshooting). So, they should not cause any delay. I also removed the LED blinking, and really there is no delay in the radio receiving function. I can still monitor the data being sent from the "gateway" moteino and confirm that '2' and '3' are being sent. Monitoring the output log of the receiving moteino, i get:

Listening at 915 Mhz...
SPI Flash Init FAIL! (is chip present?)
 No signal  No signal  No signal  No signal  No signal  No signal...
 Data_Servo0 2
 invalid 
 No signal  No signal  No signal  No signal  No signal  No signal ...
 Data_Servo0 2
 invalid 
 No signal  No signal  No signal  No signal  No signal  No signal...


I've redacted some of the repeats of "No signal" to make it more forum-friendly, but you get the point.
When the switch is off, the receiving moteino gets the "2" and outputs "invalid," which is fine--however, when the switch is on, it just repeats with no signal. The output from the gateway moteino is as follows:

Transmitting at 915 Mhz...
SPI Flash Init FAIL! (is chip present?)
   [RX_RSSI:-128]
Sending struct (2 bytes) 

2
   [RX_RSSI:-128]
Sending struct (2 bytes) 

2
   [RX_RSSI:-128]
Sending struct (2 bytes) 

2
   [RX_RSSI:-128]
Sending struct (2 bytes) 

3
   [RX_RSSI:-128]
Sending struct (2 bytes) 

3
   [RX_RSSI:-128]
Sending struct (2 bytes) 

3
   [RX_RSSI:-128]
Sending struct (2 bytes) 

2
   [RX_RSSI:-128]
Sending struct (2 bytes) 

2
   [RX_RSSI:-128]
Sending struct (2 bytes) 

2


As you can see, it seems to be sending a '2' or '3' without issue.

I've attached the updated code of the programs below as well. I will also post some photos of the hardware on breadboard to this thread. The switch is at the top left of the image, connected with a 1Kohm resistor to pin2 and GND on one side, +5v on the other side. I do not think there is anything wrong with the switch--as you can see from the serial output, the program seems to get the input without issue. Any further ideas?

Gateway:
/* Wirelessly transmit
 */

#include <RFM69.h>
#include <RFM69registers.h>
#include <SPI.h>
#include <SPIFlash.h>

#define NODEID      99
#define NETWORKID   100
#define GATEWAYID   1
#define FREQUENCY   RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 9600
//#define ACK_TIME    30  // # of ms to wait for an ack ---- Not Currently Used.

// Ack will not be used. This is a streaming application. If a packet is lost the next servo position data
// will be accurate enough to update and move the servo.

SPIFlash flash(8, 0xEF30);                           //EF40 for 16mbit windbond chip
RFM69 radio;                                         // initiate radio object
    

 typedef struct 
 {		
   int           feederstate;                              //create transmit variable & store data potentiometer 0 data
 } Payload;
 Payload theData;                                    // create transmission package 'theData'.


// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status


void setup() 
{
  pinMode(buttonPin, INPUT);     
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower();                              //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);                             // Output to serial operating frequency of transceiver       
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");          // Output to serial regarding status if Flash memory exists.
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

void loop() 
{

    Blink(LED, 1000);
    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH) {     
      // turn feeder on:
      Feed(3);
    } 
    else {
      // turn feeder off:
      Feed(2);
    }
    delay(1000);
}


void Feed(int feed_input)
{
    //initiate start feed command to second moteino
     // Output to serial some communications information - Data length, RSSI
    for (byte i = 0; i < radio.DATALEN; i++)
    Serial.print((char)radio.DATA[i]);
    Serial.print("   [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
    Serial.println();

 // translate Pot rotation to Servo position.    
    theData.feederstate = feed_input;
  
 // Transmit payload - theData
    radio.send(GATEWAYID, (const void*)(&theData), sizeof(theData));   // transmit data to other end
       
 // Output to serial information of # bytes sent.
    Serial.print("Sending struct (");
    Serial.print(sizeof(theData));
    Serial.println(" bytes) ");
    Serial.println();
    Serial.print(theData.feederstate);
    Serial.println();
}

//void Feed(int feed_time_ms)


 void Blink(byte PIN, int DELAY_MS)
  {
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
  }


Receiving:
/* Wirelessly receive */

 #include <RFM69.h>
 #include <SPI.h>
 #include <SPIFlash.h>
 #include <Servo.h>

 #define NODEID      1
 #define NETWORKID   100
 #define FREQUENCY   RF69_915MHZ                      //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
 #define KEY         "thisIsEncryptKey"               //has to be same 16 characters/bytes on all nodes, not more not less!
 #define LED         9
 #define SERIAL_BAUD 9600
 #define ACK_TIME    30                               // # of ms to wait for an ack --- Not currently used.

// Ack will not be used. This is a streaming application. If a packet is lost the next servo position data
// will be accurate enough to update and move the servo.

 RFM69 radio;                                         // initiate radio object
 SPIFlash flash(8, 0xEF30);                           //EF40 for 16mbit windbond chip
 bool promiscuousMode = false;                        //set to 'true' to sniff all packets on the same network

     //  Setup Servo Objects
 Servo Servo0;
 #define servopin         5
 typedef struct 
 {		
  int           feederstate;                                 //designate transmitted data for this potentiometer 0 value
 } Payload;
 Payload theData;

 void setup() 
 {

 // Attach Servo Objects to respective PWM pins


 // Radio initialization
  Serial.begin(SERIAL_BAUD);
  delay(100);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower(); //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  radio.promiscuous(promiscuousMode);
  char buff[50];
  sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);                                // Output operting frequency of transceiver
  
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");              // Print status of Flash memory
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
 }

 byte ackCount=0;

 void loop() 
 { 
  theData.feederstate = 2;
   //process any serial input
/*  if (Serial.available() > 0) {
    char input = Serial.read();
    }*/
      Serial.print(" No signal ");
  if (radio.receiveDone()) 
  {  
  //Output to serial information of: NodeID & RSSI  
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.println("]");
    if (promiscuousMode) 
    {
      Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
    }
	
    if (radio.DATALEN != sizeof(Payload))
      Serial.print("Invalid payload received, not matching Payload struct!");
    else
    {
      theData = *(Payload*)radio.DATA;                       //assume radio.DATA actually contains our struct and not something else

   // Print to Serial Terminal to verify RF data and Servo degree conversions.
      Serial.print(" Data_Servo0 ");
      Serial.println(theData.feederstate);
      if (theData.feederstate == 1) {
        Serial.print(" Starting to feed ");
        Serial.println();
//        Servo0.attach(servopin); 
//        Servo0.write(0);                           // Write position to servo0
      }
      else if (theData.feederstate == 0) {
        Serial.print(" Stopping feed ");
        Serial.println();
        Serial.println();
        //turn feeder off
//        Servo0.detach();
      }
      else {
        Serial.print(" invalid ");
        Serial.println();
      }
    }
    }
    
  }




 void Blink(byte PIN, int DELAY_MS)
  {
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
  }

Felix

To verify the transceiver functionality, could you load them with the Send/Receive sketches located here:
https://github.com/LowPowerLab/RFM69/tree/master/Examples
Observe the serial output at the Receiver. If the ACKs are being responded to then there is nothing wrong with the hardware and libraries, and the sketch code is where adjustments might need to be made.

Mr. Bubbles

Example sketches working fine. Not sure what is going on with my code.

Felix

Ok good. I suggest adding baby steps to a known working program like the sample sketches. One step at a time will allow you to see where the issue is. I know this seems like a dumb suggestion but I end up having to do just that a lot of times when I make big changes to the libraries or just work on some new project.
I have not worked much with motors and servos and I'm not familiar with those arduino libs, hence i can't estimate what issues those might introduce.

Mr. Bubbles

Alright, so I did as you said and started from scratch--that is, using your example send/receive structs. I took baby steps, and got to a similar result. My code is as follows:

send moteino:
#include <RFM69.h>
#include <SPI.h>
#include <SPIFlash.h>

#define NODEID      99
#define NETWORKID   100
#define GATEWAYID   1
#define FREQUENCY   RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 115200
#define ACK_TIME    30  // # of ms to wait for an ack


const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status


int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms)
byte sendSize=0;
boolean requestACK = false;
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
RFM69 radio;

typedef struct {		
  int           nodeId; //store this nodeId
  unsigned long uptime; //uptime in ms
  int         state;
} Payload;
Payload theData;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower(); //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
  
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

long lastPeriod = -1;

void loop() {
  //read the state of the switch
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    // turn feeder on:
    theData.state = 1;
  } 
  else {
    // turn feeder off:
    theData.state = 0;
  }

  //process any serial input
  if (Serial.available() > 0)
  {
    char input = Serial.read();
    if (input >= 48 && input <= 57) //[0,9]
    {
      TRANSMITPERIOD = 100 * (input-48);
      if (TRANSMITPERIOD == 0) TRANSMITPERIOD = 1000;
      Serial.print("\nChanging delay to ");
      Serial.print(TRANSMITPERIOD);
      Serial.println("ms\n");
    }
    
    if (input == 'r') //d=dump register values
      radio.readAllRegs();
    //if (input == 'E') //E=enable encryption
    //  radio.encrypt(KEY);
    //if (input == 'e') //e=disable encryption
    //  radio.encrypt(null);
    
    if (input == 'd') //d=dump flash area
    {
      Serial.println("Flash content:");
      int counter = 0;

      while(counter<=256){
        Serial.print(flash.readByte(counter++), HEX);
        Serial.print('.');
      }
      while(flash.busy());
      Serial.println();
    }
    if (input == 'e')
    {
      Serial.print("Erasing Flash chip ... ");
      flash.chipErase();
      while(flash.busy());
      Serial.println("DONE");
    }
    if (input == 'i')
    {
      Serial.print("DeviceID: ");
      word jedecid = flash.readDeviceId();
      Serial.println(jedecid, HEX);
    }
  }

  //check for any received packets
  if (radio.receiveDone())
  {
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    for (byte i = 0; i < radio.DATALEN; i++)
      Serial.print((char)radio.DATA[i]);
    Serial.print("   [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");

    if (radio.ACK_REQUESTED)
    {
      radio.sendACK();
      Serial.print(" - ACK sent");
      delay(10);
    }
    Blink(LED,5);
    Serial.println();
  }
  
  int currPeriod = millis()/TRANSMITPERIOD;
  if (currPeriod != lastPeriod)
  {
    //fill in the struct with new values
    theData.nodeId = NODEID;
    theData.uptime = millis();
    
    Serial.print("Sending struct (");
    Serial.print(sizeof(theData));
    Serial.print(" bytes) ... ");
    Serial.print("State = ");
    Serial.print(theData.state);
    if (radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData))) {
      Serial.print(" ok!");
      Serial.println();
    }
    else Serial.print(" nothing...");
    Serial.println();
    Blink(LED,3);
    lastPeriod=currPeriod;
  }
}

void Blink(byte PIN, int DELAY_MS)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}


Receive moteino:
#include <RFM69.h>
#include <SPI.h>
#include <SPIFlash.h>
#include <Servo.h>

#define NODEID      1
#define NETWORKID   100
#define FREQUENCY   RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 115200
#define ACK_TIME    30  // # of ms to wait for an ack
#define servopin    5

Servo Servo0;

RFM69 radio;
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network

typedef struct {		
  int           nodeId; //store this nodeId
  unsigned long uptime; //uptime in ms
  int           state;   //temperature maybe?
} Payload;
Payload theData;

void setup() {
  Serial.begin(SERIAL_BAUD);
  delay(10);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower(); //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  radio.promiscuous(promiscuousMode);
  char buff[50];
  sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

byte ackCount=0;
void loop() {

  //process any serial input
  if (Serial.available() > 0)
  {
    char input = Serial.read();
    if (input == 'r') //d=dump all register values
      radio.readAllRegs();
    if (input == 'E') //E=enable encryption
      radio.encrypt(KEY);
    if (input == 'e') //e=disable encryption
      radio.encrypt(null);
    if (input == 'p')
    {
      promiscuousMode = !promiscuousMode;
      radio.promiscuous(promiscuousMode);
      Serial.print("Promiscuous mode ");Serial.println(promiscuousMode ? "on" : "off");
    }
    
    if (input == 'd') //d=dump flash area
    {
      Serial.println("Flash content:");
      int counter = 0;

      while(counter<=256){
        Serial.print(flash.readByte(counter++), HEX);
        Serial.print('.');
      }
      while(flash.busy());
      Serial.println();
    }
    if (input == 'D')
    {
      Serial.print("Deleting Flash chip content... ");
      flash.chipErase();
      while(flash.busy());
      Serial.println("DONE");
    }
    if (input == 'i')
    {
      Serial.print("DeviceID: ");
      word jedecid = flash.readDeviceId();
      Serial.println(jedecid, HEX);
    }
  }

  if (radio.receiveDone())
  {
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
    if (promiscuousMode)
	{
      Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
    }
	
    if (radio.DATALEN != sizeof(Payload))
      Serial.print("Invalid payload received, not matching Payload struct!");
    else
    {
      theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else
      Serial.print(" nodeId=");
      Serial.print(theData.nodeId);
      Serial.print(" uptime=");
      Serial.print(theData.uptime);
      Serial.print(" state=");
      Serial.print(theData.state);

      if (theData.state == 1) {
        //Turn servo on
        Serial.print(" Starting feed ");
        Serial.println();
        //Servo0.attach(servopin); 
        //Servo0.write(0);
      }
      else if (theData.state == 0) {
        //Turn servo off
        Serial.print(" Stopping feed ");
        Serial.println();
        //Servo0.detach();
      }
    }
    
    if (radio.ACK_REQUESTED)
    {
      byte theNodeID = radio.SENDERID;
      radio.sendACK();
      Serial.print(" - ACK sent.");

      // When a node requests an ACK, respond to the ACK
      // and also send a packet requesting an ACK (every 3rd one only)
      // This way both TX/RX NODE functions are tested on 1 end at the GATEWAY
      if (ackCount++%3==0)
      {
        Serial.print(" Pinging node ");
        Serial.print(theNodeID);
        Serial.print(" - ACK...");
        delay(3); //need this when sending right after reception .. ?
        if (radio.sendWithRetry(theNodeID, "ACK TEST", 8, 0))  // 0 = only 1 attempt, no retries
          Serial.print("ok!");
        else Serial.print("nothing");
      }
    }
    Serial.println();
    Blink(LED,3);
  }
}

void Blink(byte PIN, int DELAY_MS)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}


Output from sending moteino:
Transmitting at 915 Mhz...
SPI Flash Init FAIL! (is chip present?)
Sending struct (8 bytes) ... State = 0 ok!

[1] ACK TEST   [RX_RSSI:-38] - ACK sent
Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

[1] ACK TEST   [RX_RSSI:-39] - ACK sent
Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

[1] ACK TEST   [RX_RSSI:-43] - ACK sent
Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

[1] ACK TEST   [RX_RSSI:-42] - ACK sent
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 1 nothing...
Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

[1] ACK TEST   [RX_RSSI:-40] - ACK sent
Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

[1] ACK TEST   [RX_RSSI:-39] - ACK sent
Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!

Sending struct (8 bytes) ... State = 0 ok!


It seems to be sending state=0 without issue (and the receiving moteino receives that packet without issue), but when I put pin 2 to HIGH, I get "nothing", which indicates a radio transmission fail? Any idea what to think of this? I just don't know where else to start.

Thanks

ekoelle

What type of servo are you using and what are you using as a power source? Does it work if you replace the servo with an LED?

Mr. Bubbles

Quote from: ekoelle on July 24, 2014, 09:28:35 PM
What type of servo are you using and what are you using as a power source? Does it work if you replace the servo with an LED?

I've tried using both a USB port as power via FTDI as well as a 5V 1A wall wart, both with the same result. The servo should be irrelevant at this point. It isn't hooked up and the lines are commented out. The issue lies somewhere in the button / switch and it affecting the radio transmission.

Mr. Bubbles

So looking at the code, it seems that the sending moteino outputs "nothing..." when the current transmission period is equal to the last, that way if the program loops and is still within the same period, it will loop again until it enters a new period. Because it is staying on "nothing," it must be staying within the same period. I'm wondering if pushing the button or activating the switch is somehow disrupting the timing millis() or is shorting/restarting the program--but this doesn't perfectly fit, because it seems that the program, if restarted, would run through the setup() commands again.

Any additional ideas?

Mr. Bubbles

I switched the button / switch pin to pin 4 on the moteino and now it works fine. Not sure if something is wrong with pin 2 or what, but it works now.

Felix

Pin D2 is the interrupt used by the radio ;)
Good to check the schematics/diagrams: moteino.com/#pinout

Mr. Bubbles