【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/
Dependents: mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn
Milkcocoa.h@3:cddf81a87de3, 2017-05-18 (annotated)
- Committer:
- jksoft
- Date:
- Thu May 18 14:47:30 2017 +0000
- Revision:
- 3:cddf81a87de3
- Parent:
- 1:8e4149b53a8a
- Child:
- 4:9cfd43d8de16
???????????push?send???????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:0a2f634d3324 | 1 | #ifndef _MILKCOCOA_H_ |
jksoft | 0:0a2f634d3324 | 2 | #define _MILKCOCOA_H_ |
jksoft | 0:0a2f634d3324 | 3 | |
jksoft | 0:0a2f634d3324 | 4 | #include "mbed.h" |
jksoft | 0:0a2f634d3324 | 5 | #include "MQTTmbed.h" |
jksoft | 0:0a2f634d3324 | 6 | #include "MQTTClient.h" |
jksoft | 0:0a2f634d3324 | 7 | #include "MClient.h" |
jksoft | 0:0a2f634d3324 | 8 | #include "rtos.h" |
jksoft | 0:0a2f634d3324 | 9 | |
jksoft | 0:0a2f634d3324 | 10 | #define RECV_TIMEOUT 500 |
jksoft | 0:0a2f634d3324 | 11 | #define MILKCOCOA_SUBSCRIBERS 8 |
jksoft | 0:0a2f634d3324 | 12 | #define START_THREAD 1 |
jksoft | 0:0a2f634d3324 | 13 | |
jksoft | 0:0a2f634d3324 | 14 | class DataElement { |
jksoft | 0:0a2f634d3324 | 15 | public: |
jksoft | 0:0a2f634d3324 | 16 | DataElement(); |
jksoft | 0:0a2f634d3324 | 17 | DataElement(char *json_string); |
jksoft | 0:0a2f634d3324 | 18 | void setValue(const char *key, const char *v); |
jksoft | 0:0a2f634d3324 | 19 | void setValue(const char *key, int v); |
jksoft | 0:0a2f634d3324 | 20 | void setValue(const char *key, double v); |
jksoft | 0:0a2f634d3324 | 21 | char *toCharArray(); |
jksoft | 0:0a2f634d3324 | 22 | char *getString(const char *key); |
jksoft | 0:0a2f634d3324 | 23 | int getInt(const char *key); |
jksoft | 0:0a2f634d3324 | 24 | float getFloat(const char *key); |
jksoft | 0:0a2f634d3324 | 25 | |
jksoft | 0:0a2f634d3324 | 26 | private: |
jksoft | 0:0a2f634d3324 | 27 | char json_msg[256]; |
jksoft | 0:0a2f634d3324 | 28 | }; |
jksoft | 0:0a2f634d3324 | 29 | |
jksoft | 0:0a2f634d3324 | 30 | typedef void (*GeneralFunction) (MQTT::MessageData& elem); |
jksoft | 0:0a2f634d3324 | 31 | |
jksoft | 0:0a2f634d3324 | 32 | class MilkcocoaSubscriber { |
jksoft | 0:0a2f634d3324 | 33 | public: |
jksoft | 0:0a2f634d3324 | 34 | GeneralFunction cb; |
jksoft | 0:0a2f634d3324 | 35 | char topic[80]; |
jksoft | 0:0a2f634d3324 | 36 | MilkcocoaSubscriber(GeneralFunction _cb); |
jksoft | 0:0a2f634d3324 | 37 | }; |
jksoft | 0:0a2f634d3324 | 38 | |
jksoft | 0:0a2f634d3324 | 39 | class Milkcocoa { |
jksoft | 0:0a2f634d3324 | 40 | public: |
jksoft | 1:8e4149b53a8a | 41 | |
jksoft | 0:0a2f634d3324 | 42 | Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id); |
jksoft | 0:0a2f634d3324 | 43 | Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session); |
jksoft | 0:0a2f634d3324 | 44 | static Milkcocoa* createWithApiKey(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret); |
jksoft | 0:0a2f634d3324 | 45 | void connect(); |
jksoft | 0:0a2f634d3324 | 46 | void loop(); |
jksoft | 0:0a2f634d3324 | 47 | bool push(const char *path, DataElement dataelement); |
jksoft | 3:cddf81a87de3 | 48 | bool push(const char *path, char *data); |
jksoft | 0:0a2f634d3324 | 49 | bool send(const char *path, DataElement dataelement); |
jksoft | 3:cddf81a87de3 | 50 | bool send(const char *path, char *data); |
jksoft | 0:0a2f634d3324 | 51 | bool on(const char *path, const char *event, GeneralFunction cb); |
jksoft | 0:0a2f634d3324 | 52 | void setLoopCycle(int cycle); |
jksoft | 0:0a2f634d3324 | 53 | void start(); |
jksoft | 0:0a2f634d3324 | 54 | |
jksoft | 0:0a2f634d3324 | 55 | private: |
jksoft | 0:0a2f634d3324 | 56 | char servername[64]; |
jksoft | 0:0a2f634d3324 | 57 | int16_t portnum; |
jksoft | 0:0a2f634d3324 | 58 | char _clientid[64]; |
jksoft | 0:0a2f634d3324 | 59 | char username[32]; |
jksoft | 0:0a2f634d3324 | 60 | char password[32]; |
jksoft | 0:0a2f634d3324 | 61 | const char *app_id; |
jksoft | 0:0a2f634d3324 | 62 | int16_t loop_cycle; |
jksoft | 0:0a2f634d3324 | 63 | |
jksoft | 0:0a2f634d3324 | 64 | MQTTInterface* ipstack; |
jksoft | 0:0a2f634d3324 | 65 | MClient *client; |
jksoft | 0:0a2f634d3324 | 66 | GeneralFunction _cb; |
jksoft | 0:0a2f634d3324 | 67 | MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS]; |
jksoft | 1:8e4149b53a8a | 68 | Thread cycleThread1; |
jksoft | 1:8e4149b53a8a | 69 | Thread cycleThread2; |
jksoft | 1:8e4149b53a8a | 70 | void cycle_Thread1(void); |
jksoft | 1:8e4149b53a8a | 71 | void cycle_Thread2(void); |
jksoft | 1:8e4149b53a8a | 72 | static void threadStarter1(void const *p); |
jksoft | 1:8e4149b53a8a | 73 | static void threadStarter2(void const *p); |
jksoft | 0:0a2f634d3324 | 74 | |
jksoft | 0:0a2f634d3324 | 75 | typedef struct { |
jksoft | 0:0a2f634d3324 | 76 | char message[256]; |
jksoft | 0:0a2f634d3324 | 77 | char topic[80]; |
jksoft | 0:0a2f634d3324 | 78 | } milkcocoa_message_t; |
jksoft | 0:0a2f634d3324 | 79 | Mail<milkcocoa_message_t, 16> message_box; |
jksoft | 0:0a2f634d3324 | 80 | }; |
jksoft | 0:0a2f634d3324 | 81 | |
jksoft | 0:0a2f634d3324 | 82 | |
jksoft | 0:0a2f634d3324 | 83 | #endif |