Published
Dependencies: DHT MQTTforLecture WIZnetInterface mbed-src
Fork of w7500-paho-mqtt by
main.cpp@14:2b30a541da82, 2015-09-20 (annotated)
- Committer:
- bangbh
- Date:
- Sun Sep 20 06:11:10 2015 +0000
- Revision:
- 14:2b30a541da82
- Parent:
- 13:36456500fb7e
- Child:
- 15:d4604c47da41
lecture
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 | 13:36456500fb7e | 36 | char* hostname = "222.98.173.244"; |
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 | 14:2b30a541da82 | 47 | sprintf(data.clientID.cstring,"WIZwiki-W7500-client-%d",rand()%1000); |
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 | MQTT::Message message; |
bangbh | 9:21574e058141 | 55 | char buf[100]; |
bangbh | 9:21574e058141 | 56 | int error = 0; |
bangbh | 9:21574e058141 | 57 | float hum = 0.0f, temp = 0.0f; |
bangbh | 13:36456500fb7e | 58 | char i = 0; |
bangbh | 13:36456500fb7e | 59 | while (true) |
justinkim | 8:f837e0d255e8 | 60 | { |
bangbh | 13:36456500fb7e | 61 | if(i > 100) i = 0; |
bangbh | 9:21574e058141 | 62 | error = sensor.readData(); |
bangbh | 9:21574e058141 | 63 | if (0 == error) { |
bangbh | 9:21574e058141 | 64 | hum = sensor.ReadHumidity(); |
bangbh | 9:21574e058141 | 65 | temp = sensor.ReadTemperature(CELCIUS); |
justinkim | 8:f837e0d255e8 | 66 | } |
bangbh | 9:21574e058141 | 67 | message.qos = MQTT::QOS0; |
bangbh | 9:21574e058141 | 68 | message.retained = false; |
bangbh | 9:21574e058141 | 69 | message.dup = false; |
bangbh | 13:36456500fb7e | 70 | |
bangbh | 13:36456500fb7e | 71 | sprintf(buf, "%3.1f", hum); |
bangbh | 9:21574e058141 | 72 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 73 | message.payloadlen = strlen(buf); |
bangbh | 13:36456500fb7e | 74 | rc = client.publish("Wiznet/humidity", message); |
bangbh | 13:36456500fb7e | 75 | |
bangbh | 9:21574e058141 | 76 | sprintf(buf, "%3.1f", temp); |
bangbh | 9:21574e058141 | 77 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 78 | message.payloadlen = strlen(buf); |
bangbh | 13:36456500fb7e | 79 | rc = client.publish("Wiznet/temperature", message); |
bangbh | 13:36456500fb7e | 80 | |
bangbh | 13:36456500fb7e | 81 | sprintf(buf, "%3.1f", (rand()%1000)/10.0); |
bangbh | 13:36456500fb7e | 82 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 83 | message.payloadlen = strlen(buf); |
bangbh | 13:36456500fb7e | 84 | rc = client.publish("Wiznet/fluidgauge/A", message); |
bangbh | 13:36456500fb7e | 85 | |
bangbh | 13:36456500fb7e | 86 | sprintf(buf, "%3.1f", (rand()%1000)/10.0); |
bangbh | 9:21574e058141 | 87 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 88 | message.payloadlen = strlen(buf); |
bangbh | 13:36456500fb7e | 89 | rc = client.publish("Wiznet/fluidgauge/B", message); |
bangbh | 13:36456500fb7e | 90 | |
bangbh | 13:36456500fb7e | 91 | sprintf(buf, "%3.1f", (rand()%1000)/10.0); |
bangbh | 13:36456500fb7e | 92 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 93 | message.payloadlen = strlen(buf); |
bangbh | 13:36456500fb7e | 94 | rc = client.publish("Wiznet/fluidgauge/C", message); |
bangbh | 9:21574e058141 | 95 | client.yield(1000); |
bangbh | 13:36456500fb7e | 96 | //wait(100); |
justinkim | 8:f837e0d255e8 | 97 | } |
justinkim | 8:f837e0d255e8 | 98 | } |
justinkim | 8:f837e0d255e8 | 99 |