hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Wed May 05 14:48:01 2021 +0000
Revision:
25:ca1b1098c77f
TEST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasonberry 25:ca1b1098c77f 1 #if !defined(MQTTSOCKET_H)
jasonberry 25:ca1b1098c77f 2 #define MQTTSOCKET_H
jasonberry 25:ca1b1098c77f 3
jasonberry 25:ca1b1098c77f 4 #include "MQTTmbed.h"
jasonberry 25:ca1b1098c77f 5 #include <EthernetInterface.h>
jasonberry 25:ca1b1098c77f 6 #include <Timer.h>
jasonberry 25:ca1b1098c77f 7
jasonberry 25:ca1b1098c77f 8 class MQTTSocket
jasonberry 25:ca1b1098c77f 9 {
jasonberry 25:ca1b1098c77f 10 public:
jasonberry 25:ca1b1098c77f 11 MQTTSocket(EthernetInterface *anet)
jasonberry 25:ca1b1098c77f 12 {
jasonberry 25:ca1b1098c77f 13 net = anet;
jasonberry 25:ca1b1098c77f 14 open = false;
jasonberry 25:ca1b1098c77f 15 }
jasonberry 25:ca1b1098c77f 16
jasonberry 25:ca1b1098c77f 17 int connect(char* hostname, int port, int timeout=1000)
jasonberry 25:ca1b1098c77f 18 {
jasonberry 25:ca1b1098c77f 19 if (open)
jasonberry 25:ca1b1098c77f 20 disconnect();
jasonberry 25:ca1b1098c77f 21 nsapi_error_t rc = mysock.open(net);
jasonberry 25:ca1b1098c77f 22 open = true;
jasonberry 25:ca1b1098c77f 23 mysock.set_blocking(true);
jasonberry 25:ca1b1098c77f 24 mysock.set_timeout((unsigned int)timeout);
jasonberry 25:ca1b1098c77f 25 rc = mysock.connect(hostname, port);
jasonberry 25:ca1b1098c77f 26 mysock.set_blocking(false); // blocking timeouts seem not to work
jasonberry 25:ca1b1098c77f 27 return rc;
jasonberry 25:ca1b1098c77f 28 }
jasonberry 25:ca1b1098c77f 29
jasonberry 25:ca1b1098c77f 30 // common read/write routine, avoiding blocking timeouts
jasonberry 25:ca1b1098c77f 31 int common(unsigned char* buffer, int len, int timeout, bool read)
jasonberry 25:ca1b1098c77f 32 {
jasonberry 25:ca1b1098c77f 33 timer.start();
jasonberry 25:ca1b1098c77f 34 mysock.set_blocking(false); // blocking timeouts seem not to work
jasonberry 25:ca1b1098c77f 35 int bytes = 0;
jasonberry 25:ca1b1098c77f 36 bool first = true;
jasonberry 25:ca1b1098c77f 37 do
jasonberry 25:ca1b1098c77f 38 {
jasonberry 25:ca1b1098c77f 39 if (first)
jasonberry 25:ca1b1098c77f 40 first = false;
jasonberry 25:ca1b1098c77f 41 else
jasonberry 25:ca1b1098c77f 42 wait_ms(timeout < 100 ? timeout : 100);
jasonberry 25:ca1b1098c77f 43 int rc;
jasonberry 25:ca1b1098c77f 44 if (read)
jasonberry 25:ca1b1098c77f 45 {
jasonberry 25:ca1b1098c77f 46 mysock.set_timeout(timeout);
jasonberry 25:ca1b1098c77f 47 rc = mysock.recv((char*)buffer, len);
jasonberry 25:ca1b1098c77f 48 mysock.set_timeout(0);
jasonberry 25:ca1b1098c77f 49 return rc;
jasonberry 25:ca1b1098c77f 50 }
jasonberry 25:ca1b1098c77f 51 //rc = mysock.recv((char*)buffer, len);
jasonberry 25:ca1b1098c77f 52 else
jasonberry 25:ca1b1098c77f 53 rc = mysock.send((char*)buffer, len);
jasonberry 25:ca1b1098c77f 54 if (rc < 0)
jasonberry 25:ca1b1098c77f 55 {
jasonberry 25:ca1b1098c77f 56 if (rc != NSAPI_ERROR_WOULD_BLOCK)
jasonberry 25:ca1b1098c77f 57 {
jasonberry 25:ca1b1098c77f 58 bytes = -1;
jasonberry 25:ca1b1098c77f 59 break;
jasonberry 25:ca1b1098c77f 60 }
jasonberry 25:ca1b1098c77f 61 }
jasonberry 25:ca1b1098c77f 62 else
jasonberry 25:ca1b1098c77f 63 bytes += rc;
jasonberry 25:ca1b1098c77f 64 }
jasonberry 25:ca1b1098c77f 65 while (bytes < len && timer.read_ms() < timeout);
jasonberry 25:ca1b1098c77f 66 timer.stop();
jasonberry 25:ca1b1098c77f 67 return bytes;
jasonberry 25:ca1b1098c77f 68 }
jasonberry 25:ca1b1098c77f 69
jasonberry 25:ca1b1098c77f 70 /* returns the number of bytes read, which could be 0.
jasonberry 25:ca1b1098c77f 71 -1 if there was an error on the socket
jasonberry 25:ca1b1098c77f 72 */
jasonberry 25:ca1b1098c77f 73 int read(unsigned char* buffer, int len, int timeout)
jasonberry 25:ca1b1098c77f 74 {
jasonberry 25:ca1b1098c77f 75 return common(buffer, len, timeout, true);
jasonberry 25:ca1b1098c77f 76 }
jasonberry 25:ca1b1098c77f 77
jasonberry 25:ca1b1098c77f 78 int write(unsigned char* buffer, int len, int timeout)
jasonberry 25:ca1b1098c77f 79 {
jasonberry 25:ca1b1098c77f 80 return common(buffer, len, timeout, false);
jasonberry 25:ca1b1098c77f 81 }
jasonberry 25:ca1b1098c77f 82
jasonberry 25:ca1b1098c77f 83 int disconnect()
jasonberry 25:ca1b1098c77f 84 {
jasonberry 25:ca1b1098c77f 85 open = false;
jasonberry 25:ca1b1098c77f 86 return mysock.close();
jasonberry 25:ca1b1098c77f 87 }
jasonberry 25:ca1b1098c77f 88
jasonberry 25:ca1b1098c77f 89 /*bool is_connected()
jasonberry 25:ca1b1098c77f 90 {
jasonberry 25:ca1b1098c77f 91 return mysock.is_connected();
jasonberry 25:ca1b1098c77f 92 }*/
jasonberry 25:ca1b1098c77f 93
jasonberry 25:ca1b1098c77f 94 private:
jasonberry 25:ca1b1098c77f 95
jasonberry 25:ca1b1098c77f 96 bool open;
jasonberry 25:ca1b1098c77f 97 TCPSocket mysock;
jasonberry 25:ca1b1098c77f 98 EthernetInterface *net;
jasonberry 25:ca1b1098c77f 99 Timer timer;
jasonberry 25:ca1b1098c77f 100
jasonberry 25:ca1b1098c77f 101 };
jasonberry 25:ca1b1098c77f 102
jasonberry 25:ca1b1098c77f 103 #endif