nc
Dependencies: mbed-src WizFi310Interface_Legacynew DHT MQTTforLecture
main.cpp@17:39b1ebb505b1, 2018-12-13 (annotated)
- Committer:
- irayya
- Date:
- Thu Dec 13 10:24:36 2018 +0000
- Revision:
- 17:39b1ebb505b1
- Parent:
- 16:2794fcca7629
nc
Who changed what in which revision?
User | Revision | Line number | New 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 |
irayya | 17:39b1ebb505b1 | 9 | #define SSID "CDI_STAFF" |
irayya | 17:39b1ebb505b1 | 10 | #define PASS "YYkpbQBC" |
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 | |
irayya | 17:39b1ebb505b1 | 45 | pc.baud(115200); |
akshaytom | 16:2794fcca7629 | 46 | |
akshaytom | 16:2794fcca7629 | 47 | printf("WizFi310 STATION. \r\n"); |
irayya | 17:39b1ebb505b1 | 48 | char* topicfan = "fan"; |
akshaytom | 16:2794fcca7629 | 49 | wizfi310.init(); |
akshaytom | 16:2794fcca7629 | 50 | printf("After Initialisation. \r\n"); |
akshaytom | 16:2794fcca7629 | 51 | |
akshaytom | 16:2794fcca7629 | 52 | printf("After Set Address. \r\n"); |
akshaytom | 16:2794fcca7629 | 53 | if ( wizfi310.connect(SECURE, SSID, PASS, WizFi310::WM_STATION)) return -1; |
akshaytom | 16:2794fcca7629 | 54 | printf("After Connect. \r\n"); |
akshaytom | 16:2794fcca7629 | 55 | printf("IP Address is %s\r\n", wizfi310.getIPAddress()); |
bangbh | 9:21574e058141 | 56 | |
akshaytom | 16:2794fcca7629 | 57 | MQTTWIZ ipstack = MQTTWIZ(); |
akshaytom | 16:2794fcca7629 | 58 | MQTT::Client<MQTTWIZ, Countdown> client = MQTT::Client<MQTTWIZ, Countdown>(ipstack); |
bangbh | 9:21574e058141 | 59 | |
akshaytom | 16:2794fcca7629 | 60 | DHT sensor(D14, DHT11); |
irayya | 17:39b1ebb505b1 | 61 | char* hostname = "iot.eclipse.org"; |
bangbh | 9:21574e058141 | 62 | int port = 1883; |
bangbh | 9:21574e058141 | 63 | |
bangbh | 9:21574e058141 | 64 | int rc = ipstack.connect(hostname, port); |
bangbh | 9:21574e058141 | 65 | if (rc != 0) |
bangbh | 9:21574e058141 | 66 | printf("rc from TCP connect is %d\n", rc); |
akshaytom | 16:2794fcca7629 | 67 | printf("rc from TCP connect is %d\n", rc); |
akshaytom | 16:2794fcca7629 | 68 | |
irayya | 17:39b1ebb505b1 | 69 | |
bangbh | 15:d4604c47da41 | 70 | char MQTTClientID[30]; |
justinkim | 8:f837e0d255e8 | 71 | |
bangbh | 9:21574e058141 | 72 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
bangbh | 9:21574e058141 | 73 | data.MQTTVersion = 3; |
bangbh | 15:d4604c47da41 | 74 | sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000); |
bangbh | 15:d4604c47da41 | 75 | data.clientID.cstring = MQTTClientID; |
bangbh | 9:21574e058141 | 76 | data.username.cstring = "testuser"; |
bangbh | 9:21574e058141 | 77 | data.password.cstring = "testpassword"; |
irayya | 17:39b1ebb505b1 | 78 | |
irayya | 17:39b1ebb505b1 | 79 | |
bangbh | 9:21574e058141 | 80 | |
bangbh | 9:21574e058141 | 81 | if ((rc = client.connect(data)) != 0) |
bangbh | 9:21574e058141 | 82 | printf("rc from MQTT connect is %d\n", rc); |
bangbh | 9:21574e058141 | 83 | |
irayya | 17:39b1ebb505b1 | 84 | |
irayya | 17:39b1ebb505b1 | 85 | if ((rc = client.subscribe(topicfan, MQTT::QOS1, messageArrived)) == 0) |
irayya | 17:39b1ebb505b1 | 86 | printf("rc from MQTT subscribe for fan is %d\n", rc); |
irayya | 17:39b1ebb505b1 | 87 | |
irayya | 17:39b1ebb505b1 | 88 | |
bangbh | 9:21574e058141 | 89 | MQTT::Message message; |
bangbh | 9:21574e058141 | 90 | char buf[100]; |
bangbh | 9:21574e058141 | 91 | int error = 0; |
bangbh | 9:21574e058141 | 92 | float hum = 0.0f, temp = 0.0f; |
bangbh | 13:36456500fb7e | 93 | char i = 0; |
bangbh | 13:36456500fb7e | 94 | while (true) |
justinkim | 8:f837e0d255e8 | 95 | { |
bangbh | 13:36456500fb7e | 96 | if(i > 100) i = 0; |
bangbh | 9:21574e058141 | 97 | error = sensor.readData(); |
bangbh | 9:21574e058141 | 98 | if (0 == error) { |
bangbh | 9:21574e058141 | 99 | hum = sensor.ReadHumidity(); |
bangbh | 9:21574e058141 | 100 | temp = sensor.ReadTemperature(CELCIUS); |
justinkim | 8:f837e0d255e8 | 101 | } |
bangbh | 9:21574e058141 | 102 | message.qos = MQTT::QOS0; |
bangbh | 9:21574e058141 | 103 | message.retained = false; |
bangbh | 9:21574e058141 | 104 | message.dup = false; |
bangbh | 13:36456500fb7e | 105 | |
bangbh | 13:36456500fb7e | 106 | sprintf(buf, "%3.1f", hum); |
bangbh | 9:21574e058141 | 107 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 108 | message.payloadlen = strlen(buf); |
akshaytom | 16:2794fcca7629 | 109 | rc = client.publish("Wiz/humidity", message); |
irayya | 17:39b1ebb505b1 | 110 | // printf("Wiz/humidity : %s\r\n",message.payload); |
bangbh | 13:36456500fb7e | 111 | |
irayya | 17:39b1ebb505b1 | 112 | //sprintf(buf, "%3.1f", temp); |
irayya | 17:39b1ebb505b1 | 113 | // message.payload = (void*)buf; |
irayya | 17:39b1ebb505b1 | 114 | // message.payloadlen = strlen(buf); |
irayya | 17:39b1ebb505b1 | 115 | // rc = client.publish("Wiz/temperature", message); |
irayya | 17:39b1ebb505b1 | 116 | // printf("Wiz/temperature : %s\r\n",message.payload); |
bangbh | 9:21574e058141 | 117 | client.yield(1000); |
justinkim | 8:f837e0d255e8 | 118 | } |
justinkim | 8:f837e0d255e8 | 119 | } |