【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/

Dependents:   mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn

Milkcocoa.h

Committer:
jksoft
Date:
2018-03-26
Revision:
13:61e0cc093180
Parent:
12:6eafbe763882

File content as of revision 13:61e0cc093180:

#ifndef _MILKCOCOA_H_
#define _MILKCOCOA_H_

#include "mbed.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"
#include "MClient.h"
#include "rtos.h"

//#define __MILKCOCOA_THREAD

#define RECV_TIMEOUT            500
#define MILKCOCOA_SUBSCRIBERS   8
#define START_THREAD            1

class DataElement {
  public:
    DataElement();
    DataElement(char *json_string);
    void setValue(const char *key, const char *v);
    void setValue(const char *key, int v);
    void setValue(const char *key, double v);
    char *toCharArray();
    char *getString(const char *key);
    int getInt(const char *key);
    float getFloat(const char *key);

  private:
	char json_msg[256];
};

typedef void (*GeneralFunction) (MQTT::MessageData& elem);

class MilkcocoaSubscriber {
	public:
		GeneralFunction cb;
        char topic[80];
		MilkcocoaSubscriber(GeneralFunction _cb);
};

class Milkcocoa {
 public:
  Milkcocoa(NetworkInterface* nif);
  Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id);
  Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session);
  static Milkcocoa* createWithApiKey(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret);
  void set_network_settinng(const char *host, uint16_t port, const char *_app_id, const char *client_id);
  void connect();
  void loop();
  void send_massage();
  void close();
  bool push(const char *path, DataElement dataelement);
  bool push(const char *path, char *data);
  bool send(const char *path, DataElement dataelement);
  bool send(const char *path, char *data);
  bool on(const char *path, const char *event, GeneralFunction cb);
#ifdef __MILKCOCOA_THREAD
  void setLoopCycle(int cycle);
  void start();
#endif
private:
  char servername[64];
  int16_t portnum;
  char _clientid[64];
  char username[32];
  char password[32];
  char app_id[32];
  char _message[256];
  int16_t loop_cycle;

  MQTTInterface* ipstack;
  MClient *client;
  GeneralFunction _cb;
  MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS];

#ifdef __MILKCOCOA_THREAD
  Thread cycleThread1;
  Thread cycleThread2;
  void cycle_Thread1(void);
  void cycle_Thread2(void);
  static void threadStarter1(void const *p);
  static void threadStarter2(void const *p);
#endif
  typedef struct {
    char message[192];
    char topic[64];
  } milkcocoa_message_t;
  Mail<milkcocoa_message_t, 4> message_box;
  int connectting;
  int mqtt_connectting;

};

#endif