Skip to content

Commit c22233f

Browse files
author
eelcohn
committed
v0.2.1 - First public release
1 parent a4f5026 commit c22233f

16 files changed

+2122
-0
lines changed

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# nRF905 Arduino API
2+
nRF905 Arduino API. Compatible with ESP8266 and ESP32 boards, tested on *NodeMCU 1.0 (ESP-12E module)* and *DOIT ESP32 Dev Module*.
3+
4+
## Compiling / Installing
5+
1. Open the .ino file in Arduino IDE
6+
2. Open `config.h` and edit the following entries:
7+
* `WIFI_SSID`: Enter the name of your WiFi network
8+
* `WIFI_PASSWORD`: Enter the password of your WiFi network
9+
3. Go to Sketch and select Upload
10+
11+
## Connecting the nRF905 module to your ESP8266 or ESP32 module
12+
13+
#### ESP8266 NodeMCU
14+
| nRF905 pin | ESP8266 pin | ESP8266 GPIO |
15+
|:----------:|:-----------:|:------------:|
16+
| AM | S3 | GPIO 10 |
17+
| CD | S2 | GPIO 9 |
18+
| CE | D2 | GPIO 4 |
19+
| DR | D1 | GPIO 5 |
20+
| PWR | D3 | GPIO 0 |
21+
| TX_EN | D0 | GPIO 16 |
22+
| MOSI | D7 / HMOSI | GPIO 13 |
23+
| MISO | D6 / HMISO | GPIO 12 |
24+
| CLK | D5 / HCLK | GPIO 14 |
25+
| CS | D8 / HCS | GPIO 15 |
26+
27+
#### ESP32
28+
| nRF905 pin | ESP32 pin | ESP32 GPIO |
29+
|:----------:|:---------:|:----------:|
30+
| AM | D32 | GPIO 32 |
31+
| CD | D33 | GPIO 33 |
32+
| CE | D27 | GPIO 27 |
33+
| DR | D35 | GPIO 35 |
34+
| PWR | D26 | GPIO 26 |
35+
| TX_EN | D25 | GPIO 25 |
36+
| MOSI | D13 | GPIO 13 |
37+
| MISO | D12 | GPIO 12 |
38+
| CLK | D14 | GPIO 14 |
39+
| CS | D15 | GPIO 15 |
40+
41+
## Usage
42+
43+
### /api/v1/config/
44+
Configure the nRF905.
45+
46+
Example:<br>
47+
`http://192.168.x.y/api/v1/config/?frequency=868400000&crc=16&txpower=10&rxpower=normal&rxaddr=a55a5aa5&txaddr=a55a5aa5&rxaddrwidth=4&txaddrwidth=4&rxpayloadwidth=16&txpayloadwidth=16`
48+
49+
| Parameter | Required | Description |
50+
|:--------------:|:--------:|:------------|
51+
| `frequency` | No | Set the RF frequency |
52+
| `crc` | No | `off`: disable CRC<br>`8`: enable 8-bit CRC<br>`16`: enable 16-bt CRC |
53+
| `txpower` | No | Set the RF transmit power<br>`-10`: -10dBm<br>`-2`: -2dBm<br>`6`: 6dBm<br>`10`: 10dBm |
54+
| `rxpower` | No | Set the RF receive power<br>`normal`: normal receive mode<br>`low`: low power receive mode |
55+
| `txaddrwidth` | No | Set the number of Tx address bytes<br>`1`: 1 byte<br>`4`: 4 bytes |
56+
| `rxaddrwidth` | No | Set the number of Rx address bytes<br>`1`: 1 byte<br>`4`: 4 bytes |
57+
|`txpayloadwidth`| No | Set the number of Tx payload bytes<br>`1...32`: Payload is 1...32 bytes |
58+
|`rxpayloadwidth`| No | Set the number of Rx payload bytes<br>`1...32`: Payload is 1...32 bytes |
59+
| `txaddr` | No | Set the Tx address<br>`0x12345678`: Set Tx address to 0x12345678 |
60+
| `rxaddr` | No | Set the Rx address<br>`0x12345678`: Set Rx address to 0x12345678 |
61+
62+
### /api/v1/receive/
63+
Not implemented, will probably be removed.
64+
65+
### /api/v1/status/
66+
This will show a status page with details about the hardware. This API call takes no parameters.
67+
68+
Example:<br>
69+
`https://192.168.x.y/api/v1/status/`
70+
71+
### /api/v1/send/
72+
This will transmit data. After the data has been transmitted, the API will wait for data to be received (a reply to the transmitted data). By default it will wait for 2 seconds, but you can change this by adding the `timeout` parameter.
73+
74+
Example:<br>
75+
`https://192.168.x.y/api/v1/send/?addr=aabbccdd&payload=0400035efa0c04a55a5aa50000000000&timeout=10`
76+
77+
| Parameter | Required | Description |
78+
|:---------:|:--------:|:------------|
79+
| `txaddr` | Yes | Address to send the data to |
80+
| `payload` | Yes | The data to be sent |
81+
| `timeout` | No | Timeout for reading data after the transmission is completed (in seconds)<br>Default: 2 seconds |
Loading
Loading

