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

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

Files at this revision

API Documentation at this revision

Comitter:
jksoft
Date:
Thu Jun 01 00:23:41 2017 +0000
Parent:
5:dd68fa651637
Child:
7:f1e123331cad
Commit message:
???????????????????????????????

Changed in this revision

Milkcocoa.cpp Show annotated file Show diff for this revision Revisions of this file
Milkcocoa.h Show annotated file Show diff for this revision Revisions of this file
--- a/Milkcocoa.cpp	Fri May 19 01:33:48 2017 +0000
+++ b/Milkcocoa.cpp	Thu Jun 01 00:23:41 2017 +0000
@@ -1,6 +1,6 @@
 #include "Milkcocoa.h"
 
-#if 1
+#if 0
 extern RawSerial pc;
 
 #define DBG(x)  x
@@ -96,10 +96,13 @@
 	strcpy(_clientid,client_id);
 	strcpy(username,"sdammy");
 	strcpy(password,app_id);
+	
+#ifdef __MILKCOCOA_THREAD
 	setLoopCycle(5000);
 	
 	cycleThread1.start(Milkcocoa::threadStarter1,this);
 	cycleThread2.start(Milkcocoa::threadStarter2,this);
+#endif
 }
 
 Milkcocoa::Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session){
@@ -111,10 +114,13 @@
 	strcpy(_clientid,client_id);
 	strcpy(username,_session);
 	strcpy(password,app_id);
+	
+#ifdef __MILKCOCOA_THREAD
 	setLoopCycle(5000);
 	
 	cycleThread1.start(Milkcocoa::threadStarter1,this);
 	cycleThread2.start(Milkcocoa::threadStarter2,this);
+#endif
 }
 
 Milkcocoa* Milkcocoa::createWithApiKey(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) {
@@ -166,6 +172,7 @@
 }
 
 bool Milkcocoa::push(const char *path, char *data) {
+
 	milkcocoa_message_t *message = message_box.alloc();
 	char *buf;
 	
@@ -197,7 +204,7 @@
 	osStatus stat = message_box.put(message);
 	
 	if( stat != osOK ) return false;
-	
+
 	return true;
 }
 
@@ -216,13 +223,32 @@
 	osStatus stat = message_box.put(message);
 	
 	if( stat != osOK ) return false;
-	
+
 	return true;
 }
 
 void Milkcocoa::loop() {
 	connect();
-	client->yield(RECV_TIMEOUT);
+	client->yield(1);
+
+#ifndef __MILKCOCOA_THREAD
+	osEvent evt = message_box.get(0);
+	if (evt.status == osEventMail) {
+		milkcocoa_message_t *message = (milkcocoa_message_t*)evt.value.p;
+		MQTT::Message mq_message;
+		if(message != NULL) {
+			mq_message.qos = MQTT::QOS0;
+			mq_message.retained = 0;
+			mq_message.dup = false;
+			mq_message.payload = (void*)message->message;
+			mq_message.payloadlen = strlen(message->message);
+		
+			client->publish(message->topic, mq_message);
+			
+			message_box.free(message);
+		}
+	}
+#endif
 }
 
 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
@@ -247,10 +273,10 @@
 	return true;
 }
 
+#ifdef __MILKCOCOA_THREAD
 void Milkcocoa::setLoopCycle(int cycle) {
 	loop_cycle = cycle;
 }
-
 void Milkcocoa::start() {
 	cycleThread1.signal_set(START_THREAD);
 	cycleThread2.signal_set(START_THREAD);
@@ -305,6 +331,7 @@
   Milkcocoa *instance = (Milkcocoa*)p;
   instance->cycle_Thread2();
 }
+#endif
 
 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
   cb = _cb;
--- a/Milkcocoa.h	Fri May 19 01:33:48 2017 +0000
+++ b/Milkcocoa.h	Thu Jun 01 00:23:41 2017 +0000
@@ -7,6 +7,8 @@
 #include "MClient.h"
 #include "rtos.h"
 
+//#define __MILKCOCOA_THREAD
+
 #define RECV_TIMEOUT            500
 #define MILKCOCOA_SUBSCRIBERS   8
 #define START_THREAD            1
@@ -49,9 +51,10 @@
   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;
@@ -59,25 +62,28 @@
   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[256];
     char topic[80];
   } milkcocoa_message_t;
   Mail<milkcocoa_message_t, 16> message_box;
+
 };
 
-
 #endif