Skip to content

Commit

Permalink
Merge pull request #13 from openenergymonitor/v1.4
Browse files Browse the repository at this point in the history
version 1.4.
  • Loading branch information
glynhudson authored Nov 16, 2018
2 parents fca9f38 + b93b685 commit 3f8227e
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 186 deletions.
Binary file modified emontx-3-phase-userguide.pdf
Binary file not shown.
66 changes: 33 additions & 33 deletions src/config.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Byte
#include <EEPROM.h>

// Available Serial Commands
const PROGMEM char helpText1[] =
const PROGMEM char helpText1[] =
"\n"
"Available commands for config during start-up:\n"
" <nnn>g - set Network Group\n"
Expand Down Expand Up @@ -59,7 +59,7 @@ static void load_config(bool verbose)
if (dataPresent)
{
for (byte j=0; j<sizeof(data); j++, src++)
*src = EEPROM.read(j);
*src = EEPROM.read(j);

nodeID = data.nodeID;
// RF_freq = data.RF_freq;
Expand All @@ -70,20 +70,20 @@ static void load_config(bool verbose)
i2Cal = data.i2Cal;
i2Lead = data.i2Lead;
i3Cal = data.i3Cal;
i3Lead = data.i3Lead;
i4Cal = data.i4Cal;
i4Lead = data.i4Lead;
i3Lead = data.i3Lead;
i4Cal = data.i4Cal;
i4Lead = data.i4Lead;

}
}

if (verbose)
{
if (dataPresent)
Serial.println(F("Loaded EEPROM config"));
else
else
Serial.println(F("No EEPROM config"));
list_calibration();
}
}
}

static void list_calibration(void)
Expand All @@ -106,8 +106,8 @@ static void save_config()

//Save new settings
byte* src = (byte*) &data;
data.nodeID = nodeID;
/*
data.nodeID = nodeID;
/*
data.RF_freq = 4; //RF_freq;
*/
data.networkGroup = networkGroup;
Expand All @@ -117,13 +117,13 @@ static void save_config()
data.i2Cal = i2Cal;
data.i2Lead = i2Lead;
data.i3Cal = i3Cal;
data.i3Lead = i3Lead;
data.i4Cal = i4Cal;
data.i4Lead = i4Lead;
data.i3Lead = i3Lead;
data.i4Cal = i4Cal;
data.i4Lead = i4Lead;


for (byte j=0; j<sizeof(data); j++, src++)
EEPROM[j] = *src;
EEPROM[j] = *src;

for (byte j=0; j<sizeof(data); j++)
{
Expand All @@ -137,7 +137,7 @@ static void wipe_eeprom(void)
byte* src = (byte*)&data;
Serial.println(F("Resetting..."));
for (byte j=0; j<sizeof(data); j++)
EEPROM[j] = 255;
EEPROM[j] = 255;
Serial.println("Done. Sketch will now restart using default config.");
delay(200);
}
Expand Down Expand Up @@ -179,19 +179,19 @@ void readInput(void)



