Example of AWS IoT connection and Web Dashboard thru STM32 Nucleo evaluation board and mbed OS.
Dependencies: X_NUCLEO_IKS01A1 mbed FP MQTTPacket DnsQuery ATParser
MQTTSocket.h
00001 #if !defined(MQTTSOCKET_H) 00002 #define MQTTSOCKET_H 00003 00004 #include "MQTTmbed.h" 00005 #include "TCPSocket.h" 00006 00007 class MQTTSocket 00008 { 00009 public: 00010 00011 MQTTSocket(): mysock(true) 00012 { 00013 00014 } 00015 00016 int open(NetworkStack *ipstack) 00017 { 00018 _time_last_ticks = 0; 00019 timesock.open(ipstack); 00020 return mysock.open(ipstack); 00021 } 00022 00023 int getNTPtime(int timeout=1000) 00024 { 00025 int err; 00026 int attempts = 0; 00027 00028 uint8_t timedata[4]; 00029 00030 timesock.set_timeout(timeout*3); 00031 do 00032 { 00033 err= timesock.connect("time-d.nist.gov", 37); 00034 00035 if (err != 0) 00036 { 00037 LOG("ERROR resolving ntp server IP and connecting to it! \r\n"); 00038 attempts++; 00039 if (attempts == 3) 00040 return err; 00041 } 00042 } while (err != 0); 00043 00044 while (err != 4) 00045 { 00046 err = timesock.recv((char*)timedata, 4); 00047 00048 if (err != 4) 00049 { 00050 LOG("ERROR receiving time from ntp server! \r\n"); 00051 //return -1; 00052 } 00053 else 00054 { 00055 /// 00056 /// Time Protocol provides the time as a binary number of seconds since 1900, 00057 /// 00058 /// 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT from 12:00:01 am on 1 January 1900 GMT 00059 /// 00060 _time_last_ticks = ((timedata[0]<<24 )|(timedata[1]<<16)|(timedata[2]<<8)| timedata[3]) - 2208988800ul; 00061 00062 LOG("Success receiving time from ntp server. Tick from 1 Jan 1970 is equal to %d. \r\n", _time_last_ticks); 00063 00064 } 00065 } 00066 00067 err = timesock.close(); 00068 00069 return err; 00070 } 00071 00072 int connect(char* hostname, int port, int timeout=1000) 00073 { 00074 int err; 00075 00076 mysock.set_timeout(timeout); 00077 err = mysock.connect(hostname, port); 00078 // t.start(); 00079 return err; 00080 } 00081 00082 int read(unsigned char* buffer, int len, int timeout) 00083 { 00084 mysock.set_timeout(timeout); 00085 //t.reset(); 00086 // int start = t.read_ms(); 00087 int rc = mysock.recv((char*)buffer, len); 00088 // int stop = t.read_ms(); 00089 // if (rc>0) printf ("recv File: %s, Line: %d Read nB: %d rc: %d timeout: %d elaps: %d\n\r",__FILE__,__LINE__, len, rc, timeout, stop-start); 00090 return rc; 00091 } 00092 00093 int write(unsigned char* buffer, int len, int timeout) 00094 { 00095 mysock.set_timeout(timeout); 00096 // mysock.set_blocking(false, timeout); 00097 // mysock.set_blocking(false); 00098 int rc = mysock.send((char*)buffer, len); 00099 // printf ("send File: %s, Line: %d Write nB: %d rc: %d\n\r",__FILE__,__LINE__, len, rc); 00100 return rc; 00101 } 00102 00103 int disconnect() 00104 { 00105 // t.stop(); 00106 return mysock.close(); 00107 } 00108 00109 inline int getTime() 00110 { 00111 return _time_last_ticks; 00112 } 00113 00114 private: 00115 TCPSocket mysock; 00116 TCPSocket timesock; 00117 00118 uint32_t _time_last_ticks; 00119 // Timer t; 00120 00121 }; 00122 #endif
Generated on Wed Jul 13 2022 20:28:49 by 1.7.2