AllWize Library
forwarder.cpp
Go to the documentation of this file.
1 /*
2 
3 FORWARDER MODULE
4 
5 */
6 
7 #include "forwarder.h"
8 #include "wifi.h"
9 #include "ntp.h"
10 #include "debug.h"
11 #include "wize.h"
12 #include "configuration.h"
13 
14 #include "AllWize.h"
15 #include <base64.h>
16 #include <Ticker.h>
17 #include <WiFiUdp.h>
18 
21 uint8_t eui[8] = {0xFF};
22 
23 struct {
24  uint16_t rxnb = 0; // Number of radio packets received (unsigned integer)
25  uint16_t rxok = 0; // Number of radio packets received with a valid PHY CRC
26  uint16_t rxfw = 0; // Number of radio packets forwarded (unsigned integer)
27  uint16_t acks = 0; // Number of acknowledges received
28  uint16_t dwnb = 0; // Number of downlink datagrams received (unsigned integer)
29  uint16_t txnb = 0; // Number of packets emitted (unsigned integer)
31 
32 void forwarderSend(char * data) {
33 
34  // Binary header
35  uint8_t header[12];
36  header[0] = 1; // protocol version
37  header[1] = random(256); // random for ack
38  header[2] = random(256); // random for ack
39  header[3] = 0; // transaction type
40  memcpy(&header[4], eui, 8); // gateway eui
41 
42  DEBUG_MSG("[FORWARDER] Sending: ");
43  for (uint8_t i=0; i<12; i++) {
44  DEBUG_MSG("%02x", header[i]);
45  }
46  DEBUG_MSG(" %s\n", data);
47 
49  _forwarder_udp.write(header, 12);
50  _forwarder_udp.write((uint8_t *) data, strlen(data));
51  _forwarder_udp.endPacket();
52 
53 }
54 
55 void forwarderPing() {
56 
57  // Check connection
58  if (!wifiConnected()) return;
59  if (!ntpSynced()) return;
60 
61  // Get % of ACK received
62  float ackr = (0 == _forwarder_stats.rxfw) ? 0 : 100.0 * (float)_forwarder_stats.acks / (float)_forwarder_stats.rxfw;
63 
64  // Build JSON payload
65  String timestamp = ntpDateTime().c_str();
66 
67  // Build JSON payload
68  char buffer[512];
69  snprintf_P(
70  buffer, sizeof(buffer),
71  PSTR("{\"stat\":{\"time\":\"%s GMT\",\"lati\":%.5f,\"long\":%.5f,\"alti\":%d,\"rxnb\":%d,\"rxok\":%d,\"rxfw\":%d,\"ackr\":%.2f,\"dwnb\":%d,\"txnb\":%d,\"pfrm\":\"%s\",\"mail\":\"%s\",\"desc\":\"%s\"}}"),
75  );
76 
77  // Send frame
78  forwarderSend(buffer);
79 
80 }
81 
83 
84  // Update RX counters
85  _forwarder_stats.rxnb = _forwarder_stats.rxnb + 1;
86  _forwarder_stats.rxok = _forwarder_stats.rxok + 1;
87 
88  // Check connection & time
89  if (!wifiConnected()) return;
90  if (!ntpSynced()) return;
91 
92  // Get BASE64 of payload
93  String data = base64::encode(message.data, message.len, false);
94 
95  // Get current time
96  String timestamp = ntpDateTime().c_str();
97  timestamp.replace(" ", "T");
98 
99  // Build JSON payload
100  char buffer[512];
101  snprintf_P(
102  buffer, sizeof(buffer),
103  PSTR("{\"rxpk\":[{\"tmst\":%lu,\"time\":\"%s.00000Z\",\"chan\":%d,\"rfch\":%d,\"freq\":%.5f,\"stat\":%d,\"modu\":\"FSK\",\"datr\":%d,\"codr\":\"wize\",\"rssi\":%d,\"lsnr\":0.0,\"size\":%d,\"data\":\"%s\"}]}"),
104  now(), timestamp.c_str(),
107  (int16_t) message.rssi / -2, message.len, data.c_str()
108  );
109 
110  // Send frame
111  forwarderSend(buffer);
112 
113  // Update FW counter
114  _forwarder_stats.rxfw =_forwarder_stats.rxfw + 1;
115 
116 }
117 
119 
120  // Set the ping message every 30 seconds
121  _forwarder_ticker.attach(30, forwarderPing);
122 
123  // Build the device EUI
124  uint8_t mac[WL_MAC_ADDR_LENGTH] = {0};
125  WiFi.macAddress(mac);
126  eui[0] = mac[0];
127  eui[1] = mac[1];
128  eui[2] = mac[2];
129  eui[3] = 0xFF;
130  eui[4] = 0xFF;
131  eui[5] = mac[3];
132  eui[6] = mac[4];
133  eui[7] = mac[5];
134  DEBUG_MSG(
135  "[FORWARDER] Gateway eui-%02x%02x%02x%02x%02x%02x%02x%02x\n",
136  eui[0], eui[1], eui[2], eui[3],
137  eui[4], eui[5], eui[6], eui[7]
138  );
139 
140  // Init random seed
141  randomSeed(analogRead(0));
142 
143 }
uint8_t rssi
Definition: AllWize.h:63
uint8_t data[RX_BUFFER_SIZE]
Definition: AllWize.h:62
double wizeFrequency(uint8_t channel)
Definition: wize.cpp:16
uint8_t len
Definition: AllWize.h:61
#define WIZE_CHANNEL
Definition: configuration.h:35
#define FORWARDER_SERVER
Definition: configuration.h:42
void forwarderSetup()
Definition: forwarder.cpp:118
uint16_t rxok
Definition: forwarder.cpp:25
WiFiUDP _forwarder_udp
Definition: forwarder.cpp:20
bool ntpSynced()
Definition: ntp.cpp:20
#define FORWARDER_EMAIL
Definition: configuration.h:48
#define FORWARDER_DESCRIPTION
Definition: configuration.h:49
#define DEBUG_MSG(...)
Definition: debug.h:12
uint16_t rxfw
Definition: forwarder.cpp:26
uint16_t rxnb
Definition: forwarder.cpp:24
struct @0 _forwarder_stats
uint16_t acks
Definition: forwarder.cpp:27
uint16_t wizeDataRateSpeed(uint8_t dr)
Definition: wize.cpp:20
void forwarderMessage(allwize_message_t message)
Definition: forwarder.cpp:82
Ticker _forwarder_ticker
Definition: forwarder.cpp:19
#define FORWARDER_PORT
Definition: configuration.h:43
String ntpDateTime(time_t t)
Definition: ntp.cpp:24
#define FORWARDER_ALTITUDE
Definition: configuration.h:46
uint16_t dwnb
Definition: forwarder.cpp:28
#define WIZE_DATARATE
Definition: configuration.h:36
uint16_t txnb
Definition: forwarder.cpp:29
bool wifiConnected()
Definition: wifi.cpp:19
#define FORWARDER_LONGITUDE
Definition: configuration.h:45
#define FORWARDER_GATEWAY_TYPE
Definition: configuration.h:47
#define FORWARDER_LATITUDE
Definition: configuration.h:44
uint8_t eui[8]
Definition: forwarder.cpp:21
void forwarderPing()
Definition: forwarder.cpp:55
void forwarderSend(char *data)
Definition: forwarder.cpp:32