gateway sensor reading issue

Started by alexthecat, April 12, 2016, 12:01:57 AM

alexthecat

Hello,
I'm working on a sensor project but using an anarduino mini wireless, it uses the mega328p chip and has an rfm69 chip on it, it is similar to the moteino, but it seems like you all have a much more active community, so I'm hoping I can bug you all for some pointers.   a brief overview of my project
I have two of these sensors, one is setup to be a node, the other a gateway, both programs are very close to the example script that comes with the rfm69 library.  I have the node setup with a temperature sensor, and sending the data to the gate way just fine.  I've then formatted the data a bit, and spit it out the serial of the gateway, this I have a python script setup on a Raspberry Pi to write it to a file, and then upload it "to the cloud" using fluentd.    what I'm trying to do is now hookup a load cell to the gateway, to tack this into the data stream going out the serial to my log file.
My Problem
I can setup the load cell on the anarduino just fine, and it will output the weight when configured with only that sketch, when I try to combine the gateway and the load cell together it appears to freeze up.  I think it may be the radio portion that is having the conflict as I can output the weight in the setup before I initialize the radio, but when I try doing this after turning on the radio, the whole sketch freezes.  I am using an HX711 to amp up the voltages, and am using Bogde's library to talk to the chip. 

what I'd like help on

If at the very least someone could give me some pointers on what to check try, or if I've made an obvious mistake I'd greatly appreciate it.  I'm not looking for an answer to get handed to me on a platter (but if someone wanted to, I won't fuss  :P )  I'm really trying to learn, I've gotten quite a bit of it figured out, I'm just at this hurdle, and not sure the best way around it. 

Thank you all,
AlexTheCat


perky

One thing to look at is interrupt routines that drive or change port pins, if your foreground code also changes bits within that port you will need to disable interrupts while wiggling the IO and then re-enable them again so that the operation is atomic. Also look for shared resources, again you're looking at disabling interrupts and possibly initializing and restoring configuration registers.
Mark.

alexthecat

Hey Perky,
Thank you for the advice!  I gave it a try, and it doesn't look to take me quite there yet, but I suspect that I may have a conflict with my pins.  I was using pins 13 and 11 for the load cell as the diagram has them labeled as sck and mosi but I am now thinking that the Radio may be using those pins as well?  can someone tell me if my thinking here is on the right track or way off?  a link to the Schematic, with the pinout right above can be found here:
http://www.anarduino.com/miniwireless/#schematic

Thank you all,
ATC

alexthecat

Also, because it may help troubleshoot, here is my code that doesn't work.  If I upload the sample sketch that just prints out the weight it works fine and shows a close (but needs calibrated weight, this one just prints a 1 where the weight should be

// TGateway - read TNodes... (see Tnode.ino example for more info)
//
// 2014 - anarduino.com
//
#include <RFM69.h>
#include <SPI.h>
#include "HX711.h"  //loadcell


//#define DOUT 12  //Load cell data
//#define CLK 13 //Loadcell clock
//HX711 scale(DOUT, CLK);
HX711 scale(A1, A0);
#define calibration_factor -22280
#define GATEWAY_ID    1     // this is ME, TGateway
#define NETWORKID     100    //the same on all nodes that talk to each other

// Uncomment only one of the following three to match radio frequency
//#define FREQUENCY     RF69_433MHZ    
//#define FREQUENCY     RF69_868MHZ
#define FREQUENCY     RF69_915MHZ

//#define IS_RFM69HW   //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW
#define ENCRYPT_KEY    "my-EncryptionKey"  // use same 16byte encryption key for all devices on net
#define ACK_TIME       50                  // max msec for ACK wait
#define LED            9                   // Anardino miniWireless has LEDs on D9
#define SERIAL_BAUD    115200
#define TGW_VERSION  "1.0"

#define MSGBUFSIZE 16   // message buffersize, but for this demo we only use: 
                        // 1-byte NODEID + 4-bytes for time + 1-byte for temp in C + 2-bytes for vcc(mV)
byte msgBuf[MSGBUFSIZE];

RFM69 radio;                  // global radio instance
bool promiscuousMode = false; // set 'true' to sniff all packets on the same network
bool requestACK=false;

union itag {
  uint8_t b[2];
  uint16_t i;
}it;
union ltag {
  byte b[4];
  long l;
   float  curwt;  //load cell
}lt; // used to force byte order in case we end up using result in various endian targets...
 float curwt;
void setup() {

 scale.set_scale(calibration_factor);
 Serial.print((scale.get_units(), 1));
  memset(msgBuf,0,sizeof(msgBuf));
  Serial.begin(SERIAL_BAUD);
  //Serial.print("TGateway ");
  //Serial.print(TGW_VERSION);
  //Serial.print(" startup at ");
  //Serial.print((FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915));
  //Serial.print("Mhz, Network ID: ");
  //Serial.println(NETWORKID);

  delay(50);
  radio.initialize(FREQUENCY, GATEWAY_ID, NETWORKID);
  radio.encrypt(0);
  radio.promiscuous(promiscuousMode);
#ifdef IS_RFM69HW
  radio.setHighPower(); //uncomment #define ONLY if radio is of type: RFM69HW or RFM69HCW 
#endif

}

void loop() {

//cli();
//curwt = (scale.get_units(), 1);
//sei();
  if (radio.receiveDone()) {
    for(int i=0; i<radio.DATALEN; i++) {
       if(i==sizeof(msgBuf))
          break;  // better stop if we've reached our buffer limit (shouldn't get here, as DATALEN should be less than MSGBUFSIZE)
          msgBuf[i] = radio.DATA[i];
    }
    // Okay, extract and print out the data received
    //Serial.print(" Received from TNODE:");
   
    Serial.print(" ");
    Serial.print((byte)msgBuf[0],DEC);
    // extract TNode millis
    lt.b[3] = msgBuf[1];
    lt.b[2] = msgBuf[2];
    lt.b[1] = msgBuf[3];
    lt.b[0] = msgBuf[4];
   // Serial.print(", time=");
    Serial.print(", ");
    Serial.print(lt.l,DEC);
   // Serial.print(", tempC=");
   Serial.print(", ");
    Serial.print(msgBuf[5]);
    //Serial.print(", tempF=");
    Serial.print(", ");
    Serial.print((msgBuf[5])*1.8+32,DEC);
  //  Serial.print(",vcc=");
    it.b[1] = msgBuf[6];
    it.b[0] = msgBuf[7];
    // convert TNode VCC to volts
    //float f = (float)it.i/1000.0;
//   Serial.print(f);
cli();
//curwt = (scale.get_units(), 1);
  Serial.print(", wt");
//  Serial.print(curwt);
  Serial.print((scale.get_units(), 1));
  Serial.println(" ");
 sei();
  }
}

alexthecat

OK so I solved it!   I believe my issue was trying to use pins 13 and 11 were interfering with the radio, and by the time I figured that out, I had mucked with my code too much and had an issue.  I cleaned out all the stuff from the gateway sketch, tested it, and it worked, then added the load cell code back in, now I'm getting the readings I want and the whole thing isn't crashing anymore. 

perky

If you're sharing the SPI peripheral you'll probably need to disable interrupts around your code and save and restore its register settings. Glad to hear you're making progress.
Mark.