Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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@3:fb2c485306d1, 2016-09-07 (annotated)
- Committer:
- jhbr
- Date:
- Wed Sep 07 10:31:13 2016 +0000
- Revision:
- 3:fb2c485306d1
- Parent:
- 2:f177b47313ba
- Child:
- 4:0285bcbbc855
Updated dn_fsm.c to new version.; Improved dn_time_ms to use full 32 bit uint range.
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 | 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 | 3:fb2c485306d1 | 40 | |
| jhbr | 3:fb2c485306d1 | 41 | for (i = 0; i < 3; i++) |
| jhbr | 3:fb2c485306d1 | 42 | { |
| jhbr | 3:fb2c485306d1 | 43 | debug("Time: %u", dn_time_ms()); |
| jhbr | 3:fb2c485306d1 | 44 | wait(1); |
| jhbr | 3:fb2c485306d1 | 45 | } |
| jhbr | 0:d3f5fdf2e6da | 46 | |
| jhbr | 0:d3f5fdf2e6da | 47 | while(TRUE) { |
| jhbr | 0:d3f5fdf2e6da | 48 | if (dn_qsl_isConnected()) |
| jhbr | 0:d3f5fdf2e6da | 49 | { |
| jhbr | 0:d3f5fdf2e6da | 50 | uint16_t val = randomWalk(); |
| jhbr | 0:d3f5fdf2e6da | 51 | static uint8_t count = 0; |
| jhbr | 2:f177b47313ba | 52 | myled = 0; // Turn off LED during send/read |
| jhbr | 0:d3f5fdf2e6da | 53 | |
| jhbr | 0:d3f5fdf2e6da | 54 | dn_write_uint16_t(payload, val); |
| jhbr | 0:d3f5fdf2e6da | 55 | payload[2] = count; |
| jhbr | 0:d3f5fdf2e6da | 56 | |
| jhbr | 0:d3f5fdf2e6da | 57 | if (dn_qsl_send(payload, sizeof (payload), DEST_PORT)) |
| jhbr | 0:d3f5fdf2e6da | 58 | { |
| jhbr | 0:d3f5fdf2e6da | 59 | log_info("Sent message nr %u: %u", count, val); |
| jhbr | 0:d3f5fdf2e6da | 60 | count++; |
| jhbr | 0:d3f5fdf2e6da | 61 | } else |
| jhbr | 0:d3f5fdf2e6da | 62 | { |
| jhbr | 0:d3f5fdf2e6da | 63 | log_info("Send failed"); |
| jhbr | 0:d3f5fdf2e6da | 64 | } |
| jhbr | 0:d3f5fdf2e6da | 65 | |
| jhbr | 0:d3f5fdf2e6da | 66 | do |
| jhbr | 0:d3f5fdf2e6da | 67 | { |
| jhbr | 0:d3f5fdf2e6da | 68 | bytesRead = dn_qsl_read(inboxBuf); |
| jhbr | 0:d3f5fdf2e6da | 69 | parsePayload(inboxBuf, bytesRead); |
| jhbr | 0:d3f5fdf2e6da | 70 | } while (bytesRead > 0); |
| jhbr | 0:d3f5fdf2e6da | 71 | |
| jhbr | 2:f177b47313ba | 72 | myled = 1; // Turn on LED |
| jhbr | 0:d3f5fdf2e6da | 73 | dn_sleep_ms(DATA_PERIOD_MS); |
| jhbr | 0:d3f5fdf2e6da | 74 | } else |
| jhbr | 0:d3f5fdf2e6da | 75 | { |
| jhbr | 0:d3f5fdf2e6da | 76 | log_info("Connecting..."); |
| jhbr | 2:f177b47313ba | 77 | myled = 0; // Not connected; turn off LED |
| jhbr | 0:d3f5fdf2e6da | 78 | if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS)) |
| jhbr | 0:d3f5fdf2e6da | 79 | { |
| jhbr | 2:f177b47313ba | 80 | myled = 1; // Connected; turn on LED |
| jhbr | 0:d3f5fdf2e6da | 81 | log_info("Connected to network"); |
| jhbr | 0:d3f5fdf2e6da | 82 | } else |
| jhbr | 0:d3f5fdf2e6da | 83 | { |
| jhbr | 0:d3f5fdf2e6da | 84 | log_info("Failed to connect"); |
| jhbr | 0:d3f5fdf2e6da | 85 | } |
| jhbr | 0:d3f5fdf2e6da | 86 | } |
| jhbr | 0:d3f5fdf2e6da | 87 | |
| jhbr | 0:d3f5fdf2e6da | 88 | } |
| jhbr | 0:d3f5fdf2e6da | 89 | } |
| jhbr | 0:d3f5fdf2e6da | 90 | |
| jhbr | 0:d3f5fdf2e6da | 91 | static uint16_t randomWalk(void) |
| jhbr | 0:d3f5fdf2e6da | 92 | { |
| jhbr | 0:d3f5fdf2e6da | 93 | static bool first = TRUE; |
| jhbr | 0:d3f5fdf2e6da | 94 | static uint16_t lastValue = 0x7fff; // Start in middle of uint16 range |
| jhbr | 0:d3f5fdf2e6da | 95 | const int powerLevel = 9001; |
| jhbr | 0:d3f5fdf2e6da | 96 | |
| jhbr | 0:d3f5fdf2e6da | 97 | // Seed random number generator on first call |
| jhbr | 0:d3f5fdf2e6da | 98 | if (first) |
| jhbr | 0:d3f5fdf2e6da | 99 | { |
| jhbr | 0:d3f5fdf2e6da | 100 | first = FALSE; |
| jhbr | 0:d3f5fdf2e6da | 101 | srand(dn_time_ms()); |
| jhbr | 0:d3f5fdf2e6da | 102 | } |
| jhbr | 0:d3f5fdf2e6da | 103 | |
| jhbr | 0:d3f5fdf2e6da | 104 | // Random walk within +/- powerLevel |
| jhbr | 0:d3f5fdf2e6da | 105 | lastValue += rand() / (RAND_MAX / (2*powerLevel) + 1) - powerLevel; |
| jhbr | 0:d3f5fdf2e6da | 106 | return lastValue; |
| jhbr | 0:d3f5fdf2e6da | 107 | } |
| jhbr | 0:d3f5fdf2e6da | 108 | |
| jhbr | 0:d3f5fdf2e6da | 109 | static void parsePayload(const uint8_t *payload, uint8_t size) |
| jhbr | 0:d3f5fdf2e6da | 110 | { |
| jhbr | 0:d3f5fdf2e6da | 111 | uint8_t i; |
| jhbr | 0:d3f5fdf2e6da | 112 | char msg[size + 1]; |
| jhbr | 0:d3f5fdf2e6da | 113 | |
| jhbr | 0:d3f5fdf2e6da | 114 | if (size == 0) |
| jhbr | 0:d3f5fdf2e6da | 115 | { |
| jhbr | 0:d3f5fdf2e6da | 116 | // Nothing to parse |
| jhbr | 0:d3f5fdf2e6da | 117 | return; |
| jhbr | 0:d3f5fdf2e6da | 118 | } |
| jhbr | 0:d3f5fdf2e6da | 119 | |
| jhbr | 0:d3f5fdf2e6da | 120 | // Parse bytes individually as well as together as a string |
| jhbr | 0:d3f5fdf2e6da | 121 | log_info("Received downstream payload of %u bytes:", size); |
| jhbr | 0:d3f5fdf2e6da | 122 | for (i = 0; i < size; i++) |
| jhbr | 0:d3f5fdf2e6da | 123 | { |
| jhbr | 0:d3f5fdf2e6da | 124 | msg[i] = payload[i]; |
| jhbr | 0:d3f5fdf2e6da | 125 | log_info("\tByte# %03u: %#.2x (%u)", i, payload[i], payload[i]); |
| jhbr | 0:d3f5fdf2e6da | 126 | } |
| jhbr | 0:d3f5fdf2e6da | 127 | msg[size] = '\0'; |
| jhbr | 0:d3f5fdf2e6da | 128 | log_info("\tMessage: %s", msg); |
| jhbr | 0:d3f5fdf2e6da | 129 | } |
