diff --git a/README.md b/README.md index ea9ea0f..afc6358 100644 --- a/README.md +++ b/README.md @@ -125,3 +125,62 @@ Possible outpout: What can be seen is that after sending the "join" message, the LMIC stack waits 5 seconds for the receive window and receives the acknowledgement from the LoRa gateway. +## Example "periodic" +Directory: /examples/periodic + +Modifications necessary: + +File /examples/periodic/main.c: + +* Adapt "application router ID (LSBF)" like already described under examples/join. +* Adapt "unique device ID (LSBF)" like already described under examples/join. +* Adapt "device-specific AES key " like already described under examples/join. + +File /examples/periodic/sensor.c: + +Added code that reads the CPU temperature of the RPi and returns it as a 2 byte integer +value. + +This examples does a "joins" the network and then sends a sensor value (the CPU temperature) +every 60 seconds as an unconfirmed message with a payload of 2 bytes. + + cd examples/periodic + make clean + make + sudo ./build/periodic.out + +Possible output: + + 000000000 HAL: Initializing ... + 000000004 HAL: Set radio RST pin to 0x00 + 000000005 HAL: Wait until 000000006 ms + 000000006 HAL: Set radio RST pin to 0x02 + 000000006 HAL: Wait until 000000011 ms + 000000013 HAL: Receiving ... + 000000041 Debug: Initializing + 000000041 Debug: JOINING + 000004897 Debug: EV_TXSTART + 000004898 HAL: Sending ... + 000009960 HAL: Receiving ... + 000009961 HAL: Wait until 000009962 ms + 000010033 Debug: JOINED + 000010034 Debug: 54230 + 000010034 Debug: Label 'val = ' value 0xd3d6 + 000010034 Debug: EV_TXSTART + 000010034 HAL: Sending ... + 000011081 HAL: Receiving ... + 000011081 HAL: Wait until 000011082 ms + 000012128 HAL: Receiving ... + 000012128 HAL: Wait until 000012130 ms + 000016360 Debug: TXCOMPLETE + 000070068 Debug: 53692 + 000070068 Debug: Label 'val = ' value 0xd1bc + 000070069 Debug: EV_TXSTART + 000070070 HAL: Sending ... + 000071117 HAL: Receiving ... + 000071117 HAL: Wait until 000071118 ms + 000072164 HAL: Receiving ... + 000072164 HAL: Wait until 000072165 ms + 000076734 Debug: TXCOMPLETE + + diff --git a/examples/periodic/main.c b/examples/periodic/main.c index 77406b6..86b1e68 100644 --- a/examples/periodic/main.c +++ b/examples/periodic/main.c @@ -39,14 +39,13 @@ extern u2_t readsensor(void); ////////////////////////////////////////////////// // application router ID (LSBF) -static const u1_t APPEUI[8] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0xEE, 0xFF, 0xC0 }; +static const u1_t APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // unique device ID (LSBF) -static const u1_t DEVEUI[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; +static const u1_t DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // device-specific AES key (derived from device EUI) -static const u1_t DEVKEY[16] = { 0xAB, 0x89, 0xEF, 0xCD, 0x23, 0x01, 0x67, 0x45, 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA }; - +static const u1_t DEVKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; ////////////////////////////////////////////////// // APPLICATION CALLBACKS @@ -113,7 +112,7 @@ static void reportfunc (osjob_t* j) { u2_t val = readsensor(); debug_val("val = ", val); // prepare and schedule data for transmission - LMIC.frame[0] = val << 8; + LMIC.frame[0] = val >> 8; LMIC.frame[1] = val; LMIC_setTxData2(1, LMIC.frame, 2, 0); // (port 1, 2 bytes, unconfirmed) // reschedule job in 60 seconds diff --git a/examples/periodic/sensor.c b/examples/periodic/sensor.c index 3cfb40b..fb2aece 100644 --- a/examples/periodic/sensor.c +++ b/examples/periodic/sensor.c @@ -1,40 +1,73 @@ -/* - * Copyright (c) 2014-2016 IBM Corporation. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ +// +// BSD 3-Clause License +// +// Hardware Abstraction Layer (HAL) targeted to Raspberry Pi and +// Dragino LoRa/GPS HAT +// +// Copyright (c) 2017, Wolfgang Klenk +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// -#include "lmic.h" -#include "hw.h" +#include +#include +#include +#include +#include +#include +#include -// use PB12 (DIP switch 1) as sensor value +#include "oslmic.h" +#include "debug.h" + +// Nothing to do void initsensor () { - RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // clock enable port B - hw_cfg_pin(GPIOB, 12, GPIOCFG_MODE_INP | GPIOCFG_OSPEED_40MHz | GPIOCFG_OTYPE_OPEN); // PB12 } -// read PB12 +// Read CPU temperature u2_t readsensor () { - return ((GPIOB->IDR & (1 << 12)) != 0); + static const char* fn = "/sys/class/thermal/thermal_zone0/temp"; + int fd = open(fn, O_RDONLY); + if(fd < 0) { + perror(fn); + exit(EXIT_FAILURE); + } + + char temp[10]; + int rc = read(fd, &temp, 10); + if (rc < 0) { + perror("Read CPU temperature."); + exit(EXIT_FAILURE); + } + + close(fd); + + debug_str(temp); + u2_t t = (u2_t) atoi(temp); + + return t; } diff --git a/examples/periodic/sensor.c.lmic_1.6_original b/examples/periodic/sensor.c.lmic_1.6_original new file mode 100644 index 0000000..3cfb40b --- /dev/null +++ b/examples/periodic/sensor.c.lmic_1.6_original @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014-2016 IBM Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lmic.h" +#include "hw.h" + +// use PB12 (DIP switch 1) as sensor value +void initsensor () { + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // clock enable port B + hw_cfg_pin(GPIOB, 12, GPIOCFG_MODE_INP | GPIOCFG_OSPEED_40MHz | GPIOCFG_OTYPE_OPEN); // PB12 +} + +// read PB12 +u2_t readsensor () { + return ((GPIOB->IDR & (1 << 12)) != 0); +} diff --git a/lmic/lmic.c.original b/lmic/lmic.c.lmic_1.6_original similarity index 100% rename from lmic/lmic.c.original rename to lmic/lmic.c.lmic_1.6_original