Skip to content

Commit

Permalink
update docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Apr 7, 2024
1 parent 76b6139 commit 30ecceb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 73 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
# EspNow RcLink

RcLink that works on ESP-NOW.
RC Link library that works on ESP-NOW for tiny models in short range.

# Features

* ESP32 and ESP8266 support
* Auto pairing
* Up to 100Hz packet rate (or maybe more)
* 8 RC channels
* Channels 1-4 are sent with full resolution and extended range 880-2120
* Channels 5-8 are sent with reduced resolution and scaled for extended range 880-2120 (packed to 8-bit size, resolution is 5 units)
* packet checksum checking

# How it works

TODO.
* when reciever is powered it sends pair request
* when transmitter is powered, it scans channels for pair request
* when transmitter receive pair request, adds peer to the list and sends pair response
* when receiver recieive pair response, adds peer to the list and start communicating only with this transmitter
* transmitter will not accept any other pair request, until power cycle
* receiver will not accept any data packet form other transmitter, until power cycle

# Examples

* [Receiver](/examples/rx/rx.cpp)
* [Transmitter](/examples/tx/tx.cpp)

# Todo

* Tests, a lot of testing required
* Telemetry
* RSSI
* More channels

# Licence

This project is distributed under MIT Licence.
29 changes: 11 additions & 18 deletions examples/rx/rx.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@

#include <Arduino.h>
#include "EspNowRcLink/Receiver.h"

using namespace EspNowRcLink;

Receiver rx;
EspNowRcLink::Receiver rx;

void setup()
{
Serial.begin(115200);
rx.begin();
// initialize receiver
// - true to init hidden WiFi AP
// - false if you manage WiFi outside (default)
rx.begin(true);
}

void loop()
{
uint32_t now = millis();
static uint32_t rcDataNext = now + 500;

// receive data
rx.update();

// check if new data available
if(rx.available())
{
if(rcDataNext < now)
for(size_t c = EspNowRcLink::RC_CHANNEL_MIN; c <= EspNowRcLink::RC_CHANNEL_MAX; c++)
{
Serial.print("RC: ");
for(size_t i = RC_CHANNEL_MIN; i <= RC_CHANNEL_MAX; i++)
{
Serial.print(rx.getChannel(i));
Serial.print(' ');
}
Serial.println();
rcDataNext = now + 500;
uint16_t v = rx.getChannel(c);
//proceedRcChannel(c, v);
}
}
}
68 changes: 20 additions & 48 deletions examples/tx/tx.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,37 @@
#include <Arduino.h>
#include "EspNowRcLink/Transmitter.h"

// uncomment to activate simultor on channel 3
//#define ESPNOW_RCLINK_DEMO_TX_SIM

using namespace EspNowRcLink;

Transmitter tx;
EspNowRcLink::Transmitter tx;

void setup()
{
Serial.begin(115200);

tx.begin();

tx.setChannel(0, 1500);
tx.setChannel(1, 1500);
tx.setChannel(2, 1000);
tx.setChannel(3, 1500);
tx.setChannel(4, 1500);
tx.setChannel(5, 1500);
tx.setChannel(6, 1500);
tx.setChannel(7, 1500);
// initialize transmitter
// - true to init hidden WiFi AP
// - false if you manage WiFi outside (default)
tx.begin(true);
}

void loop()
{
static int sim_cnt = 0;
static bool sim_up = true;
int sim_speed = 4;

uint32_t now = millis();
static uint32_t rcDataNext = now + 50;
static uint32_t sendNext = now + 20;

// send rc channels
if(rcDataNext < now)
// send rc channels every 20ms
if(now > sendNext)
{
tx.setChannel(2, 1000 + (sim_cnt * sim_speed));
tx.commit();
rcDataNext = now + 20;

#ifdef ESPNOW_RCLINK_DEMO_TX_SIM
if(sim_up)
// use rc range 1000-2000
for(size_t c = EspNowRcLink::RC_CHANNEL_MIN; c <= EspNowRcLink::RC_CHANNEL_MAX; c++)
{
sim_cnt++;
if(sim_cnt >= (1000 / sim_speed))
{
sim_up = false;
}
tx.setChannel(c, 1000);
}
else
{
sim_cnt--;
if(sim_cnt == 0)
{
sim_up = true;
}
}
#else
(void)sim_up;
#endif

// mark as ready to send
tx.commit();

// schedule next transmission
sendNext = now + 20;
}

// proceed sending and handle incoming data
tx.update();
}
}
2 changes: 1 addition & 1 deletion include/EspNowRcLink/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct MessageRc
uint8_t csum;
static int8_t encodeAux(int x)
{
x = constrain(x, PWM_INPUT_MIN, PWM_INPUT_MAX) - PWM_INPUT_CENTER;
x = constrain(x, (int)PWM_INPUT_MIN, (int)PWM_INPUT_MAX) - PWM_INPUT_CENTER;
int round = x > 0 ? 2 : -2;
return (int8_t)((x + round) / 5);
}
Expand Down
2 changes: 1 addition & 1 deletion include/EspNowRcLink/Transmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Transmitter {
};

Transmitter();
int begin();
int begin(bool enSoftAp = false);
int update();
void setChannel(size_t c, unsigned int value);
int getSensor(size_t sensorId) const;
Expand Down
2 changes: 1 addition & 1 deletion src/Receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int Receiver::begin(bool enSoftAp)
{
if(enSoftAp)
{
WiFi.softAP("ESPNOW-RX", nullptr, 0, 1, 2);
if(!WiFi.softAP("ESPNOW-RX", nullptr, 0, 1)) return 0;
}

if(!WifiEspNow.begin()) return 0;
Expand Down
7 changes: 5 additions & 2 deletions src/Transmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ void Transmitter::_handleRx(const uint8_t *mac, const uint8_t *buf, size_t count

Transmitter::Transmitter(): _channel(WIFI_CHANNEL_DEFAULT) {}

int Transmitter::begin()
int Transmitter::begin(bool enSoftAp)
{
if(!WiFi.softAP("ESPNOW-TX", nullptr, _channel, 1, 2)) return 0;
if(enSoftAp)
{
if(!WiFi.softAP("ESPNOW-TX", nullptr, _channel, 1)) return 0;
}

if (!WifiEspNow.begin()) return 0;

Expand Down

0 comments on commit 30ecceb

Please sign in to comment.