Nenad Jovicic
/
PMK2021_MQTT_ETHERNET_SERVER
Ethernet mqtt server
PMK2021_MQTT_ETHERNET_SERVER
Revision 4:5b78021768e2, committed 2021-05-07
- Comitter:
- nenad
- Date:
- Fri May 07 13:44:12 2021 +0000
- Parent:
- 3:43527b1910c7
- Commit message:
- Ethernet mqtt server
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 43527b1910c7 -r 5b78021768e2 main.cpp --- a/main.cpp Mon Apr 26 21:46:12 2021 +0000 +++ b/main.cpp Fri May 07 13:44:12 2021 +0000 @@ -1,35 +1,22 @@ -/* mbed Microcontroller Library - * Copyright (c) 2019 ARM Limited - * SPDX-License-Identifier: Apache-2.0 - */ + #include "mbed.h" -//#include "EthernetInterface.h" +#include "EthernetInterface.h" #include "platform/mbed_thread.h" #include <MQTTClientMbedOs.h> + // Blinking rate in milliseconds -#define BLINKING_RATE_MS 500 -// Initialise the digital pin LED1 as an output - DigitalOut led(LED1); - - -InterruptIn button(USER_BUTTON); +#define BLINKING_RATE_MS 500 +DigitalOut led(LED1); int arrivedcount = 0; TCPSocket socket; MQTTClient client(&socket); MQTT::Message message; -int button_pressed=0; -float version = 0.6; - char* topic = "mbed-sample-pub"; - char* topic_sub = "mbed-sample-sub"; - - - - -WiFiInterface *wifi; +char* topic_pub = "PMK-server-topic-pub"; +char* topic_sub = "PMK-client-temperature"; const char *sec2str(nsapi_security_t sec) { @@ -50,41 +37,6 @@ } } -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; -} - void messageArrived(MQTT::MessageData& md) { MQTT::Message &message = md.message; @@ -93,62 +45,30 @@ ++arrivedcount; } -void buttonFunction() { - - button_pressed=1; - -} - - - - int main() -{ - button.rise(&buttonFunction); // attach the address of the flip function to the rising edge +{ + printf("PMK2021 Ethernet MQTT example\r\n"); const char* hostname = "broker.mqttdashboard.com"; int port = 1883; // Network interface - //EthernetInterface net; - - 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; - } + EthernetInterface net; + // Bring up the ethernet interface + net.connect(); - printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); - int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, 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()); - - - + // Show the network address + const char *ip = net.get_ip_address(); + printf("IP address is: %s\r\n", ip ? ip : "No IP"); //client = MQTTClient::(&socket); - socket.open(wifi); + socket.open(&net); socket.connect(hostname, port); int rc=0; MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; - data.clientID.cstring = "mbed-sample"; + data.clientID.cstring = "PMK-server"; //data.username.cstring = "testuser"; //data.password.cstring = "testpassword"; if ((rc = client.connect(data)) != 0) @@ -156,35 +76,14 @@ if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) printf("rc from MQTT subscribe is %d\r\n", rc); - - - - - - + while (true) { led = !led; thread_sleep_for(BLINKING_RATE_MS); - if (button_pressed==1) { - button_pressed=0; - printf("sace publish\r\n"); - // QoS 0 - char buf[100]; - sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version); - message.qos = MQTT::QOS0; - message.retained = false; - message.dup = false; - message.payload = (void*)buf; - message.payloadlen = strlen(buf)+1; - client.publish(topic, message); - //pc.printf("sace jild\r\n"); - //client.yield(1000); - //pc.printf("izjildovo\r\n"); - } - printf("sace jild\r\n"); + printf("Server Yielding "); client.yield(1000); - printf("izjildovo\r\n"); + printf(" -> Yielded\r\n"); } }