nothing modified

Dependencies:   mbed-src WizFi310Interface_Legacy_ DHT MQTT

Committer:
bangbh
Date:
Mon Sep 21 23:39:16 2015 +0000
Revision:
15:d4604c47da41
Parent:
14:2b30a541da82
Child:
16:2794fcca7629
for lecture

Who changed what in which revision?

UserRevisionLine numberNew 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 MQTTEthernet ipstack = MQTTEthernet();
bangbh 9:21574e058141 32
bangbh 9:21574e058141 33 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
bangbh 9:21574e058141 34
bangbh 13:36456500fb7e 35 char* hostname = "222.98.173.244";
bangbh 9:21574e058141 36 int port = 1883;
bangbh 9:21574e058141 37
bangbh 9:21574e058141 38 int rc = ipstack.connect(hostname, port);
bangbh 9:21574e058141 39 if (rc != 0)
bangbh 9:21574e058141 40 printf("rc from TCP connect is %d\n", rc);
bangbh 9:21574e058141 41
bangbh 15:d4604c47da41 42 char MQTTClientID[30];
justinkim 8:f837e0d255e8 43
bangbh 9:21574e058141 44 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
bangbh 9:21574e058141 45 data.MQTTVersion = 3;
bangbh 15:d4604c47da41 46 sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000);
bangbh 15:d4604c47da41 47 data.clientID.cstring = MQTTClientID;
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