Compare commits

...

341 commits

Author SHA1 Message Date
Brent Rubell
0a19fd0b14
Merge pull request #232 from brentru/update-fingerprint-2025
Update SSL Fingerprint for 2025
2025-07-16 10:13:16 -04:00
brentru
380b3b5094 Remove : 2025-07-14 16:09:00 -04:00
brentru
cfb5e84f05 Update fingerprint, bump ver 2025-07-14 16:06:58 -04:00
Tyeth Gundry
520c685b55
Update library.properties - bump version to 2.5.9 2025-01-07 16:15:01 +00:00
Tyeth Gundry
81adeb8182
Update adafruitio_secure_esp8266.ino - mention expiry date 2025-01-07 16:14:26 +00:00
Brent Rubell
9c8db828c5
Merge pull request #231 from torselden/master
Update io.adafruit.com certificate fingerprint
2025-01-02 08:47:53 -06:00
Tor Seldén
df7828c500 Update io.adafruit.com certificate fingerprint 2024-12-30 15:56:11 +01:00
Brent Rubell
a022705c5b
Merge pull request #227 from brentru/fix-msg-i2c-decode-err
Fix how subscription packet length is calculated
2023-08-31 13:43:11 -04:00
brentru
4ba2fee0e0 increase sub data len to MAXBUFFERSIZE 2023-08-31 13:36:49 -04:00
brentru
c3d72a5a54 clang 2023-08-31 13:34:53 -04:00
brentru
1091b17df7 make more informative 2023-08-31 13:34:29 -04:00
brentru
34c28094f1 fix i2c decode issue 2023-08-31 13:25:58 -04:00
Brent Rubell
c84b63f945
Update library.properties 2023-08-28 12:30:51 -04:00
Brent Rubell
3a7cf33c48
Merge pull request #190 from dlizotte-uwo/master
MQTT with QOS 1 or 2 forbids a packet ID of 0.
2023-08-28 12:30:17 -04:00
dlizotte-uwo
5b939479df
Merge branch 'adafruit:master' into master 2023-08-19 09:06:32 -04:00
Brent Rubell
3468daa29b
Update library.properties 2023-07-21 10:41:20 -04:00
Justin Cooper
897577b1e2
Update root ca for example 2023-07-21 09:37:18 -05:00
Justin Cooper
2e52e8b28b
Update ssl fingerprint 2023-07-21 09:35:22 -05:00
dlizotte-uwo
f4685f4eea
Fix Adafruit_MQTT.cpp formatting 2023-07-05 18:07:22 -04:00
dlizotte-uwo
847f561215 Merge in KEEPALIVE check. 2023-07-05 17:39:49 -04:00
dherrada
0c57bf6c1d Update CI action versions 2023-05-12 11:24:05 -04:00
Brent Rubell
d56a6c7bfa
Update library.properties 2023-05-04 14:34:44 -04:00
Brent Rubell
bc35d22316
Merge pull request #223 from adafruit/revert-222-fix-esp-bsp-2-0-8
Revert "Fix connectErrorString return type for ESP-32 BSP 2.0.8"
2023-05-04 14:34:05 -04:00
Brent Rubell
e9a6c12184
Revert "Fix connectErrorString return type for ESP-32 BSP 2.0.8" 2023-05-04 14:26:19 -04:00
Brent Rubell
431f70f983
Merge pull request #222 from adafruit/fix-esp-bsp-2-0-8
Fix connectErrorString return type for ESP-32 BSP 2.0.8
2023-04-21 15:55:13 -04:00
brentru
a94f4d512e carter feedback implemented 2023-04-21 15:23:01 -04:00
brentru
dd72b39489 fix return type for esp bsp 2.0.8 2023-04-21 13:05:51 -04:00
Brent Rubell
6f82f66651
Merge pull request #218 from brentru/fix-retain-overloads
Fix `error: call of overloaded 'publish(char*&, uint8_t [512], size_t&, int)' is ambiguous`
2022-11-14 16:36:56 -05:00
brentru
d6b8436fb9 clang format 2022-11-14 16:24:03 -05:00
brentru
28435eaf58 fixes from @ben-willmore on 83af73f4e5 2022-11-14 16:23:51 -05:00
Brent Rubell
9409c6373b
Merge branch 'adafruit:master' into master 2022-11-14 16:07:25 -05:00
Brent Rubell
abe90ec6f6
Update library.properties 2022-11-11 09:56:50 -05:00
Brent Rubell
18af1b6e2a
Merge pull request #216 from ben-willmore/retain-flag
Add support for retain flag when publishing
2022-11-11 09:54:49 -05:00
Ben Willmore
33b62c7a64 Add skip files 2022-11-09 20:36:12 +00:00
Ben Willmore
da5b499307 Add example of retain usage 2022-11-09 20:01:14 +00:00
brentru
96880c03c0 larger buffer for WS 2022-11-02 16:59:00 -04:00
Ben Willmore
3f2b25339e More formatting fixes 2022-11-02 20:24:41 +00:00
Ben Willmore
8b27fdb775 Fix formatting 2022-11-02 20:17:05 +00:00
Ben Willmore
e6ab3a9631 Tidy code 2022-11-02 11:37:01 +00:00
Ben Willmore
d3fdbb2f7c Use function overloading instead of optional arguments 2022-11-02 11:33:32 +00:00
Lucas Jandrew
de5059eb92 Make publishPacket retain flag default to false 2022-11-02 11:18:59 +00:00
Lucas Jandrew
2c667d9a30 Use ternary operator to set retain bit rather than relying on compiler definition of 0/1 2022-11-02 11:18:06 +00:00
Brent Rubell
1e33ece844
Merge pull request #215 from brentru/fix-processUntil
Fix subscription packet callbacks not executing within processPacketsUntil()
2022-11-01 11:33:44 -04:00
brentru
1eb56066ef clang 2022-10-28 16:44:07 -04:00
brentru
52e04bebe9 loren review 2022-10-28 16:32:51 -04:00
brentru
a80db209cd bump lib prop 2022-10-28 14:53:54 -04:00
brentru
30d4af8179 remove debug 2022-10-28 14:53:32 -04:00
brentru
5c00e84d08 add new processSubscriptionPacket func 2022-10-28 13:01:06 -04:00
brentru
0ba089d1e8 fixes processUntil 2022-10-28 12:27:33 -04:00
Brent Rubell
7413e92e77
Merge pull request #213 from adafruit/update-ssl-cert
Update SSL Certificate for ESP8266, add warning
2022-08-15 13:35:40 -04:00
brentru
25662868c5 add cert and warning 2022-08-15 13:18:56 -04:00
Brent Rubell
223d419ebd
Update library.properties 2021-07-12 09:53:23 -04:00
Brent Rubell
37e175dcef
Merge pull request #196 from xdylanm/send_long_packet_fix
Fixed length calculation when payload exceeds 128bytes
2021-07-12 09:53:02 -04:00
Brent Rubell
d466773076
Update library.properties 2021-06-22 13:29:31 -04:00
Brent Rubell
3a319ab695
Merge pull request #195 from rgorosito/fix-loss-subscriptions
Fix: Subscriptions can't be read if arrive when waiting for response of ping or publish
2021-06-22 13:29:11 -04:00
xdylanm
a2bdbacb17 format 2021-05-30 16:16:14 -07:00
xdylanm
b007806ac1 Fixed length calculation when payload exceeds 128bytes 2021-05-30 14:13:57 -07:00
Ricardo Gorosito
ab78b291b2 more c formatting fixing 2021-05-25 13:23:51 -03:00
Ricardo Gorosito
986c1b405c fix c formatting 2021-05-25 13:16:43 -03:00
Ricardo Gorosito
4cb40f87d2 stop loosing messages 2021-05-23 15:52:35 -03:00
Brent Rubell
e524b5f3ae
Merge pull request #194 from brentru/add-kat-public-funct
Add setKeepAliveInterval() setter
2021-05-18 17:04:56 -04:00
brentru
c5cdfc9564 clang format 2021-05-18 16:54:01 -04:00
brentru
86aff92f0b fixup 2021-05-18 16:43:33 -04:00
brentru
a1e64c039e settable keepalive interval 2021-05-18 16:41:26 -04:00
Brent Rubell
ea2fb46de3
Merge pull request #193 from xdylanm/send_rcv_long_packets
Support publishing and receiving large messages.
2021-05-14 12:01:40 -04:00
xdylanm
db003d30d1 Addressing comments from PR #193 with merge from PR#166. Clang-format, version ticked to 2.3.0, replaced duplicate topicOffsetFromLength with packetAdditionalLen. 2021-05-14 08:09:07 -07:00
xdylanm
c782cc454f Merge branch 'master' of https://github.com/adafruit/Adafruit_MQTT_Library into send_rcv_long_packets
Pull in changes from flavio-fernandes
2021-05-14 07:47:38 -07:00
Brent Rubell
ca12d21eb2
Merge pull request #166 from flavio-fernandes/protect.buffer
Adafruit_MQTT::publishPacket: Protect against memory corruption.
2021-05-14 09:22:29 -04:00
xdylanm
93a35d1a3e fix typo 2021-05-12 21:54:08 -07:00
xdylanm
2ea8a0b3fc Support publishing and receiving large messages. Send multiple 250 byte packets (ref PR#113) for larger messages. Correctly identify topic start position for >127byte messages (ref PR#166). Resolves issue #102 2021-05-12 21:31:22 -07:00
Flaviof
ce30d2a146
Merge branch 'master' into protect.buffer 2021-05-12 21:25:20 -04:00
Flavio Fernandes
fff701ee62 Bump to 2.2.0 2021-05-12 21:18:33 -04:00
Brent Rubell
e8922f60a9
Merge pull request #192 from brentru/add-esp32-esp32s2-example
Add usage example for ESP32 SSL/TLS
2021-05-12 17:36:47 -04:00
brentru
1a3ff727a3 add .skip files for 8266/zero, this is an esp32 example! 2021-05-12 17:33:16 -04:00
brentru
2adac525e1 bump version 2021-05-12 17:30:17 -04:00
brentru
813585f72b add example for esp32s2/esp32 2021-05-12 17:26:26 -04:00
dlizotte-uwo
7b2061ad85 Missing space after // comment 2021-04-04 17:07:59 -04:00
Dan Lizotte
4f76558624 MQTT with QOS 1 or 2 forbids a packet ID of 0. 2021-04-02 18:09:40 -04:00
Flavio Fernandes
d6f07ed4e4 Adafruit_MQTT::publishPacket: Protect against memory corruption.
Avoid memory corruption from happening when data payload provided
in Adafruit_MQTT::publishPacket is greater than MAXBUFFERSIZE.

In order to do that, a helper function is being added to calculate
how much space is available for the payload after subtracting what
is used as the header.

Pull request https://github.com/adafruit/Adafruit_MQTT_Library/pull/166
Fixes https://github.com/adafruit/Adafruit_MQTT_Library/issues/109
Fixes https://github.com/adafruit/Adafruit_MQTT_Library/issues/122

Signed-off-by: Flavio Fernandes <flavio@flaviof.com>
2021-04-02 12:14:39 -04:00
Brent Rubell
f63a37b6fb
Merge pull request #170 from Fapiko/better-ping-handling
Better ping handling
2020-11-06 10:39:12 -05:00
Lucas Jandrew
f3f5c18c6d Reformatted with clang-format 2020-11-06 10:20:52 -05:00
Lucas Jandrew
c952333c40 Removing commented out debug statements 2020-11-05 13:06:12 -05:00
Lucas Jandrew
7e0a1573ea Merge master to resolve conflicts 2020-11-05 13:04:17 -05:00
Lucas Jandrew
1e72fa7c4a
Bump version number for release 2020-11-05 12:55:09 -05:00
Dylan Herrada
c7f716cf5d
Bump to 2.0.0 2020-09-30 15:21:46 -04:00
Brent Rubell
381c3e576c
Merge pull request #171 from frederikheld/fix/typo_in_comment
Fixes typo in comment
2020-09-23 11:49:07 -04:00
Brent Rubell
569c83af8c
Merge pull request #179 from cmumford/fix-protected
Corrected client public/private methods.
2020-09-23 11:48:02 -04:00
Brent Rubell
2f572776c1
Merge pull request #184 from mlwinters/master
Added support for the MKR1010
2020-09-23 11:43:33 -04:00
Morgan
f1f515651f
Moved defined(ARDUINO_SAMD_MKR1010) to line 25
Move "defined(ARDUINO_SAMD_MKR1010)" from lilne 24 to line 25
2020-09-23 10:27:06 +01:00
Morgan
4b360f2522
Added support for the MKR1010
Added support for the Arduino MKR1010
2020-09-23 09:51:56 +01:00
Brent Rubell
4a409caf16
Merge branch 'master' into better-ping-handling 2020-08-04 10:38:19 -04:00
Brent Rubell
956242a28b
Bump library.properties version for release 2020-07-07 10:19:42 -04:00
Justin Cooper
bf7ce5af91
Latest Adafruit IO SSL Fingerprint 2020-07-06 15:53:19 -05:00
Chris Mumford
c5599bfabc Corrected client public/private methods.
Switched the following four methods from public to
protected in Adafruit_MQTT_Client and Adafruit_MQTT_FONA:

1. connectServer
2. disconnectServer
3. readPacket
4. sendPacket

When public (previously) they could be called by users
of this library, which is not the intent of this API.
2020-07-03 10:43:51 -07:00
dherrada
8db0c2787d
Merge pull request #177 from adafruit/actionci
Moved to actions, no doxygen
2020-06-26 10:40:14 -04:00
dherrada
148cfb3591
Moved to actions, no doxygen 2020-06-25 09:56:46 -04:00
Frederik Held
ba9a03a41d Fixes typo in comment 2020-02-29 00:21:45 +01:00
Lucas Jandrew
9bf437e61c Fix missing handleSubscriptionPacket from code formatting. 2020-02-24 01:41:00 -05:00
Lucas Jandrew
adb717f7dd Removing remainder of whitespace differences 2020-02-24 01:28:48 -05:00
Lucas Jandrew
3ba30fe64b Revert code formatting changes by CLion 2020-02-24 01:22:58 -05:00
Lucas Jandrew
8e8245d734 Removed code from my previous PR 2020-02-24 01:11:43 -05:00
Lucas Jandrew
44199a17ee Fixes issue with client erroneously reading a byte from 0 length packets 2020-02-24 01:10:09 -05:00
Lucas Jandrew
0b8488d9bf Uncommented code to handle subscription packets... 2020-02-23 22:44:35 -05:00
Lucas Jandrew
5ea3e3ba72 Handle publish packets when they're not the expected packet type 2020-02-23 22:42:43 -05:00
Lucas Jandrew
259cba0837 Adds retain support for publishing messages 2020-02-19 19:15:27 -05:00
brentrubell
3693eb8800
Merge pull request #156 from brentru/patch-esp-bsp-2-5-1
Fixing example for ESP8266 (BSP 2.5.1)
2019-05-20 15:01:58 -04:00
brentru
4caf05cf8a bump version for release 2019-05-20 15:01:47 -04:00
brentru
d5aa959525 adding ssl fingerprint verification to comply with bearssl in esp8266 bsp 2.5.1 2019-05-20 11:20:45 -04:00
brentrubell
bfdece49f7
Merge pull request #155 from brentru/add-airlift-example
Update AirLift Example
2019-05-02 10:16:12 -04:00
brentru
3c71f1c560 remove print for pins 2019-04-19 16:36:14 -04:00
brentru
4a709ffc28 update for a master airlift example 2019-04-19 16:32:14 -04:00
brentrubell
4b328500e2
Merge pull request #153 from brentru/add-pyportal-mqtts
Add PyPortal MQTTS Example
2019-04-18 12:10:02 -04:00
brentru
2b3892a4b6 bump to v1.0.0 2019-04-17 20:37:20 -04:00
brentru
727dc9f986 fix travis 2019-04-16 14:18:06 -04:00
brentru
c1e0ecd30e bump to 0.3.0 for release! 2019-04-16 13:51:36 -04:00
brentru
5dd4f68de7 add skip file 2019-04-16 13:45:01 -04:00
brentru
610514e83b add PyPortal MQTTS Example 2019-04-16 13:44:28 -04:00
brentrubell
0687c95333
Merge pull request #152 from brentru/fixup-readme
Update README TravisCI link, add samd51 to compatibility table
2019-04-16 12:07:10 -04:00
brentru
03ac17773f update readme travis and add samd51 to compat table 2019-04-16 11:56:18 -04:00
brentrubell
b7838a3278
Merge pull request #148 from ACE1046/master
Message type check in readSubscription (#147)
2019-04-16 11:49:27 -04:00
brentrubell
9eb37efa1c
bumping library for compilation on m4 targets (#151) 2019-04-16 11:48:54 -04:00
Anton ACE
417d342078
Message type check in readSubscription (#147)
Added message type checking in readSubscription
https://github.com/adafruit/Adafruit_MQTT_Library/issues/147
2019-03-14 17:54:06 +03:00
brentrubell
493a1cf590
remove CC3000 from keywords.txt (#145) 2019-02-19 16:56:07 -05:00
brentrubell
12479b2592
fix sha1 fingerprint for io.adafruit.com (#143)
* fix sha1 fingerprint for io.adafruit.com

* bump library version

* add terminating character
2019-02-19 16:36:34 -05:00
brentrubell
07c9ae7153
Bump version for new release. 2018-08-28 10:54:44 -04:00
Limor "Ladyada" Fried
0786de66b6
Merge pull request #130 from brentru/mqtt-subscription-increase
increase mqtt sub. limit for non 32u4/328p devices
2018-08-27 13:17:19 -07:00
brentru
8780dd5491 increase mqtt sub. limit for non 32u4/328p devices 2018-08-27 14:51:29 -04:00
brentrubell
61f75246f4 Library Fixes (#127)
* Removing warnings

* handle reading zero-length packets

* updating travis to exclude archived libraries, addin Arduino WiFi101 lib instead

* adding esp lib to travis

* restrict builds to current hardware platforms

* travis, esp8266: skip esp target for non-esp-based examples

* adding test skips for zero, fixing fingerprint error

bump library version
2018-08-20 09:56:40 -04:00
Limor "Ladyada" Fried
a4e1ee0133
Merge pull request #110 from abachman/abachman-update-IO-tls-fingerprint
update IO TLS fingerprint
2018-02-05 17:17:02 -05:00
Adam Bachman
c25d31f813
update IO TLS fingerprint 2018-02-05 17:08:40 -05:00
ladyada
7fffb1e769 bump fix for winc1500 2018-01-29 14:37:03 -05:00
ladyada
f0e97737f7 update to wifi101 library 2018-01-29 14:27:41 -05:00
ladyada
5b25a96d1b typo 2018-01-10 00:27:24 -05:00
ladyada
13854f6487 bump! 2018-01-10 00:26:31 -05:00
Limor "Ladyada" Fried
a3f09cfa4c
Merge pull request #107 from jerryneedell/patch-1
Update Adafruit_MQTT_Client.cpp to remove use of min()
2018-01-06 19:46:31 -05:00
jerryneedell
2d384b9697
Update Adafruit_MQTT_Client.cpp to remove use of min()
see https://github.com/adafruit/Adafruit_MQTT_Library/issues/106  for discussion of link failures after upgrading esp8266 community library  BSP to version 2.4.0  --  removing the use of min() allows for successful compilation,link and execution.

Replace call to min() with ternary statement to accomplish same action.
2018-01-06 17:52:40 -05:00
Limor "Ladyada" Fried
974f4b8713
Merge pull request #95 from fpistm/min_STL
Fix min() usage when using STL C++
2017-11-21 10:34:32 -05:00
Frederic.Pillon
ce9bcab0b4 Fix min() usage when using STL C++
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
2017-10-20 16:15:05 +02:00
Limor "Ladyada" Fried
786ca3fdb2 Merge pull request #87 from per1234/fix-keywords
Use correct separator in keywords.txt
2017-09-02 13:01:03 -04:00
per1234
564f34a84b Use correct separator in keywords.txt
The Arduino IDE requires the use of a tab separator between the name and identifier. Without this tab the keyword is not highlighted.

Reference: https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#keywords
2017-09-02 07:56:17 -07:00
Justin Cooper
8a8c0b1228 Update Adafruit IO SSL Fingerprint 2017-08-15 09:31:22 -05:00
Justin Cooper
1e3fb01778 Remove reference to cc3000 2017-08-10 09:28:15 -05:00
Justin Cooper
f5bb4e4e97 Remove references to cc3000 2017-08-10 09:27:05 -05:00
Todd Treece
47ebe871e4 Merge pull request #65 from adafruit/io_callbacks
Adafruit IO MQTT Callbacks
2017-02-15 11:39:30 -05:00
Todd Treece
08a4d8bdee Merge branch 'master' of github.com:adafruit/Adafruit_MQTT_Library into io_callbacks 2017-02-15 10:47:41 -05:00
Todd Treece
c5233ead52 remove cc3k from supported boards 2016-12-29 16:09:17 -05:00
Todd Treece
3553074d45 bump lib to 0.17.0 2016-12-29 16:09:07 -05:00
Todd Treece
c50f42c563 bump version define in Adafruit_MQTT.h 2016-11-21 14:11:52 -05:00
Todd Treece
ac37a6e132 bump version in library.properties 2016-11-21 14:11:06 -05:00
Todd Treece
ae18e8a6c4 add platform ifdef for subscription length limit 2016-11-20 19:57:21 -05:00
Todd Treece
ceeedc2b3c bump to 0.16.1 2016-08-15 17:11:54 -04:00
Todd Treece
faad8998f6 simplify feeds in examples 2016-08-15 15:25:08 -04:00
Limor "Ladyada" Fried
5e46585e25 Merge pull request #53 from adafruit/progmem_fixes
Remove FlashStringHelper Methods
2016-08-15 13:19:05 -04:00
Todd Treece
e0c88d8d2c remove progmem from examples 2016-08-15 13:06:17 -04:00
Todd Treece
b61d7e7024 remove flashstring helper constructors & methods 2016-08-15 13:05:51 -04:00
Todd Treece
c56e1b935f bump to v0.16.0 2016-08-10 11:27:37 -04:00
ladyada
bdd08d443e fix esp, remove debug 2016-08-09 21:37:07 -04:00
ladyada
7886b7243c simplify Client 2016-08-09 21:12:49 -04:00
ladyada
665395482e https://github.com/adafruit/Adafruit_MQTT_Library/pull/29 2016-08-09 20:15:22 -04:00
ladyada
f8ec7fc08f simplify and no progmem on yun demo 2016-08-09 20:12:22 -04:00
ladyada
fe3aebdad8 fix winc1500 demo, works great with feather 2016-08-09 17:00:01 -04:00
ladyada
4b57d0ddcb remove progmem support (only FONA really needed it and its OK) 2016-08-09 15:52:08 -04:00
ladyada
f009049016 working fona example without progmem 2016-08-09 14:27:30 -04:00
ladyada
5f3a1e2322 drop progmem support 2016-08-09 14:21:46 -04:00
ladyada
cba09e7499 add a demo too! 2016-08-08 20:13:27 -04:00
Todd Treece
ae113941a1 add version constants & bump version in library.properties 2016-08-05 14:59:40 -04:00
Todd Treece
49eda71d17 fix io callback scope issue 2016-07-29 15:06:54 -04:00
Todd Treece
0bfffa1571 access callback_io via ->* 2016-07-29 10:54:30 -04:00
Todd Treece
ad83c09d20 add new sub callback typedef for io wrapper 2016-07-29 09:27:00 -04:00
Todd Treece
f793f8d2d0 allow unauthenticated connections to mqtt brokers 2016-07-13 15:57:00 -04:00
ladyada
9b23f6747e make sure we get all 32 bits printed out 2016-07-13 13:52:23 -04:00
ladyada
5df5ea2f1c cast for size_t! 2016-07-13 13:33:15 -04:00
ladyada
ecc300d7a3 Merge branch 'master' of github.com:adafruit/Adafruit_MQTT_Library 2016-07-12 15:20:55 -04:00
ladyada
dfc670deeb fix for long packets on cc3k 2016-07-12 15:20:43 -04:00
Todd Treece
b2a534ede9 add standard connect function to time example 2016-07-12 14:07:27 -04:00
Todd Treece
2d03ae9c89 bump to 0.14.2 2016-07-11 15:59:43 -04:00
Todd Treece
029eee956c add io time interval example 2016-07-11 15:57:55 -04:00
Todd Treece
f7bd3e31a6 bump to 0.14.1 2016-07-11 15:37:14 -04:00
Todd Treece
63d86e2ca2 parse local time in callback example 2016-07-08 18:27:01 -04:00
ladyada
82873050b6 add error output on dropped packets, and mqtt w/callbacks example 2016-07-08 17:16:07 -04:00
ladyada
4269d87492 callback support 2016-07-08 17:01:40 -04:00
ladyada
9ed79466b2 shorter suback timeout 2016-07-08 15:21:47 -04:00
ladyada
bb240928e8 fix for extra float zero-terminator https://github.com/adafruit/Adafruit_MQTT_Library/issues/35 2016-07-06 18:37:11 -04:00
ladyada
ad6ae59725 fixiy https://github.com/adafruit/Adafruit_MQTT_Library/issues/46 2016-07-06 18:34:56 -04:00
ladyada
5d133c3852 updated publish half for long packet sendings! 2016-07-06 18:32:21 -04:00
ladyada
b26b4eb32c max receivable MQTT packet is 16 bits (tested with 300 byte packet receive)
also fixd buffer printer to have 16 bit length
2016-07-06 17:20:02 -04:00
Todd Treece
121aa3865c bump to v0.13.3 2016-07-06 13:45:29 -04:00
Todd Treece
a7ae377720 add adafruit io error subscription example 2016-07-06 13:45:10 -04:00
Todd Treece
737fa4b39d fix version in lib properties 2016-07-05 17:09:00 -04:00
Todd Treece
c8253d2128 bump to 0.13.2 2016-07-05 17:08:39 -04:00
Todd Treece
7d138efc1a add delay to secure example publish loop 2016-07-05 16:16:46 -04:00
Todd Treece
c19e155bac add adafruit io ssl/tls example for esp8266 2016-07-05 16:12:06 -04:00
Todd Treece
ac4674536c bump version to 0.13.0 2016-06-30 12:43:28 -04:00
Limor "Ladyada" Fried
a05f1d9fd4 Merge pull request #47 from adafruit/connack_custom_codes
Add Adafruit IO Custom MQTT CONNACK Return Codes
2016-06-29 18:06:36 -04:00
Todd Treece
39f1169559 increase connect timeout to allow for 5 second ban response 2016-06-28 14:53:08 -04:00
Todd Treece
d7f85c1ff2 remove extra space in travis config 2016-06-28 11:39:01 -04:00
Todd Treece
7943f903e7 add winc lib to travis install 2016-06-28 11:31:14 -04:00
Todd Treece
b116845dd4 skip tests for new examples 2016-06-28 09:24:57 -04:00
Todd Treece
af8ec63ce6 add new mqtt error codes for connack 2016-06-24 17:02:57 -04:00
ladyada
1d0feb282b fixed FONA 2016-06-07 22:43:49 -04:00
ladyada
58f5d0cda6 also fixed 'multiplier overflow' bug - YAY COMPILERS 2016-06-07 22:39:07 -04:00
ladyada
7e888e576a fix for readpacket 2016-06-07 22:17:05 -04:00
Limor "Ladyada" Fried
a85fc90df0 Merge pull request #42 from AHitchcock/master
Master
2016-05-30 12:39:26 -04:00
Anne Hitchcock
de48c73e74 Added MKR1000 board to list to define the dtostrf function
Adding the MKR1000 board to the list of boards for which the dtostrf
function should be defined here (for lack of a better way to determine
if the function is available elsewhere).
2016-05-29 09:03:04 -07:00
Anne Hitchcock
e015dbbcda dtostrf for other SAMD boards
Changed preprocessor directive to create the dtostrf function whenever
it is not defined in order to support more SAMD boards (including
MKR1000) without adding more board defines to the ifdef.
2016-05-29 08:39:09 -07:00
Tony DiCola
e21fe8b3b6 Add GitHub issue template 2016-05-27 13:45:42 -07:00
Tony DiCola
877401f1a0 Add GitHub pull request template 2016-05-27 13:40:32 -07:00
Limor "Ladyada" Fried
a66f64da88 Merge pull request #40 from jmue/fix/indeterminate_variables
properly initialize variables
2016-05-20 14:47:42 -04:00
Jens Mueller
9ccd463fb9 properly initialize variables 2016-05-20 20:41:16 +02:00
ladyada
ab1a0938b9 smarter packet reading makes for faster publish-handling! also, retries subscriptions 3 times 2016-05-18 15:23:41 -04:00
Andy Doro
7068ef5afe update compatibility matrix in README 2016-04-12 14:50:13 -04:00
ladyada
8f595cdb95 properly process packets until you get the one you want (prep for callback w/good retain support) 2016-02-08 23:36:25 -05:00
ladyada
79dd62468f add support for reading retained subscriptions (only works for one subscription since the pub packet gets eaten otherwise 2016-02-01 18:42:28 -05:00
Limor "Ladyada" Fried
19b581c2b7 Merge pull request #28 from stuthedew/Arbitrary_data_publish
Arbitrary data publish
2016-02-01 15:09:20 -05:00
Stuart Feichtinger
581aac3207 Fixed README Typo 2016-02-01 12:33:33 -06:00
Stuart Feichtinger
de426a886f Clean up 2016-02-01 11:08:00 -06:00
Stuart Feichtinger
2eae3ec309 Revert "Removed .Travis file"
This reverts commit 7909e5a811.
2016-02-01 11:06:35 -06:00
Stuart Feichtinger
0be4b1c2bd Merge branch 'master' into Arbitrary_data_publish 2016-01-31 21:27:40 -06:00
Stuart Feichtinger
75d014ccf7 Added unsigned short python symbol 2016-01-31 21:20:26 -06:00
Stuart Feichtinger
97a6f57e93 Merge branch 'Arbitrary_data_publish' 2016-01-31 21:02:30 -06:00
Stuart Feichtinger
7909e5a811 Removed .Travis file 2016-01-31 21:01:53 -06:00
Stuart Feichtinger
b710c19c6d Tweak 2016-01-31 20:45:29 -06:00
Stuart Feichtinger
b2d7333706 Clean up 2016-01-31 20:41:00 -06:00
Stuart Feichtinger
17213ccecc Added more info 2016-01-31 20:39:09 -06:00
Stuart Feichtinger
c7ab1a53c5 Tweak 2016-01-31 20:36:11 -06:00
Stuart Feichtinger
93ff9b448a Fixed link 2016-01-31 20:35:01 -06:00
Stuart Feichtinger
3432e7a8d6 Fixed link 2016-01-31 20:33:28 -06:00
Stuart Feichtinger
8e8b3bf845 Fixed link 2016-01-31 20:30:39 -06:00
Stuart Feichtinger
2b5d484488 Renamed files 2016-01-31 20:29:48 -06:00
Stuart Feichtinger
9c163c288d Tweaks 2016-01-31 20:24:07 -06:00
Stuart Feichtinger
7c0420c552 Fixed typo 2016-01-31 20:17:55 -06:00
Stuart Feichtinger
d0099f9abd Added image 2016-01-31 20:17:21 -06:00
Stuart Feichtinger
1f165f8ea0 Added flow diagram 2016-01-31 20:14:01 -06:00
Stuart Feichtinger
26ea45051e Ignore all bit torrent sync files 2016-01-31 20:13:48 -06:00
Stuart Feichtinger
80d89be695 Tweaks 2016-01-31 19:46:14 -06:00
Stuart Feichtinger
02417cfdc0 Tweak 2016-01-31 19:41:20 -06:00
Stuart Feichtinger
cbf40df659 Tweak 2016-01-31 19:39:10 -06:00
Stuart Feichtinger
0806239bc5 Tweak 2016-01-31 19:38:39 -06:00
Stuart Feichtinger
2ef3dc8761 Tweak 2016-01-31 19:38:22 -06:00
Stuart Feichtinger
09d2d00901 Tweak 2016-01-31 19:37:49 -06:00
Stuart Feichtinger
6697b96495 Tweaks 2016-01-31 19:37:12 -06:00
Stuart Feichtinger
93a86299b0 Tweak 2016-01-31 19:36:36 -06:00
Stuart Feichtinger
4ef9fdf877 Tweaks 2016-01-31 19:35:02 -06:00
Stuart Feichtinger
002c1d272c Tweaks 2016-01-31 19:32:56 -06:00
Stuart Feichtinger
899104a8ac Tweaks 2016-01-31 19:29:35 -06:00
Stuart Feichtinger
184e0e1386 Tweak 2016-01-31 19:28:10 -06:00
Stuart Feichtinger
fd00721e22 Tweaks 2016-01-31 19:27:23 -06:00
Stuart Feichtinger
95aedbda18 Tweaks 2016-01-31 19:24:29 -06:00
Stuart Feichtinger
992c051c66 Tweaks 2016-01-31 19:23:01 -06:00
Stuart Feichtinger
713fdac029 Tweaks 2016-01-31 19:22:40 -06:00
Stuart Feichtinger
8caa2dcfd6 More writing 2016-01-31 19:15:46 -06:00
Stuart Feichtinger
b002526690 Tweak 2016-01-31 19:03:23 -06:00
Stuart Feichtinger
ee1440903a More work 2016-01-31 19:02:57 -06:00
Stuart Feichtinger
c0add9d638 Formatting 2016-01-31 19:00:38 -06:00
Stuart Feichtinger
d764c3e1a4 Tweaks 2016-01-31 18:59:14 -06:00
Stuart Feichtinger
60ff208e80 More work 2016-01-31 18:57:43 -06:00
Stuart Feichtinger
f84dd400ed Clean up 2016-01-31 18:57:31 -06:00
Stuart Feichtinger
bdf3e427cc Tweaks 2016-01-31 18:43:14 -06:00
Stuart Feichtinger
d02cf88a9a Clean up 2016-01-31 18:43:02 -06:00
Stuart Feichtinger
8e84287150 Added more command line arguments 2016-01-31 18:30:00 -06:00
Stuart Feichtinger
2fc3db20c7 Repurposed Adafruit example 2016-01-31 18:29:50 -06:00
Stuart Feichtinger
1b885c7fb5 Python subscriber 2016-01-31 17:48:34 -06:00
Stuart Feichtinger
23a4673565 Documented payload 2016-01-31 17:24:00 -06:00
Stuart Feichtinger
ef573ee2b2 Added Readme 2016-01-31 16:19:38 -06:00
Stuart Feichtinger
fd9cfda563 Added arbitrary buffer example 2016-01-31 16:19:30 -06:00
Stuart Feichtinger
fb8795c055 Added requirements file for pip install 2016-01-31 16:19:14 -06:00
Stuart Feichtinger
8a40f42958 Updated comment 2016-01-31 16:18:45 -06:00
Stuart Feichtinger
9e8efabec4 Ignore bit torrent sync files 2016-01-31 16:18:02 -06:00
Stuart Feichtinger
cc9a3a1161 Ignore bit torrent sync files 2016-01-31 16:17:35 -06:00
Stuart Feichtinger
0d8c841af5 Clean up duplicate functions 2016-01-31 15:33:58 -06:00
Stuart Feichtinger
e41683e195 Merge remote-tracking branch 'adafruit/master' 2016-01-31 14:59:32 -06:00
ladyada
922f87e3cc bump 2016-01-21 20:21:10 -05:00
ladyada
8c8ba766c3 atwinc demo 2016-01-20 18:24:23 -05:00
Stuart Feichtinger
78d714f97a Merge branch 'buffer_publish' 2016-01-19 16:18:12 -06:00
Stuart Feichtinger
4ec8c49caf Merge branch 'Clean_up' into buffer_publish 2016-01-19 16:17:27 -06:00
Stuart Feichtinger
e65f9b446e Deleted extraneous code 2016-01-19 16:17:15 -06:00
Stuart Feichtinger
67092945e6 Removed redundant code 2016-01-19 16:10:03 -06:00
Stuart Feichtinger
cda4a5d7d4 Working
MUST include empty void “MQTT_connect” function declaration  or will
crash with 2 publish feeds on ESP8266
2016-01-19 16:01:20 -06:00
Stuart Feichtinger
2876ac43dd Added generic buffer
Crashes when publishing to more than one stream
2016-01-17 19:27:20 -06:00
ladyada
e77be5b9ac added ssl option & details 2016-01-16 11:51:12 -05:00
ladyada
f8518bc6bd more delays - works with feather fona nicely 2016-01-10 21:17:26 -05:00
ladyada
72b3ce172e verify PUBACK for QoS 1 2016-01-10 02:21:54 -05:00
Limor "Ladyada" Fried
d58783e3a1 Merge pull request #21 from malachib/master
Fixing reversal of topic/packet identifier (topic should come first)
2016-01-10 02:00:58 -05:00
ladyada
2a40fafe4c handy example on how to parse subs, and have 2 subs 2016-01-10 01:25:12 -05:00
ladyada
db47f8f3b3 remove unnecessary ping(), delay() & added WDT reset for connection failures 2016-01-10 01:03:59 -05:00
Kevin Townsend
9fb6fbf1e8 Updated WICED Feather macro 2016-01-05 16:11:27 +09:00
Limor "Ladyada" Fried
32201ceabb Merge pull request #25 from petersimonsson/keywords
Add a keywords.txt file
2016-01-03 10:40:33 -05:00
Peter Simonsson
ce4e26f229 Add a keywords.txt file 2016-01-03 14:30:26 +01:00
Limor "Ladyada" Fried
fed051a4b4 Merge pull request #22 from karlduino/master
Yun example: change one Serial -> Console
2015-12-24 23:14:02 -05:00
Karlduino
71496c977f Yun example: change one Serial -> Console 2015-12-24 22:00:13 -06:00
Malachi Burke
f36ec4cf85 Fixing reversal of topic/packet identifier (topic should come first) 2015-12-14 20:10:00 -08:00
ladyada
0f9627c73c only kick watchdog if its enabled 2015-12-10 14:46:40 -05:00
Kevin Townsend
a339810b75 Disabled dtostrf on STM32F2 2015-12-03 11:56:31 +01:00
Kevin Townsend
bb7cdb5059 Added ARDUINO_STM32F2_FEATHER check 2015-12-03 11:49:10 +01:00
Tony DiCola
936c6b25e1 Work around mqtt_esp8266 example compile failure on Arduino 1.6.6 2015-11-20 20:52:49 -08:00
ladyada
0f2fd58345 right - fixed dtostrf for anything else 2015-11-11 12:50:23 -05:00
ladyada
dbc989ac1d essentially, add Zero support by shimming in the missing functions 2015-11-11 10:22:26 -05:00
Todd Treece
03c1a2d07b bump lib version to v0.12.0 2015-10-22 14:39:28 -04:00
Todd Treece
88e28f517d update readme todos 2015-10-22 14:37:44 -04:00
Todd Treece
97afcf92c5 Merge pull request #10 from adafruit/3.1.1
MQTT 3.1.1 Support
2015-10-22 14:36:35 -04:00
Todd Treece
4dc6f1e486 adds ping to the end of the main loop in examples 2015-10-22 14:13:19 -04:00
ladyada
7c2b558ee9 ping flushes input, tries multiple times 2015-10-22 13:03:55 -04:00
Todd Treece
2aed9ae737 remove defaults from mqtt constructors 2015-10-22 10:09:16 -04:00
Todd Treece
371b16bfab Merge branch 'master' of github.com:adafruit/Adafruit_MQTT_Library into 3.1.1 2015-10-21 15:38:55 -04:00
ladyada
632c2f2cfe back compatible ping() 2015-10-21 00:15:27 -04:00
ladyada
286245ab62 compile fix & #ifdef the MQTT version 2015-10-21 00:05:46 -04:00
ladyada
15713af468 switch to MQTT 3.1.1 by default 2015-10-20 23:51:03 -04:00
Todd Treece
9fa0c54fce Merge pull request #14 from tapiralec/master
add ethernet example
2015-10-06 08:02:35 -04:00
Alec Moore
63229fbd12 skip tests on due and esp8266 2015-10-06 00:56:32 -05:00
Alec Moore
9e6b151997 triggering travis ci 2015-10-05 20:13:18 -05:00
Alec Moore
4ccf81e923 fixed up and functional
removed unnecessary "}", and removed getFreeRam() which was dependent on "utility/debug.h"
2015-10-05 19:47:54 -05:00
Alec Moore
478e2324d5 removed username/aio key 2015-10-05 19:17:32 -05:00
Todd Treece
9a11d83cf5 removes client id from all example sketches 2015-10-05 15:26:02 -04:00
Todd Treece
f1499b484a rearrange child classes to match new constructor 2015-10-05 14:57:44 -04:00
Todd Treece
b48265faed support anon auth & generated client id in constructor 2015-10-05 14:54:37 -04:00
Todd Treece
aa6db4d5f5 rename all subclass diconnect() to disconnectServer() 2015-10-05 14:18:49 -04:00
Todd Treece
4ab72be73e adds disconnect & disconnectPacket functions 2015-10-05 14:18:31 -04:00
Todd Treece
16da46aece renames disconnect to disconnectServer and adds disconnectPacket 2015-10-05 14:07:49 -04:00
Todd Treece
f9ae6f3146 comment out suback for now 2015-10-05 13:46:38 -04:00
Todd Treece
921c132f92 adds missing will payload to connect packet 2015-10-05 13:41:14 -04:00
Todd Treece
e70e2367b6 only check for suback if using MQTT 3.1.1 or higher 2015-10-05 11:32:10 -04:00
Todd Treece
b9c9a28883 adds packet ids for publish QoS > 0
also adds packet id counter so packet ids aren't
static values. this is a step in the direction
of QoS 1 support
2015-10-05 11:21:05 -04:00
Todd Treece
765a2feaff remove broken while loop from ping() 2015-10-05 10:38:09 -04:00
Todd Treece
0fbd734646 adds unsubscribe & unsubscribePacket functions 2015-10-05 10:33:33 -04:00
Todd Treece
476d1115e6 adds will() function 2015-10-05 09:50:57 -04:00
Todd Treece
e9e154ab52 adds will support to the connect packet 2015-10-05 09:45:27 -04:00
Todd Treece
e60be3e972 adds unsubscribePacket method def 2015-10-05 09:27:31 -04:00
Todd Treece
b2a198af5a adds unsubscribe() method def 2015-10-05 09:26:06 -04:00
Todd Treece
b9a04c8be5 adds will() method def 2015-10-05 09:25:07 -04:00
Todd Treece
78d733aee3 define the missing MQTT control packets 2015-10-05 09:14:17 -04:00
Alec Moore
57885554f9 adding ethernet example
It took more than a couple minutes to get some sample code going with ethernet, so I figured it would benefit everyone if there was at least one example out there.
2015-10-03 00:59:51 -05:00
Todd Treece
dfbe6a1e11 remove client id from esp example 2015-10-02 17:22:31 -04:00
Todd Treece
4f3466bdbf comment out MQTT_DEBUG 2015-10-02 17:13:35 -04:00
Todd Treece
913cce41af adds zero byte client id option for 3.1.1 2015-10-02 17:10:16 -04:00
Todd Treece
a3925bdb6b adds MQTT 3.1.1 connect packet changes 2015-10-02 10:45:00 -04:00
Todd Treece
ea779b1daa allow user to define MQTT KEEPALIVE in sketch 2015-10-02 10:43:11 -04:00
Todd Treece
a0520d3265 default to MQTT_PROTOCOL_LEVEL 4 2015-10-01 12:00:56 -04:00
Todd Treece
798e37b4de bump lib to v0.11.1 2015-09-08 12:16:02 -04:00
Todd Treece
5ac8cda53d update mqtt client id to be __TIME__ + AIO_USERNAME in examples 2015-09-08 12:15:21 -04:00
Tony DiCola
e41e843d67 Add Travis ignore for Due using Yun test. 2015-08-31 15:11:28 -07:00
Tony DiCola
e42f3094e5 Add Arduino Yun example. 2015-08-31 14:36:06 -07:00
69 changed files with 3478 additions and 917 deletions

46
.github/ISSUE_TEMPLATE.md vendored Normal file
View file

@ -0,0 +1,46 @@
Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:
- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use
the forums at http://forums.adafruit.com to ask questions and troubleshoot why
something isn't working as expected. In many cases the problem is a common issue
that you will more quickly receive help from the forum community. GitHub issues
are meant for known defects in the code. If you don't know if there is a defect
in the code then start with troubleshooting on the forum first.
- **If following a tutorial or guide be sure you didn't miss a step.** Carefully
check all of the steps and commands to run have been followed. Consult the
forum if you're unsure or have questions about steps in a guide/tutorial.
- **For Arduino projects check these very common issues to ensure they don't apply**:
- For uploading sketches or communicating with the board make sure you're using
a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes
very hard to tell the difference between a data and charge cable! Try using the
cable with other devices or swapping to another cable to confirm it is not
the problem.
- **Be sure you are supplying adequate power to the board.** Check the specs of
your board and plug in an external power supply. In many cases just
plugging a board into your computer is not enough to power it and other
peripherals.
- **Double check all soldering joints and connections.** Flakey connections
cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints.
- **Ensure you are using an official Arduino or Adafruit board.** We can't
guarantee a clone board will have the same functionality and work as expected
with this code and don't support them.
If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:
- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE**
- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO
VERSION HERE**
- List the steps to reproduce the problem below (if possible attach a sketch or
copy the sketch code in too): **LIST REPRO STEPS BELOW**

26
.github/PULL_REQUEST_TEMPLATE.md vendored Normal file
View file

@ -0,0 +1,26 @@
Thank you for creating a pull request to contribute to Adafruit's GitHub code!
Before you open the request please review the following guidelines and tips to
help it be more easily integrated:
- **Describe the scope of your change--i.e. what the change does and what parts
of the code were modified.** This will help us understand any risks of integrating
the code.
- **Describe any known limitations with your change.** For example if the change
doesn't apply to a supported platform of the library please mention it.
- **Please run any tests or examples that can exercise your modified code.** We
strive to not break users of the code and running tests/examples helps with this
process.
Thank you again for contributing! We will try to test and integrate the change
as soon as we can, but be aware we have many GitHub repositories to manage and
can't immediately respond to every request. There is no need to bump or check in
on a pull request (it will clutter the discussion of the request).
Also don't be worried if the request is closed or not integrated--sometimes the
priorities of Adafruit's GitHub code (education, ease of use) might not match the
priorities of the pull request. Don't fret, the open source community thrives on
forks and GitHub makes it easy to keep your changes in a forked repo.
After reviewing the guidelines above you can delete this text from the pull request.

26
.github/workflows/githubci.yml vendored Normal file
View file

@ -0,0 +1,26 @@
name: Arduino Library CI
on: [pull_request, push, repository_dispatch]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- uses: actions/checkout@v3
- uses: actions/checkout@v3
with:
repository: adafruit/ci-arduino
path: ci
- name: pre-install
run: bash ci/actions_install.sh
- name: test platforms
run: python3 ci/build_platform.py esp8266 zero
- name: clang
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.bts*

View file

@ -1,12 +0,0 @@
language: c
sudo: false
before_install:
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
install:
- arduino --install-library "Adafruit SleepyDog Library,Adafruit FONA Library,Adafruit CC3000 Library"
script:
- build_main_platforms
notifications:
email:
on_success: change
on_failure: change

File diff suppressed because it is too large Load diff

View file

@ -9,8 +9,8 @@
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@ -22,45 +22,84 @@
#ifndef _ADAFRUIT_MQTT_H_
#define _ADAFRUIT_MQTT_H_
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "Arduino.h"
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_STM32_FEATHER)
#define strncpy_P(dest, src, len) strncpy((dest), (src), (len))
#define strncasecmp_P(f1, f2, len) strncasecmp((f1), (f2), (len))
#endif
#define ADAFRUIT_MQTT_VERSION_MAJOR 0
#define ADAFRUIT_MQTT_VERSION_MINOR 17
#define ADAFRUIT_MQTT_VERSION_PATCH 0
// Uncomment/comment to turn on/off debug output messages.
//#define MQTT_DEBUG
// #define MQTT_DEBUG
// Uncomment/comment to turn on/off error output messages.
#define MQTT_ERROR
// Set where debug messages will be printed.
#define DEBUG_PRINTER Serial
// If using something like Zero or Due, change the above to SerialUSB
// Define actual debug output functions when necessary.
#ifdef MQTT_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#define DEBUG_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
#define DEBUG_PRINT(...) \
{ DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) \
{ DEBUG_PRINTER.println(__VA_ARGS__); }
#define DEBUG_PRINTBUFFER(buffer, len) \
{ printBuffer(buffer, len); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#define DEBUG_PRINTBUFFER(buffer, len) {}
#define DEBUG_PRINT(...) \
{}
#define DEBUG_PRINTLN(...) \
{}
#define DEBUG_PRINTBUFFER(buffer, len) \
{}
#endif
#define MQTT_PROTOCOL_LEVEL 3
#ifdef MQTT_ERROR
#define ERROR_PRINT(...) \
{ DEBUG_PRINTER.print(__VA_ARGS__); }
#define ERROR_PRINTLN(...) \
{ DEBUG_PRINTER.println(__VA_ARGS__); }
#define ERROR_PRINTBUFFER(buffer, len) \
{ printBuffer(buffer, len); }
#else
#define ERROR_PRINT(...) \
{}
#define ERROR_PRINTLN(...) \
{}
#define ERROR_PRINTBUFFER(buffer, len) \
{}
#endif
#define MQTT_CTRL_CONNECT 0x01
#define MQTT_CTRL_CONNECTACK 0x02
#define MQTT_CTRL_PUBLISH 0x03
#define MQTT_CTRL_SUBSCRIBE 0x08
#define MQTT_CTRL_SUBACK 0x09
#define MQTT_CTRL_PINGREQ 0x0C
#define MQTT_CTRL_PINGRESP 0x0D
// Use 3 (MQTT 3.0) or 4 (MQTT 3.1.1)
#define MQTT_PROTOCOL_LEVEL 4
#define MQTT_CTRL_CONNECT 0x1
#define MQTT_CTRL_CONNECTACK 0x2
#define MQTT_CTRL_PUBLISH 0x3
#define MQTT_CTRL_PUBACK 0x4
#define MQTT_CTRL_PUBREC 0x5
#define MQTT_CTRL_PUBREL 0x6
#define MQTT_CTRL_PUBCOMP 0x7
#define MQTT_CTRL_SUBSCRIBE 0x8
#define MQTT_CTRL_SUBACK 0x9
#define MQTT_CTRL_UNSUBSCRIBE 0xA
#define MQTT_CTRL_UNSUBACK 0xB
#define MQTT_CTRL_PINGREQ 0xC
#define MQTT_CTRL_PINGRESP 0xD
#define MQTT_CTRL_DISCONNECT 0xE
#define MQTT_QOS_1 0x1
#define MQTT_QOS_0 0x0
#define CONNECT_TIMEOUT_MS 3000
#define CONNECT_TIMEOUT_MS 6000
#define PUBLISH_TIMEOUT_MS 500
#define PING_TIMEOUT_MS 500
#define SUBACK_TIMEOUT_MS 500
// Adjust as necessary, in seconds. Default to 5 minutes.
#define MQTT_CONN_KEEPALIVE 300
@ -68,34 +107,56 @@
// Largest full packet we're able to send.
// Need to be able to store at least ~90 chars for a connect packet with full
// 23 char client ID.
#define MAXBUFFERSIZE (125)
// Future TODO: This should be replaced by the ability to dynamically allocate a
// buffer as needed.
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || \
defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD)
#define MAXBUFFERSIZE (512)
#else
#define MAXBUFFERSIZE (150)
#endif
#define MQTT_CONN_USERNAMEFLAG 0x80
#define MQTT_CONN_PASSWORDFLAG 0x40
#define MQTT_CONN_WILLRETAIN 0x20
#define MQTT_CONN_WILLQOS 0x08
#define MQTT_CONN_WILLFLAG 0x04
#define MQTT_CONN_CLEANSESSION 0x02
// how many subscriptions we want to be able to
// track
#define MAXSUBSCRIPTIONS 5
#define MQTT_CONN_WILLRETAIN 0x20
#define MQTT_CONN_WILLQOS_1 0x08
#define MQTT_CONN_WILLQOS_2 0x18
#define MQTT_CONN_WILLFLAG 0x04
#define MQTT_CONN_CLEANSESSION 0x02
// how much data we save in a subscription object
// eg max-subscription-payload-size
// and how many subscriptions we want to be able to track.
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__)
#define MAXSUBSCRIPTIONS 5
#define SUBSCRIPTIONDATALEN 20
#else
#define MAXSUBSCRIPTIONS 15
#define SUBSCRIPTIONDATALEN MAXBUFFERSIZE
#endif
class AdafruitIO_MQTT; // forward decl
extern void printBuffer(uint8_t *buffer, uint8_t len);
// Function pointer that returns an int
typedef void (*SubscribeCallbackUInt32Type)(uint32_t);
// returns a double
typedef void (*SubscribeCallbackDoubleType)(double);
// returns a chunk of raw data
typedef void (*SubscribeCallbackBufferType)(char *str, uint16_t len);
// returns an io data wrapper instance
typedef void (AdafruitIO_MQTT::*SubscribeCallbackIOType)(char *str,
uint16_t len);
class Adafruit_MQTT_Subscribe; // forward decl
extern void printBuffer(uint8_t *buffer, uint16_t len);
class Adafruit_MQTT_Subscribe; // forward decl
class Adafruit_MQTT {
public:
public:
Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
const char *user, const char *pass);
Adafruit_MQTT(const __FlashStringHelper *server, uint16_t port, const __FlashStringHelper *cid,
const __FlashStringHelper *user, const __FlashStringHelper *pass);
Adafruit_MQTT(const char *server, uint16_t port, const char *user = "",
const char *pass = "");
virtual ~Adafruit_MQTT() {}
// Connect to the MQTT server. Returns 0 on success, otherwise an error code
@ -110,28 +171,35 @@ class Adafruit_MQTT {
// Use connectErrorString() to get a printable string version of the
// error.
int8_t connect();
int8_t connect(const char *user, const char *pass);
// Return a printable string version of the error code returned by
// connect(). This returns a __FlashStringHelper*, which points to a
// string stored in flash, but can be directly passed to e.g.
// Serial.println without any further processing.
const __FlashStringHelper* connectErrorString(int8_t code);
const __FlashStringHelper *connectErrorString(int8_t code);
// Disconnect from the MQTT server. Returns true if disconnected, false
// otherwise.
virtual bool disconnect() = 0; // Subclasses need to fill this in!
// Sends MQTT disconnect packet and calls disconnectServer()
bool disconnect();
// Return true if connected to the MQTT server, otherwise false.
virtual bool connected() = 0; // Subclasses need to fill this in!
virtual bool connected() = 0; // Subclasses need to fill this in!
// Set MQTT last will topic, payload, QOS, and retain. This needs
// to be called before connect() because it is sent as part of the
// connect control packet.
bool will(const char *topic, const char *payload, uint8_t qos = 0,
uint8_t retain = 0);
// Sets the KeepAlive Interval, in seconds.
bool setKeepAliveInterval(uint16_t keepAlive);
// Publish a message to a topic using the specified QoS level. Returns true
// if the message was published, false otherwise.
// The topic must be stored in PROGMEM. It can either be a
// char*, or a __FlashStringHelper* (the result of the F() macro).
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
bool publish(const __FlashStringHelper *topic, const char *payload, uint8_t qos = 0) {
return publish((const char *)topic, payload, qos);
}
bool publish(const char *topic, const char *payload, uint8_t qos = 0,
bool retain = false);
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
uint8_t qos = 0, bool retain = false);
// Add a subscription to receive messages for a topic. Returns true if the
// subscription could be added or was already present, false otherwise.
@ -139,31 +207,51 @@ class Adafruit_MQTT {
// is made is not currently supported.
bool subscribe(Adafruit_MQTT_Subscribe *sub);
// Unsubscribe from a previously subscribed MQTT topic.
bool unsubscribe(Adafruit_MQTT_Subscribe *sub);
// Check if any subscriptions have new messages. Will return a reference to
// an Adafruit_MQTT_Subscribe object which has a new message. Should be called
// in the sketch's loop function to ensure new messages are recevied. Note
// that subscribe should be called first for each topic that receives messages!
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0);
// an Adafruit_MQTT_Subscribe object which has a new message. Should be
// called in the sketch's loop function to ensure new messages are recevied.
// Note that subscribe should be called first for each topic that receives
// messages!
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0);
// Ping the server to ensure the connection is still alive. Returns true if
// successful, otherwise false.
bool ping(uint8_t t);
// Handle any data coming in for subscriptions
Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len);
protected:
// Execute a subscription packet's associated callback and mark as "read"
void processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub);
void processPackets(int16_t timeout);
// Ping the server to ensure the connection is still alive.
bool ping(uint8_t n = 1);
protected:
// Interface that subclasses need to implement:
// Connect to the server and return true if successful, false otherwise.
virtual bool connectServer() = 0;
// Disconnect from the MQTT server. Returns true if disconnected, false
// otherwise.
virtual bool disconnectServer() = 0; // Subclasses need to fill this in!
// Send data to the server specified by the buffer and length of data.
virtual bool sendPacket(uint8_t *buffer, uint8_t len) = 0;
virtual bool sendPacket(uint8_t *buffer, uint16_t len) = 0;
// Read MQTT packet from the server. Will read up to maxlen bytes and store
// the data in the provided buffer. Waits up to the specified timeout (in
// milliseconds) for data to be available. If checkForValidPubPacket is true
// then the received data is verified to make sure it's a complete packet.
virtual uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
bool checkForValidPubPacket = false) = 0;
// milliseconds) for data to be available.
virtual uint16_t readPacket(uint8_t *buffer, uint16_t maxlen,
int16_t timeout) = 0;
// Read a full packet, keeping note of the correct length
uint16_t readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16_t timeout);
// Properly process packets until you get to one you want
uint16_t processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype,
uint16_t timeout);
// Shared state that subclasses can use:
const char *servername;
@ -171,29 +259,46 @@ class Adafruit_MQTT {
const char *clientid;
const char *username;
const char *password;
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
const char *will_topic;
const char *will_payload;
uint8_t will_qos;
uint8_t will_retain;
uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
uint16_t packet_id_counter;
private:
private:
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];
void flushIncoming(uint16_t timeout);
// Functions to generate MQTT packets.
uint8_t connectPacket(uint8_t *packet);
uint8_t publishPacket(uint8_t *packet, const char *topic, const char *payload, uint8_t qos);
uint8_t disconnectPacket(uint8_t *packet);
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen = 0,
bool retain = false);
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
uint8_t pingPacket(uint8_t *packet);
uint8_t pubackPacket(uint8_t *packet, uint16_t packetid);
};
class Adafruit_MQTT_Publish {
public:
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const __FlashStringHelper *feed, uint8_t qos = 0);
public:
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed,
uint8_t qos = 0);
bool publish(const char *s);
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
// This might be ignored and a higher precision value sent.
bool publish(int32_t i);
bool publish(uint32_t i);
bool publish(const char *s, bool retain = false);
bool publish(
double f,
uint8_t precision =
2, // Precision controls the minimum number of digits after decimal.
// This might be ignored and a higher precision value sent.
bool retain = false);
bool publish(int32_t i, bool retain = false);
bool publish(uint32_t i, bool retain = false);
bool publish(uint8_t *b, uint16_t bLen, bool retain = false);
private:
Adafruit_MQTT *mqtt;
@ -202,11 +307,15 @@ private:
};
class Adafruit_MQTT_Subscribe {
public:
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const __FlashStringHelper *feedname, uint8_t q=0);
public:
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname,
uint8_t q = 0);
bool setCallback(void (*callback)(char *));
void setCallback(SubscribeCallbackUInt32Type callb);
void setCallback(SubscribeCallbackDoubleType callb);
void setCallback(SubscribeCallbackBufferType callb);
void setCallback(AdafruitIO_MQTT *io, SubscribeCallbackIOType callb);
void removeCallback(void);
const char *topic;
uint8_t qos;
@ -214,10 +323,19 @@ class Adafruit_MQTT_Subscribe {
uint8_t lastread[SUBSCRIPTIONDATALEN];
// Number valid bytes in lastread. Limited to SUBSCRIPTIONDATALEN-1 to
// ensure nul terminating lastread.
uint8_t datalen;
private:
uint16_t datalen;
SubscribeCallbackUInt32Type callback_uint32t;
SubscribeCallbackDoubleType callback_double;
SubscribeCallbackBufferType callback_buffer;
SubscribeCallbackIOType callback_io;
AdafruitIO_MQTT *io_mqtt;
bool new_message;
private:
Adafruit_MQTT *mqtt;
};
#endif
#endif

