Arduino Code

The Transmitter Code (NEEDS TO INPUT CALL SIGN)

#include <Adafruit_LSM303.h>
#include <RF22.h>
#include <SPI.h>
#include <Wire.h>

RF22 rf22;
unsigned long previousMillis = 0; //will be changed periodically for interval purposes
Adafruit_LSM303 lsm303;
//Configuration Options————————————————————–//
//==================================
//MAKE SURE TO ADD YOUR CALL SIGN
//==================================

uint8_t callsign[] = “NOCALL”;
char callsignRep[] = “NOCALL”; //cant cast uint8_t
const unsigned long interval = 590000; //60*10*1000-10000 should be 9 minutes, 50 second
const unsigned long intermissionPeriod = 10000; //10 seconds
//note: 60000 milliseconds per minute
float frequency = 431.300;
float ppm = -8; //transmitter correction offset
unsigned int packet =0;

void setup()
{
Serial.begin(9600);
Wire.begin();
if (! rf22.init())
Serial.println(“RF22 init failed”);
if (!lsm303.begin())
{
Serial.println(“unable to initialize the LSM303. Check your wiring!”);
}
rf22.setFrequency(frequency * ((1000000 + ppm)/1000000));
//rf22.setTxPower(RF22_TXPOW_20DBM);
rf22.setModemConfig(RF22::GFSK_Rb57_6Fd28_8);

//send callsign at beginning of transmissions
rf22.send(callsign,sizeof(callsign));
lsm303.setMagGain(Adafruit_LSM303::LSM303_MAGGAIN_1_3);

}

void loop()
{
uint8_t msg[18];

//packet counter
uint16_t counter = (uint16_t) packet;
msg[2] = counter & 0xff;
msg[3] = (counter >> 8);

//Extract data from Magnetometer and Accelerometer, these are float values (change to real things later)
lsm303.read();
float ax = lsm303.accelData.x;
int temp = (int) ax;
uint16_t acc_x = (uint16_t) temp;
msg[4] = acc_x & 0xff;
msg[5] = (acc_x >> 8);

float ay = lsm303.accelData.y;
temp = (int) ay;
uint16_t acc_y = (uint16_t) temp;
msg[6] = acc_y & 0xff;
msg[7] = (acc_y >> 8);

float az = lsm303.accelData.z;
temp = (int) az;
uint16_t acc_z = (uint16_t) temp;
msg[8] = acc_z & 0xff;
msg[9] = (acc_z >> 8);

float mx = lsm303.magData.x;
temp = (int) mx;
uint16_t mag_x = (uint16_t) temp;
msg[10] = mag_x & 0xff;
msg[11] = (mag_x >> 8);

float my = lsm303.magData.y;
temp = (int) my;
uint16_t mag_y = (uint16_t) temp;
msg[12] = mag_y & 0xff;
msg[13] = (mag_y >> 8);

float mz = lsm303.magData.z;
temp = (int) mz;
uint16_t mag_z = (uint16_t) temp;
msg[14] = mag_z & 0xff;
msg[15] = (mag_z >> 8);

//get time of transmission
unsigned long ttime = millis();
uint32_t tCast = (uint32_t) ttime;
msg[0] = tCast & 0xff;
msg[1] = (tCast >> 8) & 0xff;
msg[16] = (tCast >> 16) & 0xff;
msg[17] = (tCast >> 24);

//For testing purposes, output the values that are sent
delay(10);
#ifdef TRANSMIT_DEBUG
Serial.print(ttime);
Serial.print(“,”);
Serial.print(ax, 2);
Serial.print(“,”);
Serial.print(ay, 2);
Serial.print(“,”);
Serial.print(az, 2);
Serial.print(“,”);
Serial.print(mx, 2);
Serial.print(“,”);
Serial.print(my, 2);
Serial.print(“,”);
Serial.println(mz, 2);
#endif

rf22.send(msg, sizeof(msg));
rf22.waitPacketSent();
packet++;
unsigned long currentMillis = millis();

if (currentMillis – previousMillis > interval) //check if the allowed interval is up
{
rf22.send(callsign,sizeof(callsign));
rf22.setModeIdle();
delay(intermissionPeriod); //delay by a time before starting loop again
rf22.setModeTx(); //If this is the problem…
previousMillis = millis(); //can’t use current millis because time has elapsed
}
}

