Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: esp8266-driver MQTT
main.cpp@97:d87239079f29, 2019-08-23 (annotated)
- Committer:
- UlricL
- Date:
- Fri Aug 23 13:13:42 2019 +0000
- Revision:
- 97:d87239079f29
- Parent:
- 91:dab9882e2b49
Jnu726wifiMQTT
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 0:857719181846 | 1 | /* WiFi Example |
| mbed_official | 0:857719181846 | 2 | * Copyright (c) 2016 ARM Limited |
| mbed_official | 0:857719181846 | 3 | * |
| mbed_official | 0:857719181846 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| mbed_official | 0:857719181846 | 5 | * you may not use this file except in compliance with the License. |
| mbed_official | 0:857719181846 | 6 | * You may obtain a copy of the License at |
| mbed_official | 0:857719181846 | 7 | * |
| mbed_official | 0:857719181846 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| mbed_official | 0:857719181846 | 9 | * |
| mbed_official | 0:857719181846 | 10 | * Unless required by applicable law or agreed to in writing, software |
| mbed_official | 0:857719181846 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| mbed_official | 0:857719181846 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| mbed_official | 0:857719181846 | 13 | * See the License for the specific language governing permissions and |
| mbed_official | 0:857719181846 | 14 | * limitations under the License. |
| mbed_official | 0:857719181846 | 15 | */ |
| mbed_official | 0:857719181846 | 16 | |
| mbed_official | 0:857719181846 | 17 | #include "mbed.h" |
| UlricL | 97:d87239079f29 | 18 | #include "MQTTNetwork.h" |
| UlricL | 97:d87239079f29 | 19 | #include "MQTTmbed.h" |
| UlricL | 97:d87239079f29 | 20 | #include "MQTTClient.h" |
| UlricL | 97:d87239079f29 | 21 | #include "ESP8266Interface.h" |
| UlricL | 97:d87239079f29 | 22 | |
| UlricL | 97:d87239079f29 | 23 | const char* hostname = "172.20.16.92"; //修改服务器IP及端口号 |
| UlricL | 97:d87239079f29 | 24 | int port = 1883; |
| UlricL | 97:d87239079f29 | 25 | |
| mbed_official | 0:857719181846 | 26 | |
| mbed_official | 71:a0fbcc153b55 | 27 | WiFiInterface *wifi; |
| mbed_official | 21:b2f2f6a840b4 | 28 | |
| mbed_official | 0:857719181846 | 29 | const char *sec2str(nsapi_security_t sec) |
| mbed_official | 0:857719181846 | 30 | { |
| mbed_official | 0:857719181846 | 31 | switch (sec) { |
| mbed_official | 0:857719181846 | 32 | case NSAPI_SECURITY_NONE: |
| mbed_official | 0:857719181846 | 33 | return "None"; |
| mbed_official | 0:857719181846 | 34 | case NSAPI_SECURITY_WEP: |
| mbed_official | 0:857719181846 | 35 | return "WEP"; |
| mbed_official | 0:857719181846 | 36 | case NSAPI_SECURITY_WPA: |
| mbed_official | 0:857719181846 | 37 | return "WPA"; |
| mbed_official | 0:857719181846 | 38 | case NSAPI_SECURITY_WPA2: |
| mbed_official | 0:857719181846 | 39 | return "WPA2"; |
| mbed_official | 0:857719181846 | 40 | case NSAPI_SECURITY_WPA_WPA2: |
| mbed_official | 0:857719181846 | 41 | return "WPA/WPA2"; |
| mbed_official | 0:857719181846 | 42 | case NSAPI_SECURITY_UNKNOWN: |
| mbed_official | 0:857719181846 | 43 | default: |
| mbed_official | 0:857719181846 | 44 | return "Unknown"; |
| mbed_official | 0:857719181846 | 45 | } |
| mbed_official | 0:857719181846 | 46 | } |
| mbed_official | 0:857719181846 | 47 | |
| mbed_official | 37:3a31525e2971 | 48 | int scan_demo(WiFiInterface *wifi) |
| mbed_official | 0:857719181846 | 49 | { |
| mbed_official | 0:857719181846 | 50 | WiFiAccessPoint *ap; |
| mbed_official | 0:857719181846 | 51 | |
| mbed_official | 35:052c1ba06ce7 | 52 | printf("Scan:\n"); |
| mbed_official | 0:857719181846 | 53 | |
| mbed_official | 0:857719181846 | 54 | int count = wifi->scan(NULL,0); |
| mbed_official | 0:857719181846 | 55 | |
| mbed_official | 66:2cf02c7d430c | 56 | if (count <= 0) { |
| mbed_official | 66:2cf02c7d430c | 57 | printf("scan() failed with return value: %d\n", count); |
| mbed_official | 66:2cf02c7d430c | 58 | return 0; |
| mbed_official | 66:2cf02c7d430c | 59 | } |
| mbed_official | 66:2cf02c7d430c | 60 | |
| mbed_official | 0:857719181846 | 61 | /* Limit number of network arbitrary to 15 */ |
| mbed_official | 0:857719181846 | 62 | count = count < 15 ? count : 15; |
| mbed_official | 0:857719181846 | 63 | |
| mbed_official | 0:857719181846 | 64 | ap = new WiFiAccessPoint[count]; |
| mbed_official | 0:857719181846 | 65 | count = wifi->scan(ap, count); |
| mbed_official | 66:2cf02c7d430c | 66 | |
| mbed_official | 66:2cf02c7d430c | 67 | if (count <= 0) { |
| mbed_official | 66:2cf02c7d430c | 68 | printf("scan() failed with return value: %d\n", count); |
| mbed_official | 66:2cf02c7d430c | 69 | return 0; |
| mbed_official | 66:2cf02c7d430c | 70 | } |
| mbed_official | 66:2cf02c7d430c | 71 | |
| mbed_official | 66:2cf02c7d430c | 72 | for (int i = 0; i < count; i++) { |
| mbed_official | 35:052c1ba06ce7 | 73 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
| mbed_official | 0:857719181846 | 74 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
| mbed_official | 0:857719181846 | 75 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
| mbed_official | 0:857719181846 | 76 | } |
| mbed_official | 35:052c1ba06ce7 | 77 | printf("%d networks available.\n", count); |
| mbed_official | 0:857719181846 | 78 | |
| mbed_official | 0:857719181846 | 79 | delete[] ap; |
| mbed_official | 37:3a31525e2971 | 80 | return count; |
| mbed_official | 0:857719181846 | 81 | } |
| mbed_official | 0:857719181846 | 82 | |
| mbed_official | 0:857719181846 | 83 | int main() |
| mbed_official | 0:857719181846 | 84 | { |
| mbed_official | 67:ebff4a8d228d | 85 | printf("WiFi example\n"); |
| mbed_official | 67:ebff4a8d228d | 86 | |
| mbed_official | 67:ebff4a8d228d | 87 | #ifdef MBED_MAJOR_VERSION |
| mbed_official | 67:ebff4a8d228d | 88 | printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
| mbed_official | 67:ebff4a8d228d | 89 | #endif |
| mbed_official | 0:857719181846 | 90 | |
| mbed_official | 71:a0fbcc153b55 | 91 | wifi = WiFiInterface::get_default_instance(); |
| mbed_official | 72:b4761c52cc91 | 92 | if (!wifi) { |
| mbed_official | 72:b4761c52cc91 | 93 | printf("ERROR: No WiFiInterface found.\n"); |
| mbed_official | 72:b4761c52cc91 | 94 | return -1; |
| mbed_official | 72:b4761c52cc91 | 95 | } |
| mbed_official | 71:a0fbcc153b55 | 96 | |
| mbed_official | 91:dab9882e2b49 | 97 | int count = scan_demo(wifi); |
| mbed_official | 37:3a31525e2971 | 98 | if (count == 0) { |
| mbed_official | 91:dab9882e2b49 | 99 | printf("No WIFI APs found - can't continue further.\n"); |
| mbed_official | 37:3a31525e2971 | 100 | return -1; |
| mbed_official | 37:3a31525e2971 | 101 | } |
| mbed_official | 0:857719181846 | 102 | |
| mbed_official | 37:3a31525e2971 | 103 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
| UlricL | 97:d87239079f29 | 104 | int ret = wifi->connect("OPPO_R11s", "12345678", NSAPI_SECURITY_WPA_WPA2); |
| mbed_official | 0:857719181846 | 105 | if (ret != 0) { |
| mbed_official | 66:2cf02c7d430c | 106 | printf("\nConnection error: %d\n", ret); |
| mbed_official | 0:857719181846 | 107 | return -1; |
| mbed_official | 0:857719181846 | 108 | } |
| mbed_official | 0:857719181846 | 109 | |
| mbed_official | 35:052c1ba06ce7 | 110 | printf("Success\n\n"); |
| mbed_official | 71:a0fbcc153b55 | 111 | printf("MAC: %s\n", wifi->get_mac_address()); |
| mbed_official | 71:a0fbcc153b55 | 112 | printf("IP: %s\n", wifi->get_ip_address()); |
| mbed_official | 71:a0fbcc153b55 | 113 | printf("Netmask: %s\n", wifi->get_netmask()); |
| mbed_official | 71:a0fbcc153b55 | 114 | printf("Gateway: %s\n", wifi->get_gateway()); |
| mbed_official | 71:a0fbcc153b55 | 115 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
| mbed_official | 0:857719181846 | 116 | |
| mbed_official | 71:a0fbcc153b55 | 117 | wifi->disconnect(); |
| mbed_official | 0:857719181846 | 118 | |
| mbed_official | 35:052c1ba06ce7 | 119 | printf("\nDone\n"); |
| UlricL | 97:d87239079f29 | 120 | MQTTNetwork mqttNetwork(network); |
| UlricL | 97:d87239079f29 | 121 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
| UlricL | 97:d87239079f29 | 122 | |
| UlricL | 97:d87239079f29 | 123 | int rc = mqttNetwork.connect(hostname, port); |
| UlricL | 97:d87239079f29 | 124 | |
| UlricL | 97:d87239079f29 | 125 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
| UlricL | 97:d87239079f29 | 126 | data.MQTTVersion = 3; |
| UlricL | 97:d87239079f29 | 127 | data.clientID.cstring = "mbed-sample"; |
| UlricL | 97:d87239079f29 | 128 | data.username.cstring = "testuser"; |
| UlricL | 97:d87239079f29 | 129 | data.password.cstring = "testpassword"; |
| UlricL | 97:d87239079f29 | 130 | if ((rc = client.connect(data)) != 0) |
| UlricL | 97:d87239079f29 | 131 | printf("rc from MQTT connect is %d\r\n", rc); |
| UlricL | 97:d87239079f29 | 132 | |
| UlricL | 97:d87239079f29 | 133 | if ((rc = client.subscribe(“sensor”, MQTT::QOS2, messageArrived)) != 0) //订阅消息 |
| UlricL | 97:d87239079f29 | 134 | printf("rc from MQTT subscribe is %d\r\n", rc); |
| UlricL | 97:d87239079f29 | 135 | while(true){ |
| UlricL | 97:d87239079f29 | 136 | client.yield(100); //在循环中一直等待接收消息 |
| mbed_official | 0:857719181846 | 137 | } |
| UlricL | 97:d87239079f29 | 138 | void messageArrived(MQTT::MessageData& md) //消息接收回调函数 |
| UlricL | 97:d87239079f29 | 139 | { |
| UlricL | 97:d87239079f29 | 140 | MQTT::Message &message = md.message; |
| UlricL | 97:d87239079f29 | 141 | if(((char*)message.payload)[0] == '0') //解析服务器消息 |
| UlricL | 97:d87239079f29 | 142 | { |
| UlricL | 97:d87239079f29 | 143 | led = 0; //处理消息开关灯,可在此修改为其他控制指令 |
| UlricL | 97:d87239079f29 | 144 | } |
| UlricL | 97:d87239079f29 | 145 | else |
| UlricL | 97:d87239079f29 | 146 | { |
| UlricL | 97:d87239079f29 | 147 | led = 1; |
| UlricL | 97:d87239079f29 | 148 | } |
| UlricL | 97:d87239079f29 | 149 | } |
| UlricL | 97:d87239079f29 | 150 | } |