View file

@ -1,157 +0,0 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Adafruit Industries
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef _ADAFRUIT_MQTT_CC3000_H_
#define _ADAFRUIT_MQTT_CC3000_H_
#include <Adafruit_SleepyDog.h>
#include <Adafruit_CC3000.h>
#include "Adafruit_MQTT.h"
// delay in ms between calls of available()
#define MQTT_CC3000_INTERAVAILDELAY 10
// CC3000-specific version of the Adafruit_MQTT class.
// Note that this is defined as a header-only class to prevent issues with using
// the library on non-CC3000 platforms (since Arduino will include all .cpp files
// in the compilation of the library).
class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
public:
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, const char *server, uint16_t port,
const char *cid, const char *user, const char *pass):
Adafruit_MQTT(server, port, cid, user, pass),
cc3000(cc3k)
{}
bool connectServer() {
uint32_t ip = 0;
Watchdog.reset();
// look up IP address
if (serverip == 0) {
// Try looking up the website's IP address using CC3K's built in getHostByName
strcpy_P((char *)buffer, servername);
Serial.print((char *)buffer); Serial.print(F(" -> "));
uint8_t dnsretries = 5;
Watchdog.reset();
while (ip == 0) {
if (! cc3000->getHostByName((char *)buffer, &ip)) {
Serial.println(F("Couldn't resolve!"));
dnsretries--;
Watchdog.reset();
}
//Serial.println("OK"); Serial.println(ip, HEX);
if (!dnsretries) return false;
delay(500);
}
serverip = ip;
cc3000->printIPdotsRev(serverip);
Serial.println();
}
Watchdog.reset();
// connect to server
DEBUG_PRINTLN(F("Connecting to TCP"));
mqttclient = cc3000->connectTCP(serverip, portnum);
return mqttclient.connected();
}
bool disconnect() {
if (connected()) {
return (mqttclient.close() == 0);
}
else {
return true;
}
}
bool connected() {
return mqttclient.connected();
}
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
bool checkForValidPubPacket = false) {
/* Read data until either the connection is closed, or the idle timeout is reached. */
uint16_t len = 0;
int16_t t = timeout;
while (mqttclient.connected() && (timeout >= 0)) {
//DEBUG_PRINT('.');
while (mqttclient.available()) {
//DEBUG_PRINT('!');
char c = mqttclient.read();
timeout = t; // reset the timeout
buffer[len] = c;
//DEBUG_PRINTLN((uint8_t)c, HEX);
len++;
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read packet:\t"));
DEBUG_PRINTBUFFER(buffer, len);
return len;
}
// special case where we just one one publication packet at a time
if (checkForValidPubPacket) {
if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
// oooh a valid publish packet!
DEBUG_PRINT(F("Read PUBLISH packet:\t"));
DEBUG_PRINTBUFFER(buffer, len);
return len;
}
}
}
Watchdog.reset();
timeout -= MQTT_CC3000_INTERAVAILDELAY;
delay(MQTT_CC3000_INTERAVAILDELAY);
}
return len;
}
bool sendPacket(uint8_t *buffer, uint8_t len) {
if (mqttclient.connected()) {
uint16_t ret = mqttclient.write(buffer, len);
DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
if (ret != len) {
DEBUG_PRINTLN("Failed to send complete packet.")
return false;
}
} else {
DEBUG_PRINTLN(F("Connection failed!"));
return false;
}
return true;
}
private:
uint32_t serverip;
Adafruit_CC3000 *cc3000;
Adafruit_CC3000_Client mqttclient;
};
#endif

View file

@ -9,8 +9,8 @@
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@ -21,19 +21,20 @@
// SOFTWARE.
#include "Adafruit_MQTT_Client.h"
bool Adafruit_MQTT_Client::connectServer() {
// Grab server name from flash and copy to buffer for name resolution.
memset(buffer, 0, sizeof(buffer));
strcpy_P((char *)buffer, servername);
DEBUG_PRINT(F("Connecting to: ")); DEBUG_PRINTLN((char *)buffer);
strcpy((char *)buffer, servername);
DEBUG_PRINT(F("Connecting to: "));
DEBUG_PRINTLN((char *)buffer);
// Connect and check for success (0 result).
int r = client->connect((char *)buffer, portnum);
DEBUG_PRINT(F("Connect result: ")); DEBUG_PRINTLN(r);
DEBUG_PRINT(F("Connect result: "));
DEBUG_PRINTLN(r);
return r != 0;
}
bool Adafruit_MQTT_Client::disconnect() {
bool Adafruit_MQTT_Client::disconnectServer() {
// Stop connection if connected and return success (stop has no indication of
// failure).
if (client->connected()) {
@ -47,37 +48,32 @@ bool Adafruit_MQTT_Client::connected() {
return client->connected();
}
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen,
int16_t timeout,
bool checkForValidPubPacket) {
/* Read data until either the connection is closed, or the idle timeout is reached. */
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
int16_t timeout) {
/* Read data until either the connection is closed, or the idle timeout is
* reached. */
uint16_t len = 0;
int16_t t = timeout;
if (maxlen == 0) { // handle zero-length packets
return 0;
}
while (client->connected() && (timeout >= 0)) {
//DEBUG_PRINT('.');
// DEBUG_PRINT('.');
while (client->available()) {
//DEBUG_PRINT('!');
// DEBUG_PRINT('!');
char c = client->read();
timeout = t; // reset the timeout
timeout = t; // reset the timeout
buffer[len] = c;
//DEBUG_PRINTLN((uint8_t)c, HEX);
// DEBUG_PRINTLN((uint8_t)c, HEX);
len++;
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read packet:\t"));
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read data:\t"));
DEBUG_PRINTBUFFER(buffer, len);
return len;
}
// special case where we just one one publication packet at a time
if (checkForValidPubPacket) {
if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
// oooh a valid publish packet!
DEBUG_PRINT(F("Read PUBLISH packet:\t"));
DEBUG_PRINTBUFFER(buffer, len);
return len;
}
}
}
timeout -= MQTT_CLIENT_READINTERVAL_MS;
delay(MQTT_CLIENT_READINTERVAL_MS);
@ -85,17 +81,29 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen,
return len;
}
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint8_t len) {
if (client->connected()) {
uint16_t ret = client->write(buffer, len);
DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
if (ret != len) {
DEBUG_PRINTLN("Failed to send complete packet.")
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
uint16_t ret = 0;
uint16_t offset = 0;
while (len > 0) {
if (client->connected()) {
// send 250 bytes at most at a time, can adjust this later based on Client
uint16_t sendlen = len > 250 ? 250 : len;
// Serial.print("Sending: "); Serial.println(sendlen);
ret = client->write(buffer + offset, sendlen);
DEBUG_PRINT(F("Client sendPacket returned: "));
DEBUG_PRINTLN(ret);
len -= ret;
offset += ret;
if (ret != sendlen) {
DEBUG_PRINTLN("Failed to send packet.");
return false;
}
} else {
DEBUG_PRINTLN(F("Connection failed!"));
return false;
}
} else {
DEBUG_PRINTLN(F("Connection failed!"));
return false;
}
return true;
}

View file

@ -9,8 +9,8 @@
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@ -22,36 +22,36 @@
#ifndef _ADAFRUIT_MQTT_CLIENT_H_
#define _ADAFRUIT_MQTT_CLIENT_H_
#include "Client.h"
#include "Adafruit_MQTT.h"
#include "Client.h"
// How long to delay waiting for new data to be available in readPacket.
#define MQTT_CLIENT_READINTERVAL_MS 10
// MQTT client implementation for a generic Arduino Client interface. Can work
// with almost all Arduino network hardware like ethernet shield, wifi shield,
// and even other platforms like ESP8266.
class Adafruit_MQTT_Client : public Adafruit_MQTT {
public:
public:
Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port,
const char *cid, const char *user,
const char *pass):
Adafruit_MQTT(server, port, cid, user, pass),
client(client)
{}
const char *cid, const char *user, const char *pass)
: Adafruit_MQTT(server, port, cid, user, pass), client(client) {}
bool connectServer();
bool disconnect();
bool connected();
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
bool checkForValidPubPacket = false);
bool sendPacket(uint8_t *buffer, uint8_t len);
Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port,
const char *user = "", const char *pass = "")
: Adafruit_MQTT(server, port, user, pass), client(client) {}
private:
Client* client;
bool connected() override;
protected:
bool connectServer() override;
bool disconnectServer() override;
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen,
int16_t timeout) override;
bool sendPacket(uint8_t *buffer, uint16_t len) override;
private:
Client *client;
};
#endif