images/nodemcu_pins.png

285 KB
Loading

src/nRF905API/board.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef __BOARD_H__
2+
#define __BOARD_H__
3+
4+
#include "config.h"
5+
6+
#ifdef ARDUINO
7+
#ifdef ARDUINO_ARCH_ESP32
8+
#include "esp32.h"
9+
#else
10+
#include "esp8266.h"
11+
#endif
12+
#endif
13+
14+
extern Board * board;
15+
16+
#endif
17+

src/nRF905API/config.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __NRF905API_CONFIG_H__
2+
#define __NRF905API_CONFIG_H__
3+
4+
#define WIFI_HOSTNAME "nrf905-api"
5+
#define WIFI_SSID "WiFiNetwork"
6+
#define WIFI_PASSWORD "SuperSecretPassword"
7+
8+
#define SERIAL_SPEED 115200
9+
#define NRF905API_HTTPS 0 // Enable HTTPS server
10+
#define NRF905API_MDNS 0 // Enable mDNS
11+
#define NRF905API_NTP 0 // Enable NTP client
12+
#define NRF905API_IPV6 0 // Enable IPv6
13+
#define NRF905API_OTA 0 // Enable OTA (Over-The-Air) firmware upgrades for the ESP module
14+
#define NRF905API_OTA_PORT 8233 // OTA TCP port
15+
#define NRF905API_OTA_PASSWORD "c19555f1193996117d477eebde5c913e" // MD5 password hash for password `nrf905`
16+
#define NRF905API_WWW_USERNAME "admin" // HTTP authentication: username
17+
#define NRF905API_WWW_PASSWORD "nrf905" // HTTP authentication: password TODO use hash value (if possible)
18+
#define NRF905API_WWW_PORT 80
19+
#define NRF905API_NTP_SERVER "pool.ntp.org"
20+
#define NRF905API_SPI_FREQUENCY 10000000 // SPI clock frequency: 10MHz
21+
22+
/* Uncomment to use custom pin definitions */
23+
//#define PIN_AM 10 // nRF905 AM pin (Address Match)
24+
//#define PIN_CD 9 // nRF905 CD pin (Carrier detect)
25+
//#define PIN_CE D2 // nRF905 CS pin (Enable pin)
26+
//#define PIN_DR D1 // nRF905 DR pin (Data Ready)
27+
//#define PIN_PWR D3 // nRF905 PWR pin (Power mode pin)
28+
//#define PIN_TXEN D0 // nRF905 TX_EN pin (Transmit enable pin)
29+
//#define SPI_MOSI 13 // nRF905 CSN pin (SPI slave select pin)
30+
//#define SPI_MISO 12 // nRF905 CSN pin (SPI slave select pin)
31+
//#define SPI_CLK 14 // nRF905 CSN pin (SPI slave select pin)
32+
//#define SPI_CS 15 // nRF905 CSN pin (SPI slave select pin)
33+
34+
#endif
35+

