QSL / Mbed 2 deprecated QSL_SimplePublish Featured

Dependencies:   mbed millis

Fork of QSL_SimplePublish by Jon-Håkon Bøe Røli

QSL SimplePublish

SmartMesh IP QuickStart Library

Committer:
jhbr
Date:
Thu Sep 08 14:26:15 2016 +0000
Revision:
4:0285bcbbc855
Parent:
3:fb2c485306d1
Child:
5:eb02ec8b90f8
Replaced sleep() with simple wait_ms(), as board seems to not always wake up or go to sleep as it should.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhbr 0:d3f5fdf2e6da 1 #include "mbed.h"
jhbr 0:d3f5fdf2e6da 2 #include "dn_qsl_api.h" // Only really need this include from
jhbr 0:d3f5fdf2e6da 3 #include "dn_debug.h" // Included to borrow debug macros
jhbr 0:d3f5fdf2e6da 4 #include "dn_endianness.h" // Included to borrow array copying
jhbr 0:d3f5fdf2e6da 5 #include "dn_time.h" // Included to borrow sleep function
jhbr 0:d3f5fdf2e6da 6
jhbr 0:d3f5fdf2e6da 7 #define NETID 0 // Factory default value used if zero (1229)
jhbr 0:d3f5fdf2e6da 8 #define JOINKEY NULL // Factory default value used if NULL (44 55 53 54 4E 45 54 57 4F 52 4B 53 52 4F 43 4B)
jhbr 4:0285bcbbc855 9 #define BANDWIDTH_MS 0//5000 // Not changed if zero (default base bandwidth given by manager is 9 s)
jhbr 0:d3f5fdf2e6da 10 #define SRC_PORT 60000 // Default port used if zero (0xf0b8)
jhbr 0:d3f5fdf2e6da 11 #define DEST_PORT 0 // Default port used if zero (0xf0b8)
jhbr 4:0285bcbbc855 12 #define DATA_PERIOD_MS 9000 // Should be longer than (or equal to) bandwidth
jhbr 0:d3f5fdf2e6da 13
jhbr 0:d3f5fdf2e6da 14 // We can use debug macros from dn_debug, as stdio defaults to this serial
jhbr 0:d3f5fdf2e6da 15 Serial serialDebug(SERIAL_TX, SERIAL_RX);
jhbr 2:f177b47313ba 16 // LED2 is the green LED on the NUCLEO-L053R8; might change for other boards
jhbr 2:f177b47313ba 17 DigitalOut myled(LED2);
jhbr 0:d3f5fdf2e6da 18
jhbr 0:d3f5fdf2e6da 19 static uint16_t randomWalk(void);
jhbr 0:d3f5fdf2e6da 20 static void parsePayload(const uint8_t *payload, uint8_t size);
jhbr 0:d3f5fdf2e6da 21
jhbr 0:d3f5fdf2e6da 22 int main()
jhbr 0:d3f5fdf2e6da 23 {
jhbr 0:d3f5fdf2e6da 24 uint8_t payload[3];
jhbr 0:d3f5fdf2e6da 25 uint8_t inboxBuf[DN_DEFAULT_PAYLOAD_SIZE_LIMIT];
jhbr 0:d3f5fdf2e6da 26 uint8_t bytesRead;
jhbr 2:f177b47313ba 27 uint8_t i;
jhbr 0:d3f5fdf2e6da 28
jhbr 0:d3f5fdf2e6da 29 serialDebug.baud(115200);
jhbr 0:d3f5fdf2e6da 30
jhbr 0:d3f5fdf2e6da 31 log_info("Initializing...");
jhbr 0:d3f5fdf2e6da 32 dn_qsl_init();
jhbr 2:f177b47313ba 33
jhbr 2:f177b47313ba 34 // Flash LED to indicate start-up complete
jhbr 2:f177b47313ba 35 for (i = 0; i < 10; i++)
jhbr 2:f177b47313ba 36 {
jhbr 2:f177b47313ba 37 myled = !myled;
jhbr 2:f177b47313ba 38 dn_sleep_ms(50);
jhbr 2:f177b47313ba 39 }
jhbr 0:d3f5fdf2e6da 40
jhbr 0:d3f5fdf2e6da 41 while(TRUE) {
jhbr 0:d3f5fdf2e6da 42 if (dn_qsl_isConnected())
jhbr 0:d3f5fdf2e6da 43 {
jhbr 0:d3f5fdf2e6da 44 uint16_t val = randomWalk();
jhbr 0:d3f5fdf2e6da 45 static uint8_t count = 0;
jhbr 2:f177b47313ba 46 myled = 0; // Turn off LED during send/read
jhbr 0:d3f5fdf2e6da 47
jhbr 0:d3f5fdf2e6da 48 dn_write_uint16_t(payload, val);
jhbr 0:d3f5fdf2e6da 49 payload[2] = count;
jhbr 0:d3f5fdf2e6da 50
jhbr 0:d3f5fdf2e6da 51 if (dn_qsl_send(payload, sizeof (payload), DEST_PORT))
jhbr 0:d3f5fdf2e6da 52 {
jhbr 0:d3f5fdf2e6da 53 log_info("Sent message nr %u: %u", count, val);
jhbr 0:d3f5fdf2e6da 54 count++;
jhbr 0:d3f5fdf2e6da 55 } else
jhbr 0:d3f5fdf2e6da 56 {
jhbr 0:d3f5fdf2e6da 57 log_info("Send failed");
jhbr 0:d3f5fdf2e6da 58 }
jhbr 0:d3f5fdf2e6da 59
jhbr 0:d3f5fdf2e6da 60 do
jhbr 0:d3f5fdf2e6da 61 {
jhbr 0:d3f5fdf2e6da 62 bytesRead = dn_qsl_read(inboxBuf);
jhbr 0:d3f5fdf2e6da 63 parsePayload(inboxBuf, bytesRead);
jhbr 0:d3f5fdf2e6da 64 } while (bytesRead > 0);
jhbr 0:d3f5fdf2e6da 65
jhbr 2:f177b47313ba 66 myled = 1; // Turn on LED
jhbr 0:d3f5fdf2e6da 67 dn_sleep_ms(DATA_PERIOD_MS);
jhbr 0:d3f5fdf2e6da 68 } else
jhbr 0:d3f5fdf2e6da 69 {
jhbr 0:d3f5fdf2e6da 70 log_info("Connecting...");
jhbr 2:f177b47313ba 71 myled = 0; // Not connected; turn off LED
jhbr 0:d3f5fdf2e6da 72 if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS))
jhbr 0:d3f5fdf2e6da 73 {
jhbr 2:f177b47313ba 74 myled = 1; // Connected; turn on LED
jhbr 0:d3f5fdf2e6da 75 log_info("Connected to network");
jhbr 0:d3f5fdf2e6da 76 } else
jhbr 0:d3f5fdf2e6da 77 {
jhbr 0:d3f5fdf2e6da 78 log_info("Failed to connect");
jhbr 0:d3f5fdf2e6da 79 }
jhbr 0:d3f5fdf2e6da 80 }
jhbr 0:d3f5fdf2e6da 81
jhbr 0:d3f5fdf2e6da 82 }
jhbr 0:d3f5fdf2e6da 83 }
jhbr 0:d3f5fdf2e6da 84
jhbr 0:d3f5fdf2e6da 85 static uint16_t randomWalk(void)
jhbr 0:d3f5fdf2e6da 86 {
jhbr 0:d3f5fdf2e6da 87 static bool first = TRUE;
jhbr 0:d3f5fdf2e6da 88 static uint16_t lastValue = 0x7fff; // Start in middle of uint16 range
jhbr 0:d3f5fdf2e6da 89 const int powerLevel = 9001;
jhbr 0:d3f5fdf2e6da 90
jhbr 0:d3f5fdf2e6da 91 // Seed random number generator on first call
jhbr 0:d3f5fdf2e6da 92 if (first)
jhbr 0:d3f5fdf2e6da 93 {
jhbr 0:d3f5fdf2e6da 94 first = FALSE;
jhbr 0:d3f5fdf2e6da 95 srand(dn_time_ms());
jhbr 0:d3f5fdf2e6da 96 }
jhbr 0:d3f5fdf2e6da 97
jhbr 0:d3f5fdf2e6da 98 // Random walk within +/- powerLevel
jhbr 0:d3f5fdf2e6da 99 lastValue += rand() / (RAND_MAX / (2*powerLevel) + 1) - powerLevel;
jhbr 0:d3f5fdf2e6da 100 return lastValue;
jhbr 0:d3f5fdf2e6da 101 }
jhbr 0:d3f5fdf2e6da 102
jhbr 0:d3f5fdf2e6da 103 static void parsePayload(const uint8_t *payload, uint8_t size)
jhbr 0:d3f5fdf2e6da 104 {
jhbr 0:d3f5fdf2e6da 105 uint8_t i;
jhbr 0:d3f5fdf2e6da 106 char msg[size + 1];
jhbr 0:d3f5fdf2e6da 107
jhbr 0:d3f5fdf2e6da 108 if (size == 0)
jhbr 0:d3f5fdf2e6da 109 {
jhbr 0:d3f5fdf2e6da 110 // Nothing to parse
jhbr 0:d3f5fdf2e6da 111 return;
jhbr 0:d3f5fdf2e6da 112 }
jhbr 0:d3f5fdf2e6da 113
jhbr 0:d3f5fdf2e6da 114 // Parse bytes individually as well as together as a string
jhbr 0:d3f5fdf2e6da 115 log_info("Received downstream payload of %u bytes:", size);
jhbr 0:d3f5fdf2e6da 116 for (i = 0; i < size; i++)
jhbr 0:d3f5fdf2e6da 117 {
jhbr 0:d3f5fdf2e6da 118 msg[i] = payload[i];
jhbr 0:d3f5fdf2e6da 119 log_info("\tByte# %03u: %#.2x (%u)", i, payload[i], payload[i]);
jhbr 0:d3f5fdf2e6da 120 }
jhbr 0:d3f5fdf2e6da 121 msg[size] = '\0';
jhbr 0:d3f5fdf2e6da 122 log_info("\tMessage: %s", msg);
jhbr 0:d3f5fdf2e6da 123 }