Khakimov Rishat
/
Kurs_copyed
ADFWQEF
Revision 100:7c75f1700889, committed 2022-11-17
- Comitter:
- rishat
- Date:
- Thu Nov 17 11:07:36 2022 +0000
- Parent:
- 99:86dca48315d8
- Commit message:
- WEFDE
Changed in this revision
--- a/main.cpp Tue Oct 08 14:00:11 2019 +0100 +++ b/main.cpp Thu Nov 17 11:07:36 2022 +0000 @@ -1,112 +1,103 @@ -/* 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 "MQTTmbed.h" +#include <MQTTClientMbedOs.h> +#include <cstdio> #include "mbed.h" -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; - } +AnalogIn my_adc(PA_7); //D11 on board +AnalogIn my_abc(PA_6); +//DigitalOut led(LED1); +DigitalOut dat(D15); + int main(int argc, char *argv[]) { + //long distance = sensor.distance(); + //long distanza = distance; + //printf("distanza %d \n",distanza); + - 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("Starting IBM MQTT demo:\n"); + TCPSocket socket; + NetworkInterface *net = NetworkInterface::get_default_instance(); + if (!net) { + printf("Error! No 11network inteface11 found.\n"); + return 0; } + printf("Connecting to the network...\r\n"); + nsapi_size_or_error_t rc = net->connect(); + if (rc != 0) { + printf("Error! _net->connect() returned: %d\r\n", rc); + return -1; } + rc = socket.open(net); + if (rc != 0) { + printf("Error! _socket.open() returned: %d\r\n", rc); + return -1; } + SocketAddress address; + net->gethostbyname("dev.rightech.io", + &address); + address.set_port(1883); + printf("Opening connection to remote %s port %d\r\n", + address.get_ip_address(), address.get_port()); + rc = socket.connect(address); + if (rc != 0) { + printf("Error! _socket.connect() returned: %d\r\n", rc); + return -1; } + printf("Connected socket\n"); + MQTTClient client(&socket); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "XXXXX"; + data.username.cstring = "ZZZ"; + data.password.cstring = "ZZZ"; + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\r\n", rc); + MQTT::Message message; + + while(1) { - 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; - } + + char *topic = "post"; + char buf[100]; + int distanza = round(my_adc*100); + //sprintf(buf, + // "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%d,\"Pressure\":" + // "%d,\"Humidity\":%d}}", temp, press, hum); + + sprintf(buf, + "{\"distanse\": %d}", distanza); + wait_ns(1000000); + +printf("Sending message: \n%s\n", buf); +message.qos = MQTT::QOS0; +message.retained = false; +message.dup = false; +message.payload = (void *)buf; +message.payloadlen = strlen(buf); +rc = client.publish(topic, message); +printf("Message sent\n"); +printf("Demo concluded successfully \r\n"); + wait_ns(9000000000); - 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(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()); - - wifi->disconnect(); - - printf("\nDone\n"); -} + +char buf2[10]; +int ogon = round(my_abc*100); +sprintf(buf2, + "{\"ogon\": %d}", ogon); + + + wait_ns(9000000000); + + printf("Sending message: \n%s\n", buf2); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void *)buf2; + message.payloadlen = strlen(buf2); + rc = client.publish(topic, message); + printf("Message sent\n"); + printf("Demo concluded successfully \r\n"); + wait_ns(10000000000); + } + return 0; } + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-mqtt.lib Thu Nov 17 11:07:36 2022 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-mqtt/#7fa219e87b3355e8a6fd281659697fe3a0c87630
--- a/mbed-os.lib Tue Oct 08 14:00:11 2019 +0100 +++ b/mbed-os.lib Thu Nov 17 11:07:36 2022 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#b6e5a0a8afa34dec9dae8963778aebce0c82a54b +https://github.com/ARMmbed/mbed-os/#b1796dedeb8accde1cbaecf136fab96895e23d81
--- a/mbed_app.json Tue Oct 08 14:00:11 2019 +0100 +++ b/mbed_app.json Thu Nov 17 11:07:36 2022 +0000 @@ -1,17 +1,28 @@ { - "config": { - "wifi-ssid": { - "help": "WiFi SSID", - "value": "\"SSID\"" - }, - "wifi-password": { - "help": "WiFi Password", - "value": "\"PASSWORD\"" - } - }, - "target_overrides": { - "*": { - "platform.stdio-convert-newlines": true - } - } +"config": { +"hostname": { +"help": "The demo will try to connect to this web address on port 80 (or port 443 when using tls).", +"value": "\"ifconfig.io\"" +}, +"use-tls-socket": { +"value": false +}, +"socket-bufsize": { +"help": "Max socket data heap usage", +"value": "1024" } +}, +"target_overrides": { +"*": { +"target.network-default-interface-type": "WIFI", +"nsapi.default-wifi-security": "WPA_WPA2", +"nsapi.default-wifi-ssid": "\"AFACTAP\"", +"nsapi.default-wifi-password": "\"1209348756\"", +"esp8266.tx": "D8", +"esp8266.rx": "D2", +"esp8266.socket-bufsize": "1024", +"esp8266.debug": false, +"esp8266.provide-default" : true +} +} +} \ No newline at end of file