RF24Mesh_Ncurses_Master.cpp

A very limited ncurses interface used for initial monitoring/testing of RF24Mesh.

../../_images/RF24Mesh_Ncurses.JPG
examples_RPi/RF24Mesh_Ncurses_Master.cpp
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * RF24Mesh Master Node Monitoring Tool
 * This is a generic tool for master nodes running RF24Mesh that will display address
 * assignments, and information regarding incoming data, regardless of the specific
 * configuration details.
 *
 * Requirements: NCurses
 * Install NCurses: apt-get install libncurses5-dev
 * Setup:
 * 1: make
 * 2: sudo ./RF24Mesh_Ncurses_Master
 *
 * NOTE: DEBUG MUST BE DISABLED IN RF24Mesh_config.h
 *
 * Once configured and running, the interface will display the header information, data rate,
 * and address assignments for all connected nodes.*
 * The master node will also continuously ping each of the child nodes, one per second, while indicating
 * the results.
 *
 */

#include <ncurses.h>
#include "RF24Mesh/RF24Mesh.h"
#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>

RF24 radio(22, 0);

RF24Network network(radio);
RF24Mesh mesh(radio, network);

void printNodes(uint8_t boldID);
void pingNode(uint8_t listNo);

uint8_t nodeCounter;

uint16_t failID = 0;
WINDOW* win;
WINDOW* topoPad;
void drawTopology();
int nodeY, nodeX = 0;
int maxY, maxX = 0;

int main()
{

    printf("Establishing mesh...\n");
    mesh.setNodeID(0);
    if (!mesh.begin()) {
        // if mesh.begin() returns false for a master node, then radio.begin() returned false.
        printf("Radio hardware not responding.\n");
        return 0;
    }
    radio.printDetails();
    mesh.setStaticAddress(8, 01);
    win = initscr(); /* Start curses mode 		  */
    getmaxyx(win, maxX, maxY);
    start_color();
    curs_set(0);
    //keypad(stdscr, TRUE); //Enable user interaction
    init_pair(1, COLOR_GREEN, COLOR_BLACK);
    init_pair(2, COLOR_BLUE, COLOR_GREEN);
    init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
    init_pair(4, COLOR_YELLOW, COLOR_BLACK);
    init_pair(5, COLOR_CYAN, COLOR_BLACK);
    topoPad = newpad(31, 75);
    attron(COLOR_PAIR(1));
    printw("RF24Mesh Master Node Monitoring Interface by TMRh20 - 2014-2022\n");
    attroff(COLOR_PAIR(1));
    refresh(); /* Print it on to the real screen */

    uint32_t kbTimer = 0, kbCount = 0, pingTimer = millis();
    unsigned long totalPayloads = 0;
    uint8_t boldID = 0;

    while (1)
    {

        // Call mesh.update to keep the network updated
        mesh.update();
        // In addition, keep the 'DHCP service' running on the master node so addresses will
        // be assigned to the sensor nodes
        mesh.DHCP();

        attron(A_BOLD | COLOR_PAIR(1));
        mvprintw(2, 0, "[Last Payload Info]\n");
        attroff(A_BOLD | COLOR_PAIR(1));

        // Check for incoming data from the sensors
        while (network.available()) {
            RF24NetworkHeader header;
            network.peek(header);

            // Print the total number of received payloads
            mvprintw(9, 0, " Total: %lu\n", totalPayloads++);

            kbCount++;

            // Read the network payload
            network.read(header, 0, 0);

            // Display the header info
            mvprintw(3, 0, " HeaderID: %u  \n Type: %d  \n From: 0%o  \n ", header.id, header.type, header.from_node);

            for (uint8_t i = 0; i < mesh.addrListTop; i++) {
                if (header.from_node == mesh.addrList[i].address) {
                    boldID = mesh.addrList[i].nodeID;
                }
            }
        }
        printNodes(boldID);

        if (millis() - kbTimer > 1000) {
            kbTimer = millis();
            attron(A_BOLD | COLOR_PAIR(1));
            mvprintw(7, 0, "[Data Rate (In)]");
            attroff(A_BOLD | COLOR_PAIR(1));
            mvprintw(8, 0, " Kbps: %.2f", (kbCount * 32 * 8) / 1000.00);
            kbCount = 0;
        }

        // Ping each connected node, one per second
        if (millis() - pingTimer > 1003 && mesh.addrListTop > 0) {
            pingTimer = millis();
            if (nodeCounter == mesh.addrListTop) {
                nodeCounter = 0;
            }
            pingNode(nodeCounter);
            nodeCounter++;
            drawTopology();
        }

        /*
        uint32_t nOK, nFails;
        network.failures(&nFails, &nOK);
        attron(A_BOLD | COLOR_PAIR(1));
        mvprintw(2, 24, "[Transmit Results] ");
        attroff(A_BOLD | COLOR_PAIR(1));
        mvprintw(3, 25, " #OK: %u   ", nOK);
        mvprintw(4, 25, " #Fail: %u   ", nFails);
        */

        prefresh(topoPad, 0, 0, 18, 1, maxX - 1, maxY - 2);
        refresh();

        delay(2);
    } //while 1

    endwin(); /* End curses mode		  */
    return 0;
}

