nothing modified
Dependencies: mbed-src WizFi310Interface_Legacy_ DHT MQTT
main.cpp@19:2e11b8a64d23, 2019-02-06 (annotated)
- Committer:
- irayya
- Date:
- Wed Feb 06 10:09:00 2019 +0000
- Revision:
- 19:2e11b8a64d23
- Parent:
- 18:0596e033d027
nothing modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
irayya | 18:0596e033d027 | 1 | |
irayya | 18:0596e033d027 | 2 | |
justinkim | 8:f837e0d255e8 | 3 | #include "mbed.h" |
akshaytom | 16:2794fcca7629 | 4 | #include "WizFi310Interface.h" |
bangbh | 9:21574e058141 | 5 | #include "DHT.h" |
bangbh | 9:21574e058141 | 6 | #include "MQTTClient.h" |
akshaytom | 16:2794fcca7629 | 7 | #include "MQTTmbed.h" |
akshaytom | 16:2794fcca7629 | 8 | #include "MQTTSocket.h" |
irayya | 18:0596e033d027 | 9 | #include "string.h" |
akshaytom | 16:2794fcca7629 | 10 | #define SECURE WizFi310::SEC_WPA2_MIXED |
irayya | 18:0596e033d027 | 11 | #define SSID "CDI_STAFF" |
irayya | 18:0596e033d027 | 12 | #define PASS "uDQmAfJY" // should use perticular passd for each wizfi-310 device with CDI_STAFF network |
irayya | 18:0596e033d027 | 13 | DigitalOut led1(D0); |
irayya | 18:0596e033d027 | 14 | char msg[16]; |
irayya | 18:0596e033d027 | 15 | char result[10]; |
akshaytom | 16:2794fcca7629 | 16 | /* |
akshaytom | 16:2794fcca7629 | 17 | SET DHCP |
akshaytom | 16:2794fcca7629 | 18 | */ |
akshaytom | 16:2794fcca7629 | 19 | #define USE_DHCP 1 |
akshaytom | 16:2794fcca7629 | 20 | //--------- Have to modify the mac address------------- |
akshaytom | 16:2794fcca7629 | 21 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; |
akshaytom | 16:2794fcca7629 | 22 | #if defined(TARGET_WIZwiki_W7500) |
akshaytom | 16:2794fcca7629 | 23 | WizFi310Interface wizfi310(D1, D0, D7, D6, D8, NC, 115200); |
akshaytom | 16:2794fcca7629 | 24 | Serial pc(USBTX, USBRX); |
akshaytom | 16:2794fcca7629 | 25 | #endif |
justinkim | 8:f837e0d255e8 | 26 | |
bangbh | 9:21574e058141 | 27 | int arrivedcount = 0; |
bangbh | 9:21574e058141 | 28 | void messageArrived(MQTT::MessageData& md) |
justinkim | 8:f837e0d255e8 | 29 | { |
irayya | 18:0596e033d027 | 30 | |
bangbh | 9:21574e058141 | 31 | MQTT::Message &message = md.message; |
irayya | 18:0596e033d027 | 32 | |
irayya | 18:0596e033d027 | 33 | sprintf(result, "%.*s", message.payloadlen,(char*)message.payload); |
irayya | 18:0596e033d027 | 34 | if(strcmp(result,"on") == 0) |
irayya | 18:0596e033d027 | 35 | { |
irayya | 18:0596e033d027 | 36 | led1 = 1; |
irayya | 18:0596e033d027 | 37 | printf("LED blink \n"); |
irayya | 18:0596e033d027 | 38 | } |
irayya | 18:0596e033d027 | 39 | if(strcmp(result,"off")== 0) |
irayya | 18:0596e033d027 | 40 | { |
irayya | 18:0596e033d027 | 41 | led1 = 0; |
irayya | 18:0596e033d027 | 42 | printf("buzz\n"); |
irayya | 18:0596e033d027 | 43 | } |
irayya | 18:0596e033d027 | 44 | memset(result,'\0', sizeof(result)); |
bangbh | 9:21574e058141 | 45 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
bangbh | 9:21574e058141 | 46 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
bangbh | 9:21574e058141 | 47 | ++arrivedcount; |
bangbh | 9:21574e058141 | 48 | } |
bangbh | 9:21574e058141 | 49 | |
irayya | 18:0596e033d027 | 50 | |
akshaytom | 16:2794fcca7629 | 51 | class MQTTWIZ: public MQTTSocket |
bangbh | 9:21574e058141 | 52 | { |
akshaytom | 16:2794fcca7629 | 53 | public: |
akshaytom | 16:2794fcca7629 | 54 | MQTTWIZ() |
akshaytom | 16:2794fcca7629 | 55 | { |
akshaytom | 16:2794fcca7629 | 56 | wait(1); |
akshaytom | 16:2794fcca7629 | 57 | this->createSocket(); |
akshaytom | 16:2794fcca7629 | 58 | } |
akshaytom | 16:2794fcca7629 | 59 | }; |
akshaytom | 16:2794fcca7629 | 60 | |
akshaytom | 16:2794fcca7629 | 61 | int main() { |
akshaytom | 16:2794fcca7629 | 62 | |
akshaytom | 16:2794fcca7629 | 63 | |
akshaytom | 16:2794fcca7629 | 64 | |
irayya | 19:2e11b8a64d23 | 65 | pc.baud(115200); |
irayya | 19:2e11b8a64d23 | 66 | char* topicled = "/led"; |
akshaytom | 16:2794fcca7629 | 67 | printf("WizFi310 STATION. \r\n"); |
akshaytom | 16:2794fcca7629 | 68 | wizfi310.init(); |
akshaytom | 16:2794fcca7629 | 69 | printf("After Initialisation. \r\n"); |
akshaytom | 16:2794fcca7629 | 70 | |
akshaytom | 16:2794fcca7629 | 71 | printf("After Set Address. \r\n"); |
akshaytom | 16:2794fcca7629 | 72 | if ( wizfi310.connect(SECURE, SSID, PASS, WizFi310::WM_STATION)) return -1; |
akshaytom | 16:2794fcca7629 | 73 | printf("After Connect. \r\n"); |
akshaytom | 16:2794fcca7629 | 74 | printf("IP Address is %s\r\n", wizfi310.getIPAddress()); |
bangbh | 9:21574e058141 | 75 | |
akshaytom | 16:2794fcca7629 | 76 | MQTTWIZ ipstack = MQTTWIZ(); |
akshaytom | 16:2794fcca7629 | 77 | MQTT::Client<MQTTWIZ, Countdown> client = MQTT::Client<MQTTWIZ, Countdown>(ipstack); |
bangbh | 9:21574e058141 | 78 | |
akshaytom | 16:2794fcca7629 | 79 | DHT sensor(D14, DHT11); |
akshaytom | 16:2794fcca7629 | 80 | char* hostname = "172.16.73.4"; |
bangbh | 9:21574e058141 | 81 | int port = 1883; |
bangbh | 9:21574e058141 | 82 | |
bangbh | 9:21574e058141 | 83 | int rc = ipstack.connect(hostname, port); |
bangbh | 9:21574e058141 | 84 | if (rc != 0) |
bangbh | 9:21574e058141 | 85 | printf("rc from TCP connect is %d\n", rc); |
akshaytom | 16:2794fcca7629 | 86 | printf("rc from TCP connect is %d\n", rc); |
akshaytom | 16:2794fcca7629 | 87 | |
irayya | 19:2e11b8a64d23 | 88 | /*char MQTTClientID[30]; |
irayya | 18:0596e033d027 | 89 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
irayya | 18:0596e033d027 | 90 | data.MQTTVersion = 3; |
irayya | 19:2e11b8a64d23 | 91 | sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000); |
irayya | 19:2e11b8a64d23 | 92 | data.clientID.cstring = MQTTClientID; |
irayya | 19:2e11b8a64d23 | 93 | data.username.cstring = "testuser"; |
irayya | 19:2e11b8a64d23 | 94 | data.password.cstring = "testpassword"; |
irayya | 19:2e11b8a64d23 | 95 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
irayya | 19:2e11b8a64d23 | 96 | data.MQTTVersion = 3; |
irayya | 19:2e11b8a64d23 | 97 | data.clientID.cstring = "parents";*/ |
irayya | 19:2e11b8a64d23 | 98 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
bangbh | 9:21574e058141 | 99 | if ((rc = client.connect(data)) != 0) |
bangbh | 9:21574e058141 | 100 | printf("rc from MQTT connect is %d\n", rc); |
bangbh | 9:21574e058141 | 101 | |
irayya | 19:2e11b8a64d23 | 102 | if ((rc = client.subscribe(topicled, MQTT::QOS1, messageArrived)) == 0) |
irayya | 18:0596e033d027 | 103 | printf("rc from MQTT subscribe for fan is %d\n", rc); |
irayya | 18:0596e033d027 | 104 | |
bangbh | 9:21574e058141 | 105 | MQTT::Message message; |
irayya | 19:2e11b8a64d23 | 106 | //char buf[100]; |
irayya | 19:2e11b8a64d23 | 107 | char buf[]="hello"; |
bangbh | 9:21574e058141 | 108 | int error = 0; |
bangbh | 9:21574e058141 | 109 | float hum = 0.0f, temp = 0.0f; |
bangbh | 13:36456500fb7e | 110 | char i = 0; |
bangbh | 13:36456500fb7e | 111 | while (true) |
justinkim | 8:f837e0d255e8 | 112 | { |
bangbh | 13:36456500fb7e | 113 | if(i > 100) i = 0; |
bangbh | 9:21574e058141 | 114 | error = sensor.readData(); |
bangbh | 9:21574e058141 | 115 | if (0 == error) { |
bangbh | 9:21574e058141 | 116 | hum = sensor.ReadHumidity(); |
bangbh | 9:21574e058141 | 117 | temp = sensor.ReadTemperature(CELCIUS); |
justinkim | 8:f837e0d255e8 | 118 | } |
bangbh | 9:21574e058141 | 119 | message.qos = MQTT::QOS0; |
bangbh | 9:21574e058141 | 120 | message.retained = false; |
bangbh | 9:21574e058141 | 121 | message.dup = false; |
bangbh | 13:36456500fb7e | 122 | |
irayya | 19:2e11b8a64d23 | 123 | //sprintf(buf, "%3.1f", hum); |
irayya | 19:2e11b8a64d23 | 124 | |
bangbh | 9:21574e058141 | 125 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 126 | message.payloadlen = strlen(buf); |
akshaytom | 16:2794fcca7629 | 127 | rc = client.publish("Wiz/humidity", message); |
akshaytom | 16:2794fcca7629 | 128 | printf("Wiz/humidity : %s\r\n",message.payload); |
bangbh | 13:36456500fb7e | 129 | |
irayya | 19:2e11b8a64d23 | 130 | /* sprintf(buf, "%3.1f", temp); |
bangbh | 9:21574e058141 | 131 | message.payload = (void*)buf; |
bangbh | 13:36456500fb7e | 132 | message.payloadlen = strlen(buf); |
akshaytom | 16:2794fcca7629 | 133 | rc = client.publish("Wiz/temperature", message); |
irayya | 19:2e11b8a64d23 | 134 | printf("Wiz/temperature : %s\r\n",message.payload);*/ |
bangbh | 9:21574e058141 | 135 | client.yield(1000); |
justinkim | 8:f837e0d255e8 | 136 | } |
justinkim | 8:f837e0d255e8 | 137 | } |