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

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

MQTT/MQTTSocket.h

Committer:
jksoft
Date:
2017-02-09
Revision:
0:0a2f634d3324

File content as of revision 0:0a2f634d3324:

#if !defined(MQTTSOCKET_H)
#define MQTTSOCKET_H

#include "MQTTmbed.h"
#include "TCPSocket.h"

class MQTTSocket
{
public:
	int open(NetworkInterface* nif)
	{
		return socket.open(nif);
	}
	
    int connect(char* hostname, int port, int timeout=1000)
    {
		socket.set_blocking (false);
		socket.set_timeout(timeout);
		_mutex.lock();
        int ret = (int)socket.connect(hostname, port);
		_mutex.unlock();
		
		return ret;
    }

    int read(unsigned char* buffer, int len, int timeout)
    {
		socket.set_blocking (false);
		socket.set_timeout(timeout);
		_mutex.lock();
        int ret = (int)socket.recv((char*)buffer, len);
		_mutex.unlock();
		
		return ret;
    }
    
    int write(unsigned char* buffer, int len, int timeout)
    {
		socket.set_blocking (false);
		socket.set_timeout(timeout); 
		_mutex.lock();
        int ret = (int)socket.send((char*)buffer, len);
		_mutex.unlock();
		
		return ret;
    }
    
    int disconnect()
    {
		_mutex.lock();
    	int ret = (int)socket.close();
		_mutex.unlock();
		
		return ret;
    }
    
private:
	Mutex _mutex;
    TCPSocket socket;
    
};



#endif