Kim jihyeon
/
HelloMQTT_jh
,ss
Fork of HelloMQTT by
Revision 24:9269143e00f4, committed 2018-01-05
- Comitter:
- 2814970
- Date:
- Fri Jan 05 16:27:00 2018 +0000
- Parent:
- 23:e3422d0d3cc2
- Commit message:
- jihyeon hw4;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed_app.json | Show annotated file Show diff for this revision Revisions of this file |
diff -r e3422d0d3cc2 -r 9269143e00f4 main.cpp --- a/main.cpp Fri Jan 05 12:20:31 2018 +0000 +++ b/main.cpp Fri Jan 05 16:27:00 2018 +0000 @@ -1,165 +1,154 @@ -/* -#include "C12832.h" +#define logMessage printf +#define MQTTCLIENT_QOS2 1 + +#include <string> +#include "mbed.h" #include "easy-connect.h" +#include "TCPSocket.h" +#include "DHT.h" #include "MQTTNetwork.h" #include "MQTTmbed.h" #include "MQTTClient.h" -*/ -#define logMessage printf -//#define MQTTCLIENT_QOS2 1 -#include "mbed.h" -#include "easy-connect.h" -#include "MQTTNetwork.h" -#include "MQTTmbed.h" -#include "MQTTClient.h" -#include "DHT.h" /* connect this pin to both the CH_PD (aka EN) & RST pins on the ESP8266 just in case */ #define WIFI_HW_RESET_PIN D4 + /* See if you can try using a hostname here */ #define MQTT_BROKER_ADDR "192.168.0.9" #define MQTT_BROKER_PORT 1883 -DHT sensor(D7,DHT22); + +Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); +DHT sensor(D7, DHT22); + DigitalOut wifiHwResetPin(WIFI_HW_RESET_PIN); DigitalOut led2(LED2); -Serial pc(USBTX, USBRX); // computer to mbedboardSerialesp(D1, D0); - - - volatile bool flag = false; Mutex mqttMtx; -float t = 0; +char* topic1 = "LED2"; +char* topic2 = "Temp"; +/*char* topic3 = "setOff"; +char* topic4 = "setOn";*/ + +void task_DHT(); -char* topic1 = "LED"; -char* topic2 = "TEMP"; -char* topic3 = "LEDS"; -void messageArrived(MQTT::MessageData& md) -{ +void messageArrived(MQTT::MessageData& md){ + MQTT::Message &message = md.message; + - MQTT::Message &message = md.message; - /* our messaging standard says the first byte denotes which thread to fwdto */ - logMessage("Message arrived: qos%d, retained %d, dup %d, packetid%d\r\n", message.qos, message.retained, message.dup, message.id); + logMessage("\rMessage arrived: qos %d, retained %d, dup %d, packetid %d\r\n", + message.qos, message.retained, message.dup, message.id); + logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); - char fwdTarget= ((char *)message.payload)[0]; - switch(fwdTarget) - { + char fwdTarget = ((char *)message.payload)[0]; + + switch(fwdTarget){ case '0': // turn off - led2 = 0; - break; - + led2 = 0; + logMessage("setOff: turn off the Red\r\n\r\n"); + break; + case '1': //turn on - led2 = 1; - break; - - case '2': //LED status - flag = true; - break; + led2 = 1; + logMessage("setOff: turn on the Red\r\n\r\n"); + break; default: - pc.printf("Unknown MQTT message\n"); - break; - } + pc.printf("Unknown MQTT msg\r\n\r\n"); + break; } +} -int main(int argc, char* argv[]) -{ +int main(int argc, char* argv[]){ + + float version = 0.5; pc.baud(115200); + logMessage("MQTT example: version is %.2f\r\n", version); wait(0.2); //delay startup - pc.printf("Resetting ESP8266 Hardware...\r\n"); - wifiHwResetPin= 0; - wait_ms(500); - wifiHwResetPin= 1; - pc.printf("Starting MQTT example with an ESP8266 wifidevice using MbedOS.\r\n"); + + pc.printf("\rResetting ESP8266 Hardware...\r\n"); + wifiHwResetPin = 0; + wait_ms(100); + wifiHwResetPin = 1; + + pc.printf("Starting MQTT example with an ESP8266 wifi device using Mbed OS.\r\n"); pc.printf("Attempting to connect to access point...\r\n"); - + NetworkInterface* network = easy_connect(true); + if (!network) { pc.printf("Error in east connection\r\n"); return -1; } - + MQTTNetwork mqttNetwork(network); + MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); logMessage("Connecting to %s:%d\r\n", MQTT_BROKER_ADDR, MQTT_BROKER_PORT); - int rc= mqttNetwork.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT); - - if (rc!= 0) - logMessage("rcfrom TCP connect is %d\r\n", rc); + int rc = mqttNetwork.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT); + if (rc != 0) + logMessage("rc from TCP connect is %d\r\n", rc); + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "mbed-jihyeon"; + data.username.cstring = "user-jihyeon"; + data.password.cstring = "testpassword"; - MQTTPacket_connectData data= MQTTPacket_connectData_initializer; - data.MQTTVersion= 3; - data.clientID.cstring= "mbed-ljw"; - data.username.cstring= "user-ljw"; - data.password.cstring= "testpassword"; - - if ((rc= client.connect(data)) != 0) - logMessage("rcfrom MQTT connect is %d\r\n", rc); - if ((rc= client.subscribe(topic1, MQTT::QOS0, messageArrived)) != 0) - logMessage("rcfrom MQTT subscribe is %d\r\n", rc); + if ((rc = client.connect(data)) != 0) + logMessage("rc from MQTT connect is %d\r\n", rc); + if ((rc = client.subscribe(topic1, MQTT::QOS0, messageArrived))&(rc = client.subscribe(topic2, MQTT::QOS0, messageArrived)) != 0) + logMessage("rc from MQTT subscribe is %d\r\n", rc); int count = 0; + + int error = 0; + int c; while(1) { - Thread::wait(1000); - - char buf[64]; - MQTT::Message message; - - - t = sensor.ReadTemperature(CELCIUS); - - - - if (count == 30) { - pc.printf("Publish: TEMP\r\n"); + error = sensor.readData(); - sprintf(buf, "{ \"temp\": \"%.1f\"}\r\n", t); - - message.qos= MQTT::QOS2; - message.retained= false; - message.dup= false; - message.payload= (void*)buf; - message.payloadlen= strlen(buf)+1; - mqttMtx.lock(); - rc= client.publish(topic2, message); - mqttMtx.unlock(); - count = 0; - - } + if (0 == error) { + + c = sensor.ReadTemperature(CELCIUS); - if (flag) { - pc.printf("Publish: LEDS\r\n"); - - if (led2 == 0) - sprintf(buf, "LED OFF"); - else - sprintf(buf, "LED ON"); + Thread::wait(1000); + char buf[5]; + MQTT::Message message; + + if (count == 5) { + pc.printf("\rPublish: temp(%d *C)\r\n", c); - message.qos= MQTT::QOS1; - message.retained= false; - message.dup= false; - message.payload= (void*)buf; - message.payloadlen= strlen(buf)+1; + sprintf(buf, "%d", c); + message.qos = MQTT::QOS2; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + mqttMtx.lock(); + rc = client.publish(topic2, message); + mqttMtx.unlock(); + count = 0; + } - mqttMtx.lock(); - rc= client.publish(topic3, message); - mqttMtx.unlock(); - flag = false; - } + if(!client.isConnected()) + NVIC_SystemReset(); // soft reset + - if(!client.isConnected()) - NVIC_SystemReset();// soft reset - /* yield() needs to be called at least once per keepAliveInterval*/ - client.yield(10); - count++; - } + client.yield(10); + count++; + } else { + printf("Error: %d\r\n", error); + } + } + return 0; } +
diff -r e3422d0d3cc2 -r 9269143e00f4 mbed_app.json --- a/mbed_app.json Fri Jan 05 12:20:31 2018 +0000 +++ b/mbed_app.json Fri Jan 05 16:27:00 2018 +0000 @@ -23,7 +23,7 @@ "value": "\"netlab3@414\"" }, "esp8266-debug": { - "value": true + "value": false }, "lcd-mosi": { "value": "D11",