Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn
Milkcocoa.h@8:e2f15b1b4f70, 2017-06-14 (annotated)
- Committer:
- jksoft
- Date:
- Wed Jun 14 07:48:45 2017 +0000
- Revision:
- 8:e2f15b1b4f70
- Parent:
- 6:1b5ec3a15d67
- Child:
- 12:6eafbe763882
?????
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 | 6:1b5ec3a15d67 | 10 | //#define __MILKCOCOA_THREAD |
| jksoft | 6:1b5ec3a15d67 | 11 | |
| jksoft | 0:0a2f634d3324 | 12 | #define RECV_TIMEOUT 500 |
| jksoft | 0:0a2f634d3324 | 13 | #define MILKCOCOA_SUBSCRIBERS 8 |
| jksoft | 0:0a2f634d3324 | 14 | #define START_THREAD 1 |
| jksoft | 0:0a2f634d3324 | 15 | |
| jksoft | 0:0a2f634d3324 | 16 | class DataElement { |
| jksoft | 0:0a2f634d3324 | 17 | public: |
| jksoft | 0:0a2f634d3324 | 18 | DataElement(); |
| jksoft | 0:0a2f634d3324 | 19 | DataElement(char *json_string); |
| jksoft | 0:0a2f634d3324 | 20 | void setValue(const char *key, const char *v); |
| jksoft | 0:0a2f634d3324 | 21 | void setValue(const char *key, int v); |
| jksoft | 0:0a2f634d3324 | 22 | void setValue(const char *key, double v); |
| jksoft | 0:0a2f634d3324 | 23 | char *toCharArray(); |
| jksoft | 0:0a2f634d3324 | 24 | char *getString(const char *key); |
| jksoft | 0:0a2f634d3324 | 25 | int getInt(const char *key); |
| jksoft | 0:0a2f634d3324 | 26 | float getFloat(const char *key); |
| jksoft | 0:0a2f634d3324 | 27 | |
| jksoft | 0:0a2f634d3324 | 28 | private: |
| jksoft | 0:0a2f634d3324 | 29 | char json_msg[256]; |
| jksoft | 0:0a2f634d3324 | 30 | }; |
| jksoft | 0:0a2f634d3324 | 31 | |
| jksoft | 0:0a2f634d3324 | 32 | typedef void (*GeneralFunction) (MQTT::MessageData& elem); |
| jksoft | 0:0a2f634d3324 | 33 | |
| jksoft | 0:0a2f634d3324 | 34 | class MilkcocoaSubscriber { |
| jksoft | 0:0a2f634d3324 | 35 | public: |
| jksoft | 0:0a2f634d3324 | 36 | GeneralFunction cb; |
| jksoft | 0:0a2f634d3324 | 37 | char topic[80]; |
| jksoft | 0:0a2f634d3324 | 38 | MilkcocoaSubscriber(GeneralFunction _cb); |
| jksoft | 0:0a2f634d3324 | 39 | }; |
| jksoft | 0:0a2f634d3324 | 40 | |
| jksoft | 0:0a2f634d3324 | 41 | class Milkcocoa { |
| jksoft | 0:0a2f634d3324 | 42 | public: |
| jksoft | 1:8e4149b53a8a | 43 | |
| jksoft | 0:0a2f634d3324 | 44 | Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id); |
| jksoft | 0:0a2f634d3324 | 45 | Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session); |
| jksoft | 0:0a2f634d3324 | 46 | 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 | 47 | void connect(); |
| jksoft | 0:0a2f634d3324 | 48 | void loop(); |
| jksoft | 8:e2f15b1b4f70 | 49 | void close(); |
| jksoft | 0:0a2f634d3324 | 50 | bool push(const char *path, DataElement dataelement); |
| jksoft | 3:cddf81a87de3 | 51 | bool push(const char *path, char *data); |
| jksoft | 0:0a2f634d3324 | 52 | bool send(const char *path, DataElement dataelement); |
| jksoft | 3:cddf81a87de3 | 53 | bool send(const char *path, char *data); |
| jksoft | 0:0a2f634d3324 | 54 | bool on(const char *path, const char *event, GeneralFunction cb); |
| jksoft | 6:1b5ec3a15d67 | 55 | #ifdef __MILKCOCOA_THREAD |
| jksoft | 0:0a2f634d3324 | 56 | void setLoopCycle(int cycle); |
| jksoft | 0:0a2f634d3324 | 57 | void start(); |
| jksoft | 6:1b5ec3a15d67 | 58 | #endif |
| jksoft | 0:0a2f634d3324 | 59 | private: |
| jksoft | 0:0a2f634d3324 | 60 | char servername[64]; |
| jksoft | 0:0a2f634d3324 | 61 | int16_t portnum; |
| jksoft | 0:0a2f634d3324 | 62 | char _clientid[64]; |
| jksoft | 0:0a2f634d3324 | 63 | char username[32]; |
| jksoft | 0:0a2f634d3324 | 64 | char password[32]; |
| jksoft | 4:9cfd43d8de16 | 65 | char app_id[32]; |
| jksoft | 6:1b5ec3a15d67 | 66 | char _message[256]; |
| jksoft | 0:0a2f634d3324 | 67 | int16_t loop_cycle; |
| jksoft | 0:0a2f634d3324 | 68 | |
| jksoft | 0:0a2f634d3324 | 69 | MQTTInterface* ipstack; |
| jksoft | 0:0a2f634d3324 | 70 | MClient *client; |
| jksoft | 0:0a2f634d3324 | 71 | GeneralFunction _cb; |
| jksoft | 0:0a2f634d3324 | 72 | MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS]; |
| jksoft | 6:1b5ec3a15d67 | 73 | |
| jksoft | 6:1b5ec3a15d67 | 74 | #ifdef __MILKCOCOA_THREAD |
| jksoft | 1:8e4149b53a8a | 75 | Thread cycleThread1; |
| jksoft | 1:8e4149b53a8a | 76 | Thread cycleThread2; |
| jksoft | 1:8e4149b53a8a | 77 | void cycle_Thread1(void); |
| jksoft | 1:8e4149b53a8a | 78 | void cycle_Thread2(void); |
| jksoft | 1:8e4149b53a8a | 79 | static void threadStarter1(void const *p); |
| jksoft | 1:8e4149b53a8a | 80 | static void threadStarter2(void const *p); |
| jksoft | 6:1b5ec3a15d67 | 81 | #endif |
| jksoft | 0:0a2f634d3324 | 82 | typedef struct { |
| jksoft | 0:0a2f634d3324 | 83 | char message[256]; |
| jksoft | 0:0a2f634d3324 | 84 | char topic[80]; |
| jksoft | 0:0a2f634d3324 | 85 | } milkcocoa_message_t; |
| jksoft | 0:0a2f634d3324 | 86 | Mail<milkcocoa_message_t, 16> message_box; |
| jksoft | 8:e2f15b1b4f70 | 87 | int connectting; |
| jksoft | 8:e2f15b1b4f70 | 88 | int mqtt_connectting; |
| jksoft | 6:1b5ec3a15d67 | 89 | |
| jksoft | 0:0a2f634d3324 | 90 | }; |
| jksoft | 0:0a2f634d3324 | 91 | |
| jksoft | 0:0a2f634d3324 | 92 | #endif |