Michael Sapozhnikov
/
Cursach_Rigtech_copy
11
main.cpp@0:7e56962875b0, 2022-01-25 (annotated)
- Committer:
- graf2112
- Date:
- Tue Jan 25 08:56:42 2022 +0000
- Revision:
- 0:7e56962875b0
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
graf2112 | 0:7e56962875b0 | 1 | /* WiFi Example |
graf2112 | 0:7e56962875b0 | 2 | * Copyright (c) 2016 ARM Limited |
graf2112 | 0:7e56962875b0 | 3 | * |
graf2112 | 0:7e56962875b0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
graf2112 | 0:7e56962875b0 | 5 | * you may not use this file except in compliance with the License. |
graf2112 | 0:7e56962875b0 | 6 | * You may obtain a copy of the License at |
graf2112 | 0:7e56962875b0 | 7 | * |
graf2112 | 0:7e56962875b0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
graf2112 | 0:7e56962875b0 | 9 | * |
graf2112 | 0:7e56962875b0 | 10 | * Unless required by applicable law or agreed to in writing, software |
graf2112 | 0:7e56962875b0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
graf2112 | 0:7e56962875b0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
graf2112 | 0:7e56962875b0 | 13 | * See the License for the specific language governing permissions and |
graf2112 | 0:7e56962875b0 | 14 | * limitations under the License. |
graf2112 | 0:7e56962875b0 | 15 | */ |
graf2112 | 0:7e56962875b0 | 16 | |
graf2112 | 0:7e56962875b0 | 17 | #include "mbed.h" |
graf2112 | 0:7e56962875b0 | 18 | #include "MQTTmbed.h" |
graf2112 | 0:7e56962875b0 | 19 | #include "MQTTClientMbedOs.h" |
graf2112 | 0:7e56962875b0 | 20 | #include "Sht31.h" |
graf2112 | 0:7e56962875b0 | 21 | #define MQTT_MAX_PACKET_SIZE 400 |
graf2112 | 0:7e56962875b0 | 22 | #define MQTT_MAX_PAYLOAD_SIZE 300 |
graf2112 | 0:7e56962875b0 | 23 | |
graf2112 | 0:7e56962875b0 | 24 | Sht31 temp_sensor(I2C_SDA, I2C_SCL); |
graf2112 | 0:7e56962875b0 | 25 | int temp = 1; |
graf2112 | 0:7e56962875b0 | 26 | AnalogIn my_adc(D11); |
graf2112 | 0:7e56962875b0 | 27 | float t; |
graf2112 | 0:7e56962875b0 | 28 | float h; |
graf2112 | 0:7e56962875b0 | 29 | void ibm_cloud_demo(NetworkInterface *net) |
graf2112 | 0:7e56962875b0 | 30 | { |
graf2112 | 0:7e56962875b0 | 31 | TCPSocket socket; |
graf2112 | 0:7e56962875b0 | 32 | MQTTClient client(&socket); |
graf2112 | 0:7e56962875b0 | 33 | SocketAddress a; |
graf2112 | 0:7e56962875b0 | 34 | char* hostname = "dev.rightech.io"; |
graf2112 | 0:7e56962875b0 | 35 | net->gethostbyname(hostname, &a); |
graf2112 | 0:7e56962875b0 | 36 | int port = 1883; |
graf2112 | 0:7e56962875b0 | 37 | a.set_port(port); |
graf2112 | 0:7e56962875b0 | 38 | printf("Connecting to %s:%d\r\n", hostname, port); |
graf2112 | 0:7e56962875b0 | 39 | socket.open(net); |
graf2112 | 0:7e56962875b0 | 40 | printf("Opened socket\n\r"); |
graf2112 | 0:7e56962875b0 | 41 | int rc = socket.connect(a); |
graf2112 | 0:7e56962875b0 | 42 | if (rc != 0) |
graf2112 | 0:7e56962875b0 | 43 | printf("rc from TCP connect is %d\r\n", rc); |
graf2112 | 0:7e56962875b0 | 44 | printf("Connected socket\n\r"); |
graf2112 | 0:7e56962875b0 | 45 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
graf2112 | 0:7e56962875b0 | 46 | data.MQTTVersion = 3; |
graf2112 | 0:7e56962875b0 | 47 | data.clientID.cstring = "mqtt-graf2114-ylbl1u"; |
graf2112 | 0:7e56962875b0 | 48 | data.username.cstring = "Qwerty1"; |
graf2112 | 0:7e56962875b0 | 49 | data.password.cstring = "Qwerty1"; |
graf2112 | 0:7e56962875b0 | 50 | //{clientId:"Qwerty1",userName:"Qwerty2",password:"Qwerty3"} |
graf2112 | 0:7e56962875b0 | 51 | if ((rc = client.connect(data)) != 0) |
graf2112 | 0:7e56962875b0 | 52 | printf("rc from MQTT connect is %d\r\n", rc); |
graf2112 | 0:7e56962875b0 | 53 | t = temp_sensor.readTemperature(); |
graf2112 | 0:7e56962875b0 | 54 | MQTT::Message message; |
graf2112 | 0:7e56962875b0 | 55 | char buf[MQTT_MAX_PAYLOAD_SIZE]; |
graf2112 | 0:7e56962875b0 | 56 | sprintf(buf,"%d", t); |
graf2112 | 0:7e56962875b0 | 57 | message.qos = MQTT::QOS0; |
graf2112 | 0:7e56962875b0 | 58 | message.retained = false; |
graf2112 | 0:7e56962875b0 | 59 | message.dup = false; |
graf2112 | 0:7e56962875b0 | 60 | message.payload = (void*)buf; |
graf2112 | 0:7e56962875b0 | 61 | message.payloadlen = strlen(buf); |
graf2112 | 0:7e56962875b0 | 62 | char* topic = "temp"; |
graf2112 | 0:7e56962875b0 | 63 | if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE ) |
graf2112 | 0:7e56962875b0 | 64 | printf("message too long!\r\n"); |
graf2112 | 0:7e56962875b0 | 65 | rc = client.publish(topic, message); |
graf2112 | 0:7e56962875b0 | 66 | |
graf2112 | 0:7e56962875b0 | 67 | h = temp_sensor.readHumidity(); |
graf2112 | 0:7e56962875b0 | 68 | sprintf(buf,"%f", h); |
graf2112 | 0:7e56962875b0 | 69 | message.qos = MQTT::QOS0; |
graf2112 | 0:7e56962875b0 | 70 | message.retained = false; |
graf2112 | 0:7e56962875b0 | 71 | message.dup = false; |
graf2112 | 0:7e56962875b0 | 72 | message.payload = (void*)buf; |
graf2112 | 0:7e56962875b0 | 73 | message.payloadlen = strlen(buf); |
graf2112 | 0:7e56962875b0 | 74 | char* topic = "uhum"; |
graf2112 | 0:7e56962875b0 | 75 | if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE ) |
graf2112 | 0:7e56962875b0 | 76 | printf("message too long!\r\n"); |
graf2112 | 0:7e56962875b0 | 77 | rc = client.publish(topic, message); |
graf2112 | 0:7e56962875b0 | 78 | |
graf2112 | 0:7e56962875b0 | 79 | sprintf(buf,"%f"(my_adc.read()*100); |
graf2112 | 0:7e56962875b0 | 80 | message.qos = MQTT::QOS0; |
graf2112 | 0:7e56962875b0 | 81 | message.retained = false; |
graf2112 | 0:7e56962875b0 | 82 | message.dup = false; |
graf2112 | 0:7e56962875b0 | 83 | message.payload = (void*)buf; |
graf2112 | 0:7e56962875b0 | 84 | message.payloadlen = strlen(buf); |
graf2112 | 0:7e56962875b0 | 85 | char* topic = "hum"; |
graf2112 | 0:7e56962875b0 | 86 | if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE ) |
graf2112 | 0:7e56962875b0 | 87 | printf("message too long!\r\n"); |
graf2112 | 0:7e56962875b0 | 88 | rc = client.publish(topic, message); |
graf2112 | 0:7e56962875b0 | 89 | |
graf2112 | 0:7e56962875b0 | 90 | return; |
graf2112 | 0:7e56962875b0 | 91 | } |
graf2112 | 0:7e56962875b0 | 92 | |
graf2112 | 0:7e56962875b0 | 93 | |
graf2112 | 0:7e56962875b0 | 94 | WiFiInterface *wifi; |
graf2112 | 0:7e56962875b0 | 95 | |
graf2112 | 0:7e56962875b0 | 96 | const char *sec2str(nsapi_security_t sec) |
graf2112 | 0:7e56962875b0 | 97 | { |
graf2112 | 0:7e56962875b0 | 98 | switch (sec) { |
graf2112 | 0:7e56962875b0 | 99 | case NSAPI_SECURITY_NONE: |
graf2112 | 0:7e56962875b0 | 100 | return "None"; |
graf2112 | 0:7e56962875b0 | 101 | case NSAPI_SECURITY_WEP: |
graf2112 | 0:7e56962875b0 | 102 | return "WEP"; |
graf2112 | 0:7e56962875b0 | 103 | case NSAPI_SECURITY_WPA: |
graf2112 | 0:7e56962875b0 | 104 | return "WPA"; |
graf2112 | 0:7e56962875b0 | 105 | case NSAPI_SECURITY_WPA2: |
graf2112 | 0:7e56962875b0 | 106 | return "WPA2"; |
graf2112 | 0:7e56962875b0 | 107 | case NSAPI_SECURITY_WPA_WPA2: |
graf2112 | 0:7e56962875b0 | 108 | return "WPA/WPA2"; |
graf2112 | 0:7e56962875b0 | 109 | case NSAPI_SECURITY_UNKNOWN: |
graf2112 | 0:7e56962875b0 | 110 | default: |
graf2112 | 0:7e56962875b0 | 111 | return "Unknown"; |
graf2112 | 0:7e56962875b0 | 112 | } |
graf2112 | 0:7e56962875b0 | 113 | } |
graf2112 | 0:7e56962875b0 | 114 | |
graf2112 | 0:7e56962875b0 | 115 | int scan_demo(WiFiInterface *wifi) |
graf2112 | 0:7e56962875b0 | 116 | { |
graf2112 | 0:7e56962875b0 | 117 | WiFiAccessPoint *ap; |
graf2112 | 0:7e56962875b0 | 118 | |
graf2112 | 0:7e56962875b0 | 119 | printf("Scan:\n"); |
graf2112 | 0:7e56962875b0 | 120 | |
graf2112 | 0:7e56962875b0 | 121 | int count = wifi->scan(NULL,0); |
graf2112 | 0:7e56962875b0 | 122 | |
graf2112 | 0:7e56962875b0 | 123 | if (count <= 0) { |
graf2112 | 0:7e56962875b0 | 124 | printf("scan() failed with return value: %d\n", count); |
graf2112 | 0:7e56962875b0 | 125 | return 0; |
graf2112 | 0:7e56962875b0 | 126 | } |
graf2112 | 0:7e56962875b0 | 127 | |
graf2112 | 0:7e56962875b0 | 128 | /* Limit number of network arbitrary to 15 */ |
graf2112 | 0:7e56962875b0 | 129 | count = count < 15 ? count : 15; |
graf2112 | 0:7e56962875b0 | 130 | |
graf2112 | 0:7e56962875b0 | 131 | ap = new WiFiAccessPoint[count]; |
graf2112 | 0:7e56962875b0 | 132 | count = wifi->scan(ap, count); |
graf2112 | 0:7e56962875b0 | 133 | |
graf2112 | 0:7e56962875b0 | 134 | if (count <= 0) { |
graf2112 | 0:7e56962875b0 | 135 | printf("scan() failed with return value: %d\n", count); |
graf2112 | 0:7e56962875b0 | 136 | return 0; |
graf2112 | 0:7e56962875b0 | 137 | } |
graf2112 | 0:7e56962875b0 | 138 | |
graf2112 | 0:7e56962875b0 | 139 | for (int i = 0; i < count; i++) { |
graf2112 | 0:7e56962875b0 | 140 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
graf2112 | 0:7e56962875b0 | 141 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
graf2112 | 0:7e56962875b0 | 142 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
graf2112 | 0:7e56962875b0 | 143 | } |
graf2112 | 0:7e56962875b0 | 144 | printf("%d networks available.\n", count); |
graf2112 | 0:7e56962875b0 | 145 | |
graf2112 | 0:7e56962875b0 | 146 | delete[] ap; |
graf2112 | 0:7e56962875b0 | 147 | return count; |
graf2112 | 0:7e56962875b0 | 148 | } |
graf2112 | 0:7e56962875b0 | 149 | |
graf2112 | 0:7e56962875b0 | 150 | int main() |
graf2112 | 0:7e56962875b0 | 151 | { |
graf2112 | 0:7e56962875b0 | 152 | printf("WiFi example\n"); |
graf2112 | 0:7e56962875b0 | 153 | |
graf2112 | 0:7e56962875b0 | 154 | #ifdef MBED_MAJOR_VERSION |
graf2112 | 0:7e56962875b0 | 155 | printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
graf2112 | 0:7e56962875b0 | 156 | #endif |
graf2112 | 0:7e56962875b0 | 157 | |
graf2112 | 0:7e56962875b0 | 158 | wifi = WiFiInterface::get_default_instance(); |
graf2112 | 0:7e56962875b0 | 159 | if (!wifi) { |
graf2112 | 0:7e56962875b0 | 160 | printf("ERROR: No WiFiInterface found.\n"); |
graf2112 | 0:7e56962875b0 | 161 | return -1; |
graf2112 | 0:7e56962875b0 | 162 | } |
graf2112 | 0:7e56962875b0 | 163 | |
graf2112 | 0:7e56962875b0 | 164 | int count = scan_demo(wifi); |
graf2112 | 0:7e56962875b0 | 165 | if (count == 0) { |
graf2112 | 0:7e56962875b0 | 166 | printf("No WIFI APs found - can't continue further.\n"); |
graf2112 | 0:7e56962875b0 | 167 | return -1; |
graf2112 | 0:7e56962875b0 | 168 | } |
graf2112 | 0:7e56962875b0 | 169 | |
graf2112 | 0:7e56962875b0 | 170 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
graf2112 | 0:7e56962875b0 | 171 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
graf2112 | 0:7e56962875b0 | 172 | if (ret != 0) { |
graf2112 | 0:7e56962875b0 | 173 | printf("\nConnection error: %d\n", ret); |
graf2112 | 0:7e56962875b0 | 174 | return -1; |
graf2112 | 0:7e56962875b0 | 175 | } |
graf2112 | 0:7e56962875b0 | 176 | |
graf2112 | 0:7e56962875b0 | 177 | printf("Success\n\n"); |
graf2112 | 0:7e56962875b0 | 178 | printf("MAC: %s\n", wifi->get_mac_address()); |
graf2112 | 0:7e56962875b0 | 179 | printf("IP: %s\n", wifi->get_ip_address()); |
graf2112 | 0:7e56962875b0 | 180 | printf("Netmask: %s\n", wifi->get_netmask()); |
graf2112 | 0:7e56962875b0 | 181 | printf("Gateway: %s\n", wifi->get_gateway()); |
graf2112 | 0:7e56962875b0 | 182 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
graf2112 | 0:7e56962875b0 | 183 | while (1) |
graf2112 | 0:7e56962875b0 | 184 | { |
graf2112 | 0:7e56962875b0 | 185 | if (temp<2) { |
graf2112 | 0:7e56962875b0 | 186 | ibm_cloud_demo(wifi); |
graf2112 | 0:7e56962875b0 | 187 | wait_ms(1000); |
graf2112 | 0:7e56962875b0 | 188 | temp++; |
graf2112 | 0:7e56962875b0 | 189 | } |
graf2112 | 0:7e56962875b0 | 190 | else |
graf2112 | 0:7e56962875b0 | 191 | while(1) |
graf2112 | 0:7e56962875b0 | 192 | { |
graf2112 | 0:7e56962875b0 | 193 | relay = !relay; |
graf2112 | 0:7e56962875b0 | 194 | wait_ms(500); |
graf2112 | 0:7e56962875b0 | 195 | } |
graf2112 | 0:7e56962875b0 | 196 | } |
graf2112 | 0:7e56962875b0 | 197 | printf("Done\n\n"); |
graf2112 | 0:7e56962875b0 | 198 | } |