static bool config(char c)
static bool config(char c)
{

if ('0' <= c && c <= '9')
if ('0' <= c && c <= '9')
{
value = 10 * value + c - '0';
return false;
}

if (c > ' ')
if (c > ' ')
{

switch (c)
switch (c)
{
case 'i': //set node ID
if (value)
Expand Down Expand Up @@ -241,7 +241,7 @@ static bool config(char c)
/*
//Print Current RF config
if (RF_STATUS==1)
if (RF_STATUS==1)
{
Serial.print(F(" "));
Expand All @@ -262,7 +262,7 @@ static bool config(char c)
*/
Serial.println(F(" "));

}
}
value = 0;
return false;

Expand All @@ -274,32 +274,32 @@ void getCalibration(void)
{
/*
* Reads calibration information (if available) from the serial port. Data is expected in the format
*
*
* k[x] [y] [z]
*
*
* where:
* [x] = a single numeral: 0 = voltage calibration, 1 = ct1 calibration, 2 = ct2 calibration, etc
* [y] = a floating point number for the voltage/current calibration constant
* [z] = a floating point number for the phase calibration for this c.t. (z is not needed, or ignored if supplied, when x = 0)
*
*
* e.g. k0 256.8
* k1 90.9 1.7
*
* k1 90.9 1.7
*
* If power factor is not displayed, it is impossible to calibrate for phase errors,
* and the standard value of phase calibration MUST BE SENT when a current calibration is changed.
*
*
*/

if (Serial.available())
{
char c = Serial.peek();
if (c == 'k')
{
int k1 = Serial.parseFloat();
double k2 = Serial.parseFloat();
double k3 = Serial.parseFloat();
int k1 = Serial.parseFloat();
double k2 = Serial.parseFloat();
double k3 = Serial.parseFloat();
while (Serial.available())
Serial.read();
Serial.read();

// Write the values back as Globals, re-calculate intermediate values.
switch (k1) {
Expand Down Expand Up @@ -356,7 +356,7 @@ void getCalibration(void)
save_config(); // Save to EEPROM. Atemga328p has 1kB EEPROM
// flush the input buffer
while (Serial.available())
Serial.read();
Serial.read();

}
}
Expand All @@ -367,7 +367,7 @@ static byte bandToFreq (byte band) {
}
*/

static void showString(PGM_P s) {
static void showString (PGM_P s) {
for (;;) {
char c = pgm_read_byte(s++);
if (c == 0)
Expand Down
36 changes: 18 additions & 18 deletions src/rfm.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Interface for the RFM69CW Radio Module

#ifdef RFM69CW

#include <avr/sleep.h>
#define REG_FIFO 0x00
#include <avr/sleep.h>
#define REG_FIFO 0x00
#define REG_OPMODE 0x01
#define MODE_TRANSMITTER 0x0C
#define REG_DIOMAPPING1 0x25
#define REG_DIOMAPPING1 0x25
#define REG_IRQFLAGS2 0x28
#define IRQ2_FIFOFULL 0x80
#define IRQ2_FIFONOTEMPTY 0x40
Expand All @@ -18,7 +18,7 @@ Interface for the RFM69CW Radio Module


void rfm_init(void)
{
{
// Set up to drive the Radio Module
digitalWrite(RFMSELPIN, HIGH);
pinMode(RFMSELPIN, OUTPUT);
Expand All @@ -28,37 +28,37 @@ void rfm_init(void)
SPI.setClockDivider(SPI_CLOCK_DIV4); // decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present

// Initialise RFM69CW
do
do
writeReg(0x2F, 0xAA); // RegSyncValue1
while (readReg(0x2F) != 0xAA) ;
do
writeReg(0x2F, 0x55);
writeReg(0x2F, 0x55);
while (readReg(0x2F) != 0x55);

writeReg(0x01, 0x04); // RegOpMode: RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY
writeReg(0x02, 0x00); // RegDataModul: RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 = no shaping
writeReg(0x03, 0x02); // RegBitrateMsb ~49.23k BPS
writeReg(0x04, 0x8A); // RegBitrateLsb
writeReg(0x05, 0x05); // RegFdevMsb: ~90 kHz
writeReg(0x05, 0x05); // RegFdevMsb: ~90 kHz
writeReg(0x06, 0xC3); // RegFdevLsb
#ifdef RF12_868MHZ
writeReg(0x07, 0xD9); // RegFrfMsb: Frf = Rf Freq / 61.03515625 Hz = 0xD90000 = 868.00 MHz as used JeeLib
writeReg(0x07, 0xD9); // RegFrfMsb: Frf = Rf Freq / 61.03515625 Hz = 0xD90000 = 868.00 MHz as used JeeLib
writeReg(0x08, 0x00); // RegFrfMid
writeReg(0x09, 0x00); // RegFrfLsb
#elif defined RF12_915MHZ // JeeLib uses 912.00 MHz
writeReg(0x07, 0xE4); // RegFrfMsb: Frf = Rf Freq / 61.03515625 Hz = 0xE40000 = 912.00 MHz as used JeeLib
#elif defined RF12_915MHZ // JeeLib uses 912.00 MHz
writeReg(0x07, 0xE4); // RegFrfMsb: Frf = Rf Freq / 61.03515625 Hz = 0xE40000 = 912.00 MHz as used JeeLib
writeReg(0x08, 0x00); // RegFrfMid
writeReg(0x09, 0x00); // RegFrfLsb
#else // default to 433 MHz band
writeReg(0x07, 0x6C); // RegFrfMsb: Frf = Rf Freq / 61.03515625 Hz = 0x6C8000 = 434.00 MHz as used JeeLib
writeReg(0x07, 0x6C); // RegFrfMsb: Frf = Rf Freq / 61.03515625 Hz = 0x6C8000 = 434.00 MHz as used JeeLib
writeReg(0x08, 0x80); // RegFrfMid
writeReg(0x09, 0x00); // RegFrfLsb
#endif

// writeReg(0x0B, 0x20); // RegAfcCtrl:
writeReg(0x11, RFPWR); // RegPaLevel = 0x9F = PA0 on, +13 dBm -- RFM12B equivalent: 0x99 | 0x88 (-10dBm) appears to be the max before the AC power supply fails @ 230 V mains. Min value is 0x80 (-18 dBm)
writeReg(0x1E, 0x2C); //
writeReg(0x25, 0x80); // RegDioMapping1: DIO0 is used as IRQ
writeReg(0x25, 0x80); // RegDioMapping1: DIO0 is used as IRQ
writeReg(0x26, 0x03); // RegDioMapping2: ClkOut off
writeReg(0x28, 0x00); // RegIrqFlags2: FifoOverrun

Expand All @@ -80,7 +80,7 @@ void rfm_send(const byte *data, const byte size, const byte group, const byte no

volatile uint8_t txstate = 0;
byte i = 0;
uint16_t crc = _crc16_update(~0, group);
uint16_t crc = _crc16_update(~0, group);

while(txstate < 7)
{
Expand Down Expand Up @@ -108,13 +108,13 @@ void rfm_send(const byte *data, const byte size, const byte group, const byte no
}

void rfm_sleep(void)
{
{
// Put into sleep mode when buffer is empty
while (!(readReg(REG_IRQFLAGS2) & IRQ2_PACKETSENT)) // wait for transmission to complete (not present in JeeLib)
delay(1); //
while (!(readReg(REG_IRQFLAGS2) & IRQ2_PACKETSENT)) // wait for transmission to complete (not present in JeeLib)
delay(1); //

writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | 0x01); // Standby Mode
}
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | 0x01); // Standby Mode
}



Expand Down
Loading

0 comments on commit 3f8227e

Please sign in to comment.