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 hw4f HW4 MQTT
Fork of HW4 by
main.cpp@23:d508f2806faa, 2018-01-05 (annotated)
- Committer:
- Hyoeunchoi
- Date:
- Fri Jan 05 17:51:46 2018 +0000
- Revision:
- 23:d508f2806faa
- Parent:
- 21:a68bd76740f9
- Child:
- 24:4b8390b87ab3
gw4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Jan Jongboom |
21:a68bd76740f9 | 1 | // change this to 1 to output messages to LCD instead of serial |
| Jan Jongboom |
20:49c9daf2b0ff | 2 | #define logMessage printf |
| icraggs | 17:0811bdbdd78a | 3 | #define MQTTCLIENT_QOS2 1 |
| icraggs | 0:0cae29831d01 | 4 | |
| Hyoeunchoi | 23:d508f2806faa | 5 | #include <string> |
| Hyoeunchoi | 23:d508f2806faa | 6 | #include "mbed.h" |
| Jan Jongboom |
20:49c9daf2b0ff | 7 | #include "easy-connect.h" |
| Jan Jongboom |
20:49c9daf2b0ff | 8 | #include "MQTTNetwork.h" |
| Jan Jongboom |
20:49c9daf2b0ff | 9 | #include "MQTTmbed.h" |
| icraggs | 2:638c854c0695 | 10 | #include "MQTTClient.h" |
| Hyoeunchoi | 23:d508f2806faa | 11 | #include "TCPSocket.h" |
| Hyoeunchoi | 23:d508f2806faa | 12 | #include "DHT22.h" |
| icraggs | 2:638c854c0695 | 13 | |
| Hyoeunchoi | 23:d508f2806faa | 14 | #define WIFI_HW_RESET_PIN D4 |
| Hyoeunchoi | 23:d508f2806faa | 15 | |
| Hyoeunchoi | 23:d508f2806faa | 16 | #define MQTT_BROKER_ADDR "192.168.0.10" |
| Hyoeunchoi | 23:d508f2806faa | 17 | #define MQTT_BROKER_PORT 1883 |
| icraggs | 2:638c854c0695 | 18 | |
| Hyoeunchoi | 23:d508f2806faa | 19 | DigitalOut wifiHwResetPin(WIFI_HW_RESET_PIN); |
| Hyoeunchoi | 23:d508f2806faa | 20 | DigitalOut led2(LED2); |
| Hyoeunchoi | 23:d508f2806faa | 21 | DHT22 dht22(D7); |
| Hyoeunchoi | 23:d508f2806faa | 22 | Serial pc(USBTX, USBRX, 115200); |
| Hyoeunchoi | 23:d508f2806faa | 23 | |
| Hyoeunchoi | 23:d508f2806faa | 24 | volatile bool flag = false; |
| Hyoeunchoi | 23:d508f2806faa | 25 | |
| Hyoeunchoi | 23:d508f2806faa | 26 | Mutex mqttMtx; |
| Hyoeunchoi | 23:d508f2806faa | 27 | |
| Hyoeunchoi | 23:d508f2806faa | 28 | char* topic_toGW = "toGW"; |
| Hyoeunchoi | 23:d508f2806faa | 29 | char* topic_fromGW = "toNUCLEO"; |
| icraggs | 8:a3e3113054a1 | 30 | |
| icraggs | 9:5beb8609e9f7 | 31 | void messageArrived(MQTT::MessageData& md) |
| icraggs | 2:638c854c0695 | 32 | { |
| icraggs | 9:5beb8609e9f7 | 33 | MQTT::Message &message = md.message; |
| Hyoeunchoi | 23:d508f2806faa | 34 | //logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
| Hyoeunchoi | 23:d508f2806faa | 35 | //logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
| Hyoeunchoi | 23:d508f2806faa | 36 | |
| Hyoeunchoi | 23:d508f2806faa | 37 | pc.printf("Subscribe from GW: %.*s\r\n", message.payloadlen, (char*)message.payload); |
| Hyoeunchoi | 23:d508f2806faa | 38 | char fwdTarget = ((char*)message.payload)[0]; |
| Hyoeunchoi | 23:d508f2806faa | 39 | switch(fwdTarget){ |
| Hyoeunchoi | 23:d508f2806faa | 40 | case '0': |
| Hyoeunchoi | 23:d508f2806faa | 41 | led2 = 0; |
| Hyoeunchoi | 23:d508f2806faa | 42 | pc.printf("LED Turn Off\r\n\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 43 | break; |
| Hyoeunchoi | 23:d508f2806faa | 44 | case '1': |
| Hyoeunchoi | 23:d508f2806faa | 45 | led2 = 1; |
| Hyoeunchoi | 23:d508f2806faa | 46 | pc.printf("LED Turn On\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 47 | break; |
| Hyoeunchoi | 23:d508f2806faa | 48 | default: |
| Hyoeunchoi | 23:d508f2806faa | 49 | pc.printf("Unknown MQTT message\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 50 | break; |
| Hyoeunchoi | 23:d508f2806faa | 51 | } |
| icraggs | 2:638c854c0695 | 52 | } |
| icraggs | 0:0cae29831d01 | 53 | |
| Hyoeunchoi | 23:d508f2806faa | 54 | double getTemp(void){ |
| Hyoeunchoi | 23:d508f2806faa | 55 | double temp; |
| Hyoeunchoi | 23:d508f2806faa | 56 | if(dht22.sample()){ |
| Hyoeunchoi | 23:d508f2806faa | 57 | temp=dht22.getTemperature()/10.0; |
| Hyoeunchoi | 23:d508f2806faa | 58 | pc.printf("temperature: %3.1f\r\n",temp); |
| Hyoeunchoi | 23:d508f2806faa | 59 | } else{ |
| Hyoeunchoi | 23:d508f2806faa | 60 | temp=-999; |
| Hyoeunchoi | 23:d508f2806faa | 61 | pc.printf("Checksum error\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 62 | } |
| Hyoeunchoi | 23:d508f2806faa | 63 | return temp; |
| Hyoeunchoi | 23:d508f2806faa | 64 | } |
| icraggs | 2:638c854c0695 | 65 | |
| icraggs | 2:638c854c0695 | 66 | int main(int argc, char* argv[]) |
| Jan Jongboom |
20:49c9daf2b0ff | 67 | { |
| Hyoeunchoi | 23:d508f2806faa | 68 | float version = 0.5; |
| Hyoeunchoi | 23:d508f2806faa | 69 | NetworkInterface* net = easy_connect(true); |
| Hyoeunchoi | 23:d508f2806faa | 70 | if (!net) { |
| Hyoeunchoi | 23:d508f2806faa | 71 | pc.printf("Error in easy connection\r\n"); |
| Jan Jongboom |
20:49c9daf2b0ff | 72 | return -1; |
| Jan Jongboom |
20:49c9daf2b0ff | 73 | } |
| Hyoeunchoi | 23:d508f2806faa | 74 | logMessage("HelloMQTT: version is %.2f\r\n", version); |
| Jan Jongboom |
20:49c9daf2b0ff | 75 | |
| Hyoeunchoi | 23:d508f2806faa | 76 | wait(0.2); |
| Hyoeunchoi | 23:d508f2806faa | 77 | pc.printf("Resetting ESP8266 Hardware...\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 78 | wifiHwResetPin = 0; |
| Hyoeunchoi | 23:d508f2806faa | 79 | wait_ms(500); |
| Hyoeunchoi | 23:d508f2806faa | 80 | wifiHwResetPin = 1; |
| Hyoeunchoi | 23:d508f2806faa | 81 | |
| Hyoeunchoi | 23:d508f2806faa | 82 | MQTTNetwork mqttNetwork(net); |
| Jan Jongboom |
21:a68bd76740f9 | 83 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
| Jan Jongboom |
20:49c9daf2b0ff | 84 | |
| Hyoeunchoi | 23:d508f2806faa | 85 | logMessage("Connecting to %s:%d\r\n", MQTT_BROKER_ADDR, MQTT_BROKER_PORT); |
| Hyoeunchoi | 23:d508f2806faa | 86 | int rc = mqttNetwork.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT); |
| icraggs | 6:e4c690c45021 | 87 | if (rc != 0) |
| Hyoeunchoi | 23:d508f2806faa | 88 | logMessage("rc from GW connect is %d\r\n", rc); |
| Jan Jongboom |
20:49c9daf2b0ff | 89 | |
| Jan Jongboom |
20:49c9daf2b0ff | 90 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
| icraggs | 6:e4c690c45021 | 91 | data.MQTTVersion = 3; |
| Hyoeunchoi | 23:d508f2806faa | 92 | data.clientID.cstring = "mbed-sample-Hyoeun"; |
| icraggs | 12:086a9314e8a5 | 93 | data.username.cstring = "testuser"; |
| icraggs | 12:086a9314e8a5 | 94 | data.password.cstring = "testpassword"; |
| icraggs | 16:28d062c5522b | 95 | if ((rc = client.connect(data)) != 0) |
| Jan Jongboom |
20:49c9daf2b0ff | 96 | logMessage("rc from MQTT connect is %d\r\n", rc); |
| Jan Jongboom |
20:49c9daf2b0ff | 97 | |
| Hyoeunchoi | 23:d508f2806faa | 98 | if ((rc = client.subscribe(topic_fromGW, MQTT::QOS2, messageArrived)) != 0) |
| Jan Jongboom |
20:49c9daf2b0ff | 99 | logMessage("rc from MQTT subscribe is %d\r\n", rc); |
| icraggs | 2:638c854c0695 | 100 | |
| Hyoeunchoi | 23:d508f2806faa | 101 | int count = 0; |
| Hyoeunchoi | 23:d508f2806faa | 102 | while(1){ |
| Hyoeunchoi | 23:d508f2806faa | 103 | Thread::wait(1000); |
| Hyoeunchoi | 23:d508f2806faa | 104 | char buf[100]; |
| Hyoeunchoi | 23:d508f2806faa | 105 | MQTT::Message message; |
| Hyoeunchoi | 23:d508f2806faa | 106 | |
| Hyoeunchoi | 23:d508f2806faa | 107 | if(count == 5){ |
| Hyoeunchoi | 23:d508f2806faa | 108 | pc.printf("Publish: toGW\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 109 | sprintf(buf, "{\"temp\": %3.1f}", getTemp()); |
| Hyoeunchoi | 23:d508f2806faa | 110 | message.qos = MQTT::QOS0; |
| Hyoeunchoi | 23:d508f2806faa | 111 | message.retained = false; |
| Hyoeunchoi | 23:d508f2806faa | 112 | message.dup = false; |
| Hyoeunchoi | 23:d508f2806faa | 113 | message.payload = (void*)buf; |
| Hyoeunchoi | 23:d508f2806faa | 114 | message.payloadlen = strlen(buf)+1; |
| Hyoeunchoi | 23:d508f2806faa | 115 | pc.printf("%s",message); |
| Hyoeunchoi | 23:d508f2806faa | 116 | mqttMtx.lock(); |
| Hyoeunchoi | 23:d508f2806faa | 117 | rc = client.publish(topic_toGW, message); |
| Hyoeunchoi | 23:d508f2806faa | 118 | mqttMtx.unlock(); |
| Hyoeunchoi | 23:d508f2806faa | 119 | count = 0; |
| Hyoeunchoi | 23:d508f2806faa | 120 | pc.printf("\r\n"); |
| Hyoeunchoi | 23:d508f2806faa | 121 | } |
| Hyoeunchoi | 23:d508f2806faa | 122 | if(!client.isConnected()) NVIC_SystemReset(); |
| Hyoeunchoi | 23:d508f2806faa | 123 | client.yield(10); |
| Hyoeunchoi | 23:d508f2806faa | 124 | count++; |
| Hyoeunchoi | 23:d508f2806faa | 125 | } |
| Hyoeunchoi | 23:d508f2806faa | 126 | } |
| Jan Jongboom |
20:49c9daf2b0ff | 127 |
