Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DHT MQTT WIZnetInterface mbed-src
Fork of MQTTw7500 by
main.cpp
00001 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay 00002 //This code is based on Martin Olejar's MFRC522 library. Minimal changes 00003 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too 00004 00005 //---------------------------------------- 00006 00007 #include "mbed.h" 00008 #include "DHT.h" 00009 #include "MQTTEthernet.h" 00010 #include "MQTTClient.h" 00011 #include <sstream> 00012 #include <string> 00013 #define ECHO_SERVER_PORT 7 00014 00015 Serial out(USBTX,USBRX); 00016 int arrivedcount = 0; 00017 00018 void messageArrived(MQTT::MessageData& md) 00019 { 00020 MQTT::Message &message = md.message; 00021 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); 00022 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); 00023 ++arrivedcount; 00024 } 00025 00026 void baud(int baudrate) 00027 { 00028 Serial s(USBTX, USBRX); 00029 s.baud(baudrate); 00030 } 00031 00032 00033 char* store_buf; 00034 unsigned long lastConnectionTime = 0; 00035 const unsigned long postingInterval = 20L * 1000L; 00036 00037 00038 int main(void) 00039 { 00040 DHT sensor(D4, DHT11); 00041 printf("starting...\n"); 00042 00043 MQTTEthernet ipstack = MQTTEthernet(); 00044 //printf("setting up mqtt ethernet"); 00045 00046 while (true) { 00047 if(store_buf[0]=='\0') { 00048 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); 00049 00050 char* hostname = "mqtt.thingspeak.com"; 00051 int port = 1883; 00052 00053 00054 00055 int rc = ipstack.connect(hostname, port); 00056 00057 out.printf("rc from TCP connect is %d\n", rc); 00058 00059 if (rc != 0) 00060 out.printf("rc from TCP connect is %d\n", rc); 00061 00062 char MQTTClientID[30]; 00063 00064 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00065 data.MQTTVersion = 3; 00066 sprintf(MQTTClientID,"WIZwikiW7500mqttclient%d",rand()%1000); 00067 data.clientID.cstring = MQTTClientID; 00068 data.username.cstring = "tsmqtttuser"; 00069 data.password.cstring = "tsmqtttpasswrd"; 00070 00071 data.writeapikey.cstring = "tsmqtttpasswrd"; 00072 data.channelid.cstring = "tsmqtttpasswrd"; 00073 00074 00075 00076 if ((rc = client.connect(data)) != 0) 00077 printf("rc from MQTT connect is %d\n", rc); 00078 00079 MQTT::Message message; 00080 00081 char buf[100]; 00082 int error = 0; 00083 float hum = 0.0f, temp = 0.0f; 00084 char i = 0; 00085 while (true) 00086 { 00087 if(i > 100) i = 0; 00088 error = sensor.readData(); 00089 if (0 == error) { 00090 hum = sensor.ReadHumidity(); 00091 temp = sensor.ReadTemperature(CELCIUS); 00092 } 00093 message.qos = MQTT::QOS0; 00094 message.retained = false; 00095 message.dup = false; 00096 00097 f (millis() - lastConnectionTime > postingInterval) 00098 { 00099 00100 00101 00102 sprintf(buf, "%3.1f", hum); 00103 message.payload = (void*)buf; 00104 message.payloadlen = strlen(buf); 00105 rc = client.publish("/wiznet/humidity17",message); 00106 printf("publish humidity data %s\r\n",(char*)message.payload); 00107 00108 sprintf(buf, "%3.1f", temp); 00109 message.payload = (void*)buf; 00110 message.payloadlen = strlen(buf); 00111 rc = client.publish("/wiznet/temperature17", message); 00112 printf("publish temperature data %s\r\n",(char*)message.payload); 00113 00114 00115 lastConnectionTime = millis(); 00116 /* 00117 if (millis() - lastConnectionTime > postingInterval) 00118 { 00119 00120 // do publish operation 00121 00122 00123 Use this function to publish to a single field directly. 00124 00125 void mqttpublish() { 00126 00127 float t = dht.readTemperature(true); // Read temperature from DHT sensor. 00128 00129 String data = String(t, DEC); 00130 int length = data.length(); 00131 char msgBuffer[length]; 00132 data.toCharArray(msgBuffer,length+1); 00133 printf(msgBuffer); 00134 00135 // Create a topic string and publish data to ThingSpeak channel feed. 00136 String topicString ="channels/" + String( channelID ) + "/publish/"+String(writeAPIKey); 00137 length=topicString.length(); 00138 char topicBuffer[length]; 00139 topicString.toCharArray(topicBuffer,length+1); 00140 00141 mqttClient.publish( topicBuffer, msgBuffer ); 00142 00143 lastConnectionTime = millis(); 00144 } 00145 00146 } 00147 00148 */ 00149 00150 wait_ms(3000); 00151 00152 } 00153 } 00154 00155 } 00156 }
Generated on Mon Jul 18 2022 01:11:54 by
1.7.2
