Skip to content

Instantly share code, notes, and snippets.

@sticilface
Created July 29, 2018 17:15
Show Gist options
  • Save sticilface/f2b78669240322fe628cfc3e38d2b18d to your computer and use it in GitHub Desktop.
Save sticilface/f2b78669240322fe628cfc3e38d2b18d to your computer and use it in GitHub Desktop.
Receiver
#define USE_SERIAL
//#define USE_ACK
#define SERIAL_METHOD ThroughAsyncSerial // ThroughSerial or ThroughAsyncSerial
#define SERIAL_SPEED 115200
#define SWBB_PIN 2 // use 2 for arduino, 5 for ESP8266
#if defined (USE_SERIAL) && defined (ESP32)
#define SERIAL_PORT Serial2
#elif defined (USE_SERIAL)
#define SERIAL_PORT Serial1
#endif
#ifndef USE_SERIAL // THIS IS A SWBB DEVICE.. on segment 0
#define USE_SWBB
#define PJON_INCLUDE_SWBB
#define PJON_BUS_ID 5 //
#define PJON_SEND_TO_ID 155
#else // THIS IS A Serial DEVICE.. on segment 1
#define PJON_INCLUDE_TAS
#define PJON_INCLUDE_TS
#define PJON_BUS_ID 155
#define PJON_SEND_TO_ID 5
#endif
// Uncomment to run SoftwareBitBang in MODE 2
// #define SWBB_MODE 2
// Uncomment to run SoftwareBitBang in MODE 3
// #define SWBB_MODE 3
/* Acknowledge Latency maximum duration (1000 microseconds default).
Can be necessary to higher SWBB_RESPONSE_TIMEOUT to leave enough time to
receiver to compute the CRC and to respond with a synchronous acknowledgement
SWBB_RESPONSE_TIMEOUT can be reduced to higher communication speed if
devices are near and able to compute CRC fast enough. */
//#define SWBB_RESPONSE_TIMEOUT 1000
#ifdef USE_ACK
#define PJON_INCLUDE_ASYNC_ACK true
#endif
#include <PJON.h>
float test{0};
float mistakes{0};
int busy{0};
int fail{0};
int unknown{0};
float averagesum{0};
int runcount{0};
#ifdef USE_SWBB
// <Strategy name> bus(selected device id)
PJON<SoftwareBitBang> bus(PJON_BUS_ID);
#else
PJON<SERIAL_METHOD> bus(PJON_BUS_ID);
#endif
void setup() {
#ifdef USE_SWBB
bus.strategy.set_pin(SWBB_PIN);
#else
SERIAL_PORT.begin(SERIAL_SPEED);
bus.strategy.set_serial(&SERIAL_PORT);
#endif
bus.set_receiver(receiver_function);
#ifdef USE_ACK
bus.set_asynchronous_acknowledge(true);
#endif
bus.begin();
Serial.begin(115200);
Serial.println("Ready to recieve...");
};
void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
// Do nothing to avoid affecting speed analysis
}
uint32_t test_time = 0;
void loop() {
unsigned int response = bus.receive();
if (response == PJON_ACK)
test++;
if (response == PJON_NAK)
mistakes++;
if (response == PJON_BUSY)
busy++;
if (response == PJON_FAIL)
fail++;
if (millis() - test_time > 1000) {
test_time = millis();
Serial.println("1 second communication speed test: ");
Serial.print("Packet Overhead: ");
Serial.print(bus.packet_overhead(bus.last_packet_info.header) + 1);
Serial.print("B - Total: ");
Serial.print((unsigned int)((bus.packet_overhead(bus.last_packet_info.header) + 1) * test));
Serial.println("B");
Serial.print("Bandwidth: ");
Serial.print(test * (20 + bus.packet_overhead(bus.last_packet_info.header) + 1));
Serial.println("B/s");
Serial.print("Data throughput: ");
Serial.print(test * 20);
Serial.println("B/s");
Serial.print("Packets rec: ");
Serial.println(test);
Serial.print("Mistakes (error found with CRC): ");
Serial.println(mistakes);
Serial.print("Fail (no data found): ");
Serial.println(fail);
Serial.print("Busy (Channel is busy or affected by interference): ");
Serial.println(busy);
Serial.print("Accuracy: ");
Serial.print(100 - (100 / (test / mistakes)));
Serial.println(" %");
Serial.println(" --------------------- ");
// Avoid Serial interference during test flushing
Serial.flush();
test = 0;
mistakes = 0;
busy = 0;
fail = 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment