source: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/include/Adafruit/Adafruit_MQTT_CC3000.h@ 144

Last change on this file since 144 was 144, checked in by ertl-honda, 8 years ago

Milkcocoa用のMQTTライブラリの追加と,ESP8266用ライブラリの名称変更.

File size: 4.8 KB
Line 
1// The MIT License (MIT)
2//
3// Copyright (c) 2015 Adafruit Industries
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22#ifndef _ADAFRUIT_MQTT_CC3000_H_
23#define _ADAFRUIT_MQTT_CC3000_H_
24
25#include <Adafruit_SleepyDog.h>
26#include <Adafruit_CC3000.h>
27#include "Adafruit_MQTT.h"
28
29
30// delay in ms between calls of available()
31#define MQTT_CC3000_INTERAVAILDELAY 10
32
33
34// CC3000-specific version of the Adafruit_MQTT class.
35// Note that this is defined as a header-only class to prevent issues with using
36// the library on non-CC3000 platforms (since Arduino will include all .cpp files
37// in the compilation of the library).
38class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
39 public:
40 Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, const char *server, uint16_t port,
41 const char *cid, const char *user, const char *pass):
42 Adafruit_MQTT(server, port, cid, user, pass),
43 cc3000(cc3k)
44 {}
45
46 bool connectServer() {
47 uint32_t ip = 0;
48
49 Watchdog.reset();
50
51 // look up IP address
52 if (serverip == 0) {
53 // Try looking up the website's IP address using CC3K's built in getHostByName
54 strcpy_P((char *)buffer, servername);
55 Serial.print((char *)buffer); Serial.print(F(" -> "));
56 uint8_t dnsretries = 5;
57
58 Watchdog.reset();
59 while (ip == 0) {
60 if (! cc3000->getHostByName((char *)buffer, &ip)) {
61 Serial.println(F("Couldn't resolve!"));
62 dnsretries--;
63 Watchdog.reset();
64 }
65 //Serial.println("OK"); Serial.println(ip, HEX);
66 if (!dnsretries) return false;
67 delay(500);
68 }
69
70 serverip = ip;
71 cc3000->printIPdotsRev(serverip);
72 Serial.println();
73 }
74
75 Watchdog.reset();
76
77 // connect to server
78 DEBUG_PRINTLN(F("Connecting to TCP"));
79 mqttclient = cc3000->connectTCP(serverip, portnum);
80
81 return mqttclient.connected();
82 }
83
84 bool disconnect() {
85 if (connected()) {
86 return (mqttclient.close() == 0);
87 }
88 else {
89 return true;
90 }
91 }
92
93 bool connected() {
94 return mqttclient.connected();
95 }
96
97 uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
98 bool checkForValidPubPacket = false) {
99 /* Read data until either the connection is closed, or the idle timeout is reached. */
100 uint16_t len = 0;
101 int16_t t = timeout;
102
103 while (mqttclient.connected() && (timeout >= 0)) {
104 //DEBUG_PRINT('.');
105 while (mqttclient.available()) {
106 //DEBUG_PRINT('!');
107 char c = mqttclient.read();
108 timeout = t; // reset the timeout
109 buffer[len] = c;
110 //DEBUG_PRINTLN((uint8_t)c, HEX);
111 len++;
112 if (len == maxlen) { // we read all we want, bail
113 DEBUG_PRINT(F("Read packet:\t"));
114 DEBUG_PRINTBUFFER(buffer, len);
115 return len;
116 }
117
118 // special case where we just one one publication packet at a time
119 if (checkForValidPubPacket) {
120 if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
121 // oooh a valid publish packet!
122 DEBUG_PRINT(F("Read PUBLISH packet:\t"));
123 DEBUG_PRINTBUFFER(buffer, len);
124 return len;
125 }
126 }
127 }
128 Watchdog.reset();
129 timeout -= MQTT_CC3000_INTERAVAILDELAY;
130 delay(MQTT_CC3000_INTERAVAILDELAY);
131 }
132 return len;
133 }
134
135 bool sendPacket(uint8_t *buffer, uint8_t len) {
136 if (mqttclient.connected()) {
137 uint16_t ret = mqttclient.write(buffer, len);
138 DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
139 if (ret != len) {
140 DEBUG_PRINTLN("Failed to send complete packet.")
141 return false;
142 }
143 } else {
144 DEBUG_PRINTLN(F("Connection failed!"));
145 return false;
146 }
147 return true;
148 }
149
150 private:
151 uint32_t serverip;
152 Adafruit_CC3000 *cc3000;
153 Adafruit_CC3000_Client mqttclient;
154};
155
156
157#endif
Note: See TracBrowser for help on using the repository browser.