View file

@ -9,8 +9,8 @@
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@ -22,110 +22,107 @@
#ifndef _ADAFRUIT_MQTT_FONA_H_
#define _ADAFRUIT_MQTT_FONA_H_
#include <Adafruit_FONA.h>
#include "Adafruit_MQTT.h"
#include <Adafruit_FONA.h>
#define MQTT_FONA_INTERAVAILDELAY 100
#define MQTT_FONA_QUERYDELAY 500
// FONA-specific version of the Adafruit_MQTT class.
// Note that this is defined as a header-only class to prevent issues with using
// the library on non-FONA platforms (since Arduino will include all .cpp files
// in the compilation of the library).
class Adafruit_MQTT_FONA : public Adafruit_MQTT {
public:
public:
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
const char *cid, const char *user, const char *pass):
Adafruit_MQTT(server, port, cid, user, pass),
fona(f)
{}
const char *cid, const char *user, const char *pass)
: Adafruit_MQTT(server, port, cid, user, pass), fona(f) {}
bool connectServer() {
char server[40];
strncpy_P(server, servername, 40);
Watchdog.reset();
// connect to server
DEBUG_PRINTLN(F("Connecting to TCP"));
return fona->TCPconnect(server, portnum);
}
bool disconnect() {
return fona->TCPclose();
}
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
const char *user = "", const char *pass = "")
: Adafruit_MQTT(server, port, user, pass), fona(f) {}
bool connected() {
// Return true if connected, false if not connected.
return fona->TCPconnected();
}
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
bool checkForValidPubPacket = false) {
protected:
bool connectServer() override {
char server[40];
strncpy(server, servername, 40);
#ifdef ADAFRUIT_SLEEPYDOG_H
Watchdog.reset();
#endif
// connect to server
DEBUG_PRINTLN(F("Connecting to TCP"));
return fona->TCPconnect(server, portnum);
}
bool disconnectServer() override { return fona->TCPclose(); }
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen,
int16_t timeout) override {
uint8_t *buffp = buffer;
DEBUG_PRINTLN(F("Reading a packet.."));
DEBUG_PRINTLN(F("Reading data.."));
if (!fona->TCPconnected()) return 0;
if (!fona->TCPconnected())
return 0;
/* Read data until either the connection is closed, or the idle timeout is reached. */
/* Read data until either the connection is closed, or the idle timeout is
* reached. */
uint16_t len = 0;
int16_t t = timeout;
uint16_t avail;
while (fona->TCPconnected() && (timeout >= 0)) {
DEBUG_PRINT('.');
// DEBUG_PRINT('.');
while (avail = fona->TCPavailable()) {
DEBUG_PRINT('!');
// DEBUG_PRINT('!');
if (len + avail > maxlen) {
avail = maxlen - len;
if (avail == 0) return len;
avail = maxlen - len;
if (avail == 0)
return len;
}
// try to read the data into the end of the pointer
if (! fona->TCPread(buffp, avail)) return len;
if (!fona->TCPread(buffp, avail))
return len;
// read it! advance pointer
buffp += avail;
len += avail;
timeout = t; // reset the timeout
timeout = t; // reset the timeout
//DEBUG_PRINTLN((uint8_t)c, HEX);
// DEBUG_PRINTLN((uint8_t)c, HEX);
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read packet:\t"));
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read:\t"));
DEBUG_PRINTBUFFER(buffer, len);
return len;
return len;
}
// special case where we just one one publication packet at a time
if (checkForValidPubPacket) {
if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
// oooh a valid publish packet!
DEBUG_PRINT(F("Read PUBLISH packet:\t"));
DEBUG_PRINTBUFFER(buffer, len);
return len;
}
}
}
#ifdef ADAFRUIT_SLEEPYDOG_H
Watchdog.reset();
#endif
timeout -= MQTT_FONA_INTERAVAILDELAY;
timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the FONA for avail()
timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the
// FONA for avail()
delay(MQTT_FONA_INTERAVAILDELAY);
}
return len;
}
bool sendPacket(uint8_t *buffer, uint8_t len) {
bool sendPacket(uint8_t *buffer, uint16_t len) override {
DEBUG_PRINTLN(F("Writing packet"));
if (fona->TCPconnected()) {
boolean ret = fona->TCPsend((char *)buffer, len);
//DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
// DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
if (!ret) {
DEBUG_PRINTLN("Failed to send packet.")
DEBUG_PRINTLN("Failed to send packet.");
return false;
}
} else {
@ -135,10 +132,9 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
return true;
}
private:
private:
uint32_t serverip;
Adafruit_FONA *fona;
};
#endif

View file

@ -1,7 +1,7 @@
# Adafruit MQTT Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_MQTT_Library.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_MQTT_Library)
# Adafruit MQTT Library [![Build Status](https://github.com/adafruit/Adafruit_MQTT_Library/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_MQTT_Library/actions)
Arduino library for MQTT support, including access to Adafruit IO. Works with
the Adafruit CC3000, FONA, ESP8266 Arduino platforms, and anything that supports
the Adafruit FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
Arduino's Client interface (like Ethernet shield).
See included examples for how to use the library to access an MQTT service to
@ -11,22 +11,47 @@ spec but is intended to support enough for QoS 0 and 1 publishing.
Depends on the following other libraries depending on the target platform:
- [Adafruit SleepyDog](https://github.com/adafruit/Adafruit_SleepyDog), watchdog
library used by FONA and CC3000 code for reliability.
- [Adafruit CC3000](https://github.com/adafruit/Adafruit_CC3000_Library), required
for the CC3000 hardware.
library used by FONA code for reliability.
- [Adafruit FONA](https://github.com/adafruit/Adafruit_FONA_Library), required for
the FONA hardware.
Future todos:
- QoS > 0 for publication
- Subscription callbacks
- example with way more timeout/reconnection protection
- Wills
- remove watchdog
<!-- START COMPATIBILITY TABLE -->
## Compatibility
MCU | Tested Works | Doesn't Work | Not Tested | Notes
------------------ | :----------: | :----------: | :---------: | -----
Atmega328 @ 16MHz | | | X |
Atmega328 @ 12MHz | | | X |
Atmega32u4 @ 16MHz | | | X |
Atmega32u4 @ 8MHz | | | X |
ESP8266 | | | X |
Atmega2560 @ 16MHz | | | X |
ATSAM3X8E | | | X |
ATSAM21D | | | X |
ATSAMD51J20 | | | X |
ATtiny85 @ 16MHz | | | X |
ATtiny85 @ 8MHz | | | X |
Intel Curie @ 32MHz | | | X |
STM32F2 | | | X |
* ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini
* ATmega328 @ 12MHz : Adafruit Pro Trinket 3V
* ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0
* ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro
* ESP8266 : Adafruit Huzzah
* ATmega2560 @ 16MHz : Arduino Mega
* ATSAM3X8E : Arduino Due
* ATSAM21D : Arduino Zero, M0 Pro
* ATSAMD51J20: Adafruit PyPortal
* ATtiny85 @ 16MHz : Adafruit Trinket 5V
* ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V
<!-- END COMPATIBILITY TABLE -->

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,122 @@
/***************************************************
Adafruit MQTT Library ESP8266 Adafruit IO Anonymous Time Query
Must use the latest version of ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "network"
#define WLAN_PASS "password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 8883
WiFiClientSecure client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT);
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
// set timezone offset from UTC
int timeZone = -4; // UTC - 4 eastern daylight time (nyc)
int interval = 4; // trigger every X hours
int last_min = -1;
void timecallback(uint32_t current) {
// adjust to local time zone
current += (timeZone * 60 * 60);
int curr_hour = (current / 60 / 60) % 24;
int curr_min = (current / 60 ) % 60;
int curr_sec = (current) % 60;
Serial.print("Time: ");
Serial.print(curr_hour); Serial.print(':');
Serial.print(curr_min); Serial.print(':');
Serial.println(curr_sec);
// only trigger on minute change
if(curr_min != last_min) {
last_min = curr_min;
Serial.println("This will print out every minute!");
}
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.print(F("\nAdafruit IO anonymous Time Demo"));
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(" WiFi connected."));
timefeed.setCallback(timecallback);
mqtt.subscribe(&timefeed);
}
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// wait 10 seconds for subscription messages
// since we have no other tasks in this example.
mqtt.processPackets(10000);
// keep the connection alive
mqtt.ping();
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,155 @@
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
Error examples by Todd Treece for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "...your SSID..."
#define WLAN_PASS "...your password..."
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // 8883 for MQTTS
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/*************************** Error Reporting *********************************/
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/errors");
Adafruit_MQTT_Subscribe throttle = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/throttle");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff feed
mqtt.subscribe(&onoffbutton);
// Setup MQTT subscriptions for throttle & error messages
mqtt.subscribe(&throttle);
mqtt.subscribe(&errors);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got onoff: "));
Serial.println((char *)onoffbutton.lastread);
} else if(subscription == &errors) {
Serial.print(F("ERROR: "));
Serial.println((char *)errors.lastread);
} else if(subscription == &throttle) {
Serial.println((char *)throttle.lastread);
}
}
// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,183 @@
/****************************************************************
Adafruit MQTT Library, Adafruit IO SSL/TLS Example for AirLift
Must use the latest version of nina-fw from:
https://github.com/adafruit/nina-fw
Works great with Adafruit AirLift ESP32 Co-Processors!
--> https://www.adafruit.com/product/4201
--> https://www.adafruit.com/product/4116
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Brent Rubell for Adafruit Industries.
MIT license, all text above must be included in any redistribution
*******************************************************************/
#include <WiFiNINA.h>
#include <SPI.h>
// For AirLift Breakout/Wing/Shield: Configure the following to match the ESP32 Pins!
#if !defined(SPIWIFI_SS)
// Don't change the names of these #define's! they match the variant ones
#define SPIWIFI SPI
#define SPIWIFI_SS 11 // Chip select pin
#define SPIWIFI_ACK 10 // a.k.a BUSY or READY pin
#define ESP32_RESETN 9 // Reset pin
#define ESP32_GPIO0 -1 // Not connected
#define SET_PINS 1 // Pins were set using this IFNDEF
#endif
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "WLAN_SSID"
#define WLAN_PASS "WIFI_PASSWORD"
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
// Using port 8883 for MQTTS
#define AIO_SERVERPORT 8883
// Adafruit IO Account Configuration
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
/************ Global State (you don't need to change this!) ******************/
// WiFiSSLClient for SSL/TLS support
WiFiSSLClient client;
// Setup the MQTT client class by WLAN_PASSing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'test' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
/*************************** Sketch Code ************************************/
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
// if the AirLift's pins were defined above...
#ifdef SET_PINS
WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);
#endif
// check for the wifi module
while (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
delay(1000);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0")
{
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(WLAN_SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
do
{
status = WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(100); // wait until connected
} while (status != WL_CONNECTED);
Serial.println("Connected to wifi");
printWiFiStatus();
}
uint32_t x = 0;
void loop()
{
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Now we can publish stuff!
Serial.print(F("\nSending val "));
Serial.print(x);
Serial.print(F(" to test feed..."));
if (!test.publish(x++))
{
Serial.println(F("Failed"));
}
else
{
Serial.println(F("OK!"));
}
// wait a couple seconds to avoid rate limit
delay(2000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect()
{
int8_t ret;
// Stop if already connected.
if (mqtt.connected())
{
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0)
{ // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0)
{
// basically die and wait for WDT to reset me
while (1)
;
}
}
Serial.println("MQTT Connected!");
}
void printWiFiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,165 @@
/***********************************************************************
Adafruit MQTT Library ESP32 Adafruit IO SSL/TLS example
Use the latest version of the ESP32 Arduino Core:
https://github.com/espressif/arduino-esp32
Works great with Adafruit Huzzah32 Feather and Breakout Board:
https://www.adafruit.com/product/3405
https://www.adafruit.com/products/4172
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
Modified by Brent Rubell for Adafruit Industries
MIT license, all text above must be included in any redistribution
**********************************************************************/
#include <WiFi.h>
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "WLAN_SSID"
#define WLAN_PASS "WIFI_PASSWORD"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
// Using port 8883 for MQTTS
#define AIO_SERVERPORT 8883
// Adafruit IO Account Configuration
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
/************ Global State (you don't need to change this!) ******************/
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// io.adafruit.com root CA
const char* adafruitio_root_ca = \
"-----BEGIN CERTIFICATE-----\n"
"MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n"
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\n"
"MjAeFw0xNzExMDIxMjIzMzdaFw0yNzExMDIxMjIzMzdaMGAxCzAJBgNVBAYTAlVT\n"
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
"b20xHzAdBgNVBAMTFkdlb1RydXN0IFRMUyBSU0EgQ0EgRzEwggEiMA0GCSqGSIb3\n"
"DQEBAQUAA4IBDwAwggEKAoIBAQC+F+jsvikKy/65LWEx/TMkCDIuWegh1Ngwvm4Q\n"
"yISgP7oU5d79eoySG3vOhC3w/3jEMuipoH1fBtp7m0tTpsYbAhch4XA7rfuD6whU\n"
"gajeErLVxoiWMPkC/DnUvbgi74BJmdBiuGHQSd7LwsuXpTEGG9fYXcbTVN5SATYq\n"
"DfbexbYxTMwVJWoVb6lrBEgM3gBBqiiAiy800xu1Nq07JdCIQkBsNpFtZbIZhsDS\n"
"fzlGWP4wEmBQ3O67c+ZXkFr2DcrXBEtHam80Gp2SNhou2U5U7UesDL/xgLK6/0d7\n"
"6TnEVMSUVJkZ8VeZr+IUIlvoLrtjLbqugb0T3OYXW+CQU0kBAgMBAAGjggFAMIIB\n"
"PDAdBgNVHQ4EFgQUlE/UXYvkpOKmgP792PkA76O+AlcwHwYDVR0jBBgwFoAUTiJU\n"
"IBiV5uNu5g/6+rkS7QYXjzkwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsG\n"
"AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB\n"
"BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n"
"HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n"
"b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
"HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
"AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
"c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
"ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
"jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
"TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
"7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
"-----END CERTIFICATE-----\n";
/****************************** Feeds ***************************************/
// Setup a feed called 'test' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Set Adafruit IO's root CA
client.setCACert(adafruitio_root_ca);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Now we can publish stuff!
Serial.print(F("\nSending val "));
Serial.print(x);
Serial.print(F(" to test feed..."));
if (! test.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// wait a couple seconds to avoid rate limit
delay(2000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,140 @@
/***************************************************
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
Must use the latest version of ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
SSL/TLS additions by Todd Treece for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "WLAN_SSID"
#define WLAN_PASS "WIFI_PASSWORD"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
// Using port 8883 for MQTTS
#define AIO_SERVERPORT 8883
// Adafruit IO Account Configuration
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
/************ Global State (you don't need to change this!) ******************/
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// io.adafruit.com SHA1 fingerprint
/* WARNING - This value was last updated on 07/14/25 and may not be up-to-date!
* If security is a concern for your project, we strongly recommend users impacted by this moving
* to ESP32 which has certificate verification by storing root certs and having a
* chain-of-trust rather than doing individual certificate fingerprints.
*/
static const char *fingerprint PROGMEM = "47 D2 CB 14 DF 38 97 59 C6 65 1A 1F 3E 00 1E 53 CC A5 17 E0";
/****************************** Feeds ***************************************/
// Setup a feed called 'test' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// check the fingerprint of io.adafruit.com's SSL cert
client.setFingerprint(fingerprint);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Now we can publish stuff!
Serial.print(F("\nSending val "));
Serial.print(x);
Serial.print(F(" to test feed..."));
if (! test.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// wait a couple seconds to avoid rate limit
delay(2000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,122 @@
/***************************************************
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
Must use the latest version of ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
Time additions by Todd Treece for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "network"
#define WLAN_PASS "password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 8883
#define AIO_USERNAME "user"
#define AIO_KEY "key"
WiFiClientSecure client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
// set timezone offset from UTC
int timeZone = -4; // UTC - 4 eastern daylight time (nyc)
int interval = 4; // trigger every X hours
int hour = 0; // current hour
void timecallback(uint32_t current) {
// stash previous hour
int previous = hour;
// adjust to local time zone
current += (timeZone * 60 * 60);
// calculate current hour
hour = (current / 60 / 60) % 24;
// only trigger on interval
if((hour != previous) && (hour % interval) == 0) {
Serial.println("Run your code here");
}
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.print(F("Adafruit IO Time Demo"));
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(" WiFi connected."));
timefeed.setCallback(timecallback);
mqtt.subscribe(&timefeed);
}
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// wait 10 seconds for subscription messages
// since we have no other tasks in this example.
mqtt.processPackets(10000);
// keep the connection alive
mqtt.ping();
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,157 @@
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// the on off button feed turns this LED on/off
#define LED 2
// the slider feed sets the PWM output of this pin
#define PWMOUT 12
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "...your SSID..."
#define WLAN_PASS "...your password..."
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
pinMode(LED, OUTPUT);
pinMode(PWMOUT, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff & slider feed.
mqtt.subscribe(&onoffbutton);
mqtt.subscribe(&slider);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
// Check if its the onoff button feed
if (subscription == &onoffbutton) {
Serial.print(F("On-Off button: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
digitalWrite(LED, LOW);
}
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
digitalWrite(LED, HIGH);
}
}
// check if its the slider feed
if (subscription == &slider) {
Serial.print(F("Slider: "));
Serial.println((char *)slider.lastread);
uint16_t sliderval = atoi((char *)slider.lastread); // convert to a number
analogWrite(PWMOUT, sliderval);
}
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1,108 @@
# Adafruit MQTT Library Arbitrary Data Publish Example
This example illustrates publishing an arbitrary data packet using the Adafruit MQTT library to an MQTT feed which can then be parsed by the included python subscriber client. Possible usage cases include adding metadata (collection time, sensor info etc) to a datapoint.
![alt-text](https://raw.githubusercontent.com/stuthedew/Adafruit_MQTT_Library/Arbitrary_data_publish/examples/mqtt_arbitrary_data/python_subscriber/mqtt_figure.png "Arbitrary data flow diagram")
My motivation for this was wanting to be able to include metadata to a post.
Specifically, I was playing around with a [Teviso RD3024 radiation sensor](http://www.teviso.com/en/products/radiation-sensor-rd3024.htm), and a salvaged Americium radiation source from a smoke detector, at varying distances from the sensor. I wanted a way to associate the collection time, and distance between the source and sensor with the actual radiation reading itself.
---
## Installing and configuring Mosquitto broker (minimal working setup):
####_Installing on Raspberry Pi/Linux:_
```bash
sudo apt-get install mosquitto
cd /etc/mosquitto/
#See "Configuring Mosquitto Broker below"
```
####_Installing On a Mac:_
```bash
brew install mosquitto
cd /usr/local/etc/mosquitto
#See "Configuring Mosquitto Broker below"
```
---
####Configuring Mosquitto broker
```bash
sudo nano mosquitto.conf
```
Now we have to enable a password file to correctly interface with the Adafruit MQTT library. Scroll about two thirds of the way down until you see:
```bash
# -----------------------------------------------------------------
# Default authentication and topic access control
# -----------------------------------------------------------------
```
You should see `#password_file` about a paragraph after that.
Change
```bash
#password_file
```
To
```bash
password_file pwfile
```
Now `ctrl-x` to save and exit.
You're almost done! We just have to create and populate the password file we just configured. The default user info is:
* **Arduino Subscriber:**
* Username: TestUser
* Password: TestUser
* **Python Subscriber:**
* Username: TestPy
* Password: TestPy
```bash
touch pwfile #create the password file
mosquitto_passwd pwfile TestUser #Enter and confirm password when prompted
mosquitto_passwd pwfile TestPy #Enter and confirm password when prompted
```
####Running Mosquitto broker
Now run Mosquitto broker to allow Arduino publisher and Python subscriber to communicate
```bash
mosquitto
```
---
## Using Example Python Subscriber:
####Installing Python subscriber
Install dependencies if you haven't already
```bash
cd ../Adafruit_MQTT_Library/examples/mqtt_arbitrary_buffer/python_subscriber
pip install -r requirements.txt
```
####Installing Python subscriber
Run python script with default values and watch your parsed data print out.
```bash
python subscriber.py #Add -h flag to see modifiable options
```
Assuming that the Mosquitto broker is running in the background and the Adafruit_MQTT client (Arduino) is publishing, you should see the example data print out every 10 seconds.
```bash
MQTT: Connection successful
Connection successful
Subscribed to /feeds/arb_packet
Received char Array: "Hello!", val1: -4533, val2: 73102, val3: 3354...
Received char Array: "Hello!", val1: -4533, val2: 83611, val3: 3354...
Received char Array: "Hello!", val1: -4533, val2: 94115, val3: 3354...
```

View file

@ -0,0 +1,184 @@
/***************************************************
Adafruit MQTT Library Arbitrary Data Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Stuart Feichtinger
Modifed from the mqtt_esp8266 example written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "...your SSID..."
#define WLAN_PASS "...your password..."
/************************* Adafruit.io Setup *********************************/
#define ARB_SERVER "...host computer ip address..."
#define ARB_SERVERPORT 1883 // use 8883 for SSL
#define ARB_USERNAME "TestUser"
#define ARB_PW "TestUser"
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, ARB_SERVER, ARB_SERVERPORT, ARB_USERNAME, ARB_PW);
/****************************** Feeds ***************************************/
// Setup a feed called 'arb_packet' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
#define ARB_FEED "/feeds/arb_packet"
Adafruit_MQTT_Publish ap = Adafruit_MQTT_Publish(&mqtt, ARB_FEED);
// Arbitrary Payload
// Union allows for easier interaction of members in struct form with easy publishing
// of "raw" bytes
typedef union{
//Customize struct with whatever variables/types you like.
struct __attribute__((__packed__)){ // packed to eliminate padding for easier parsing.
char charAry[10];
int16_t val1;
unsigned long val2;
uint16_t val3;
}s;
uint8_t raw[sizeof(s)]; // For publishing
/*
// Alternate Option with anonymous struct, but manual byte count:
struct __attribute__((__packed__)){ // packed to eliminate padding for easier parsing.
char charAry[10]; // 10 x 1 byte = 10 bytes
int16_t val1; // 1 x 2 bytes = 2 bytes
unsigned long val2; // 1 x 4 bytes = 4 bytes
uint16_t val3; // 1 x 2 bytes = 2 bytes
-------------------
TOTAL = 18 bytes
};
uint8_t raw[18]; // For publishing
*/
} packet_t;
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: ")); Serial.println(WiFi.localIP());
}
packet_t arbPac;
const char strVal[] PROGMEM = "Hello!";
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
//Update arbitrary packet values
strcpy_P(arbPac.s.charAry, strVal);
arbPac.s.val1 = -4533;
arbPac.s.val2 = millis();
arbPac.s.val3 = 3354;
/*
// Alternate Union with anonymous struct
// (see union declaration above)
strcpy_P(arbPac.charAry, strVal);
arbPac.val1 = -4533;
arbPac.val2 = millis();
arbPac.val3 = 3354;
*/
if (! ap.publish(arbPac.raw, sizeof(packet_t)))
Serial.println(F("Publish Failed."));
else {
Serial.println(F("Publish Success!"));
delay(500);
}
delay(10000);
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print(F("Connecting to MQTT... "));
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println(F("Retrying MQTT connection in 5 seconds..."));
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println(F("MQTT Connected!"));
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 865 KiB

View file

@ -0,0 +1 @@
paho-mqtt>=1.1

View file

@ -0,0 +1,114 @@
'''MQTT subscriber for Adafruit MQTT library mqtt_arbitrary_buffer example'''
import paho.mqtt.client as mqtt
import argparse
import struct
import array
import sys
return_str =[
"Connection successful",
"incorrect protocol version",
"invalid client identifier",
"server unavailable",
"bad username or password",
"not authorised"
]
args = None
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
"""callback function on connect. Subscribes or exits depending on outcome"""
print("MQTT: "),
print(return_str[rc])
if(rc > 1):
print("Connection refused - " + return_str[rc])
sys.exit(rc)
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
else:
print(return_str[rc])
client.subscribe(args.topic)
print("Subscribed to {}".format(args.topic))
def on_disconnect(client, userdata, rc):
"""Callback for disconnect"""
if rc != 0:
print("Unexpected disconnection.")
client.reconnect()
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
try:
pMsg = parseMsg(msg.payload)
print("Received char Array: \"{}\", val1: {}, val2: {}, val3: {}...".format(pMsg[0], pMsg[1], pMsg[2], pMsg[3]))
except Exception as err:
print err
def argBegin():
parser = argparse.ArgumentParser(description='MQTT subscriber for Adafruit MQTT library mqtt_arbitrary_buffer example')
parser.add_argument("--host", default="localhost", help='mqtt host to connect to. Defaults to localhost.')
parser.add_argument("-p", "--port", default=1883, help='network port to connect to. Defaults to 1883.')
parser.add_argument("-t", "--topic", nargs='*', default="/feeds/arb_packet", help="mqtt topic to subscribe to. May be repeated multiple times.")
parser.add_argument("-u", "--username", default="testPy", help="provide a username (requires MQTT 3.1 broker)")
parser.add_argument("-P", "--password", default="testPy", help="provide a password (requires MQTT 3.1 broker)")
parser.add_argument("-k", "--keepalive", default=60, help="keep alive in seconds for this client. Defaults to 60.")
return parser.parse_args()
def parseMsg(payload):
"""Parses C struct from MQTT publisher Adafruit MQTT client to Python list"""
arr = array.array('B', payload) #convert python list to C-like array of unsigned char (B)
parsedStruct = struct.Struct('< 10s h L H') #define struct template (see below)
'''
Format of Struct from Adafruit MQTT client, Arduino, etc:
Adafruit MQTT client == Little endian (<)
Var NAME | C TYPE (python symbol) | size of member x bytes
-------------------------------------------------------------------
"charAry" | uchar (s) | 10s x 1 = 10 bytes
"val1" | int16 / short (h) | 1h x 2 = 2 bytes
"val2" | unsigned long (L) | 1L x 4 = 4 bytes
"val3" | uint16/unsigned short(H)| 1H x 2 = 2 bytes
------------------------------------------------------------------
Total packet size = | 18 bytes |
See Section 7.3.2 of Python struct module documentation for complete format list
https://docs.python.org/2/library/struct.html
'''
charAry, val1, val2, val3 = parsedStruct.unpack_from(arr) #convert byte array to formatted struct
charAry = charAry.rstrip(' \t\r\n\0') #remove trailing white space from buffer
return charAry, val1, val2, val3
def main():
"""Wait for incoming message published by Adafruit MQTT client"""
global args
args = argBegin()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(args.username, args.password)
client.connect(args.host, args.port, args.keepalive)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
if __name__ == '__main__':
sys.exit(main())

View file

@ -1,131 +0,0 @@
#include <Adafruit_SleepyDog.h>
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
//#define STATICIP
#define halt(s) { Serial.println(F( s )); while(1); }
uint16_t checkFirmwareVersion(void);
bool displayConnectionDetails(void);
extern Adafruit_CC3000 cc3000;
boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) {
Watchdog.reset();
// Check for compatible firmware
if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!");
// Delete any old connection data on the module
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) halt("Failed!");
#ifdef STATICIP
Serial.println(F("Setting static IP"));
uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19);
uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0);
uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1);
uint32_t dns = cc3000.IP2U32(8, 8, 4, 4);
if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
Serial.println(F("Failed to set static IP!"));
while(1);
}
#endif
// Attempt to connect to an access point
Serial.print(F("\nAttempting to connect to "));
Serial.print(wlan_ssid); Serial.print(F("..."));
Watchdog.disable();
// try 3 times
if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) {
return false;
}
Watchdog.enable(8000);
Serial.println(F("Connected!"));
uint8_t retries;
#ifndef STATICIP
/* Wait for DHCP to complete */
Serial.println(F("Requesting DHCP"));
retries = 10;
while (!cc3000.checkDHCP())
{
Watchdog.reset();
delay(1000);
retries--;
if (!retries) return false;
}
#endif
/* Display the IP address DNS, Gateway, etc. */
retries = 10;
while (! displayConnectionDetails()) {
Watchdog.reset();
delay(1000);
retries--;
if (!retries) return false;
}
Watchdog.reset();
return true;
}
/**************************************************************************/
/*!
@brief Tries to read the CC3000's internal firmware patch ID
*/
/**************************************************************************/
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
if(!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
version = major; version <<= 8; version |= minor;
}
return version;
}
/**************************************************************************/
/*!
@brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}

View file

@ -1,163 +0,0 @@
/***************************************************
Adafruit MQTT Library CC3000 Example
Designed specifically to work with the Adafruit WiFi products:
----> https://www.adafruit.com/products/1469
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_SleepyDog.h>
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_CC3000.h"
/*************************** CC3000 Pins ***********************************/
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins.
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "...your SSID..." // can't be longer than 32 characters!
#define WLAN_PASS "...your password..."
#define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
// WLAN_SEC_WPA or WLAN_SEC_WPA2
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
/************ Global State (you don't need to change this!) ******************/
// Setup the main CC3000 class, just like a normal CC3000 sketch.
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT);
// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details.
Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }
// CC3000connect is a helper function that sets up the CC3000 and connects to
// the WiFi network. See the cc3000helper.cpp tab above for the source!
boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security);
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
// Setup a feed called 'onoff' for subscribing to changes.
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
Serial.println(F("Adafruit MQTT demo"));
Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);
// Initialise the CC3000 module
Serial.print(F("\nInit the CC3000..."));
if (!cc3000.begin())
halt("Failed");
mqtt.subscribe(&onoffbutton);
while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Retrying WiFi"));
delay(1000);
}
}
uint32_t x=0;
void loop() {
// Make sure to reset watchdog every loop iteration!
Watchdog.reset();
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Try to ping the MQTT server
/*
if (! mqtt.ping(3) ) {
// MQTT pings failed, let's reconnect by forcing a watchdog reset.
Serial.println("Ping fail! Resetting...");
Watchdog.enable(8000);
delay(10000);
}
*/
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
}
}
// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
if (ret < 0)
CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); // y0w, lets connect to wifi again
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -4,8 +4,9 @@
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
@ -26,7 +27,7 @@
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
@ -34,33 +35,27 @@
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Setup a feed called 'onoff' for subscribing to changes.
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
@ -94,17 +89,11 @@ void loop() {
// function definition further below.
MQTT_connect();
// Try to ping the MQTT server
/*
if (! mqtt.ping(3) ) {
// MQTT pings failed, lets reconnect
Serial.println("Ping fail!");
}
*/
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
@ -121,7 +110,13 @@ void loop() {
Serial.println(F("OK!"));
}
delay(1000);
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
// Function to connect and reconnect as necessary to the MQTT server.
@ -136,11 +131,17 @@ void MQTT_connect() {
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,185 @@
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "network"
#define WLAN_PASS "password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "user"
#define AIO_KEY "key"
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'time' for subscribing to current time
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
// Setup a feed called 'slider' for subscribing to changes on the slider
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider", MQTT_QOS_1);
// Setup a feed called 'onoff' for subscribing to changes to the button
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff", MQTT_QOS_1);
/*************************** Sketch Code ************************************/
int sec;
int min;
int hour;
int timeZone = -4; // utc-4 eastern daylight time (nyc)
void timecallback(uint32_t current) {
// adjust to local time zone
current += (timeZone * 60 * 60);
// calculate current time
sec = current % 60;
current /= 60;
min = current % 60;
current /= 60;
hour = current % 24;
// print hour
if(hour == 0 || hour == 12)
Serial.print("12");
if(hour < 12)
Serial.print(hour);
else
Serial.print(hour - 12);
// print mins
Serial.print(":");
if(min < 10) Serial.print("0");
Serial.print(min);
// print seconds
Serial.print(":");
if(sec < 10) Serial.print("0");
Serial.print(sec);
if(hour < 12)
Serial.println(" am");
else
Serial.println(" pm");
}
void slidercallback(double x) {
Serial.print("Hey we're in a slider callback, the slider value is: ");
Serial.println(x);
}
void onoffcallback(char *data, uint16_t len) {
Serial.print("Hey we're in a onoff callback, the button value is: ");
Serial.println(data);
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
timefeed.setCallback(timecallback);
slider.setCallback(slidercallback);
onoffbutton.setCallback(onoffcallback);
// Setup MQTT subscription for time feed.
mqtt.subscribe(&timefeed);
mqtt.subscribe(&slider);
mqtt.subscribe(&onoffbutton);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets and callback em' busy subloop
// try to spend your time here:
mqtt.processPackets(10000);
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 10 seconds...");
mqtt.disconnect();
delay(10000); // wait 10 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

View file

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,128 @@
/***************************************************
Adafruit MQTT Library Ethernet Example
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Alec Moore
Derived from the code written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Ethernet.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <Dhcp.h>
/************************* Ethernet Client Setup *****************************/
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//Uncomment the following, and set to a valid ip if you don't have dhcp available.
//IPAddress iotIP (192, 168, 0, 42);
//Uncomment the following, and set to your preference if you don't have automatic dns.
//IPAddress dnsIP (8, 8, 8, 8);
//If you uncommented either of the above lines, make sure to change "Ethernet.begin(mac)" to "Ethernet.begin(mac, iotIP)" or "Ethernet.begin(mac, iotIP, dnsIP)"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
/************ Global State (you don't need to change this!) ******************/
//Set up the ethernet client
EthernetClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
Serial.println(F("Adafruit MQTT demo"));
// Initialise the Client
Serial.print(F("\nInit the Client..."));
Ethernet.begin(mac);
delay(1000); //give the ethernet a second to initialize
mqtt.subscribe(&onoffbutton);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
}
}
// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}

View file

@ -0,0 +1 @@

View file

@ -1,20 +1,27 @@
#include <Adafruit_SleepyDog.h>
#include "Adafruit_FONA.h"
#include <Adafruit_SleepyDog.h>
#include <SoftwareSerial.h>
#define halt(s) { Serial.println(F( s )); while(1); }
#define halt(s) \
{ \
Serial.println(F(s)); \
while (1) \
; \
}
extern Adafruit_FONA fona;
extern SoftwareSerial fonaSS;
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) {
Watchdog.enable(8000);
boolean FONAconnect(const __FlashStringHelper *apn,
const __FlashStringHelper *username,
const __FlashStringHelper *password) {
Watchdog.reset();
Serial.println(F("Initializing FONA....(May take 3 seconds)"));
fonaSS.begin(4800); // if you're using software serial
if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
if (!fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
Serial.println(F("Couldn't find FONA"));
return false;
}
@ -23,23 +30,28 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
Watchdog.reset();
Serial.println(F("Checking for network..."));
while (fona.getNetworkStatus() != 1) {
delay(500);
delay(500);
}
Watchdog.reset();
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset();
fona.setGPRSNetworkSettings(apn, username, password);
Serial.println(F("Disabling GPRS"));
fona.enableGPRS(false);
Watchdog.reset();
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset();
Serial.println(F("Enabling GPRS"));
if (!fona.enableGPRS(true)) {
Serial.println(F("Failed to turn GPRS on"));
Serial.println(F("Failed to turn GPRS on"));
return false;
}
Watchdog.reset();
return true;
}

View file

@ -25,8 +25,9 @@
/*************************** FONA Pins ***********************************/
#define FONA_RX 2
#define FONA_TX 3
// Default pins for Feather 32u4 FONA
#define FONA_RX 9
#define FONA_TX 8
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
@ -52,18 +53,8 @@ Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
/************ Global State (you don't need to change this!) ******************/
// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.
Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }
@ -76,12 +67,10 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Setup a feed called 'onoff' for subscribing to changes.
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/*************************** Sketch Code ************************************/
@ -92,22 +81,28 @@ uint8_t txfailures = 0;
void setup() {
while (!Serial);
// Watchdog is optional!
//Watchdog.enable(8000);
Serial.begin(115200);
Serial.println(F("Adafruit FONA MQTT demo"));
mqtt.subscribe(&onoffbutton);
Watchdog.reset();
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset();
// Initialise the FONA module
while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) {
halt("Retrying FONA");
Serial.println("Retrying FONA");
}
Serial.println(F("Connected to Cellular!"));
Watchdog.reset();
delay(3000); // wait a few seconds to stabilize connection
delay(5000); // wait a few seconds to stabilize connection
Watchdog.reset();
}
@ -117,40 +112,12 @@ void loop() {
// Make sure to reset watchdog every loop iteration!
Watchdog.reset();
// check if we're still connected
if (!fona.TCPconnected() || (txfailures >= MAXTXFAILURES)) {
Serial.println(F("Connecting to MQTT..."));
int8_t ret, retries = 5;
while (retries && (ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println(F("Retrying MQTT connection"));
retries--;
if (retries == 0) halt("Resetting system");
delay(5000);
}
Serial.println(F("MQTT Connected!"));
txfailures = 0;
}
// Try to ping the MQTT server
/*
if (! mqtt.ping(3) ) {
// MQTT pings failed, lets reconnect
Serial.println("Ping fail!");
}
*/
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
}
}
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
Watchdog.reset();
// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
@ -162,4 +129,41 @@ void loop() {
Serial.println(F("OK!"));
txfailures = 0;
}
Watchdog.reset();
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
}
}
// ping the server to keep the mqtt connection alive, only needed if we're not publishing
//if(! mqtt.ping()) {
// Serial.println(F("MQTT Ping failed."));
//}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}

