nothing modified
Dependencies: mbed-src WizFi310Interface_Legacy_ DHT MQTT
Diff: main.cpp
- Revision:
- 9:21574e058141
- Parent:
- 8:f837e0d255e8
- Child:
- 13:36456500fb7e
diff -r f837e0d255e8 -r 21574e058141 main.cpp --- a/main.cpp Tue Jun 23 00:36:34 2015 +0000 +++ b/main.cpp Sun Jun 28 22:48:46 2015 +0000 @@ -1,55 +1,86 @@ #include "mbed.h" -#include "EthernetInterface.h" +#include "DHT.h" +#include "MQTTEthernet.h" +#include "MQTTClient.h" #define ECHO_SERVER_PORT 7 -int main (void) +#include "mbed.h" + +int arrivedcount = 0; + +void messageArrived(MQTT::MessageData& md) { + MQTT::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); + printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); + ++arrivedcount; +} + +void baud(int baudrate) { + Serial s(USBTX, USBRX); + s.baud(baudrate); +} + +int main (void) +{ + DHT sensor(D4, DHT11); + AnalogIn ain(A1); + baud(115200); printf("Wait a second...\r\n"); - uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; - EthernetInterface eth; - eth.init(mac_addr); //Use DHCP - eth.connect(); - printf("Server IP Address is %s\r\n", eth.getIPAddress()); + char* topic = "hello/wiznet"; + MQTTEthernet ipstack = MQTTEthernet(); + + MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); + + char* hostname = "104.199.146.45"; + int port = 1883; + + int rc = ipstack.connect(hostname, port); + if (rc != 0) + printf("rc from TCP connect is %d\n", rc); + + printf("Topic: %s\r\n",topic); - TCPSocketServer server; - server.bind(ECHO_SERVER_PORT); - server.listen(); - + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "WIZwiki-W7500"; + data.username.cstring = "testuser"; + data.password.cstring = "testpassword"; + + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\n", rc); + + if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + + MQTT::Message message; + char buf[100]; + int error = 0; + float hum = 0.0f, temp = 0.0f; while (true) { - printf("Wait for new connection...\r\n"); - TCPSocketConnection client; - server.accept(client); - client.set_blocking(false, 15000); // Timeout after (1.5)s - - printf("Connection from: %s\r\n", client.get_address()); - char buffer[256]; - while (true) { - int n = client.receive(buffer, sizeof(buffer)); - if (n <= 0) break; - - // print received message to terminal - buffer[n] = '\0'; - printf("Received message from Client :'%s'\r\n",buffer); - - // reverse the message - char temp; - for(int f = 0, l = n-1; f<l; f++,l--){ - temp = buffer[f]; - buffer[f] = buffer[l]; - buffer[l] = temp; - } - - // print reversed message to terminal - printf("Sending message to Client: '%s'\r\n",buffer); - - // Echo received message back to client - client.send_all(buffer, n); - if (n <= 0) break; + error = sensor.readData(); + if (0 == error) { + hum = sensor.ReadHumidity(); + temp = sensor.ReadTemperature(CELCIUS); } - - client.close(); + sprintf(buf, "%3.1f", hum); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + rc = client.publish("wiznet/hum", message); + sprintf(buf, "%3.1f", temp); + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + rc = client.publish("wiznet/temp", message); + sprintf(buf, "%3.1f", ain.read()*100.0f); + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + rc = client.publish("wiznet/cds", message); + client.yield(1000); } }