RF24Ethernet - TCP/IP over RF24Network v1.6.17
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
RF24Ethernet.h
Go to the documentation of this file.
1/*
2 RF24Ethernet by TMRh20 2014-2015
3
4 https://github.com/TMRh20
5
6 RF24Ethernet.h - Arduino implementation of a uIP wrapper class.
7 Copyright (c) 2014 tmrh20@gmail.com, github.com/TMRh20
8 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
9 All rights reserved.
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef RF24Ethernet_h
23#define RF24Ethernet_h
24
25/**
26 * @file RF24Ethernet.h
27 *
28 * Class declaration for RF24Ethernet
29 */
30
31#include <Arduino.h>
32
33extern "C" {
34#include "uip-conf.h"
35#include "utility/uip.h"
36#include "utility/uiptimer.h"
37#include "utility/uip_arp.h"
38}
39
40#include "RF24Ethernet_config.h"
41#if defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52833)
42 #include <nrf_to_nrf.h>
43#endif
44#include <RF24.h>
45#include <RF24Network.h>
46#if !defined(RF24_TAP) // Using RF24Mesh
47 #include <RF24Mesh.h>
48#endif
49
50#include "ethernet_comp.h"
51#include "IPAddress.h"
52#include "RF24Client.h"
53#include "RF24Server.h"
54
55#if UIP_CONF_UDP > 0
56 #include "RF24Udp.h"
57 #include "Dns.h"
58#endif
59
60#define UIPETHERNET_FREEPACKET 1
61#define UIPETHERNET_SENDPACKET 2
62
63//#define TUN // Use only the tcp protocol, no ethernet headers or arps
64#define TAP // Include ethernet headers
65
66#if defined(TAP)
67 #define BUF ((struct uip_eth_hdr*)&uip_buf[0])
68#endif
69//#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
70
71#define uip_seteth_addr(eaddr) \
72 do { \
73 uip_ethaddr.addr[0] = eaddr[0]; \
74 uip_ethaddr.addr[1] = eaddr[1]; \
75 uip_ethaddr.addr[2] = eaddr[2]; \
76 uip_ethaddr.addr[3] = eaddr[3]; \
77 uip_ethaddr.addr[4] = eaddr[4]; \
78 uip_ethaddr.addr[5] = eaddr[5]; \
79 } while (0)
80#define uip_ip_addr(addr, ip) memcpy(addr, &ip[0], 4)
81
82#define ip_addr_uip(a) IPAddress(a[0] & 0xFF, a[0] >> 8, a[1] & 0xFF, a[1] >> 8) // TODO this is not IPV6 capable
83
84#define uip_seteth_addr(eaddr) \
85 do { \
86 uip_ethaddr.addr[0] = eaddr[0]; \
87 uip_ethaddr.addr[1] = eaddr[1]; \
88 uip_ethaddr.addr[2] = eaddr[2]; \
89 uip_ethaddr.addr[3] = eaddr[3]; \
90 uip_ethaddr.addr[4] = eaddr[4]; \
91 uip_ethaddr.addr[5] = eaddr[5]; \
92 } while (0)
93
94/**
95 * @warning <b>This is used internally. Use IPAddress instead. </b>
96 */
97typedef struct
98{
99 int a, b, c, d;
100} IP_ADDR;
101
102class RF24;
103template<class radio_t>
105
107{ //: public Print {
108public:
109/**
110 * Constructor to set up the Ethernet layer. Requires the radio and network to be configured by the user
111 * this allows users to set custom settings at the radio or network level
112 */
113#if !defined(RF24_TAP) // Using RF24Mesh
114 RF24EthernetClass(RF24& _radio, RF24Network& _network, RF24Mesh& _mesh);
115#else
116 RF24EthernetClass(RF24& _radio, RF24Network& _network);
117#endif
118#if defined NRF52_RADIO_LIBRARY
119 #if !defined(RF24_TAP)
120 RF24EthernetClass(nrf_to_nrf& _radio, RF52Network& _network, RF52Mesh& _mesh);
121 #else
122 RF24EthernetClass(nrf_to_nrf& _radio, RF52Network& _network);
123 #endif
124#endif
125
126 /** Basic constructor */
128
129 /**
130 * @note Deprecated, maintained for backwards compatibility with old examples
131 *
132 * This function is no longer needed, and does nothing
133 */
134 void use_device();
135
136 /**
137 * Configure the IP address and subnet mask of the node. This is independent of the RF24Network layer, so the IP
138 * and subnet only have to conform to standard IP routing rules within your network
139 */
140 void begin(IP_ADDR myIP, IP_ADDR subnet);
141
142 /**
143 * Configure the IP address and subnet mask of the node. This is independent of the RF24Network layer, so the IP
144 * and subnet only have to conform to standard IP routing rules within your network
145 */
146 void begin(IPAddress ip);
147 void begin(IPAddress ip, IPAddress dns);
148 void begin(IPAddress ip, IPAddress dns, IPAddress gateway);
149 void begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
150
151 /**
152 * Configure the gateway IP address. This is generally going to be your master node with RF24Network address 00.
153 */
154 void set_gateway(IPAddress gwIP);
155
156 /**
157 * Listen to a specified port - This will likely be changed to closer match the Arduino Ethernet API with server.begin();
158 */
159 void listen(uint16_t port);
160
161 /**
162 * Sets the MAC address of the RF24 module, which is an RF24Network address
163 * Specify an Octal address to assign to this node, which will be used as the Ethernet mac address
164 * If setting up only a few nodes, use 01 to 05
165 * Please reference the RF24Network documentation for information on setting up a static network
166 * RF24Mesh will be integrated to provide this automatically
167 */
168 void setMac(uint16_t address);
169
170 /** Sets the Radio channel/frequency to use (0-127) */
171 void setChannel(uint8_t channel);
172
173 /** Indicates whether data is available. */
175
176 /** Returns the local IP address */
177 IPAddress localIP();
178
179 /** Returns the subnet mask */
180 IPAddress subnetMask();
181
182 /** Returns the gateway IP address */
183 IPAddress gatewayIP();
184
185 /** Returns the DNS server IP address */
186 IPAddress dnsServerIP();
187
188 /** Keeps the TCP/IP stack running & processing incoming data */
189 void update();
190 // uint8_t *key;
191
192private:
193#if defined NRF52_RADIO_LIBRARY
194 nrf_to_nrf& radio;
195#else
196 RF24& radio;
197#endif
198#if !defined NRF52_RADIO_LIBRARY
199 RF24Network& network;
200 #if !defined(RF24_TAP) // Using RF24Mesh
201 RF24Mesh& mesh;
202 #endif
203#else
204 RF52Network& network;
205 #if !defined(RF24_TAP) // Using RF24Mesh
206 RF52Mesh& mesh;
207 #endif
208#endif
209
210 static IPAddress _dnsServerAddress;
211 void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
212 // tick() must be called at regular intervals to process the incoming serial
213 // data and issue IP events to the sketch. It does not return until all IP
214 // events have been processed.
215 static void tick();
216 static void network_send();
217
218 uint8_t RF24_Channel;
219
220 struct timer periodic_timer;
221#if defined RF24_TAP
222 struct timer arp_timer;
223#endif
224 friend class RF24Server;
225 friend class RF24Client;
226 friend class RF24UDP;
227};
228
230
232
233/**
234 * @example Getting_Started_SimpleServer_Mesh.ino
235 * <b>Updated: TMRh20 2014 </b><br>
236 *
237 * This is an example of how to use the RF24Ethernet class to create a web server which will allow you to connect via
238 * any device with a web-browser. The url is http://your_chosen_IP:1000
239 */
240
241/**
242 * @example Getting_Started_SimpleClient_Mesh.ino
243 *
244 * This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP.
245 */
246
247/**
248 * @example Getting_Started_SimpleClient_Mesh_DNS.ino
249 *
250 * This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP,
251 * using DNS lookups instead of IP address.
252 */
253
254/**
255 * @example SimpleClient_Mesh.ino
256 *
257 * This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP.
258 * It also demonstrates how to read from the HTTP header to read the Content-Length before reading the data.
259 */
260
261/**
262 * @example InteractiveServer_Mesh.ino
263 *
264 * This is an example of how to create a more advanced interactive web server.<br>
265 * This example uses [HTML.h](InteractiveServer__Mesh_2HTML_8h.html) from the
266 * example's directory.
267 */
268
269/**
270 * @example mqtt_basic.ino
271 *
272 * This is the example taken from the PubSub library (https://github.com/knolleary/pubsubclient) & slightly modified to include RF24Ethernet/RF24Mesh.
273 */
274
275/**
276 * @example mqtt_basic_no_blk.ino
277 *
278 * This is similar to the mqtt_basic example, but uses a non-blocking connect function.
279 */
280/**
281 * @example mqtt_basic_2.ino
282 *
283 * A copy of the initial MQTT example using MQTT library https://github.com/256dpi/arduino-mqtt/ slightly modified to include RF24Ethernet/RF24Mesh.
284 */
285
286/**
287 * @example SLIP_Gateway.ino
288 *
289 * This example demonstrates how to use an Arduino as a gateway to a SLIP enabled device.
290 */
291
292/**
293 * @example SLIP_InteractiveServer.ino
294 *
295 * This example demonstrates how to use RF24Mesh with RF24Ethernet when working with a SLIP or TUN interface.
296 * <br>This example uses [HTML.h](SLIP__InteractiveServer_2HTML_8h.html) from the
297 * example's directory.
298 */
299
300#endif // RF24Ethernet_h
RF24EthernetClass RF52EthernetClass
RF24EthernetClass RF24Ethernet
friend class RF24UDP
void setMac(uint16_t address)
void setChannel(uint8_t channel)
IPAddress dnsServerIP()
void listen(uint16_t port)
IPAddress subnetMask()
void set_gateway(IPAddress gwIP)
void begin(IP_ADDR myIP, IP_ADDR subnet)
IPAddress gatewayIP()