This is the part of Connect_Home. It is for MQTT publisher with WIZwiki-W7500. It publishes real time information to MQTT broker and it is controlled from it.
Dependencies: DHT MQTT WIZnet_Library mbed
Revision 0:7cb36af7fae4, committed 2017-09-05
- Comitter:
- joon874
- Date:
- Tue Sep 05 04:20:31 2017 +0000
- Commit message:
- Connect Home project
Changed in this revision
diff -r 000000000000 -r 7cb36af7fae4 DHT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT.lib Tue Sep 05 04:20:31 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Wimpie/code/DHT/#9b5b3200688f
diff -r 000000000000 -r 7cb36af7fae4 MQTT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT.lib Tue Sep 05 04:20:31 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/mqtt/code/MQTT/#e335fcc1a663
diff -r 000000000000 -r 7cb36af7fae4 WIZnet_Library.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet_Library.lib Tue Sep 05 04:20:31 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/WIZnet/code/WIZnet_Library/#cb8808b47e69
diff -r 000000000000 -r 7cb36af7fae4 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Sep 05 04:20:31 2017 +0000 @@ -0,0 +1,160 @@ +#include "mbed.h" +#include "DHT.h" +#include "MQTTEthernet.h" +#include "MQTTClient.h" + +#define ECHO_SERVER_PORT 7 + +#include "mbed.h" + +int arrivedcount = 0; +DigitalOut sw1(D9); +DigitalOut sw2(D10); +DigitalOut sw3(D11); + + +void sw1_messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); + + if( strncmp((char*)message.payload,"1",message.payloadlen) == 0 ){ + sw1 = 1; + } else{ + sw1 = 0; + } +} + +void sw2_messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); + if( strncmp((char*)message.payload,"1",message.payloadlen) == 0 ){ + sw2 = 1; + } else{ + sw2 = 0; + } +} + +void sw3_messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); + if( strncmp((char*)message.payload,"1",message.payloadlen) == 0 ){ + sw3 = 1; + } else{ + sw3 = 0; + } +} + +void baud(int baudrate) { + Serial s(USBTX, USBRX); + s.baud(baudrate); +} + +int main (void) +{ + DHT sensor(D4, DHT11); + DigitalOut myled(LED1); + + sw1 = 0; sw2 = 0; sw3 = 0; + baud(115200); + printf("Wait a second...\r\n"); + + + myled = 0; + + MQTTEthernet ipstack = MQTTEthernet(); + printf("IP Address : %s\r\n",ipstack.getEth().getIPAddress()); + MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); + + + //char* hostname = "192.168.1.213"; // server ip address + char* hostname = "192.168.0.196"; // server ip address + + + printf("connect to %s\r\n",hostname); + int port = 1883; // server port + + int rc = ipstack.connect(hostname, port); + if (rc != 0) + { + printf("rc from TCP connect is %d\n", rc); + return 0; + } + printf("TCP connection is successful\r\n"); + + char MQTTClientID[30]; + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000); + data.clientID.cstring = MQTTClientID; + data.username.cstring = ""; + data.password.cstring = ""; + + if ((rc = client.connect(data)) != 0) + { + printf("rc from MQTT connect is %d\r\n", rc); + return 0; + } + printf("MQTT connection is successful\r\n"); + + + char *sw1 = "/wiznet/sw1"; + if ((rc = client.subscribe(sw1, MQTT::QOS1, sw1_messageArrived)) != 0) + { + printf("rc from MQTT subscribe is %d\r\n", rc); + return 0; + } + printf("Added subscription for sw1\r\n"); + + char *sw2 = "/wiznet/sw2"; + if ((rc = client.subscribe(sw2, MQTT::QOS1, sw2_messageArrived)) != 0) + { + printf("rc from MQTT subscribe is %d\r\n", rc); + return 0; + } + printf("Added subscription for sw2\r\n"); + + char *sw3 = "/wiznet/sw3"; + if ((rc = client.subscribe(sw3, MQTT::QOS1, sw3_messageArrived)) != 0) + { + printf("rc from MQTT subscribe is %d\r\n", rc); + return 0; + } + printf("Added subscription for sw3\r\n"); + + MQTT::Message message; + char buf[100]; + int error = 0; + float hum = 0.0f, temp = 0.0f; + char i = 0; + while (true) + { + if(i > 100) i = 0; + error = sensor.readData(); + if (0 == error) { + hum = sensor.ReadHumidity(); + temp = sensor.ReadTemperature(CELCIUS); + } + message.qos = MQTT::QOS1; + message.retained = false; + message.dup = false; + + sprintf(buf, "%3.1f", hum); + message.payload = (void*)buf; + message.payloadlen = strlen(buf); + rc = client.publish("/wiznet/humidity",message); + printf("publish humidity data %s\r\n",(char*)message.payload); + + sprintf(buf, "%3.1f", temp); + message.payload = (void*)buf; + message.payloadlen = strlen(buf); + rc = client.publish("/wiznet/temperature", message); + printf("publish temperature data %s\r\n",(char*)message.payload); + + client.yield(5000); + } +} +
diff -r 000000000000 -r 7cb36af7fae4 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Sep 05 04:20:31 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a330f0fddbec \ No newline at end of file