src/nRF905API/esp32.cpp

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#ifdef ARDUINO_ARCH_ESP32
2+
3+
#include <SPI.h>
4+
#include "board.h" // board-> functions
5+
#include "esp32.h"
6+
#include "utils.h" // serialPrintHex()
7+
8+
// Create an instance of the server
9+
#if NRF905API_HTTPS == 1
10+
WebServerSecure server(NRF905API_WWW_PORT);
11+
#else
12+
WebServer server(NRF905API_WWW_PORT);
13+
#endif
14+
15+
16+
17+
/* ESP32 Class functions */
18+
String Board::get_arch(void) {
19+
return "esp32";
20+
}
21+
22+
uint16_t Board::get_vcc(void) {
23+
return 0;
24+
}
25+
26+
uint32_t Board::get_cpu_id(void) {
27+
return (ESP.getEfuseMac());
28+
}
29+
30+
uint32_t Board::get_flash_chip_id(void) {
31+
return 0;
32+
}
33+
34+
uint32_t Board::get_flash_chip_real_size(void) {
35+
return 0;
36+
}
37+
38+
String Board::get_core_version(void) {
39+
return "unknown";
40+
}
41+
42+
uint32_t Board::get_core_revision(void) {
43+
return 0;
44+
}
45+
46+
String Board::get_hostname(void) {
47+
return WiFi.getHostname();
48+
}
49+
50+
void Board::set_hostname(const char *hostname) {
51+
WiFi.setHostname(hostname);
52+
}
53+
54+
String Board::get_localIPv6(void) {
55+
return WiFi.localIPv6().toString();
56+
}
57+
58+
bool Board::enable_IPv6(void) {
59+
return WiFi.enableIpV6();
60+
}
61+
62+
void Board::setPinMode(const uint8_t pin, const uint8_t mode) {
63+
pinMode(pin, mode);
64+
}
65+
66+
void Board::writePin(const uint8_t pin, const uint8_t value) {
67+
digitalWrite(pin, value);
68+
}
69+
70+
uint8_t Board::readPin(const uint8_t pin) {
71+
return digitalRead(pin);
72+
}
73+
74+
void Board::onBoardLED(const bool value) {
75+
if (value == true)
76+
digitalWrite(2, LOW);
77+
else
78+
digitalWrite(2, HIGH);
79+
}
80+
81+
bool Board::SPIBegin(const uint8_t mosi, const uint8_t miso, const uint8_t clk, const uint8_t cs) {
82+
this->_spi_cs = cs;
83+
this->_spi_cs_active = LOW;
84+
this->spi = new SPIClass(HSPI); // TODO: use pin definitions
85+
this->spi->begin();
86+
87+
return true;
88+
}
89+
90+
void Board::SPIEnd(void) {
91+
spi->end();
92+
free(spi);
93+
}
94+
95+
void Board::SPISetChipSelectPolarity (const uint8_t cs, const uint8_t active) {
96+
this->_spi_cs_active = active;
97+
if (this->_spi_cs_active == LOW)
98+
digitalWrite(this->_spi_cs, HIGH);
99+
else
100+
digitalWrite(this->_spi_cs, LOW);
101+
}
102+
103+
void Board::SPISetBitOrder(const uint8_t order) {
104+
this->_spi_bitorder = SPI_MSBFIRST; // TODO: use order and not fixed value
105+
spi->setBitOrder(this->_spi_bitorder);
106+
107+
}
108+
109+
void Board::SPISetDataMode(const uint8_t mode) {
110+
this->_spi_datamode = mode;
111+
spi->setDataMode(this->_spi_datamode);
112+
}
113+
114+
void Board::SPISetFrequency(const uint32_t frequency) {
115+
this->_spi_frequency = frequency;
116+
spi->setFrequency(this->_spi_frequency);
117+
}
118+
119+
uint8_t Board::SPITransfer(const uint8_t out) {
120+
uint8_t in;
121+
122+
if (this->_spi_cs_active == LOW)
123+
digitalWrite(this->_spi_cs, LOW);
124+
else
125+
digitalWrite(this->_spi_cs, HIGH);
126+
127+
Serial.print("SPI Tx: ");
128+
serialPrintHex(&out, 1);
129+
130+
spi->beginTransaction(SPISettings(this->_spi_frequency, this->_spi_bitorder, this->_spi_datamode));
131+
in = spi->transfer(out);
132+
spi->endTransaction();
133+
134+
Serial.print("SPI Rx: ");
135+
serialPrintHex(&in, 1);
136+
137+
if (this->_spi_cs_active == LOW)
138+
digitalWrite(this->_spi_cs, HIGH);
139+
else
140+
digitalWrite(this->_spi_cs, LOW);
141+
142+
return in;
143+
}
144+
145+
void Board::SPITransfern(uint8_t * buffer, const size_t size) {
146+
size_t i;
147+
148+
if (this->_spi_cs_active == LOW)
149+
digitalWrite(this->_spi_cs, LOW);
150+
else
151+
digitalWrite(this->_spi_cs, HIGH);
152+
153+
Serial.print("SPI Tx len=");
154+
Serial.print(size);
155+
Serial.print(": ");
156+
serialPrintHex(buffer, size);
157+
158+
spi->beginTransaction(SPISettings(this->_spi_frequency, this->_spi_bitorder, this->_spi_datamode));
159+
for (i = 0; i < size; i++)
160+
buffer[i] = spi->transfer(buffer[i]);
161+
// spi->transfer(buffer, size);
162+
spi->endTransaction();
163+
164+
Serial.print("SPI Rx len=");
165+
Serial.print(size);
166+
Serial.print(": ");
167+
serialPrintHex(buffer, size);
168+
169+
if (this->_spi_cs_active == LOW)
170+
digitalWrite(this->_spi_cs, HIGH);
171+
else
172+
digitalWrite(this->_spi_cs, LOW);
173+
}
174+
175+
#endif
176+

0 commit comments

Comments
 (0)