MQTT_Ethernet
Dependencies: DHT MQTT WIZnetInterface mbed-src
Fork of MQTTw7500 by
main.cpp@12:4f0489448955, 2015-10-03 (annotated)
- Committer:
- jamesabruce
- Date:
- Sat Oct 03 10:54:52 2015 +0000
- Revision:
- 12:4f0489448955
- Parent:
- 9:21574e058141
- Child:
- 13:20ede19a43cd
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 | #define ECHO_SERVER_PORT 7 |
bangbh | 9:21574e058141 | 6 | |
bangbh | 9:21574e058141 | 7 | int arrivedcount = 0; |
bangbh | 9:21574e058141 | 8 | |
bangbh | 9:21574e058141 | 9 | void messageArrived(MQTT::MessageData& md) |
justinkim | 8:f837e0d255e8 | 10 | { |
bangbh | 9:21574e058141 | 11 | MQTT::Message &message = md.message; |
bangbh | 9:21574e058141 | 12 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
bangbh | 9:21574e058141 | 13 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
bangbh | 9:21574e058141 | 14 | ++arrivedcount; |
bangbh | 9:21574e058141 | 15 | } |
bangbh | 9:21574e058141 | 16 | |
bangbh | 9:21574e058141 | 17 | void baud(int baudrate) { |
bangbh | 9:21574e058141 | 18 | Serial s(USBTX, USBRX); |
bangbh | 9:21574e058141 | 19 | s.baud(baudrate); |
bangbh | 9:21574e058141 | 20 | } |
bangbh | 9:21574e058141 | 21 | |
bangbh | 9:21574e058141 | 22 | int main (void) |
bangbh | 9:21574e058141 | 23 | { |
bangbh | 9:21574e058141 | 24 | DHT sensor(D4, DHT11); |
bangbh | 9:21574e058141 | 25 | baud(115200); |
justinkim | 8:f837e0d255e8 | 26 | printf("Wait a second...\r\n"); |
jamesabruce | 12:4f0489448955 | 27 | char* topic = "openhab/parents/command"; |
bangbh | 9:21574e058141 | 28 | MQTTEthernet ipstack = MQTTEthernet(); |
bangbh | 9:21574e058141 | 29 | |
bangbh | 9:21574e058141 | 30 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
bangbh | 9:21574e058141 | 31 | |
jamesabruce | 12:4f0489448955 | 32 | char* hostname = "192.168.1.99"; |
bangbh | 9:21574e058141 | 33 | int port = 1883; |
bangbh | 9:21574e058141 | 34 | |
bangbh | 9:21574e058141 | 35 | int rc = ipstack.connect(hostname, port); |
bangbh | 9:21574e058141 | 36 | if (rc != 0) |
bangbh | 9:21574e058141 | 37 | printf("rc from TCP connect is %d\n", rc); |
bangbh | 9:21574e058141 | 38 | |
bangbh | 9:21574e058141 | 39 | printf("Topic: %s\r\n",topic); |
justinkim | 8:f837e0d255e8 | 40 | |
bangbh | 9:21574e058141 | 41 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
bangbh | 9:21574e058141 | 42 | data.MQTTVersion = 3; |
jamesabruce | 12:4f0489448955 | 43 | data.clientID.cstring = "parents"; |
bangbh | 9:21574e058141 | 44 | |
bangbh | 9:21574e058141 | 45 | if ((rc = client.connect(data)) != 0) |
bangbh | 9:21574e058141 | 46 | printf("rc from MQTT connect is %d\n", rc); |
bangbh | 9:21574e058141 | 47 | |
bangbh | 9:21574e058141 | 48 | if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) |
bangbh | 9:21574e058141 | 49 | printf("rc from MQTT subscribe is %d\n", rc); |
bangbh | 9:21574e058141 | 50 | |
bangbh | 9:21574e058141 | 51 | MQTT::Message message; |
bangbh | 9:21574e058141 | 52 | char buf[100]; |
bangbh | 9:21574e058141 | 53 | int error = 0; |
bangbh | 9:21574e058141 | 54 | float hum = 0.0f, temp = 0.0f; |
justinkim | 8:f837e0d255e8 | 55 | while (true) |
justinkim | 8:f837e0d255e8 | 56 | { |
bangbh | 9:21574e058141 | 57 | error = sensor.readData(); |
bangbh | 9:21574e058141 | 58 | if (0 == error) { |
bangbh | 9:21574e058141 | 59 | hum = sensor.ReadHumidity(); |
bangbh | 9:21574e058141 | 60 | temp = sensor.ReadTemperature(CELCIUS); |
justinkim | 8:f837e0d255e8 | 61 | } |
bangbh | 9:21574e058141 | 62 | sprintf(buf, "%3.1f", hum); |
bangbh | 9:21574e058141 | 63 | message.qos = MQTT::QOS0; |
bangbh | 9:21574e058141 | 64 | message.retained = false; |
bangbh | 9:21574e058141 | 65 | message.dup = false; |
bangbh | 9:21574e058141 | 66 | message.payload = (void*)buf; |
jamesabruce | 12:4f0489448955 | 67 | message.payloadlen = strlen(buf); |
jamesabruce | 12:4f0489448955 | 68 | rc = client.publish("openhab/parents/humidity", message); |
bangbh | 9:21574e058141 | 69 | sprintf(buf, "%3.1f", temp); |
bangbh | 9:21574e058141 | 70 | message.payload = (void*)buf; |
jamesabruce | 12:4f0489448955 | 71 | message.payloadlen = strlen(buf); |
jamesabruce | 12:4f0489448955 | 72 | rc = client.publish("openhab/parents/temperature", message); |
jamesabruce | 12:4f0489448955 | 73 | |
jamesabruce | 12:4f0489448955 | 74 | client.yield(60000); |
justinkim | 8:f837e0d255e8 | 75 | } |
justinkim | 8:f837e0d255e8 | 76 | } |
justinkim | 8:f837e0d255e8 | 77 |