Published
Dependencies: DHT MQTTforLecture WIZnetInterface mbed-src
Fork of w7500-paho-mqtt by
main.cpp@9:21574e058141, 2015-06-28 (annotated)
- Committer:
- bangbh
- Date:
- Sun Jun 28 22:48:46 2015 +0000
- Revision:
- 9:21574e058141
- Parent:
- 8:f837e0d255e8
- Child:
- 13:36456500fb7e
Paho MQTT Client with W7500.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
justinkim | 8:f837e0d255e8 | 1 | #include "mbed.h" |
bangbh | 9:21574e058141 | 2 | #include "DHT.h" |
bangbh | 9:21574e058141 | 3 | #include "MQTTEthernet.h" |
bangbh | 9:21574e058141 | 4 | #include "MQTTClient.h" |
justinkim | 8:f837e0d255e8 | 5 | |
justinkim | 8:f837e0d255e8 | 6 | #define ECHO_SERVER_PORT 7 |
justinkim | 8:f837e0d255e8 | 7 | |
bangbh | 9:21574e058141 | 8 | #include "mbed.h" |
bangbh | 9:21574e058141 | 9 | |
bangbh | 9:21574e058141 | 10 | int arrivedcount = 0; |
bangbh | 9:21574e058141 | 11 | |
bangbh | 9:21574e058141 | 12 | void messageArrived(MQTT::MessageData& md) |
justinkim | 8:f837e0d255e8 | 13 | { |
bangbh | 9:21574e058141 | 14 | MQTT::Message &message = md.message; |
bangbh | 9:21574e058141 | 15 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
bangbh | 9:21574e058141 | 16 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
bangbh | 9:21574e058141 | 17 | ++arrivedcount; |
bangbh | 9:21574e058141 | 18 | } |
bangbh | 9:21574e058141 | 19 | |
bangbh | 9:21574e058141 | 20 | void baud(int baudrate) { |
bangbh | 9:21574e058141 | 21 | Serial s(USBTX, USBRX); |
bangbh | 9:21574e058141 | 22 | s.baud(baudrate); |
bangbh | 9:21574e058141 | 23 | } |
bangbh | 9:21574e058141 | 24 | |
bangbh | 9:21574e058141 | 25 | int main (void) |
bangbh | 9:21574e058141 | 26 | { |
bangbh | 9:21574e058141 | 27 | DHT sensor(D4, DHT11); |
bangbh | 9:21574e058141 | 28 | AnalogIn ain(A1); |
bangbh | 9:21574e058141 | 29 | baud(115200); |
justinkim | 8:f837e0d255e8 | 30 | printf("Wait a second...\r\n"); |
bangbh | 9:21574e058141 | 31 | char* topic = "hello/wiznet"; |
bangbh | 9:21574e058141 | 32 | MQTTEthernet ipstack = MQTTEthernet(); |
bangbh | 9:21574e058141 | 33 | |
bangbh | 9:21574e058141 | 34 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
bangbh | 9:21574e058141 | 35 | |
bangbh | 9:21574e058141 | 36 | char* hostname = "104.199.146.45"; |
bangbh | 9:21574e058141 | 37 | int port = 1883; |
bangbh | 9:21574e058141 | 38 | |
bangbh | 9:21574e058141 | 39 | int rc = ipstack.connect(hostname, port); |
bangbh | 9:21574e058141 | 40 | if (rc != 0) |
bangbh | 9:21574e058141 | 41 | printf("rc from TCP connect is %d\n", rc); |
bangbh | 9:21574e058141 | 42 | |
bangbh | 9:21574e058141 | 43 | printf("Topic: %s\r\n",topic); |
justinkim | 8:f837e0d255e8 | 44 | |
bangbh | 9:21574e058141 | 45 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
bangbh | 9:21574e058141 | 46 | data.MQTTVersion = 3; |
bangbh | 9:21574e058141 | 47 | data.clientID.cstring = "WIZwiki-W7500"; |
bangbh | 9:21574e058141 | 48 | data.username.cstring = "testuser"; |
bangbh | 9:21574e058141 | 49 | data.password.cstring = "testpassword"; |
bangbh | 9:21574e058141 | 50 | |
bangbh | 9:21574e058141 | 51 | if ((rc = client.connect(data)) != 0) |
bangbh | 9:21574e058141 | 52 | printf("rc from MQTT connect is %d\n", rc); |
bangbh | 9:21574e058141 | 53 | |
bangbh | 9:21574e058141 | 54 | if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) |
bangbh | 9:21574e058141 | 55 | printf("rc from MQTT subscribe is %d\n", rc); |
bangbh | 9:21574e058141 | 56 | |
bangbh | 9:21574e058141 | 57 | MQTT::Message message; |
bangbh | 9:21574e058141 | 58 | char buf[100]; |
bangbh | 9:21574e058141 | 59 | int error = 0; |
bangbh | 9:21574e058141 | 60 | float hum = 0.0f, temp = 0.0f; |
justinkim | 8:f837e0d255e8 | 61 | while (true) |
justinkim | 8:f837e0d255e8 | 62 | { |
bangbh | 9:21574e058141 | 63 | error = sensor.readData(); |
bangbh | 9:21574e058141 | 64 | if (0 == error) { |
bangbh | 9:21574e058141 | 65 | hum = sensor.ReadHumidity(); |
bangbh | 9:21574e058141 | 66 | temp = sensor.ReadTemperature(CELCIUS); |
justinkim | 8:f837e0d255e8 | 67 | } |
bangbh | 9:21574e058141 | 68 | sprintf(buf, "%3.1f", hum); |
bangbh | 9:21574e058141 | 69 | message.qos = MQTT::QOS0; |
bangbh | 9:21574e058141 | 70 | message.retained = false; |
bangbh | 9:21574e058141 | 71 | message.dup = false; |
bangbh | 9:21574e058141 | 72 | message.payload = (void*)buf; |
bangbh | 9:21574e058141 | 73 | message.payloadlen = strlen(buf)+1; |
bangbh | 9:21574e058141 | 74 | rc = client.publish("wiznet/hum", message); |
bangbh | 9:21574e058141 | 75 | sprintf(buf, "%3.1f", temp); |
bangbh | 9:21574e058141 | 76 | message.payload = (void*)buf; |
bangbh | 9:21574e058141 | 77 | message.payloadlen = strlen(buf)+1; |
bangbh | 9:21574e058141 | 78 | rc = client.publish("wiznet/temp", message); |
bangbh | 9:21574e058141 | 79 | sprintf(buf, "%3.1f", ain.read()*100.0f); |
bangbh | 9:21574e058141 | 80 | message.payload = (void*)buf; |
bangbh | 9:21574e058141 | 81 | message.payloadlen = strlen(buf)+1; |
bangbh | 9:21574e058141 | 82 | rc = client.publish("wiznet/cds", message); |
bangbh | 9:21574e058141 | 83 | client.yield(1000); |
justinkim | 8:f837e0d255e8 | 84 | } |
justinkim | 8:f837e0d255e8 | 85 | } |
justinkim | 8:f837e0d255e8 | 86 |