View file

View file

View file

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,140 @@
/***************************************************
Adafruit MQTT Library Retain Flag Example
This example demonstrates use of the retain flag when publishing messages.
If retain is set, the MQTT broker will store the message. When a new
client subscribes to the topic, the retained message will be republished
to that client. This is useful for configuration messages and 'last known
good' values.
Written by Ben Willmore.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h> // use <WiFi.h> for ESP32
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "...your SSID..."
#define WLAN_PASS "...your password..."
/************************* Adafruit.io Setup *********************************/
#define MQTT_SERVER "...your MQTT server..."
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
#define MQTT_USERNAME "MQTT username"
#define MQTT_KEY "MQTT key"
#define DEVICE_ID "mqtt-retain-example"
/************ Global State (you don't need to change this!) ******************/
// Create a WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);
/****************************** Feeds ***************************************/
// Set up for publishing and subscribing to the same feed, for demonstration only
Adafruit_MQTT_Publish publish_feed = Adafruit_MQTT_Publish(&mqtt, DEVICE_ID "/temp");
Adafruit_MQTT_Subscribe subscribe_feed = Adafruit_MQTT_Subscribe(&mqtt, DEVICE_ID "/temp");
/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("MQTT retain flag demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Connect to MQTT broker, then publish a retained message and a
// non-retained message.
Serial.print("\nConnecting to MQTT broker...");
MQTT_connect();
Serial.println("connected");
Serial.println("Publishing messages while not subscribed");
publish_feed.publish("This message should be retained", true);
publish_feed.publish("This message should not be retained");
Serial.println("Disconnecting from MQTT broker\n");
mqtt.disconnect();
subscribe_feed.setCallback(subscribe_callback);
mqtt.subscribe(&subscribe_feed);
}
void subscribe_callback(char *data, uint16_t len) {
Serial.print("--> Message received: \"");
Serial.print(data);
Serial.println("\"\n");
}
void loop() {
// Connect to MQTT broker. We should receive the retained message only.
Serial.println("Connecting to broker. Expect to receive retained message:");
MQTT_connect();
mqtt.processPackets(1000);
Serial.println("Publishing non-retained message. Expect to receive it immediately:");
publish_feed.publish("This message should be received immediately but not retained");
mqtt.processPackets(1000);
Serial.println("Publishing retained message. Expect to receive it immediately and on re-subscribing:");
publish_feed.publish("This message should be received immediately AND retained", true);
mqtt.processPackets(10000);
Serial.println("Disconnecting from broker\n");
mqtt.disconnect();
delay(15000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
}

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,151 @@
/***************************************************
Adafruit MQTT Library WINC1500 Example
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi101.h>
/************************* WiFI Setup *****************************/
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC
char ssid[] = "yournetwork"; // your network SSID (name)
char pass[] = "yourpassword"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "adafruitiousername"
#define AIO_KEY "adafruitiokey"
/************ Global State (you don't need to change this!) ******************/
//Set up the wifi client
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/*************************** Sketch Code ************************************/
#define LEDPIN 13
void setup() {
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
while (!Serial);
Serial.begin(115200);
Serial.println(F("Adafruit MQTT demo for WINC1500"));
// Initialise the Client
Serial.print(F("\nInit the WiFi module..."));
// check for the presence of the breakout
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WINC1500 not present");
// don't continue:
while (true);
}
Serial.println("ATWINC OK!");
pinMode(LEDPIN, OUTPUT);
mqtt.subscribe(&onoffbutton);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) {
digitalWrite(LEDPIN, LOW);
}
if (0 == strcmp((char *)onoffbutton.lastread, "ON")) {
digitalWrite(LEDPIN, HIGH);
}
}
}
// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}