The Receiver Code

#include <SPI.h>
#include <RF22.h>

RF22 rf22;
float frequency = 431.300;
float ppm = -8; //transmitter correction offset

void setup()
{
delay(6000);
Serial.begin(115200);
if (!rf22.init())
Serial.println(“RF22 init failed”);
rf22.setFrequency(frequency * ((1000000 + ppm)/1000000));
rf22.setModemConfig(RF22::GFSK_Rb57_6Fd28_8 );
uint8_t reginitialval = rf22.spiRead(RF22_REG_6A_AGC_OVERRIDE2);
reginitialval |= 0xB;//Set bits for 001011
reginitialval &= ~(0x34);//Unset bits for 001011
rf22.spiWrite(RF22_REG_6A_AGC_OVERRIDE2, reginitialval);
}

float convert(uint8_t& small, uint8_t& large)
{
uint16_t lsmall = (uint16_t) small;
uint16_t llarge = (((uint16_t) large) << 8);
int isum = (int)(lsmall + llarge);
float fsum = (float) isum;
return fsum;
}

int convert_int(uint8_t& small, uint8_t& large)
{
uint16_t lsmall = (uint16_t) small;
uint16_t llarge = (((uint16_t) large) << 8);
uint16_t isum = lsmall + llarge;
return isum;
}

unsigned long timeConvert(uint8_t& small, uint8_t& med, uint8_t& large, uint8_t& huge)
{
uint32_t lsmall = (uint32_t) small;
uint32_t lmed = (((uint32_t) med) << 8);
uint32_t llarge = (((uint32_t) large) << 16);
uint32_t lhuge = (((uint32_t) huge) << 24);
unsigned long sum = (unsigned long)(lsmall + lmed + llarge +lhuge);
return sum;
}

void loop()
{

while (true)
{
if (rf22.waitAvailableTimeout(170) == true)
{
uint8_t rssi = rf22.lastRssi(); //have to take this value first otherwise returns junk
float dbm= 0.4979 * rssi – 122.54; //linear regression performed using data found from https://www.sparkfun.com/datasheets/Wireless/General/RFM22.PDF pg 64
Serial.print(dbm, 4);
Serial.print(“,”);
uint8_t buf[RF22_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf22.recv(buf, &len))
{
unsigned long rtime = millis();
Serial.print(rtime, DEC); //print receiver time
Serial.print(‘,’);
unsigned long ttime = timeConvert(buf[0], buf[1], buf[16], buf[17]);
Serial.print(ttime, DEC); //print transmit time
Serial.print(‘,’);
uint16_t packet = convert_int(buf[2], buf[3]);
Serial.print(packet); //print transmit time
Serial.print(‘,’);

int incrm = 4;
while (incrm <= 14) //the last useful values are stored in indexes 12,13
{
float dataOutput = convert(buf[incrm], buf[incrm + 1]);
Serial.print(dataOutput); //print out the accelerometer and magnetometer values in order listed
//Serial.print(incrm);
if (incrm == 14) //14 & 15 are the last useful data
{
Serial.print(‘\n’); //we’ve already outputted the last piece of useful data
break;
}

Serial.print(‘,’);
incrm += 2;
}
}
}
else
{
uint8_t rssi = rf22.rssiRead(); //have to take this value first otherwise returns junk
float dbm= 0.4979 * rssi – 122.54;
Serial.println(dbm, DEC); //convert output into dbm instead of rssi
} //end of if statement
} //end of infinite while loop

}//end of arduino loop

Advertisements
Posted in Uncategorized
Hours & Info
w6yra@ucla.edu
Nets: Thursdays 5PM, 448.540 MHz-, Tone/PL 82.5 Hz