Program that uses the QuickStart Library to interface a SmartMesh IP mote: Connects to the default network and starts publishing a random walk value every 5 seconds.
Fork of QSL_SimplePublish by
QSL SimplePublish
SmartMesh IP QuickStart Library
- GitHub repository
- Current release used: REL-1.0.2.2
- Documentation
- Discussion
main.cpp@0:d3f5fdf2e6da, 2016-09-01 (annotated)
- Committer:
- jhbr
- Date:
- Thu Sep 01 14:53:55 2016 +0000
- Revision:
- 0:d3f5fdf2e6da
- Child:
- 2:f177b47313ba
Initial commit of working example
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:d3f5fdf2e6da | 9 | #define BANDWIDTH_MS 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 | 0:d3f5fdf2e6da | 12 | #define DATA_PERIOD_MS 5000 // 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 | 0:d3f5fdf2e6da | 16 | // LED1 is the green LED on the NUCLEO-L053R8; might change for other boards |
jhbr | 0:d3f5fdf2e6da | 17 | DigitalOut myled(LED1); |
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 | 0:d3f5fdf2e6da | 27 | |
jhbr | 0:d3f5fdf2e6da | 28 | serialDebug.baud(115200); |
jhbr | 0:d3f5fdf2e6da | 29 | |
jhbr | 0:d3f5fdf2e6da | 30 | log_info("Initializing..."); |
jhbr | 0:d3f5fdf2e6da | 31 | dn_qsl_init(); |
jhbr | 0:d3f5fdf2e6da | 32 | |
jhbr | 0:d3f5fdf2e6da | 33 | while(TRUE) { |
jhbr | 0:d3f5fdf2e6da | 34 | if (dn_qsl_isConnected()) |
jhbr | 0:d3f5fdf2e6da | 35 | { |
jhbr | 0:d3f5fdf2e6da | 36 | uint16_t val = randomWalk(); |
jhbr | 0:d3f5fdf2e6da | 37 | static uint8_t count = 0; |
jhbr | 0:d3f5fdf2e6da | 38 | |
jhbr | 0:d3f5fdf2e6da | 39 | dn_write_uint16_t(payload, val); |
jhbr | 0:d3f5fdf2e6da | 40 | payload[2] = count; |
jhbr | 0:d3f5fdf2e6da | 41 | |
jhbr | 0:d3f5fdf2e6da | 42 | if (dn_qsl_send(payload, sizeof (payload), DEST_PORT)) |
jhbr | 0:d3f5fdf2e6da | 43 | { |
jhbr | 0:d3f5fdf2e6da | 44 | log_info("Sent message nr %u: %u", count, val); |
jhbr | 0:d3f5fdf2e6da | 45 | count++; |
jhbr | 0:d3f5fdf2e6da | 46 | } else |
jhbr | 0:d3f5fdf2e6da | 47 | { |
jhbr | 0:d3f5fdf2e6da | 48 | log_info("Send failed"); |
jhbr | 0:d3f5fdf2e6da | 49 | } |
jhbr | 0:d3f5fdf2e6da | 50 | |
jhbr | 0:d3f5fdf2e6da | 51 | do |
jhbr | 0:d3f5fdf2e6da | 52 | { |
jhbr | 0:d3f5fdf2e6da | 53 | bytesRead = dn_qsl_read(inboxBuf); |
jhbr | 0:d3f5fdf2e6da | 54 | parsePayload(inboxBuf, bytesRead); |
jhbr | 0:d3f5fdf2e6da | 55 | } while (bytesRead > 0); |
jhbr | 0:d3f5fdf2e6da | 56 | |
jhbr | 0:d3f5fdf2e6da | 57 | dn_sleep_ms(DATA_PERIOD_MS); |
jhbr | 0:d3f5fdf2e6da | 58 | } else |
jhbr | 0:d3f5fdf2e6da | 59 | { |
jhbr | 0:d3f5fdf2e6da | 60 | log_info("Connecting..."); |
jhbr | 0:d3f5fdf2e6da | 61 | myled = 0; |
jhbr | 0:d3f5fdf2e6da | 62 | if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS)) |
jhbr | 0:d3f5fdf2e6da | 63 | { |
jhbr | 0:d3f5fdf2e6da | 64 | myled = 1; |
jhbr | 0:d3f5fdf2e6da | 65 | log_info("Connected to network"); |
jhbr | 0:d3f5fdf2e6da | 66 | } else |
jhbr | 0:d3f5fdf2e6da | 67 | { |
jhbr | 0:d3f5fdf2e6da | 68 | log_info("Failed to connect"); |
jhbr | 0:d3f5fdf2e6da | 69 | } |
jhbr | 0:d3f5fdf2e6da | 70 | } |
jhbr | 0:d3f5fdf2e6da | 71 | |
jhbr | 0:d3f5fdf2e6da | 72 | } |
jhbr | 0:d3f5fdf2e6da | 73 | } |
jhbr | 0:d3f5fdf2e6da | 74 | |
jhbr | 0:d3f5fdf2e6da | 75 | static uint16_t randomWalk(void) |
jhbr | 0:d3f5fdf2e6da | 76 | { |
jhbr | 0:d3f5fdf2e6da | 77 | static bool first = TRUE; |
jhbr | 0:d3f5fdf2e6da | 78 | static uint16_t lastValue = 0x7fff; // Start in middle of uint16 range |
jhbr | 0:d3f5fdf2e6da | 79 | const int powerLevel = 9001; |
jhbr | 0:d3f5fdf2e6da | 80 | |
jhbr | 0:d3f5fdf2e6da | 81 | // Seed random number generator on first call |
jhbr | 0:d3f5fdf2e6da | 82 | if (first) |
jhbr | 0:d3f5fdf2e6da | 83 | { |
jhbr | 0:d3f5fdf2e6da | 84 | first = FALSE; |
jhbr | 0:d3f5fdf2e6da | 85 | srand(dn_time_ms()); |
jhbr | 0:d3f5fdf2e6da | 86 | } |
jhbr | 0:d3f5fdf2e6da | 87 | |
jhbr | 0:d3f5fdf2e6da | 88 | // Random walk within +/- powerLevel |
jhbr | 0:d3f5fdf2e6da | 89 | lastValue += rand() / (RAND_MAX / (2*powerLevel) + 1) - powerLevel; |
jhbr | 0:d3f5fdf2e6da | 90 | return lastValue; |
jhbr | 0:d3f5fdf2e6da | 91 | } |
jhbr | 0:d3f5fdf2e6da | 92 | |
jhbr | 0:d3f5fdf2e6da | 93 | static void parsePayload(const uint8_t *payload, uint8_t size) |
jhbr | 0:d3f5fdf2e6da | 94 | { |
jhbr | 0:d3f5fdf2e6da | 95 | uint8_t i; |
jhbr | 0:d3f5fdf2e6da | 96 | char msg[size + 1]; |
jhbr | 0:d3f5fdf2e6da | 97 | |
jhbr | 0:d3f5fdf2e6da | 98 | if (size == 0) |
jhbr | 0:d3f5fdf2e6da | 99 | { |
jhbr | 0:d3f5fdf2e6da | 100 | // Nothing to parse |
jhbr | 0:d3f5fdf2e6da | 101 | return; |
jhbr | 0:d3f5fdf2e6da | 102 | } |
jhbr | 0:d3f5fdf2e6da | 103 | |
jhbr | 0:d3f5fdf2e6da | 104 | // Parse bytes individually as well as together as a string |
jhbr | 0:d3f5fdf2e6da | 105 | log_info("Received downstream payload of %u bytes:", size); |
jhbr | 0:d3f5fdf2e6da | 106 | for (i = 0; i < size; i++) |
jhbr | 0:d3f5fdf2e6da | 107 | { |
jhbr | 0:d3f5fdf2e6da | 108 | msg[i] = payload[i]; |
jhbr | 0:d3f5fdf2e6da | 109 | log_info("\tByte# %03u: %#.2x (%u)", i, payload[i], payload[i]); |
jhbr | 0:d3f5fdf2e6da | 110 | } |
jhbr | 0:d3f5fdf2e6da | 111 | msg[size] = '\0'; |
jhbr | 0:d3f5fdf2e6da | 112 | log_info("\tMessage: %s", msg); |
jhbr | 0:d3f5fdf2e6da | 113 | } |