View file

View file

@ -0,0 +1 @@

View file

View file

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,116 @@
/***************************************************
Adafruit MQTT Library Arduino Yun Example
Make sure your Arduino Yun is connected to a WiFi access point which
has internet access. Also note this sketch uses the Console class
for debug output so make sure to connect to the Yun over WiFi and
open the serial monitor to see the console output.
Works great with the Arduino Yun:
----> https://www.adafruit.com/products/1498
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <Bridge.h>
#include <Console.h>
#include <BridgeClient.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
/************ Global State (you don't need to change this!) ******************/
// Create a BridgeClient instance to communicate using the Yun's bridge & Linux OS.
BridgeClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/*************************** Sketch Code ************************************/
void setup() {
Bridge.begin();
Console.begin();
Console.println(F("Adafruit MQTT demo"));
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&onoffbutton);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &onoffbutton) {
Console.print(F("Got: "));
Console.println((char *)onoffbutton.lastread);
}
}
// Now we can publish stuff!
Console.print(F("\nSending photocell val "));
Console.print(x);
Console.print("...");
if (! photocell.publish(x++)) {
Console.println(F("Failed"));
} else {
Console.println(F("OK!"));
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
Console.println(F("MQTT Ping failed."));
}
delay(1000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Console.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Console.println(mqtt.connectErrorString(ret));
Console.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Console.println("MQTT Connected!");
}

20
keywords.txt Normal file
View file

@ -0,0 +1,20 @@
Adafruit_MQTT KEYWORD1
Adafruit_MQTT_FONA KEYWORD1
Adafruit_MQTT_Client KEYWORD1
Adafruit_MQTT_Publish KEYWORD1
Adafruit_MQTT_Subscribe KEYWORD1
connect KEYWORD2
connectErrorString KEYWORD2
disconnect KEYWORD2
connected KEYWORD2
will KEYWORD2
publish KEYWORD2
subscribe KEYWORD2
unsubscribe KEYWORD2
readSubscription KEYWORD2
ping KEYWORD2
setCallback KEYWORD2
connectServer KEYWORD2
disconnectServer KEYWORD2
readPacket KEYWORD2
sendPacket KEYWORD2

View file

@ -1,9 +1,10 @@
name=Adafruit MQTT Library
version=0.10.0
version=2.6.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=MQTT library that supports the CC3000, FONA, ESP8266, and generic Arduino Client hardware.
sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware.
paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
category=Communication
url=https://github.com/adafruit/Adafruit_MQTT_Library
architectures=*
depends=Adafruit SleepyDog Library, Adafruit FONA Library, WiFi101