the code of obtain senor data and connect to MQTT
Dependencies: C12832 EthernetInterface MMA7660 MQTT USBDevice mbed-rtos mbed
Fork of HelloMQTT by
main.cpp@20:63e59fe8d75d, 2016-05-09 (annotated)
- Committer:
- co838_pg297
- Date:
- Mon May 09 20:46:07 2016 +0000
- Revision:
- 20:63e59fe8d75d
- Parent:
- 19:7f7aba7a4a8b
the code of obtain senor data and connect to MQTT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co838_pg297 | 20:63e59fe8d75d | 1 | |
icraggs | 18:07a79d8f01c3 | 2 | #define USE_LCD 1 |
icraggs | 1:a1d5c7a6acbc | 3 | |
icraggs | 18:07a79d8f01c3 | 4 | #if USE_LCD |
icraggs | 17:0811bdbdd78a | 5 | #include "C12832.h" |
icraggs | 2:638c854c0695 | 6 | |
icraggs | 17:0811bdbdd78a | 7 | #if defined(TARGET_UBLOX_C027) |
icraggs | 17:0811bdbdd78a | 8 | #warning "Compiling for mbed C027" |
icraggs | 17:0811bdbdd78a | 9 | #include "C027.h" |
icraggs | 17:0811bdbdd78a | 10 | #elif defined(TARGET_LPC1768) |
icraggs | 17:0811bdbdd78a | 11 | #warning "Compiling for mbed LPC1768" |
icraggs | 17:0811bdbdd78a | 12 | #include "LPC1768.h" |
icraggs | 17:0811bdbdd78a | 13 | #elif defined(TARGET_K64F) |
icraggs | 17:0811bdbdd78a | 14 | #warning "Compiling for mbed K64F" |
icraggs | 17:0811bdbdd78a | 15 | #include "K64F.h" |
icraggs | 17:0811bdbdd78a | 16 | #endif |
icraggs | 17:0811bdbdd78a | 17 | |
icraggs | 18:07a79d8f01c3 | 18 | #define printf lcd.cls();lcd.printf |
icraggs | 18:07a79d8f01c3 | 19 | |
icraggs | 18:07a79d8f01c3 | 20 | #endif |
icraggs | 18:07a79d8f01c3 | 21 | |
icraggs | 17:0811bdbdd78a | 22 | #define MQTTCLIENT_QOS2 1 |
icraggs | 0:0cae29831d01 | 23 | |
icraggs | 8:a3e3113054a1 | 24 | #include "MQTTEthernet.h" |
icraggs | 2:638c854c0695 | 25 | #include "MQTTClient.h" |
co838_pg297 | 20:63e59fe8d75d | 26 | #include "USBSerial.h" |
co838_pg297 | 20:63e59fe8d75d | 27 | #include "C12832.h" |
co838_pg297 | 20:63e59fe8d75d | 28 | #include "MMA7660.h" |
co838_pg297 | 20:63e59fe8d75d | 29 | |
co838_pg297 | 20:63e59fe8d75d | 30 | MMA7660 MMA(D14, D15); |
icraggs | 2:638c854c0695 | 31 | |
icraggs | 2:638c854c0695 | 32 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 33 | |
icraggs | 8:a3e3113054a1 | 34 | |
icraggs | 9:5beb8609e9f7 | 35 | void messageArrived(MQTT::MessageData& md) |
icraggs | 2:638c854c0695 | 36 | { |
icraggs | 9:5beb8609e9f7 | 37 | MQTT::Message &message = md.message; |
icraggs | 9:5beb8609e9f7 | 38 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
icraggs | 9:5beb8609e9f7 | 39 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
icraggs | 2:638c854c0695 | 40 | ++arrivedcount; |
icraggs | 2:638c854c0695 | 41 | } |
icraggs | 0:0cae29831d01 | 42 | |
icraggs | 2:638c854c0695 | 43 | |
co838_pg297 | 20:63e59fe8d75d | 44 | int main(int argc, char* argv[],float c,float ang) |
icraggs | 2:638c854c0695 | 45 | { |
co838_pg297 | 20:63e59fe8d75d | 46 | lcd.cls(); |
co838_pg297 | 20:63e59fe8d75d | 47 | lcd.locate(0,3); |
co838_pg297 | 20:63e59fe8d75d | 48 | printf("mbed application shield!"); |
co838_pg297 | 20:63e59fe8d75d | 49 | |
co838_pg297 | 20:63e59fe8d75d | 50 | |
co838_pg297 | 20:63e59fe8d75d | 51 | |
co838_pg297 | 20:63e59fe8d75d | 52 | while(1) { |
co838_pg297 | 20:63e59fe8d75d | 53 | wait(0.1f); |
co838_pg297 | 20:63e59fe8d75d | 54 | lcd.locate(0,14); |
co838_pg297 | 20:63e59fe8d75d | 55 | float c=MMA.y()/MMA.z(); |
co838_pg297 | 20:63e59fe8d75d | 56 | float ang=pow(c,(-1)); |
co838_pg297 | 20:63e59fe8d75d | 57 | printf("%.2f,%.2f,%.2f",MMA.x(),MMA.y(),MMA.z()); |
co838_pg297 | 20:63e59fe8d75d | 58 | printf("%.2f",ang); |
co838_pg297 | 20:63e59fe8d75d | 59 | |
co838_pg297 | 20:63e59fe8d75d | 60 | } |
co838_pg297 | 20:63e59fe8d75d | 61 | if (ang<=-1){ |
icraggs | 8:a3e3113054a1 | 62 | MQTTEthernet ipstack = MQTTEthernet(); |
icraggs | 17:0811bdbdd78a | 63 | float version = 0.5; |
co838_pg297 | 20:63e59fe8d75d | 64 | char* topic = "door"; |
icraggs | 2:638c854c0695 | 65 | |
icraggs | 18:07a79d8f01c3 | 66 | printf("HelloMQTT: version is %f\n", version); |
icraggs | 2:638c854c0695 | 67 | |
icraggs | 8:a3e3113054a1 | 68 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
icraggs | 3:7a6a899de7cc | 69 | |
co838_pg297 | 20:63e59fe8d75d | 70 | char* hostname = "doughnut.kent.ac.uk"; |
icraggs | 6:e4c690c45021 | 71 | int port = 1883; |
icraggs | 18:07a79d8f01c3 | 72 | printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 6:e4c690c45021 | 73 | int rc = ipstack.connect(hostname, port); |
icraggs | 6:e4c690c45021 | 74 | if (rc != 0) |
icraggs | 18:07a79d8f01c3 | 75 | printf("rc from TCP connect is %d\n", rc); |
icraggs | 6:e4c690c45021 | 76 | |
icraggs | 6:e4c690c45021 | 77 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 78 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 79 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 80 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 81 | data.password.cstring = "testpassword"; |
icraggs | 16:28d062c5522b | 82 | if ((rc = client.connect(data)) != 0) |
icraggs | 18:07a79d8f01c3 | 83 | printf("rc from MQTT connect is %d\n", rc); |
icraggs | 2:638c854c0695 | 84 | |
icraggs | 19:7f7aba7a4a8b | 85 | if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) |
icraggs | 18:07a79d8f01c3 | 86 | printf("rc from MQTT subscribe is %d\n", rc); |
icraggs | 2:638c854c0695 | 87 | |
icraggs | 2:638c854c0695 | 88 | MQTT::Message message; |
icraggs | 0:0cae29831d01 | 89 | |
icraggs | 2:638c854c0695 | 90 | // QoS 0 |
icraggs | 2:638c854c0695 | 91 | char buf[100]; |
co838_pg297 | 20:63e59fe8d75d | 92 | sprintf(buf, "door opened %f\n", version); |
icraggs | 2:638c854c0695 | 93 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 94 | message.retained = false; |
icraggs | 2:638c854c0695 | 95 | message.dup = false; |
icraggs | 2:638c854c0695 | 96 | message.payload = (void*)buf; |
icraggs | 2:638c854c0695 | 97 | message.payloadlen = strlen(buf)+1; |
icraggs | 16:28d062c5522b | 98 | rc = client.publish(topic, message); |
icraggs | 12:086a9314e8a5 | 99 | while (arrivedcount < 1) |
icraggs | 2:638c854c0695 | 100 | client.yield(100); |
icraggs | 2:638c854c0695 | 101 | |
co838_pg297 | 20:63e59fe8d75d | 102 | |
icraggs | 17:0811bdbdd78a | 103 | |
icraggs | 17:0811bdbdd78a | 104 | if ((rc = client.unsubscribe(topic)) != 0) |
icraggs | 18:07a79d8f01c3 | 105 | printf("rc from unsubscribe was %d\n", rc); |
icraggs | 2:638c854c0695 | 106 | |
icraggs | 8:a3e3113054a1 | 107 | if ((rc = client.disconnect()) != 0) |
icraggs | 18:07a79d8f01c3 | 108 | printf("rc from disconnect was %d\n", rc); |
icraggs | 2:638c854c0695 | 109 | |
icraggs | 2:638c854c0695 | 110 | ipstack.disconnect(); |
icraggs | 2:638c854c0695 | 111 | |
icraggs | 18:07a79d8f01c3 | 112 | printf("Version %.2f: finish %d msgs\n", version, arrivedcount); |
icraggs | 2:638c854c0695 | 113 | |
icraggs | 0:0cae29831d01 | 114 | return 0; |
icraggs | 0:0cae29831d01 | 115 | } |
co838_pg297 | 20:63e59fe8d75d | 116 | } |