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: C12832 EthernetInterface MMA7660 MQTT USBDevice mbed-rtos mbed
Fork of HelloMQTT by
main.cpp
00001 00002 #define USE_LCD 1 00003 00004 #if USE_LCD 00005 #include "C12832.h" 00006 00007 #if defined(TARGET_UBLOX_C027) 00008 #warning "Compiling for mbed C027" 00009 #include "C027.h" 00010 #elif defined(TARGET_LPC1768) 00011 #warning "Compiling for mbed LPC1768" 00012 #include "LPC1768.h" 00013 #elif defined(TARGET_K64F) 00014 #warning "Compiling for mbed K64F" 00015 #include "K64F.h" 00016 #endif 00017 00018 #define printf lcd.cls();lcd.printf 00019 00020 #endif 00021 00022 #define MQTTCLIENT_QOS2 1 00023 00024 #include "MQTTEthernet.h" 00025 #include "MQTTClient.h" 00026 #include "USBSerial.h" 00027 #include "C12832.h" 00028 #include "MMA7660.h" 00029 00030 MMA7660 MMA(D14, D15); 00031 00032 int arrivedcount = 0; 00033 00034 00035 void messageArrived(MQTT::MessageData& md) 00036 { 00037 MQTT::Message &message = md.message; 00038 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); 00039 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); 00040 ++arrivedcount; 00041 } 00042 00043 00044 int main(int argc, char* argv[],float c,float ang) 00045 { 00046 lcd.cls(); 00047 lcd.locate(0,3); 00048 printf("mbed application shield!"); 00049 00050 00051 00052 while(1) { 00053 wait(0.1f); 00054 lcd.locate(0,14); 00055 float c=MMA.y()/MMA.z(); 00056 float ang=pow(c,(-1)); 00057 printf("%.2f,%.2f,%.2f",MMA.x(),MMA.y(),MMA.z()); 00058 printf("%.2f",ang); 00059 00060 } 00061 if (ang<=-1){ 00062 MQTTEthernet ipstack = MQTTEthernet(); 00063 float version = 0.5; 00064 char* topic = "door"; 00065 00066 printf("HelloMQTT: version is %f\n", version); 00067 00068 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); 00069 00070 char* hostname = "doughnut.kent.ac.uk"; 00071 int port = 1883; 00072 printf("Connecting to %s:%d\n", hostname, port); 00073 int rc = ipstack.connect(hostname, port); 00074 if (rc != 0) 00075 printf("rc from TCP connect is %d\n", rc); 00076 00077 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00078 data.MQTTVersion = 3; 00079 data.clientID.cstring = "mbed-sample"; 00080 data.username.cstring = "testuser"; 00081 data.password.cstring = "testpassword"; 00082 if ((rc = client.connect(data)) != 0) 00083 printf("rc from MQTT connect is %d\n", rc); 00084 00085 if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) 00086 printf("rc from MQTT subscribe is %d\n", rc); 00087 00088 MQTT::Message message; 00089 00090 // QoS 0 00091 char buf[100]; 00092 sprintf(buf, "door opened %f\n", version); 00093 message.qos = MQTT::QOS0; 00094 message.retained = false; 00095 message.dup = false; 00096 message.payload = (void*)buf; 00097 message.payloadlen = strlen(buf)+1; 00098 rc = client.publish(topic, message); 00099 while (arrivedcount < 1) 00100 client.yield(100); 00101 00102 00103 00104 if ((rc = client.unsubscribe(topic)) != 0) 00105 printf("rc from unsubscribe was %d\n", rc); 00106 00107 if ((rc = client.disconnect()) != 0) 00108 printf("rc from disconnect was %d\n", rc); 00109 00110 ipstack.disconnect(); 00111 00112 printf("Version %.2f: finish %d msgs\n", version, arrivedcount); 00113 00114 return 0; 00115 } 00116 }
Generated on Sun Jul 17 2022 11:40:56 by
1.7.2