void printNodes(uint8_t boldID)
{

    uint8_t xCoord = 2;
    attron(A_BOLD | COLOR_PAIR(1));
    mvprintw(xCoord++, 27, "[Address Assignments]\n");
    attroff(A_BOLD | COLOR_PAIR(1));
    for (uint8_t i = 0; i < mesh.addrListTop; i++) {
        if (failID == mesh.addrList[i].nodeID) {
            attron(COLOR_PAIR(2));
        }
        else if (boldID == mesh.addrList[i].nodeID) {
            attron(A_BOLD | COLOR_PAIR(1));
        }
        mvprintw(xCoord++, 28, "ID: %d  Network: 0%o   ", mesh.addrList[i].nodeID, mesh.addrList[i].address);
        attroff(A_BOLD | COLOR_PAIR(1));
        attroff(COLOR_PAIR(2));
    }
    //mvprintw(xCoord++, 28, "                   ");
    //mvprintw(xCoord++, 28, "                   ");
    //mvprintw(xCoord++, 28, "                   ");
    getyx(win, nodeY, nodeX);
}

void pingNode(uint8_t listNo)
{

    attron(A_BOLD | COLOR_PAIR(1));
    mvprintw(11, 0, "[Ping Test]\n");
    attroff(A_BOLD | COLOR_PAIR(1));

    RF24NetworkHeader headers(mesh.addrList[listNo].address, NETWORK_PING);
    uint32_t pingtime = millis();
    bool ok = false;
    if (headers.to_node) {
        ok = network.write(headers, 0, 0);
        if (ok && failID == mesh.addrList[listNo].nodeID) {
            failID = 0;
        }
        if (!ok) {
            failID = mesh.addrList[listNo].nodeID;
        }
    }
    pingtime = millis() - pingtime;
    mvprintw(12, 0, " ID:%d    ", mesh.addrList[listNo].nodeID);
    mvprintw(13, 0, " Net:0%o    ", mesh.addrList[listNo].address);
    mvprintw(14, 0, " Time:%ums       ", pingtime);

    if (ok || !headers.to_node) {
        mvprintw(15, 0, " OK  ");
    }
    else {
        attron(A_BOLD);
        mvprintw(15, 0, " FAIL");
        attroff(A_BOLD);
    }
}

/****************************************************************************************/

void drawTopology()
{
    wclear(topoPad);
    wattroff(topoPad, COLOR_PAIR(1));
    mvprintw(17, 10, "Mesh Topology");
    mvwprintw(topoPad, nodeY > 15 ? nodeY - 16 : 0, 0, "");
    wattron(topoPad, COLOR_PAIR(1));
    int topoPadmaxX;
    topoPadmaxX = getmaxx(topoPad);

    for (int i = 01; i < 06; i++) {
        for (int j = 0; j < mesh.addrListTop; j++) {

            if (mesh.addrList[j].address == i) {
                wprintw(topoPad, "0%o[%d]   |    ", mesh.addrList[j].address, mesh.addrList[j].nodeID);
            }
        }
    }
    wprintw(topoPad, "\n");
    wattron(topoPad, COLOR_PAIR(4));
    uint16_t g = 051;
    for (int h = 011; h <= 015; h++) {

        for (int i = h; i <= g; i += 010) {

            for (int j = 0; j < mesh.addrListTop; j++) {
                if (mesh.addrList[j].address == i) {
                    int y = 0;
                    int x = 0;
                    getyx(topoPad, y, x);
                    if (x >= topoPadmaxX) wprintw(topoPad, "\n");
                    wprintw(topoPad, "0%o[%d] ", mesh.addrList[j].address, mesh.addrList[j].nodeID);
                }
            }
        }
        g++;
        wprintw(topoPad, "| ");
    }
    wprintw(topoPad, "\n");
    wattron(topoPad, COLOR_PAIR(5));
    g = 0411;
    for (int h = 0111; h <= 0145; h++) {

        for (int i = h; i <= g; i += 0100) {

            for (int j = 0; j < mesh.addrListTop; j++) {
                if (mesh.addrList[j].address == i) {
                    int y = 0;
                    int x = 0;
                    getyx(topoPad, y, x);
                    if (x >= topoPadmaxX) wprintw(topoPad, "\n");
                    wprintw(topoPad, "0%o[%d] ", mesh.addrList[j].address, mesh.addrList[j].nodeID);
                }
            }
        }
        g++;
    }
    wprintw(topoPad, "\n");
    wattron(topoPad, COLOR_PAIR(3));
    g = 04111;

    for (int h = 01111; h <= 01445; h++) {

        for (int i = h; i <= g; i += 01000) {

            for (int j = 0; j < mesh.addrListTop; j++) {
                if (mesh.addrList[j].address == i) {
                    int y = 0;
                    int x = 0;
                    getyx(topoPad, y, x);
                    if (x >= topoPadmaxX) wprintw(topoPad, "\n");
                    wprintw(topoPad, "0%o[%d] ", mesh.addrList[j].address, mesh.addrList[j].nodeID);
                }
            }
        }
        g++;
    }
    wattroff(topoPad, COLOR_PAIR(2));
}