nc

Dependencies:   mbed-src WizFi310Interface_Legacynew DHT MQTTforLecture

Committer:
akshaytom
Date:
Thu Dec 28 06:32:51 2017 +0000
Revision:
16:2794fcca7629
Parent:
15:d4604c47da41
Child:
17:39b1ebb505b1
MQTT connection through WizFi310

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 8:f837e0d255e8 1 #include "mbed.h"
akshaytom 16:2794fcca7629 2 #include "WizFi310Interface.h"
bangbh 9:21574e058141 3 #include "DHT.h"
bangbh 9:21574e058141 4 #include "MQTTClient.h"
akshaytom 16:2794fcca7629 5 #include "MQTTmbed.h"
akshaytom 16:2794fcca7629 6 #include "MQTTSocket.h"
justinkim 8:f837e0d255e8 7
akshaytom 16:2794fcca7629 8 #define SECURE WizFi310::SEC_WPA2_MIXED
akshaytom 16:2794fcca7629 9 #define SSID "CDI"
akshaytom 16:2794fcca7629 10 #define PASS "Cdi*1717"
akshaytom 16:2794fcca7629 11 /*
akshaytom 16:2794fcca7629 12 SET DHCP
akshaytom 16:2794fcca7629 13 */
akshaytom 16:2794fcca7629 14 #define USE_DHCP 1
akshaytom 16:2794fcca7629 15 //--------- Have to modify the mac address-------------
akshaytom 16:2794fcca7629 16 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
akshaytom 16:2794fcca7629 17 #if defined(TARGET_WIZwiki_W7500)
akshaytom 16:2794fcca7629 18 WizFi310Interface wizfi310(D1, D0, D7, D6, D8, NC, 115200);
akshaytom 16:2794fcca7629 19 Serial pc(USBTX, USBRX);
akshaytom 16:2794fcca7629 20 #endif
justinkim 8:f837e0d255e8 21
bangbh 9:21574e058141 22 int arrivedcount = 0;
bangbh 9:21574e058141 23 void messageArrived(MQTT::MessageData& md)
justinkim 8:f837e0d255e8 24 {
bangbh 9:21574e058141 25 MQTT::Message &message = md.message;
bangbh 9:21574e058141 26 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
bangbh 9:21574e058141 27 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
bangbh 9:21574e058141 28 ++arrivedcount;
bangbh 9:21574e058141 29 }
bangbh 9:21574e058141 30
akshaytom 16:2794fcca7629 31 class MQTTWIZ: public MQTTSocket
bangbh 9:21574e058141 32 {
akshaytom 16:2794fcca7629 33 public:
akshaytom 16:2794fcca7629 34 MQTTWIZ()
akshaytom 16:2794fcca7629 35 {
akshaytom 16:2794fcca7629 36 wait(1);
akshaytom 16:2794fcca7629 37 this->createSocket();
akshaytom 16:2794fcca7629 38 }
akshaytom 16:2794fcca7629 39 };
akshaytom 16:2794fcca7629 40
akshaytom 16:2794fcca7629 41 int main() {
akshaytom 16:2794fcca7629 42
akshaytom 16:2794fcca7629 43
akshaytom 16:2794fcca7629 44
akshaytom 16:2794fcca7629 45 pc.baud(115200);
akshaytom 16:2794fcca7629 46
akshaytom 16:2794fcca7629 47 printf("WizFi310 STATION. \r\n");
akshaytom 16:2794fcca7629 48 wizfi310.init();
akshaytom 16:2794fcca7629 49 printf("After Initialisation. \r\n");
akshaytom 16:2794fcca7629 50
akshaytom 16:2794fcca7629 51 printf("After Set Address. \r\n");
akshaytom 16:2794fcca7629 52 if ( wizfi310.connect(SECURE, SSID, PASS, WizFi310::WM_STATION)) return -1;
akshaytom 16:2794fcca7629 53 printf("After Connect. \r\n");
akshaytom 16:2794fcca7629 54 printf("IP Address is %s\r\n", wizfi310.getIPAddress());
bangbh 9:21574e058141 55
akshaytom 16:2794fcca7629 56 MQTTWIZ ipstack = MQTTWIZ();
akshaytom 16:2794fcca7629 57 MQTT::Client<MQTTWIZ, Countdown> client = MQTT::Client<MQTTWIZ, Countdown>(ipstack);
bangbh 9:21574e058141 58
akshaytom 16:2794fcca7629 59 DHT sensor(D14, DHT11);
akshaytom 16:2794fcca7629 60 char* hostname = "172.16.73.4";
bangbh 9:21574e058141 61 int port = 1883;
bangbh 9:21574e058141 62
bangbh 9:21574e058141 63 int rc = ipstack.connect(hostname, port);
bangbh 9:21574e058141 64 if (rc != 0)
bangbh 9:21574e058141 65 printf("rc from TCP connect is %d\n", rc);
akshaytom 16:2794fcca7629 66 printf("rc from TCP connect is %d\n", rc);
akshaytom 16:2794fcca7629 67
bangbh 15:d4604c47da41 68 char MQTTClientID[30];
justinkim 8:f837e0d255e8 69
bangbh 9:21574e058141 70 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
bangbh 9:21574e058141 71 data.MQTTVersion = 3;
bangbh 15:d4604c47da41 72 sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000);
bangbh 15:d4604c47da41 73 data.clientID.cstring = MQTTClientID;
bangbh 9:21574e058141 74 data.username.cstring = "testuser";
bangbh 9:21574e058141 75 data.password.cstring = "testpassword";
bangbh 9:21574e058141 76
bangbh 9:21574e058141 77 if ((rc = client.connect(data)) != 0)
bangbh 9:21574e058141 78 printf("rc from MQTT connect is %d\n", rc);
bangbh 9:21574e058141 79
bangbh 9:21574e058141 80 MQTT::Message message;
bangbh 9:21574e058141 81 char buf[100];
bangbh 9:21574e058141 82 int error = 0;
bangbh 9:21574e058141 83 float hum = 0.0f, temp = 0.0f;
bangbh 13:36456500fb7e 84 char i = 0;
bangbh 13:36456500fb7e 85 while (true)
justinkim 8:f837e0d255e8 86 {
bangbh 13:36456500fb7e 87 if(i > 100) i = 0;
bangbh 9:21574e058141 88 error = sensor.readData();
bangbh 9:21574e058141 89 if (0 == error) {
bangbh 9:21574e058141 90 hum = sensor.ReadHumidity();
bangbh 9:21574e058141 91 temp = sensor.ReadTemperature(CELCIUS);
justinkim 8:f837e0d255e8 92 }
bangbh 9:21574e058141 93 message.qos = MQTT::QOS0;
bangbh 9:21574e058141 94 message.retained = false;
bangbh 9:21574e058141 95 message.dup = false;
bangbh 13:36456500fb7e 96
bangbh 13:36456500fb7e 97 sprintf(buf, "%3.1f", hum);
bangbh 9:21574e058141 98 message.payload = (void*)buf;
bangbh 13:36456500fb7e 99 message.payloadlen = strlen(buf);
akshaytom 16:2794fcca7629 100 rc = client.publish("Wiz/humidity", message);
akshaytom 16:2794fcca7629 101 printf("Wiz/humidity : %s\r\n",message.payload);
bangbh 13:36456500fb7e 102
bangbh 9:21574e058141 103 sprintf(buf, "%3.1f", temp);
bangbh 9:21574e058141 104 message.payload = (void*)buf;
bangbh 13:36456500fb7e 105 message.payloadlen = strlen(buf);
akshaytom 16:2794fcca7629 106 rc = client.publish("Wiz/temperature", message);
akshaytom 16:2794fcca7629 107 printf("Wiz/temperature : %s\r\n",message.payload);
bangbh 9:21574e058141 108 client.yield(1000);
justinkim 8:f837e0d255e8 109 }
justinkim 8:f837e0d255e8 110 }