Sends broadcast of time messages.

This commit is contained in:
Dirk Jahnke 2018-11-15 13:01:09 +01:00
parent a18d0321aa
commit a6db04c201
7 changed files with 356 additions and 73 deletions

View File

@ -22,7 +22,13 @@ build_flags = -D WITH_DISPLAY
platform = atmelavr
board = nanoatmega168
framework = arduino
lib_deps = ArduinoJson, RadioHead
# lib_deps = ArduinoJson, RadioHead
lib_deps = ArduinoJson, RF24
# build_flags = -D FCRF_RadioHead
build_flags = -D FCRF_RF24
#upload_port = /dev/cu.wchusbserial1410
#upload_speed = 38400
#upload_protocol =
[env:nanoatmega328]
platform = atmelavr

View File

@ -3,6 +3,7 @@
#include "clockMsg.h"
#include "client.h"
static struct clockMsg_s clockMsg;
int holdRelay_ms = DEFAULT_HOLD_RELAY_MS;
int minRelayOffTime_ms = DEFAULT_MIN_RELAY_OFF_TIME_MS;
@ -97,6 +98,7 @@ static void updateRelays(struct clockMsg_s currentTime) {
}
}
#ifdef FCRF_RadioHead
#if WITH_DISPLAY
void clientLoop(RHDatagram Datagram, Adafruit_SSD1306 display)
#else
@ -117,8 +119,15 @@ void clientLoop(RHDatagram Datagram)
Serial.print(" h:m:s="); Serial.print(clockMsg.hour); Serial.print(":"); Serial.print(clockMsg.minute); Serial.print(":"); Serial.println(clockMsg.second);
updateRelays(clockMsg);
} else {
Serial.print("got request: ");
Serial.println((char*)buf);
Serial.print("got request: "); Serial.print(len); Serial.println(" bytes");
Serial.print(buf[0], HEX); Serial.print(" ");
Serial.print(buf[1], HEX); Serial.print(" ");
Serial.print(buf[2], HEX); Serial.print(" ");
Serial.print(buf[3], HEX); Serial.print(" ");
Serial.print(buf[4], HEX); Serial.print(" ");
Serial.print(buf[5], HEX); Serial.print(" ");
Serial.print(buf[6], HEX); Serial.print(" ");
Serial.println(buf[7], HEX);
}
}
else
@ -137,3 +146,81 @@ void clientLoop(RHDatagram Datagram)
display.print(displayedTime.minute);
#endif
}
#endif
#ifdef FCRF_RF24
#define RF24_MAX_MESSAGE_LEN 32
extern const byte *addresses[];
static bool TX=1,RX=0,role=TX;
/*
static void switchToSenderRole(RF24 radio)
{
Serial.println("*** client: CHANGING TO TRANSMIT ROLE");
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
radio.stopListening();
role = TX; // Become the primary transmitter (ping out)
}
*/
static void switchToReceiverRole(RF24 radio)
{
Serial.println("*** client: CHANGING TO RECEIVER ROLE");
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
role = RX; // Become the primary receiver (pong back)
}
static unsigned long rxTimer=0, receivedCounter=0;
#if WITH_DISPLAY
void clientLoop(RF24 radio, Adafruit_SSD1306 display)
#else
void clientLoop(RF24 radio)
#endif
{
//Serial.println("*** RF Rcf loop");
uint8_t buf[RF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
unsigned int counter=0;
switchToReceiverRole(radio);
if (radio.available()) {
radio.read(buf, len);
counter++;
Serial.print(counter); Serial.print(": ");
Serial.print(buf[0], HEX); Serial.print(" ");
Serial.print(buf[1], HEX); Serial.print(" ");
Serial.print(buf[2], HEX); Serial.print(" ");
Serial.print(buf[3], HEX); Serial.print(" ");
Serial.print(buf[4], HEX); Serial.print(" ");
Serial.print(buf[5], HEX); Serial.println(" ");
if (len == sizeof(clockMsg) && buf[0]==msgType_Clock) {
Serial.print("Clock Msg: ");
memcpy(&clockMsg, buf, sizeof(clockMsg));
Serial.print(" h:m:s="); Serial.print(clockMsg.hour); Serial.print(":"); Serial.print(clockMsg.minute); Serial.print(":"); Serial.println(clockMsg.second);
updateRelays(clockMsg);
} else {
Serial.print("got request: "); Serial.print(len); Serial.println(" bytes");
Serial.print(buf[0], HEX); Serial.print(" ");
Serial.print(buf[1], HEX); Serial.print(" ");
Serial.print(buf[2], HEX); Serial.print(" ");
Serial.print(buf[3], HEX); Serial.print(" ");
Serial.print(buf[4], HEX); Serial.print(" ");
Serial.print(buf[5], HEX); Serial.print(" ");
Serial.print(buf[6], HEX); Serial.print(" ");
Serial.println(buf[7], HEX);
}
}
if (millis() - rxTimer > 1000) {
rxTimer = millis();
receivedCounter += counter;
unsigned long numBytes = counter*len;
Serial.print("Bytes: "); Serial.print(numBytes);
Serial.print(", Msg count: "); Serial.println(counter);
counter = 0;
}
}
#endif

View File

@ -1,13 +1,30 @@
#ifndef CLIENT_H_INCLUDED
#define CLIENT_H_INCLUDED
#ifdef FCRF_RadioHead
#include <RH_NRF24.h>
#include <RHDatagram.h>
#endif
#ifdef FCRF_RF24
#include <SPI.h>
#include <RF24.h>
#endif
extern void clientInit();
#ifdef FCRF_RadioHead
#if WITH_DISPLAY
extern void clientLoop(RHDatagram Datagram, Adafruit_SSD1306 display);
#else
extern void clientLoop(RHDatagram Datagram);
#endif
#endif
#ifdef FCRF_RF24
#if WITH_DISPLAY
extern void clientLoop(RF24 radio, Adafruit_SSD1306 display);
#else
extern void clientLoop(RF24 radio);
#endif
#endif
#endif

View File

@ -5,7 +5,7 @@
#define ROLE_MASTER LOW
#define ROLE_CLIENT HIGH
#define PIN_NRF24_CE 10
#define PIN_NRF24_CSN 8
#define PIN_NRF24_CSN 7
#define PIN_RELAY1 5
#define PIN_RELAY2 6

View File

@ -1,7 +1,12 @@
#include <Arduino.h>
#include <SPI.h>
#ifdef FCRF_RadioHead
#include <RH_NRF24.h>
#include <RHDatagram.h>
#endif
#ifdef FCRF_RF24
#include <RF24.h>
#endif
#include "config.h"
#include "clockMsg.h"
#include "master.h"
@ -15,17 +20,28 @@
Adafruit_SSD1306 display(OLED_RESET);
#endif
#ifdef FCRF_RadioHead
// Singleton instance of the radio driver
RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_CE);
// Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination
RHDatagram Datagram(nrf24, THIS_ADRESS);
#endif
#ifdef FCRF_RF24
RF24 radio(PIN_NRF24_CE, PIN_NRF24_CSN);
// const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
const byte *addresses[] = {"1Mstr","2Mstr"};
byte data[32];
unsigned long sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info
unsigned long receivedCounter=0;
unsigned long startTime, stopTime, pauseTime;
#endif
int masterConfigPin = PIN_MASTER_CLIENT_SELECT;
static boolean isMaster = true;
void setup() {
Serial.begin(9600);
Serial.begin(115200);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
@ -42,8 +58,33 @@ void setup() {
Serial.println("In Client-Mode");
}
#ifdef FCRF_RadioHead
if (!Datagram.init())
Serial.println("Init datagram with nrf24 failed");
#endif
#ifdef FCRF_RF24
radio.begin();
if (radio.isChipConnected()) { Serial.println("*** RF chip found"); }
else { Serial.println("*** ERROR: RF chip not found!"); }
radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_2MBPS);
radio.setAutoAck(0);
//radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
radio.printDetails();
// @TODO: real random seed!
randomSeed(analogRead(0));
//randomSeed(22);
radio.powerUp();
Serial.print("*** RF payload size="); Serial.print(radio.getPayloadSize()); Serial.println(" bytes");
if (radio.testCarrier() || radio.testRPD()) { Serial.println("*** Carrier/RPD seen on radio"); }
if (radio.failureDetected) { Serial.println("*** Radio error detected!"); }
#endif
#if WITH_DISPLAY
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
@ -69,6 +110,7 @@ void setup() {
}
void loop() {
#ifdef FCRF_RadioHead
if (isMaster) {
masterLoop(Datagram);
} else {
@ -78,4 +120,17 @@ void loop() {
clientLoop(Datagram);
#endif
}
#endif
#ifdef FCRF_RF24
if (isMaster) {
masterLoop(radio);
} else {
#if WITH_DISPLAY
clientLoop(radio, display);
#else
clientLoop(radio);
#endif
}
#endif
}

View File

@ -3,20 +3,86 @@
#include "clockMsg.h"
#include "master.h"
static struct clockMsg_s clockMsg;
static unsigned long lastSentTimeTick = 0;
static unsigned long sendFastclockTimer_ms = 3000;
static unsigned long msPerModelSecond = 50; // 500 = real time
static unsigned long sendFailedCounter = 0, pauseTime = 0, stopTime = 0;
static struct clock_s {
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint16_t millisecond;
} fastclock;
void masterInit() {
clockMsg.hour = 0;
clockMsg.minute = 0;
clockMsg.second = 0;
lastSentTimeTick = millis();
Serial.print("*** Setting up master, init lastSentTimeTick="); Serial.println(lastSentTimeTick);
fastclock.day = 0;
fastclock.hour = 0;
fastclock.minute = 0;
fastclock.second = 0;
fastclock.millisecond = 0;
}
static void incrementClockByMilliseconds(int amount) {
fastclock.millisecond += amount;
if (fastclock.millisecond >= 1000) {
int carryover = fastclock.millisecond / 1000;
fastclock.millisecond = fastclock.millisecond % 1000;
fastclock.second += carryover;
if (fastclock.second >= 60) {
carryover = fastclock.second / 60;
fastclock.second = fastclock.second % 60;
fastclock.minute += carryover;
if (fastclock.minute >= 60) {
carryover = fastclock.minute / 60;
fastclock.minute = fastclock.minute % 60;
fastclock.hour += carryover;
if (fastclock.hour >= 24) {
carryover = fastclock.hour / 24;
fastclock.hour = fastclock.hour % 24;
fastclock.day += carryover;
if (fastclock.day >= 7) {
fastclock.day = fastclock.day % 7;
}
}
}
}
}
Serial.print("*** new clock: ");
Serial.print(fastclock.hour); Serial.print(":");
Serial.print(fastclock.minute); Serial.print(":");
Serial.print(fastclock.second); Serial.print(".");
Serial.print(fastclock.millisecond);
Serial.print(" day "); Serial.print(fastclock.day);
Serial.print(", incBy_ms="); Serial.print(amount);
}
static void timeTick(void) {
long newTimeTick = millis();
int fastclockTimeAdvance = (newTimeTick - lastSentTimeTick) * 1000 / msPerModelSecond;
incrementClockByMilliseconds(fastclockTimeAdvance);
lastSentTimeTick = newTimeTick;
Serial.print("*** tick (adv="); Serial.print(fastclockTimeAdvance); Serial.println("%d)");
}
#ifdef FCRF_RadioHead
void masterLoop(RHDatagram Datagram)
{
static unsigned long nextTimeTick_ms = millis();
static unsigned long updateEvery_ms = 1000;
static uint8_t hour=0, minute=0, second=0;
timeTick();
if (Datagram.available())
{
// Should be a message for us now
@ -33,25 +99,10 @@ void masterLoop(RHDatagram Datagram)
Serial.println("*** Datagram.recvfrom failed");
}
} else {
// prepare clock info
if (nextTimeTick_ms < millis()) {
nextTimeTick_ms += updateEvery_ms;
second++;
if (second >= 60) {
second -= 60;
minute++;
if (minute >= 60) {
minute -= 60;
hour++;
if (hour >= 24) {
hour -= 24;
}
}
}
clockMsg.msgType = msgType_Clock;
clockMsg.hour = hour;
clockMsg.minute = minute;
clockMsg.second = second;
clockMsg.hour = fastclock.hour;
clockMsg.minute = fastclock.minute;
clockMsg.second = fastclock.second;
// send clock info as a broadcast message
@ -64,3 +115,57 @@ void masterLoop(RHDatagram Datagram)
}
}
}
#endif
#ifdef FCRF_RF24
extern const byte *addresses[];
static bool TX=1,RX=0,role=RX;
static void switchToSenderRole(RF24 radio)
{
Serial.println("*** master: CHANGING TO TRANSMIT ROLE");
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
radio.stopListening();
role = TX; // Become the primary transmitter (ping out)
}
/*
static void switchToReceiverRole(RF24 radio)
{
Serial.println("*** master: CHANGING TO RECEIVER ROLE");
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
role = RX; // Become the primary receiver (pong back)
}
*/
void masterLoop(RF24 radio) {
timeTick();
if (millis() - lastSentTimeTick > sendFastclockTimer_ms) {
lastSentTimeTick = millis();
clockMsg.msgType = msgType_Clock;
clockMsg.hour = fastclock.hour;
clockMsg.minute = fastclock.minute;
clockMsg.second = fastclock.second;
switchToSenderRole(radio);
if (!radio.write(&clockMsg, sizeof(clockMsg), true /*multicast*/)) {
sendFailedCounter++;
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
if (millis() - pauseTime > 3) {
pauseTime = millis();
radio.txStandBy(); // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
//delayMicroseconds(130); // This gives the PLL time to sync back up
}
stopTime = millis();
//This should be called to wait for completion and put the radio in standby mode after transmission, returns 0 if data still in FIFO (timed out), 1 if success
if (!radio.txStandBy()) { sendFailedCounter += 3; } //Standby, block only until FIFO empty or auto-retry timeout. Flush TX FIFO if failed
//radio.txStandBy(1000); //Standby, using extended timeout period of 1 second
Serial.println("*** send finished");
}
}
#endif

View File

@ -1,8 +1,21 @@
#ifndef MASTER_H_INCLUDED
#define MASTER_H_INCLUDED
#ifdef FCRF_RadioHead
#include <RH_NRF24.h>
#include <RHDatagram.h>
#endif
#ifdef FCRF_RF24
#include <SPI.h>
#include <RF24.h>
#endif
extern void masterInit();
#ifdef FCRF_RadioHead
extern void masterLoop(RHDatagram Datagram);
#endif
#ifdef FCRF_RF24
extern void masterLoop(RF24 radio);
#endif
#endif