source: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Milkcocoa.h@ 180

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

ESP8266のライブラリの更新に伴う変更

File size: 3.9 KB
Line 
1/*
2The MIT License (MIT)
3
4Copyright (c) 2015 Technical Rockstars
5Copyright (C) 2015 Embedded and Real-Time Systems Laboratory
6 Graduate School of Information Science, Nagoya Univ., JAPAN
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24*/
25#ifndef _MILKCOCOA_H_
26#define _MILKCOCOA_H_
27
28#include "Client.h"
29#include "include/Adafruit/Adafruit_MQTT.h"
30#include "include/Adafruit/Adafruit_MQTT_Client.h"
31#include "include/aJson/aJson.h"
32
33#define MILKCOCOA_SUBSCRIBERS 8
34#define MILKCOCOA_CONNECT_DELAY_MS 5000
35
36#define MILKCOCOA_PUSH_ERRORNO -2
37#define MILKCOCOA_SEND_ERRORNO -2
38
39//#define MILKCOCOA_DEBUG
40
41#define DEBUG_PRINTER Serial
42
43#ifdef MILKCOCOA_DEBUG
44 #define MILKCOCOA_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
45 #define MILKCOCOA_DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
46 #define MILKCOCOA_DEBUG_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
47#else
48 #define MILKCOCOA_DEBUG_PRINT(...) {}
49 #define MILKCOCOA_DEBUG_PRINTLN(...) {}
50 #define MILKCOCOA_DEBUG_PRINTBUFFER(buffer, len) {}
51#endif
52
53class DataElement {
54 public:
55 DataElement();
56 DataElement(char *json_string);
57 ~DataElement();
58 void setValue(const char *key, const char *v);
59 void setValue(const char *key, int v);
60 void setValue(const char *key, double v);
61 char *toCharArray();
62 bool getString(const char *key, char **pdata);
63 bool getInt(const char *key, int *pdata);
64 bool getFloat(const char *key, float *pdata);
65
66 private:
67 aJsonObject *params;
68 aJsonObject *paJsonObj;
69};
70
71typedef void (*GeneralFunction) (DataElement *pelem);
72
73class MilkcocoaSubscriber {
74 public:
75 Adafruit_MQTT_Subscribe *mqtt_sub;
76 GeneralFunction cb;
77 char topic[100];
78 MilkcocoaSubscriber(GeneralFunction _cb);
79 void set_mqtt_sub(Adafruit_MQTT_Subscribe *_mqtt_sub);
80};
81
82class Milkcocoa {
83 public:
84 Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id);
85 Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id, const char *_session);
86 static Milkcocoa* createWithApiKey(Client *client, const char *host, uint16_t port, const char *app_id, const char *client_id, const char *key, const char *secret);
87 int8_t connect(int32_t tmout = 0);
88 int8_t loop(uint16_t timeout = 0);
89 int8_t push(const char *path, DataElement *pdataelement, uint16_t timeout = 0);
90 int8_t send(const char *path, DataElement *pdataelement, uint16_t timeout = 0);
91 bool on(const char *path, const char *event, GeneralFunction cb);
92 const __FlashStringHelper* connectErrorString(int8_t code) {return mqtt->connectErrorString(code);};
93 const __FlashStringHelper* pushErrorString(int8_t code) {if(code == MILKCOCOA_PUSH_ERRORNO) return F("Push error"); return mqtt->connectErrorString(code);};
94
95private:
96 const char *app_id;
97 char session[128];
98 Adafruit_MQTT_Client *mqtt;
99 MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS];
100};
101
102
103#endif
Note: See TracBrowser for help on using the repository browser.