diff --git a/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h b/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h index 014e9a17d..cde7abc8e 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h +++ b/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h @@ -44,6 +44,9 @@ #ifndef BATMON_SENSOR_H_ #define BATMON_SENSOR_H_ /*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "lib/sensors.h" +/*---------------------------------------------------------------------------*/ #define BATMON_SENSOR_TYPE_TEMP 1 #define BATMON_SENSOR_TYPE_VOLT 2 /*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/Makefile b/examples/mqtt-client/Makefile new file mode 100644 index 000000000..c4741bec9 --- /dev/null +++ b/examples/mqtt-client/Makefile @@ -0,0 +1,13 @@ +CONTIKI_PROJECT = mqtt-client +all: $(CONTIKI_PROJECT) + +MODULES += os/net/app-layer/mqtt + +CONTIKI = ../.. +-include $(CONTIKI)/Makefile.identify-target + +MODULES_REL += arch/platform/$(TARGET) + +PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul + +include $(CONTIKI)/Makefile.include diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/README.md b/examples/mqtt-client/README.md similarity index 90% rename from examples/platform-specific/cc2538-common/mqtt-demo/README.md rename to examples/mqtt-client/README.md index 8b4e3b043..44bba8877 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/README.md +++ b/examples/mqtt-client/README.md @@ -1,15 +1,18 @@ -MQTT Demo -========= +MQTT Client Example +=================== The MQTT client can be used to: * Publish sensor readings to an MQTT broker. * Subscribe to a topic and receive commands from an MQTT broker -The demo will give some visual feedback with the green LED: +The demo will give some visual feedback with a LED (configurable): * Very fast blinking: Searching for a network * Fast blinking: Connecting to broker * Slow, long blinking: Sending a publish message +This example is known to work with all platforms that support the new button +API. + Publishing ---------- By default the example will attempt to publish readings to an MQTT broker @@ -20,7 +23,7 @@ running on the IPv6 address specified as `MQTT_DEMO_BROKER_IP_ADDR` in The publish messages include sensor readings but also some other information, such as device uptime in seconds and a message sequence number. The demo will publish to topic `iot-2/evt/status/fmt/json`. The device will connect using -client-id `d:quickstart:cc2538:`, where `` gets +client-id `d:contiki-ng:mqtt-client:`, where `` gets constructed from the device's IEEE address. Subscribing diff --git a/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c new file mode 100644 index 000000000..f22c33835 --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "dev/cc2538-sensors.h" +#include "mqtt-client.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define TMP_BUF_SZ 32 +/*---------------------------------------------------------------------------*/ +char tmp_buf[TMP_BUF_SZ]; +/*---------------------------------------------------------------------------*/ +static char * +temp_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"On-Chip Temp (mC)\":%d", + cc2538_temp_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +temp_init(void) +{ + SENSORS_ACTIVATE(cc2538_temp_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_cc2538_temp = { + temp_init, + temp_reading, +}; +/*---------------------------------------------------------------------------*/ +static char * +vdd3_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"VDD3 (mV)\":%d", + vdd3_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +vdd3_init(void) +{ + SENSORS_ACTIVATE(vdd3_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_vdd3 = { + vdd3_init, + vdd3_reading, +}; +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h new file mode 100644 index 000000000..50a765b52 --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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. + */ +/*---------------------------------------------------------------------------*/ +#ifndef BUILTIN_SENSORS_H_ +#define BUILTIN_SENSORS_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "mqtt-client.h" +/*---------------------------------------------------------------------------*/ +extern const mqtt_client_extension_t builtin_sensors_vdd3; +extern const mqtt_client_extension_t builtin_sensors_cc2538_temp; +/*---------------------------------------------------------------------------*/ +#endif /* BUILTIN_SENSORS_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c new file mode 100644 index 000000000..3b346fc1b --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "batmon-sensor.h" +#include "mqtt-client.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define TMP_BUF_SZ 32 +/*---------------------------------------------------------------------------*/ +char tmp_buf[TMP_BUF_SZ]; +/*---------------------------------------------------------------------------*/ +static char * +temp_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"On-Chip Temp (mC)\":%d", + batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +temp_init(void) +{ + SENSORS_ACTIVATE(batmon_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_batmon_temp = { + temp_init, + temp_reading, +}; +/*---------------------------------------------------------------------------*/ +static char * +volt_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"Volt (mV)\":%d", + (batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT) * 125) >> 5); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +volt_init(void) +{ + SENSORS_ACTIVATE(batmon_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_batmon_volt = { + volt_init, + volt_reading, +}; +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h new file mode 100644 index 000000000..38af3f62a --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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. + */ +/*---------------------------------------------------------------------------*/ +#ifndef BUILTIN_SENSORS_H_ +#define BUILTIN_SENSORS_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "mqtt-client.h" +/*---------------------------------------------------------------------------*/ +extern const mqtt_client_extension_t builtin_sensors_batmon_temp; +extern const mqtt_client_extension_t builtin_sensors_batmon_volt; +/*---------------------------------------------------------------------------*/ +#endif /* BUILTIN_SENSORS_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk b/examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk new file mode 100644 index 000000000..fcdd0583a --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc2538 diff --git a/examples/mqtt-client/arch/platform/cc2538dk/als-extend.c b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.c new file mode 100644 index 000000000..7f9845a42 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "als-extend.h" +#include "dev/als-sensor.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define TMP_BUF_SZ 32 +/*---------------------------------------------------------------------------*/ +char tmp_buf[TMP_BUF_SZ]; +/*---------------------------------------------------------------------------*/ +static void +als_init(void) +{ + SENSORS_ACTIVATE(als_sensor); +} +/*---------------------------------------------------------------------------*/ +static char * +als_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"ALS (raw)\":%d", als_sensor.value(0)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t als_extend = { + als_init, + als_reading, +}; +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/cc2538dk/als-extend.h b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.h new file mode 100644 index 000000000..f9b8dc9b0 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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. + */ +/*---------------------------------------------------------------------------*/ +#ifndef ALS_EXTEND_H_ +#define ALS_EXTEND_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "mqtt-client.h" +/*---------------------------------------------------------------------------*/ +extern const mqtt_client_extension_t als_extend; +/*---------------------------------------------------------------------------*/ +#endif /* ALS_EXTEND_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c new file mode 100644 index 000000000..835b95fa6 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "builtin-sensors.h" +#include "als-extend.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_vdd3, &builtin_sensors_cc2538_temp, + &als_extend); +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 b/examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 new file mode 100644 index 000000000..fcdd0583a --- /dev/null +++ b/examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc2538 diff --git a/examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c new file mode 100644 index 000000000..022c28e84 --- /dev/null +++ b/examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "builtin-sensors.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_vdd3, &builtin_sensors_cc2538_temp); +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx b/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx new file mode 100644 index 000000000..dcec87bad --- /dev/null +++ b/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc26xx-cc13xx diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c new file mode 100644 index 000000000..559a4ee9b --- /dev/null +++ b/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "builtin-sensors.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_batmon_temp, + &builtin_sensors_batmon_volt); +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/zoul/Makefile.zoul b/examples/mqtt-client/arch/platform/zoul/Makefile.zoul new file mode 100644 index 000000000..fcdd0583a --- /dev/null +++ b/examples/mqtt-client/arch/platform/zoul/Makefile.zoul @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc2538 diff --git a/examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c new file mode 100644 index 000000000..022c28e84 --- /dev/null +++ b/examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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 "contiki.h" +#include "builtin-sensors.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_vdd3, &builtin_sensors_cc2538_temp); +/*---------------------------------------------------------------------------*/ diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c b/examples/mqtt-client/mqtt-client.c similarity index 81% rename from examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c rename to examples/mqtt-client/mqtt-client.c index 5ed836cc1..867ef9982 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c +++ b/examples/mqtt-client/mqtt-client.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,19 +29,6 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /*---------------------------------------------------------------------------*/ -/** \addtogroup cc2538-examples - * @{ - * - * \defgroup cc2538-mqtt-demo CC2538 MQTT Demo Project - * - * Demonstrates MQTT functionality. Works with IBM Quickstart as well as - * mosquitto. - * @{ - * - * \file - * An MQTT example for the cc2538-based platforms - */ -/*---------------------------------------------------------------------------*/ #include "contiki.h" #include "net/routing/routing.h" #include "mqtt.h" @@ -52,11 +40,15 @@ #include "lib/sensors.h" #include "dev/button-hal.h" #include "dev/leds.h" -#include "dev/cc2538-sensors.h" +#include "os/sys/log.h" +#include "mqtt-client.h" #include #include /*---------------------------------------------------------------------------*/ +#define LOG_MODULE "mqtt-client" +#define LOG_LEVEL LOG_LEVEL_NONE +/*---------------------------------------------------------------------------*/ /* * IBM server: messaging.quickstart.internetofthings.ibmcloud.com * (184.172.124.189) mapped in an NAT64 (prefix 64:ff9b::/96) IPv6 address @@ -65,14 +57,20 @@ * Alternatively, publish to a local MQTT broker (e.g. mosquitto) running on * the node that hosts your border router */ -#ifdef MQTT_DEMO_BROKER_IP_ADDR -static const char *broker_ip = MQTT_DEMO_BROKER_IP_ADDR; -#define DEFAULT_ORG_ID "mqtt-demo" +#ifdef MQTT_CLIENT_CONF_BROKER_IP_ADDR +static const char *broker_ip = MQTT_CLIENT_CONF_BROKER_IP_ADDR; +#define DEFAULT_ORG_ID "contiki-ng" #else static const char *broker_ip = "0064:ff9b:0000:0000:0000:0000:b8ac:7cbd"; #define DEFAULT_ORG_ID "quickstart" #endif /*---------------------------------------------------------------------------*/ +#ifdef MQTT_CLIENT_CONF_STATUS_LED +#define MQTT_CLIENT_STATUS_LED MQTT_CLIENT_CONF_STATUS_LED +#else +#define MQTT_CLIENT_STATUS_LED LEDS_GREEN +#endif +/*---------------------------------------------------------------------------*/ /* * A timeout used when waiting for something to happen (e.g. to connect or to * disconnect) @@ -128,7 +126,7 @@ static uint8_t state; #define NO_NET_LED_DURATION (NET_CONNECT_PERIODIC >> 1) /*---------------------------------------------------------------------------*/ /* Default configuration values */ -#define DEFAULT_TYPE_ID "cc2538" +#define DEFAULT_TYPE_ID "mqtt-client" #define DEFAULT_AUTH_TOKEN "AUTHZ" #define DEFAULT_EVENT_TYPE_ID "status" #define DEFAULT_SUBSCRIBE_CMD_TYPE "+" @@ -137,14 +135,13 @@ static uint8_t state; #define DEFAULT_KEEP_ALIVE_TIMER 60 #define DEFAULT_RSSI_MEAS_INTERVAL (CLOCK_SECOND * 30) /*---------------------------------------------------------------------------*/ -/* Take a sensor reading on button press */ -#define PUBLISH_TRIGGER BUTTON_HAL_ID_USER_BUTTON - +#define MQTT_CLIENT_SENSOR_NONE (void *)0xFFFFFFFF +/*---------------------------------------------------------------------------*/ /* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */ #define ECHO_REQ_PAYLOAD_LEN 20 /*---------------------------------------------------------------------------*/ -PROCESS_NAME(mqtt_demo_process); -AUTOSTART_PROCESSES(&mqtt_demo_process); +PROCESS_NAME(mqtt_client_process); +AUTOSTART_PROCESSES(&mqtt_client_process); /*---------------------------------------------------------------------------*/ /** * \brief Data structure declaration for the MQTT client configuration @@ -164,8 +161,6 @@ typedef struct mqtt_client_config { /* Maximum TCP segment size for outgoing segments of our socket */ #define MAX_TCP_SEGMENT_SIZE 32 /*---------------------------------------------------------------------------*/ -#define STATUS_LED LEDS_GREEN -/*---------------------------------------------------------------------------*/ /* * Buffers for Client ID and Topic. * Make sure they are large enough to hold the entire respective string @@ -202,9 +197,12 @@ static int def_rt_rssi = 0; /*---------------------------------------------------------------------------*/ static mqtt_client_config_t conf; /*---------------------------------------------------------------------------*/ -PROCESS(mqtt_demo_process, "MQTT Demo"); +extern const mqtt_client_extension_t *mqtt_client_extensions[]; +extern const uint8_t mqtt_client_extension_count; /*---------------------------------------------------------------------------*/ -int +PROCESS(mqtt_client_process, "MQTT Client"); +/*---------------------------------------------------------------------------*/ +static int ipaddr_sprintf(char *buf, uint8_t buf_len, const uip_ipaddr_t *addr) { uint16_t a; @@ -241,25 +239,25 @@ echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, static void publish_led_off(void *d) { - leds_off(STATUS_LED); + leds_off(MQTT_CLIENT_STATUS_LED); } /*---------------------------------------------------------------------------*/ static void pub_handler(const char *topic, uint16_t topic_len, const uint8_t *chunk, uint16_t chunk_len) { - DBG("Pub Handler: topic='%s' (len=%u), chunk_len=%u\n", topic, topic_len, - chunk_len); + LOG_DBG("Pub Handler: topic='%s' (len=%u), chunk_len=%u\n", topic, + topic_len, chunk_len); /* If we don't like the length, ignore */ if(topic_len != 23 || chunk_len != 1) { - printf("Incorrect topic or chunk len. Ignored\n"); + LOG_ERR("Incorrect topic or chunk len. Ignored\n"); return; } /* If the format != json, ignore */ if(strncmp(&topic[topic_len - 4], "json", 4) != 0) { - printf("Incorrect format\n"); + LOG_ERR("Incorrect format\n"); } if(strncmp(&topic[10], "leds", 4) == 0) { @@ -277,16 +275,16 @@ mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) { switch(event) { case MQTT_EVENT_CONNECTED: { - DBG("APP - Application has a MQTT connection\n"); + LOG_DBG("Application has a MQTT connection\n"); timer_set(&connection_life, CONNECTION_STABLE_TIME); state = STATE_CONNECTED; break; } case MQTT_EVENT_DISCONNECTED: { - DBG("APP - MQTT Disconnect. Reason %u\n", *((mqtt_event_t *)data)); + LOG_DBG("MQTT Disconnect. Reason %u\n", *((mqtt_event_t *)data)); state = STATE_DISCONNECTED; - process_poll(&mqtt_demo_process); + process_poll(&mqtt_client_process); break; } case MQTT_EVENT_PUBLISH: { @@ -295,29 +293,28 @@ mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) /* Implement first_flag in publish message? */ if(msg_ptr->first_chunk) { msg_ptr->first_chunk = 0; - DBG("APP - Application received a publish on topic '%s'. Payload " - "size is %i bytes. Content:\n\n", - msg_ptr->topic, msg_ptr->payload_length); + LOG_DBG("Application received publish for topic '%s'. Payload " + "size is %i bytes.\n", msg_ptr->topic, msg_ptr->payload_length); } - pub_handler(msg_ptr->topic, strlen(msg_ptr->topic), msg_ptr->payload_chunk, - msg_ptr->payload_length); + pub_handler(msg_ptr->topic, strlen(msg_ptr->topic), + msg_ptr->payload_chunk, msg_ptr->payload_length); break; } case MQTT_EVENT_SUBACK: { - DBG("APP - Application is subscribed to topic successfully\n"); + LOG_DBG("Application is subscribed to topic successfully\n"); break; } case MQTT_EVENT_UNSUBACK: { - DBG("APP - Application is unsubscribed to topic successfully\n"); + LOG_DBG("Application is unsubscribed to topic successfully\n"); break; } case MQTT_EVENT_PUBACK: { - DBG("APP - Publishing complete.\n"); + LOG_DBG("Publishing complete.\n"); break; } default: - DBG("APP - Application got a unhandled MQTT event: %i\n", event); + LOG_DBG("Application got a unhandled MQTT event: %i\n", event); break; } } @@ -330,7 +327,7 @@ construct_pub_topic(void) /* len < 0: Error. Len >= BUFFER_SIZE: Buffer too small */ if(len < 0 || len >= BUFFER_SIZE) { - printf("Pub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); + LOG_INFO("Pub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); return 0; } @@ -345,7 +342,7 @@ construct_sub_topic(void) /* len < 0: Error. Len >= BUFFER_SIZE: Buffer too small */ if(len < 0 || len >= BUFFER_SIZE) { - printf("Sub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); + LOG_INFO("Sub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); return 0; } @@ -363,7 +360,7 @@ construct_client_id(void) /* len < 0: Error. Len >= BUFFER_SIZE: Buffer too small */ if(len < 0 || len >= BUFFER_SIZE) { - printf("Client ID: %d, Buffer %d\n", len, BUFFER_SIZE); + LOG_ERR("Client ID: %d, Buffer %d\n", len, BUFFER_SIZE); return 0; } @@ -438,9 +435,9 @@ subscribe(void) status = mqtt_subscribe(&conn, NULL, sub_topic, MQTT_QOS_LEVEL_0); - DBG("APP - Subscribing!\n"); + LOG_DBG("Subscribing!\n"); if(status == MQTT_STATUS_OUT_QUEUE_FULL) { - DBG("APP - Tried to subscribe but command queue was full!\n"); + LOG_ERR("Tried to subscribe but command queue was full!\n"); } } /*---------------------------------------------------------------------------*/ @@ -450,6 +447,7 @@ publish(void) /* Publish MQTT topic in IBM quickstart format */ int len; int remaining = APP_BUFFER_SIZE; + int i; seq_nr_value++; @@ -458,13 +456,17 @@ publish(void) len = snprintf(buf_ptr, remaining, "{" "\"d\":{" - "\"myName\":\"%s\"," + "\"Platform\":\""CONTIKI_TARGET_STRING"\"," +#ifdef CONTIKI_BOARD_STRING + "\"Board\":\""CONTIKI_BOARD_STRING"\"," +#endif "\"Seq #\":%d," "\"Uptime (sec)\":%lu", - BOARD_STRING, seq_nr_value, clock_seconds()); + seq_nr_value, clock_seconds()); if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); return; } @@ -476,47 +478,43 @@ publish(void) memset(def_rt_str, 0, sizeof(def_rt_str)); ipaddr_sprintf(def_rt_str, sizeof(def_rt_str), uip_ds6_defrt_choose()); - len = snprintf(buf_ptr, remaining, ",\"Def Route\":\"%s\",\"RSSI (dBm)\":%d", + len = snprintf(buf_ptr, remaining, + ",\"Def Route\":\"%s\",\"RSSI (dBm)\":%d", def_rt_str, def_rt_rssi); if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); return; } remaining -= len; buf_ptr += len; - len = snprintf(buf_ptr, remaining, ",\"On-Chip Temp (mC)\":%d", - cc2538_temp_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); + for(i = 0; i < mqtt_client_extension_count; i++) { + len = snprintf(buf_ptr, remaining, ",%s", + mqtt_client_extensions[i]->value()); - if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); - return; + if(len < 0 || len >= remaining) { + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); + return; + } + remaining -= len; + buf_ptr += len; } - remaining -= len; - buf_ptr += len; - - len = snprintf(buf_ptr, remaining, ",\"VDD3 (mV)\":%d", - vdd3_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); - - if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); - return; - } - remaining -= len; - buf_ptr += len; len = snprintf(buf_ptr, remaining, "}}"); if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); return; } mqtt_publish(&conn, NULL, pub_topic, (uint8_t *)app_buffer, strlen(app_buffer), MQTT_QOS_LEVEL_0, MQTT_RETAIN_OFF); - DBG("APP - Publish!\n"); + LOG_DBG("Publish!\n"); } /*---------------------------------------------------------------------------*/ static void @@ -546,7 +544,7 @@ state_machine(void) switch(state) { case STATE_INIT: /* If we have just been configured register MQTT connection */ - mqtt_register(&conn, &mqtt_demo_process, client_id, mqtt_event, + mqtt_register(&conn, &mqtt_client_process, client_id, mqtt_event, MAX_TCP_SEGMENT_SIZE); /* @@ -555,7 +553,7 @@ state_machine(void) */ if(strncasecmp(conf.org_id, QUICKSTART, strlen(conf.org_id)) != 0) { if(strlen(conf.auth_token) == 0) { - printf("User name set, but empty auth token\n"); + LOG_ERR("User name set, but empty auth token\n"); state = STATE_ERROR; break; } else { @@ -569,31 +567,31 @@ state_machine(void) connect_attempt = 1; state = STATE_REGISTERED; - DBG("Init\n"); + LOG_DBG("Init\n"); /* Continue */ case STATE_REGISTERED: if(uip_ds6_get_global(ADDR_PREFERRED) != NULL) { /* Registered and with a public IP. Connect */ - DBG("Registered. Connect attempt %u\n", connect_attempt); + LOG_DBG("Registered. Connect attempt %u\n", connect_attempt); ping_parent(); connect_to_broker(); } else { - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL); } etimer_set(&publish_periodic_timer, NET_CONNECT_PERIODIC); return; break; case STATE_CONNECTING: - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); ctimer_set(&ct, CONNECTING_LED_DURATION, publish_led_off, NULL); /* Not connected yet. Wait */ - DBG("Connecting (%u)\n", connect_attempt); + LOG_DBG("Connecting (%u)\n", connect_attempt); break; case STATE_CONNECTED: /* Don't subscribe unless we are a registered device */ if(strncasecmp(conf.org_id, QUICKSTART, strlen(conf.org_id)) == 0) { - DBG("Using 'quickstart': Skipping subscribe\n"); + LOG_DBG("Using 'quickstart': Skipping subscribe\n"); state = STATE_PUBLISHING; } /* Continue */ @@ -613,13 +611,12 @@ state_machine(void) subscribe(); state = STATE_PUBLISHING; } else { - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); ctimer_set(&ct, PUBLISH_LED_ON_DURATION, publish_led_off, NULL); + LOG_DBG("Publishing\n"); publish(); } etimer_set(&publish_periodic_timer, conf.pub_interval); - - DBG("Publishing\n"); /* Return here so we don't end up rescheduling the timer */ return; } else { @@ -632,12 +629,12 @@ state_machine(void) * trigger a new message and we wait for TCP to either ACK the entire * packet after retries, or to timeout and notify us. */ - DBG("Publishing... (MQTT state=%d, q=%u)\n", conn.state, - conn.out_queue_full); + LOG_DBG("Publishing... (MQTT state=%d, q=%u)\n", conn.state, + conn.out_queue_full); } break; case STATE_DISCONNECTED: - DBG("Disconnected\n"); + LOG_DBG("Disconnected\n"); if(connect_attempt < RECONNECT_ATTEMPTS || RECONNECT_ATTEMPTS == RETRY_FOREVER) { /* Disconnect and backoff */ @@ -648,7 +645,7 @@ state_machine(void) interval = connect_attempt < 3 ? RECONNECT_INTERVAL << connect_attempt : RECONNECT_INTERVAL << 3; - DBG("Disconnected. Attempt %u in %lu ticks\n", connect_attempt, interval); + LOG_DBG("Disconnected. Attempt %u in %lu ticks\n", connect_attempt, interval); etimer_set(&publish_periodic_timer, interval); @@ -657,23 +654,23 @@ state_machine(void) } else { /* Max reconnect attempts reached. Enter error state */ state = STATE_ERROR; - DBG("Aborting connection after %u attempts\n", connect_attempt - 1); + LOG_DBG("Aborting connection after %u attempts\n", connect_attempt - 1); } break; case STATE_CONFIG_ERROR: /* Idle away. The only way out is a new config */ - printf("Bad configuration.\n"); + LOG_ERR("Bad configuration.\n"); return; case STATE_ERROR: default: - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); /* * 'default' should never happen. * * If we enter here it's because of some error. Stop timers. The only thing * that can bring us out is a new config event */ - printf("Default case: State=0x%02x\n", state); + LOG_ERR("Default case: State=0x%02x\n", state); return; } @@ -681,17 +678,31 @@ state_machine(void) etimer_set(&publish_periodic_timer, STATE_MACHINE_PERIODIC); } /*---------------------------------------------------------------------------*/ -PROCESS_THREAD(mqtt_demo_process, ev, data) +static void +init_extensions(void) +{ + int i; + + for(i = 0; i < mqtt_client_extension_count; i++) { + if(mqtt_client_extensions[i]->init) { + mqtt_client_extensions[i]->init(); + } + } +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(mqtt_client_process, ev, data) { PROCESS_BEGIN(); - printf("MQTT Demo Process\n"); + printf("MQTT Client Process\n"); if(init_config() != 1) { PROCESS_EXIT(); } + init_extensions(); + update_config(); def_rt_rssi = 0x8000000; @@ -705,7 +716,7 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) PROCESS_YIELD(); if(ev == button_hal_release_event && - ((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER) { + ((button_hal_button_t *)data)->unique_id == BUTTON_HAL_ID_BUTTON_ZERO) { if(state == STATE_ERROR) { connect_attempt = 1; state = STATE_REGISTERED; @@ -715,7 +726,7 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) if((ev == PROCESS_EVENT_TIMER && data == &publish_periodic_timer) || ev == PROCESS_EVENT_POLL || (ev == button_hal_release_event && - ((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER)) { + ((button_hal_button_t *)data)->unique_id == BUTTON_HAL_ID_BUTTON_ZERO)) { state_machine(); } @@ -728,7 +739,3 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) PROCESS_END(); } /*---------------------------------------------------------------------------*/ -/** - * @} - * @} - */ diff --git a/examples/mqtt-client/mqtt-client.h b/examples/mqtt-client/mqtt-client.h new file mode 100644 index 000000000..c551c701b --- /dev/null +++ b/examples/mqtt-client/mqtt-client.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. 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. + */ +/*---------------------------------------------------------------------------*/ +#ifndef MQTT_CLIENT_H_ +#define MQTT_CLIENT_H_ +/*---------------------------------------------------------------------------*/ +#include +/*---------------------------------------------------------------------------*/ +typedef struct mqtt_client_extension_s { + void (*init)(void); + char *(*value)(void); +} mqtt_client_extension_t; +/*---------------------------------------------------------------------------*/ +#define MQTT_CLIENT_EXTENSIONS(...) \ + const mqtt_client_extension_t *mqtt_client_extensions[] = {__VA_ARGS__}; \ + const uint8_t mqtt_client_extension_count = \ + (sizeof(mqtt_client_extensions) / sizeof(mqtt_client_extensions[0])); +/*---------------------------------------------------------------------------*/ +#endif /* MQTT_CLIENT_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h b/examples/mqtt-client/project-conf.h similarity index 87% rename from examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h rename to examples/mqtt-client/project-conf.h index ac86990c6..7b041ae38 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h +++ b/examples/mqtt-client/project-conf.h @@ -29,25 +29,14 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /*---------------------------------------------------------------------------*/ -/** - * \addtogroup cc2538-mqtt-demo - * @{ - * - * \file - * Project specific configuration defines for the MQTT demo - */ -/*---------------------------------------------------------------------------*/ #ifndef PROJECT_CONF_H_ #define PROJECT_CONF_H_ /*---------------------------------------------------------------------------*/ /* Enable TCP */ #define UIP_CONF_TCP 1 -/* User configuration */ -#define MQTT_DEMO_STATUS_LED LEDS_GREEN - /* If undefined, the demo will attempt to connect to IBM's quickstart */ -#define MQTT_DEMO_BROKER_IP_ADDR "fd00::1" +#define MQTT_CLIENT_CONF_BROKER_IP_ADDR "fd00::1" /*---------------------------------------------------------------------------*/ #endif /* PROJECT_CONF_H_ */ /*---------------------------------------------------------------------------*/ diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile b/examples/platform-specific/cc2538-common/mqtt-demo/Makefile deleted file mode 100644 index ec29183b6..000000000 --- a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -CONTIKI_PROJECT = mqtt-demo -all: $(CONTIKI_PROJECT) - -PLATFORMS_ONLY = cc2538dk openmote-cc2538 zoul - -MODULES += os/net/app-layer/mqtt - -CONTIKI = ../../../.. -include $(CONTIKI)/Makefile.include diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target b/examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target deleted file mode 100644 index 777593c88..000000000 --- a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target +++ /dev/null @@ -1 +0,0 @@ -TARGET = cc2538dk