![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
HW4
Fork of HelloMQTT by
main.cpp@23:e2ab0669caeb, 2018-01-05 (annotated)
- Committer:
- Dongho
- Date:
- Fri Jan 05 06:36:32 2018 +0000
- Revision:
- 23:e2ab0669caeb
- Parent:
- 21:a68bd76740f9
HW4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
20:49c9daf2b0ff | 1 | #define logMessage printf |
icraggs | 17:0811bdbdd78a | 2 | #define MQTTCLIENT_QOS2 1 |
icraggs | 0:0cae29831d01 | 3 | |
Dongho | 23:e2ab0669caeb | 4 | #include "mbed.h" |
Jan Jongboom |
20:49c9daf2b0ff | 5 | #include "easy-connect.h" |
Jan Jongboom |
20:49c9daf2b0ff | 6 | #include "MQTTNetwork.h" |
Jan Jongboom |
20:49c9daf2b0ff | 7 | #include "MQTTmbed.h" |
icraggs | 2:638c854c0695 | 8 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 9 | |
Dongho | 23:e2ab0669caeb | 10 | #define ADT7420_TEMP_REG (0x00) // Temperature Register |
Dongho | 23:e2ab0669caeb | 11 | #define ADT7420_CONF_REG (0x03) // Configuration Register |
Dongho | 23:e2ab0669caeb | 12 | #define EVAL_ADT7420_ADDR (0x48) // EVAL_ADT7420 i2c address |
Dongho | 23:e2ab0669caeb | 13 | |
Dongho | 23:e2ab0669caeb | 14 | |
Dongho | 23:e2ab0669caeb | 15 | /* connect this pin to both the CH_PD (aka EN) & RST pins on the ESP8266 just in case */ |
Dongho | 23:e2ab0669caeb | 16 | #define WIFI_HW_RESET_PIN D4 |
icraggs | 2:638c854c0695 | 17 | |
Dongho | 23:e2ab0669caeb | 18 | /* See if you can try using a hostname here */ |
Dongho | 23:e2ab0669caeb | 19 | #define MQTT_BROKER_ADDR "192.168.0.14" |
Dongho | 23:e2ab0669caeb | 20 | #define MQTT_BROKER_PORT 1883 |
Dongho | 23:e2ab0669caeb | 21 | |
Dongho | 23:e2ab0669caeb | 22 | DigitalOut wifiHwResetPin(WIFI_HW_RESET_PIN); |
Dongho | 23:e2ab0669caeb | 23 | DigitalOut led2(LED2); |
Dongho | 23:e2ab0669caeb | 24 | Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); |
Dongho | 23:e2ab0669caeb | 25 | I2C i2c(I2C_SDA, I2C_SCL); |
Dongho | 23:e2ab0669caeb | 26 | |
Dongho | 23:e2ab0669caeb | 27 | volatile bool flag = false; |
Dongho | 23:e2ab0669caeb | 28 | Mutex mqttMtx; |
Dongho | 23:e2ab0669caeb | 29 | |
Dongho | 23:e2ab0669caeb | 30 | char* topic1 = "LED"; |
Dongho | 23:e2ab0669caeb | 31 | char* topic2 = "TEMP"; |
icraggs | 8:a3e3113054a1 | 32 | |
icraggs | 9:5beb8609e9f7 | 33 | void messageArrived(MQTT::MessageData& md) |
icraggs | 2:638c854c0695 | 34 | { |
icraggs | 9:5beb8609e9f7 | 35 | MQTT::Message &message = md.message; |
Dongho | 23:e2ab0669caeb | 36 | |
Dongho | 23:e2ab0669caeb | 37 | /* our messaging standard says the first byte denotes which thread to fwd to */ |
Dongho | 23:e2ab0669caeb | 38 | logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", |
Dongho | 23:e2ab0669caeb | 39 | message.qos, message.retained, message.dup, message.id); |
Jan Jongboom |
20:49c9daf2b0ff | 40 | logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
Dongho | 23:e2ab0669caeb | 41 | |
Dongho | 23:e2ab0669caeb | 42 | char fwdTarget = ((char *)message.payload)[0]; |
Dongho | 23:e2ab0669caeb | 43 | switch(fwdTarget) |
Dongho | 23:e2ab0669caeb | 44 | { |
Dongho | 23:e2ab0669caeb | 45 | case '0': // turn off |
Dongho | 23:e2ab0669caeb | 46 | led2 = 0; |
Dongho | 23:e2ab0669caeb | 47 | break; |
Dongho | 23:e2ab0669caeb | 48 | |
Dongho | 23:e2ab0669caeb | 49 | case '1': //turn on |
Dongho | 23:e2ab0669caeb | 50 | led2 = 1; |
Dongho | 23:e2ab0669caeb | 51 | break; |
Dongho | 23:e2ab0669caeb | 52 | |
Dongho | 23:e2ab0669caeb | 53 | default: |
Dongho | 23:e2ab0669caeb | 54 | pc.printf("Unknown MQTT message\n"); |
Dongho | 23:e2ab0669caeb | 55 | break; |
Dongho | 23:e2ab0669caeb | 56 | } |
icraggs | 2:638c854c0695 | 57 | } |
icraggs | 0:0cae29831d01 | 58 | |
icraggs | 2:638c854c0695 | 59 | int main(int argc, char* argv[]) |
Jan Jongboom |
20:49c9daf2b0ff | 60 | { |
Dongho | 23:e2ab0669caeb | 61 | float version = 0.5; |
Dongho | 23:e2ab0669caeb | 62 | pc.baud(115200); |
Dongho | 23:e2ab0669caeb | 63 | logMessage("MQTT example: version is %.2f\r\n", version); |
Dongho | 23:e2ab0669caeb | 64 | |
Dongho | 23:e2ab0669caeb | 65 | wait(0.2); //delay startup |
Dongho | 23:e2ab0669caeb | 66 | pc.printf("Resetting ESP8266 Hardware...\r\n"); |
Jan Jongboom |
20:49c9daf2b0ff | 67 | |
Dongho | 23:e2ab0669caeb | 68 | wifiHwResetPin = 0; |
Dongho | 23:e2ab0669caeb | 69 | wait_ms(500); |
Dongho | 23:e2ab0669caeb | 70 | wifiHwResetPin = 1; |
Dongho | 23:e2ab0669caeb | 71 | pc.printf("Starting MQTT example with an ESP8266 wifi device using Mbed OS.\r\n"); |
Dongho | 23:e2ab0669caeb | 72 | pc.printf("Attempting to connect to access point...\r\n"); |
Jan Jongboom |
20:49c9daf2b0ff | 73 | |
Jan Jongboom |
20:49c9daf2b0ff | 74 | NetworkInterface* network = easy_connect(true); |
Jan Jongboom |
20:49c9daf2b0ff | 75 | if (!network) { |
Dongho | 23:e2ab0669caeb | 76 | pc.printf("Error in east connection\r\n"); |
Jan Jongboom |
20:49c9daf2b0ff | 77 | return -1; |
Jan Jongboom |
20:49c9daf2b0ff | 78 | } |
Dongho | 23:e2ab0669caeb | 79 | |
Jan Jongboom |
20:49c9daf2b0ff | 80 | MQTTNetwork mqttNetwork(network); |
Jan Jongboom |
21:a68bd76740f9 | 81 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
Dongho | 23:e2ab0669caeb | 82 | logMessage("Connecting to %s:%d\r\n", MQTT_BROKER_ADDR, MQTT_BROKER_PORT); |
Dongho | 23:e2ab0669caeb | 83 | int rc = mqttNetwork.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT); |
Jan Jongboom |
20:49c9daf2b0ff | 84 | |
icraggs | 6:e4c690c45021 | 85 | if (rc != 0) |
Jan Jongboom |
20:49c9daf2b0ff | 86 | logMessage("rc from TCP connect is %d\r\n", rc); |
Jan Jongboom |
20:49c9daf2b0ff | 87 | |
Jan Jongboom |
20:49c9daf2b0ff | 88 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 89 | data.MQTTVersion = 3; |
Dongho | 23:e2ab0669caeb | 90 | data.clientID.cstring = "mbed-kdh"; |
Dongho | 23:e2ab0669caeb | 91 | data.username.cstring = "kdh"; |
Dongho | 23:e2ab0669caeb | 92 | data.password.cstring = "2ehd2gh4"; |
Dongho | 23:e2ab0669caeb | 93 | |
icraggs | 16:28d062c5522b | 94 | if ((rc = client.connect(data)) != 0) |
Jan Jongboom |
20:49c9daf2b0ff | 95 | logMessage("rc from MQTT connect is %d\r\n", rc); |
Jan Jongboom |
20:49c9daf2b0ff | 96 | |
Dongho | 23:e2ab0669caeb | 97 | if ((rc = client.subscribe(topic1, MQTT::QOS0, messageArrived)) != 0) |
Jan Jongboom |
20:49c9daf2b0ff | 98 | logMessage("rc from MQTT subscribe is %d\r\n", rc); |
icraggs | 2:638c854c0695 | 99 | |
Dongho | 23:e2ab0669caeb | 100 | int count = 0; |
Dongho | 23:e2ab0669caeb | 101 | |
Dongho | 23:e2ab0669caeb | 102 | int status; |
Dongho | 23:e2ab0669caeb | 103 | float temp; |
Dongho | 23:e2ab0669caeb | 104 | char data_write[2]; |
Dongho | 23:e2ab0669caeb | 105 | char data_read[2]; |
Dongho | 23:e2ab0669caeb | 106 | |
Dongho | 23:e2ab0669caeb | 107 | i2c.frequency(100000); // 100 k |
Dongho | 23:e2ab0669caeb | 108 | |
Dongho | 23:e2ab0669caeb | 109 | // 13-bit resolution and 1 SPS mode |
Dongho | 23:e2ab0669caeb | 110 | data_write[0] = ADT7420_CONF_REG; |
Dongho | 23:e2ab0669caeb | 111 | data_write[1] = 0x40; |
Dongho | 23:e2ab0669caeb | 112 | status = i2c.write((EVAL_ADT7420_ADDR << 1), data_write, 2); |
icraggs | 0:0cae29831d01 | 113 | |
Dongho | 23:e2ab0669caeb | 114 | if (status != 0) { // error |
Dongho | 23:e2ab0669caeb | 115 | char buf[100]; |
Dongho | 23:e2ab0669caeb | 116 | MQTT::Message message; |
Dongho | 23:e2ab0669caeb | 117 | |
Dongho | 23:e2ab0669caeb | 118 | pc.printf("I2C configuration error!!\r\n"); |
Dongho | 23:e2ab0669caeb | 119 | sprintf(buf, "{ \"actions\": [ { \"name\": 'error' , \"parameter\": \"NaN\" } ] }"); |
Dongho | 23:e2ab0669caeb | 120 | pc.printf("%s\r\n", buf); |
Dongho | 23:e2ab0669caeb | 121 | |
Dongho | 23:e2ab0669caeb | 122 | message.qos = MQTT::QOS2; |
Dongho | 23:e2ab0669caeb | 123 | message.retained = false; |
Dongho | 23:e2ab0669caeb | 124 | message.dup = false; |
Dongho | 23:e2ab0669caeb | 125 | message.payload = (void*)buf; |
Dongho | 23:e2ab0669caeb | 126 | message.payloadlen = strlen(buf)+1; |
Dongho | 23:e2ab0669caeb | 127 | mqttMtx.lock(); |
Dongho | 23:e2ab0669caeb | 128 | |
Dongho | 23:e2ab0669caeb | 129 | rc = client.publish(topic2, message); |
Dongho | 23:e2ab0669caeb | 130 | mqttMtx.unlock(); |
Dongho | 23:e2ab0669caeb | 131 | |
Dongho | 23:e2ab0669caeb | 132 | while(1) { |
Dongho | 23:e2ab0669caeb | 133 | } |
Dongho | 23:e2ab0669caeb | 134 | } |
Dongho | 23:e2ab0669caeb | 135 | |
Dongho | 23:e2ab0669caeb | 136 | while(1) { |
Dongho | 23:e2ab0669caeb | 137 | Thread::wait(1000); |
Dongho | 23:e2ab0669caeb | 138 | char buf[100]; |
Dongho | 23:e2ab0669caeb | 139 | MQTT::Message message; |
Jan Jongboom |
20:49c9daf2b0ff | 140 | |
Dongho | 23:e2ab0669caeb | 141 | if (count == 5) { |
Dongho | 23:e2ab0669caeb | 142 | |
Dongho | 23:e2ab0669caeb | 143 | // Read temperature register |
Dongho | 23:e2ab0669caeb | 144 | data_write[0] = ADT7420_TEMP_REG; |
Dongho | 23:e2ab0669caeb | 145 | i2c.write((EVAL_ADT7420_ADDR << 1), data_write, 1, 0); |
Dongho | 23:e2ab0669caeb | 146 | i2c.read(((EVAL_ADT7420_ADDR << 1) | 0x01), data_read, 2, 0); |
Dongho | 23:e2ab0669caeb | 147 | |
Dongho | 23:e2ab0669caeb | 148 | // Calculate temperature value in Celcius |
Dongho | 23:e2ab0669caeb | 149 | int tempval = ((int)data_read[0] << 8) | data_read[1]; |
Dongho | 23:e2ab0669caeb | 150 | tempval >>= 3; |
Jan Jongboom |
20:49c9daf2b0ff | 151 | |
Dongho | 23:e2ab0669caeb | 152 | // tempval = 0x1e70; //test value for a negative temperature (-25.0) |
Dongho | 23:e2ab0669caeb | 153 | if ((tempval & 0x1000) > 0) { //negative value |
Dongho | 23:e2ab0669caeb | 154 | temp = (tempval - 8192)/16.0; |
Dongho | 23:e2ab0669caeb | 155 | } else { |
Dongho | 23:e2ab0669caeb | 156 | temp = tempval/16.0; |
Dongho | 23:e2ab0669caeb | 157 | } |
Dongho | 23:e2ab0669caeb | 158 | |
Dongho | 23:e2ab0669caeb | 159 | pc.printf("Publish: TEMP\r\n"); |
Dongho | 23:e2ab0669caeb | 160 | sprintf(buf, "%4.2f", temp); |
Dongho | 23:e2ab0669caeb | 161 | pc.printf("%s\r\n", buf); |
Dongho | 23:e2ab0669caeb | 162 | |
Dongho | 23:e2ab0669caeb | 163 | message.qos = MQTT::QOS2; |
Dongho | 23:e2ab0669caeb | 164 | message.retained = false; |
Dongho | 23:e2ab0669caeb | 165 | message.dup = false; |
Dongho | 23:e2ab0669caeb | 166 | message.payload = (void*)buf; |
Dongho | 23:e2ab0669caeb | 167 | message.payloadlen = strlen(buf)+1; |
Dongho | 23:e2ab0669caeb | 168 | mqttMtx.lock(); |
Dongho | 23:e2ab0669caeb | 169 | |
Dongho | 23:e2ab0669caeb | 170 | rc = client.publish(topic2, message); |
Dongho | 23:e2ab0669caeb | 171 | mqttMtx.unlock(); |
Dongho | 23:e2ab0669caeb | 172 | count = 0; |
Dongho | 23:e2ab0669caeb | 173 | } |
Jan Jongboom |
20:49c9daf2b0ff | 174 | |
Dongho | 23:e2ab0669caeb | 175 | if(!client.isConnected()) |
Dongho | 23:e2ab0669caeb | 176 | NVIC_SystemReset(); // soft reset |
Jan Jongboom |
20:49c9daf2b0ff | 177 | |
Dongho | 23:e2ab0669caeb | 178 | /* yield() needs to be called at least once per keepAliveInterval */ |
Dongho | 23:e2ab0669caeb | 179 | client.yield(10); |
Dongho | 23:e2ab0669caeb | 180 | count++; |
Dongho | 23:e2ab0669caeb | 181 | } |
icraggs | 0:0cae29831d01 | 182 | return 0; |
Dongho | 23:e2ab0669caeb | 183 | } |