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
- Committer:
- UlricL
- Date:
- 2019-08-23
- Revision:
- 97:d87239079f29
- Parent:
- 91:dab9882e2b49
File content as of revision 97:d87239079f29:
/* WiFi Example
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"
#include "ESP8266Interface.h"
const char* hostname = "172.20.16.92"; //修改服务器IP及端口号
int port = 1883;
WiFiInterface *wifi;
const char *sec2str(nsapi_security_t sec)
{
switch (sec) {
case NSAPI_SECURITY_NONE:
return "None";
case NSAPI_SECURITY_WEP:
return "WEP";
case NSAPI_SECURITY_WPA:
return "WPA";
case NSAPI_SECURITY_WPA2:
return "WPA2";
case NSAPI_SECURITY_WPA_WPA2:
return "WPA/WPA2";
case NSAPI_SECURITY_UNKNOWN:
default:
return "Unknown";
}
}
int scan_demo(WiFiInterface *wifi)
{
WiFiAccessPoint *ap;
printf("Scan:\n");
int count = wifi->scan(NULL,0);
if (count <= 0) {
printf("scan() failed with return value: %d\n", count);
return 0;
}
/* Limit number of network arbitrary to 15 */
count = count < 15 ? count : 15;
ap = new WiFiAccessPoint[count];
count = wifi->scan(ap, count);
if (count <= 0) {
printf("scan() failed with return value: %d\n", count);
return 0;
}
for (int i = 0; i < count; i++) {
printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
}
printf("%d networks available.\n", count);
delete[] ap;
return count;
}
int main()
{
printf("WiFi example\n");
#ifdef MBED_MAJOR_VERSION
printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
wifi = WiFiInterface::get_default_instance();
if (!wifi) {
printf("ERROR: No WiFiInterface found.\n");
return -1;
}
int count = scan_demo(wifi);
if (count == 0) {
printf("No WIFI APs found - can't continue further.\n");
return -1;
}
printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
int ret = wifi->connect("OPPO_R11s", "12345678", NSAPI_SECURITY_WPA_WPA2);
if (ret != 0) {
printf("\nConnection error: %d\n", ret);
return -1;
}
printf("Success\n\n");
printf("MAC: %s\n", wifi->get_mac_address());
printf("IP: %s\n", wifi->get_ip_address());
printf("Netmask: %s\n", wifi->get_netmask());
printf("Gateway: %s\n", wifi->get_gateway());
printf("RSSI: %d\n\n", wifi->get_rssi());
wifi->disconnect();
printf("\nDone\n");
MQTTNetwork mqttNetwork(network);
MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
int rc = mqttNetwork.connect(hostname, port);
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = "mbed-sample";
data.username.cstring = "testuser";
data.password.cstring = "testpassword";
if ((rc = client.connect(data)) != 0)
printf("rc from MQTT connect is %d\r\n", rc);
if ((rc = client.subscribe(“sensor”, MQTT::QOS2, messageArrived)) != 0) //订阅消息
printf("rc from MQTT subscribe is %d\r\n", rc);
while(true){
client.yield(100); //在循环中一直等待接收消息
}
void messageArrived(MQTT::MessageData& md) //消息接收回调函数
{
MQTT::Message &message = md.message;
if(((char*)message.payload)[0] == '0') //解析服务器消息
{
led = 0; //处理消息开关灯,可在此修改为其他控制指令
}
else
{
led = 1;
}
}
}