|
| 1 | +/**----------------------------------------------------------------------------- |
| 2 | +
|
| 3 | + Example code to receive data using Arduino Nano 33 Sense board. |
| 4 | +
|
| 5 | + @file Nano33SenseReceive.ino |
| 6 | +
|
| 7 | + @brief Create a developer account at https://developers.chirp.io, |
| 8 | + and copy and paste your key, secret and config string for the "arduino" |
| 9 | + protocol into the credentials.h file. |
| 10 | +
|
| 11 | + This example will start listening for chirps and print to the terminal |
| 12 | + when anything is received. |
| 13 | +
|
| 14 | + Note: this example can be used in conjunction with the send example, |
| 15 | + to send and receive data in the same application. |
| 16 | +
|
| 17 | + Circuit: |
| 18 | + - Arduino Nano 33 BLE board |
| 19 | +
|
| 20 | + Copyright © 2011-2019, Asio Ltd. |
| 21 | + All rights reserved. |
| 22 | +
|
| 23 | + ----------------------------------------------------------------------------*/ |
| 24 | +#include <PDM.h> |
| 25 | + |
| 26 | +#include "chirp_connect.h" |
| 27 | +#include "credentials.h" |
| 28 | + |
| 29 | +#define SAMPLE_RATE 16000 |
| 30 | +#define BUFFER_SIZE 256 |
| 31 | + |
| 32 | +// Global variables ------------------------------------------------------------ |
| 33 | + |
| 34 | +static chirp_connect_t *chirp = NULL; |
| 35 | +short sampleBuffer[BUFFER_SIZE]; |
| 36 | +uint32_t counter; |
| 37 | +volatile int samplesRead; |
| 38 | + |
| 39 | +// Function definitions -------------------------------------------------------- |
| 40 | + |
| 41 | +void setupChirp(void); |
| 42 | +void chirpErrorHandler(chirp_connect_error_code_t code); |
| 43 | +void onPDMdata(void); |
| 44 | + |
| 45 | +// Main ------------------------------------------------------------------------ |
| 46 | + |
| 47 | +void setup() |
| 48 | +{ |
| 49 | + Serial.begin(115200); |
| 50 | + while (!Serial); |
| 51 | + |
| 52 | + setupChirp(); |
| 53 | + |
| 54 | + PDM.onReceive(onPDMdata); |
| 55 | + PDM.setGain(30); |
| 56 | + |
| 57 | + if (!PDM.begin(1, SAMPLE_RATE)) |
| 58 | + { |
| 59 | + Serial.println("Failed to start PDM!"); |
| 60 | + while (1); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void loop() |
| 65 | +{ |
| 66 | + if (samplesRead) |
| 67 | + { |
| 68 | + chirp_connect_error_code_t err = chirp_connect_process_shorts_input(chirp, sampleBuffer, samplesRead); |
| 69 | + chirpErrorHandler(err); |
| 70 | + samplesRead = 0; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void onPDMdata() |
| 75 | +{ |
| 76 | + int bytesAvailable = PDM.available(); |
| 77 | + PDM.read(sampleBuffer, bytesAvailable); |
| 78 | + samplesRead = bytesAvailable / sizeof(short); |
| 79 | +} |
| 80 | + |
| 81 | +// Chirp ----------------------------------------------------------------------- |
| 82 | + |
| 83 | +void onReceivingCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel) |
| 84 | +{ |
| 85 | + Serial.println("Receiving data..."); |
| 86 | +} |
| 87 | + |
| 88 | +void onReceivedCallback(void *chirp, uint8_t *payload, size_t length, uint8_t channel) |
| 89 | +{ |
| 90 | + if (payload) |
| 91 | + { |
| 92 | + char *data = (char *)calloc(length + 1, sizeof(uint8_t)); |
| 93 | + memcpy(data, payload, length * sizeof(uint8_t)); |
| 94 | + Serial.print("Received data: "); |
| 95 | + Serial.println(data); |
| 96 | + free(data); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + Serial.println("Decode failed."); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void chirpErrorHandler(chirp_connect_error_code_t code) |
| 105 | +{ |
| 106 | + if (code != CHIRP_CONNECT_OK) |
| 107 | + { |
| 108 | + const char *error_string = chirp_connect_error_code_to_string(code); |
| 109 | + Serial.println(error_string); |
| 110 | + exit(42); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void setupChirp(void) |
| 115 | +{ |
| 116 | + chirp = new_chirp_connect(CHIRP_APP_KEY, CHIRP_APP_SECRET); |
| 117 | + if (chirp == NULL) |
| 118 | + { |
| 119 | + Serial.println("Chirp initialisation failed."); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + chirp_connect_error_code_t err = chirp_connect_set_config(chirp, CHIRP_APP_CONFIG); |
| 124 | + chirpErrorHandler(err); |
| 125 | + |
| 126 | + chirp_connect_callback_set_t callback_set = { |
| 127 | + .on_state_changed = NULL, |
| 128 | + .on_sending = NULL, |
| 129 | + .on_sent = NULL, |
| 130 | + .on_receiving = onReceivingCallback, |
| 131 | + .on_received = onReceivedCallback |
| 132 | + }; |
| 133 | + |
| 134 | + err = chirp_connect_set_callbacks(chirp, callback_set); |
| 135 | + chirpErrorHandler(err); |
| 136 | + |
| 137 | + err = chirp_connect_set_input_sample_rate(chirp, SAMPLE_RATE); |
| 138 | + chirpErrorHandler(err); |
| 139 | + |
| 140 | + err = chirp_connect_set_frequency_correction(chirp, 1.0096); |
| 141 | + chirpErrorHandler(err); |
| 142 | + |
| 143 | + err = chirp_connect_start(chirp); |
| 144 | + chirpErrorHandler(err); |
| 145 | + |
| 146 | + Serial.println("Chirp SDK initialised."); |
| 147 | + Serial.flush(); |
| 148 | +} |
0 commit comments