Project for course Appliance of MCU, School of Electrical engineering, Uni of Belgrade
Dependencies: 19E042PIM_MB_PINS Adafruit_GFX MAX44000 mbed-mqtt
main.cpp@3:68c83e94576f, 2022-06-20 (annotated)
- Committer:
- pavleradojkovic
- Date:
- Mon Jun 20 20:17:32 2022 +0000
- Revision:
- 3:68c83e94576f
- Parent:
- 2:4daed4780232
- Child:
- 4:b08e3f8e89bd
Tested code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pavleradojkovic | 0:cd8f25aaf6d7 | 1 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 2 | #include "mbed.h" |
pavleradojkovic | 0:cd8f25aaf6d7 | 3 | #include "mb_pins.h" |
pavleradojkovic | 0:cd8f25aaf6d7 | 4 | #include "platform/mbed_thread.h" |
pavleradojkovic | 0:cd8f25aaf6d7 | 5 | #include "MQTTClientMbedOs.h" |
pavleradojkovic | 0:cd8f25aaf6d7 | 6 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 7 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 8 | // LED2 blinking rate: |
pavleradojkovic | 0:cd8f25aaf6d7 | 9 | #define BLINKING_RATE_MS 250 |
pavleradojkovic | 0:cd8f25aaf6d7 | 10 | // Scaler to 3v3L |
pavleradojkovic | 0:cd8f25aaf6d7 | 11 | #define VOLTAGE_SCALER 3.3f |
pavleradojkovic | 0:cd8f25aaf6d7 | 12 | // Client yield timeout in miliseconds: |
pavleradojkovic | 0:cd8f25aaf6d7 | 13 | #define YIELD_TIMEOUT_MS 1000 |
pavleradojkovic | 0:cd8f25aaf6d7 | 14 | // Maximum number of networks to scan for: |
pavleradojkovic | 0:cd8f25aaf6d7 | 15 | #define MAX_NETWORKS 15 |
pavleradojkovic | 0:cd8f25aaf6d7 | 16 | // Small delay for network information printing: |
pavleradojkovic | 0:cd8f25aaf6d7 | 17 | #define PRINTF_DELAY_MS 10 |
pavleradojkovic | 0:cd8f25aaf6d7 | 18 | |
pavleradojkovic | 2:4daed4780232 | 19 | // Global Variables |
pavleradojkovic | 0:cd8f25aaf6d7 | 20 | // Left potentiometer: |
pavleradojkovic | 0:cd8f25aaf6d7 | 21 | AnalogIn pot1(MB_POT1); |
pavleradojkovic | 0:cd8f25aaf6d7 | 22 | // Left button on the motherboard: |
pavleradojkovic | 0:cd8f25aaf6d7 | 23 | InterruptIn sw1(MB_SW1); |
pavleradojkovic | 1:dbf4e2903500 | 24 | // LEFT LED on the motherboard: |
pavleradojkovic | 1:dbf4e2903500 | 25 | DigitalOut led1(MB_LED1); |
pavleradojkovic | 0:cd8f25aaf6d7 | 26 | // Right LED on the motherboard: |
pavleradojkovic | 0:cd8f25aaf6d7 | 27 | DigitalOut led2(MB_LED2); |
pavleradojkovic | 0:cd8f25aaf6d7 | 28 | // Pointer to a WiFi network object: |
pavleradojkovic | 0:cd8f25aaf6d7 | 29 | WiFiInterface *wifi; |
pavleradojkovic | 0:cd8f25aaf6d7 | 30 | // Creating TCP socket: - not in use |
pavleradojkovic | 0:cd8f25aaf6d7 | 31 | TCPSocket socket; |
pavleradojkovic | 0:cd8f25aaf6d7 | 32 | // Creating TLS socket: |
pavleradojkovic | 0:cd8f25aaf6d7 | 33 | TLSSocket tlsSocket; |
pavleradojkovic | 0:cd8f25aaf6d7 | 34 | // Creating MQTT client using the TCP socket; |
pavleradojkovic | 0:cd8f25aaf6d7 | 35 | MQTTClient client(&socket); |
pavleradojkovic | 0:cd8f25aaf6d7 | 36 | // Message handler: |
pavleradojkovic | 0:cd8f25aaf6d7 | 37 | MQTT::Message message; |
pavleradojkovic | 2:4daed4780232 | 38 | //Topics |
pavleradojkovic | 1:dbf4e2903500 | 39 | char* topic = "test/Pavle/Pot1"; |
pavleradojkovic | 1:dbf4e2903500 | 40 | char* topic_sub = "test/Olja/Pot1"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 41 | // Counter of arrived messages: |
pavleradojkovic | 0:cd8f25aaf6d7 | 42 | int arrivedcount = 0; |
pavleradojkovic | 0:cd8f25aaf6d7 | 43 | // Flag indicating that button is not pressed: |
pavleradojkovic | 0:cd8f25aaf6d7 | 44 | int button_pressed=0; |
pavleradojkovic | 0:cd8f25aaf6d7 | 45 | // HiveMQ broker connectivity information: |
pavleradojkovic | 3:68c83e94576f | 46 | const char* hostname = "broker.hivemq.com"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 47 | int port = 1883; |
pavleradojkovic | 0:cd8f25aaf6d7 | 48 | // Returning a string for a provided network encryption: |
pavleradojkovic | 2:4daed4780232 | 49 | |
pavleradojkovic | 2:4daed4780232 | 50 | |
pavleradojkovic | 2:4daed4780232 | 51 | //Global Functions |
pavleradojkovic | 2:4daed4780232 | 52 | const char *sec2str(nsapi_security_t sec); |
pavleradojkovic | 2:4daed4780232 | 53 | int scan_networks(WiFiInterface *wifi); |
pavleradojkovic | 2:4daed4780232 | 54 | void messageArrived(MQTT::MessageData& md); |
pavleradojkovic | 2:4daed4780232 | 55 | void buttonFunction(); |
pavleradojkovic | 2:4daed4780232 | 56 | |
pavleradojkovic | 2:4daed4780232 | 57 | int main() |
pavleradojkovic | 2:4daed4780232 | 58 | { |
pavleradojkovic | 2:4daed4780232 | 59 | led1 = 0; |
pavleradojkovic | 2:4daed4780232 | 60 | // Set the interrupt event: |
pavleradojkovic | 2:4daed4780232 | 61 | sw1.fall(&buttonFunction); |
pavleradojkovic | 2:4daed4780232 | 62 | |
pavleradojkovic | 2:4daed4780232 | 63 | // Create a default network interface: |
pavleradojkovic | 2:4daed4780232 | 64 | wifi = WiFiInterface::get_default_instance(); |
pavleradojkovic | 2:4daed4780232 | 65 | if (!wifi) { |
pavleradojkovic | 2:4daed4780232 | 66 | printf("ERROR: No WiFiInterface found.\n"); |
pavleradojkovic | 2:4daed4780232 | 67 | return -1; |
pavleradojkovic | 2:4daed4780232 | 68 | } |
pavleradojkovic | 2:4daed4780232 | 69 | |
pavleradojkovic | 2:4daed4780232 | 70 | // Scan for available networks and aquire information about Access Points: |
pavleradojkovic | 2:4daed4780232 | 71 | int count = scan_networks(wifi); |
pavleradojkovic | 2:4daed4780232 | 72 | if (count == 0) { |
pavleradojkovic | 2:4daed4780232 | 73 | printf("No WIFI APs found - can't continue further.\n"); |
pavleradojkovic | 2:4daed4780232 | 74 | return -1; |
pavleradojkovic | 2:4daed4780232 | 75 | } |
pavleradojkovic | 2:4daed4780232 | 76 | |
pavleradojkovic | 2:4daed4780232 | 77 | // Connect to the network with the parameters specified in 'mbed_app.json': |
pavleradojkovic | 2:4daed4780232 | 78 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
pavleradojkovic | 2:4daed4780232 | 79 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
pavleradojkovic | 2:4daed4780232 | 80 | if (ret != 0) { |
pavleradojkovic | 2:4daed4780232 | 81 | printf("\nConnection error: %d\n", ret); |
pavleradojkovic | 2:4daed4780232 | 82 | return -1; |
pavleradojkovic | 2:4daed4780232 | 83 | } |
pavleradojkovic | 2:4daed4780232 | 84 | |
pavleradojkovic | 2:4daed4780232 | 85 | // Print out the information aquired: |
pavleradojkovic | 2:4daed4780232 | 86 | printf("Success\n\n"); |
pavleradojkovic | 2:4daed4780232 | 87 | printf("MAC: %s\n", wifi->get_mac_address()); |
pavleradojkovic | 2:4daed4780232 | 88 | printf("IP: %s\n", wifi->get_ip_address()); |
pavleradojkovic | 2:4daed4780232 | 89 | printf("Netmask: %s\n", wifi->get_netmask()); |
pavleradojkovic | 2:4daed4780232 | 90 | printf("Gateway: %s\n", wifi->get_gateway()); |
pavleradojkovic | 2:4daed4780232 | 91 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
pavleradojkovic | 2:4daed4780232 | 92 | |
pavleradojkovic | 2:4daed4780232 | 93 | // Open TCP socket using WiFi network interface: |
pavleradojkovic | 2:4daed4780232 | 94 | socket.open(wifi); |
pavleradojkovic | 2:4daed4780232 | 95 | // Connect to the HiveMQ broker: |
pavleradojkovic | 2:4daed4780232 | 96 | socket.connect(hostname, port); |
pavleradojkovic | 2:4daed4780232 | 97 | // Fill connect data with default values: |
pavleradojkovic | 2:4daed4780232 | 98 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
pavleradojkovic | 2:4daed4780232 | 99 | // Change only ID and protocol version: |
pavleradojkovic | 2:4daed4780232 | 100 | data.MQTTVersion = 3; |
pavleradojkovic | 2:4daed4780232 | 101 | data.clientID.cstring = "NUCLEO-L476RG-64"; |
pavleradojkovic | 2:4daed4780232 | 102 | // Connect the |
pavleradojkovic | 2:4daed4780232 | 103 | int rc = 0; |
pavleradojkovic | 2:4daed4780232 | 104 | if ((rc = client.connect(data)) != 0) |
pavleradojkovic | 2:4daed4780232 | 105 | printf("rc from MQTT connect is %d\n", rc); |
pavleradojkovic | 2:4daed4780232 | 106 | |
pavleradojkovic | 3:68c83e94576f | 107 | if ((rc = client.subscribe(topic_sub, MQTT::QOS0, messageArrived)) != 0) |
pavleradojkovic | 2:4daed4780232 | 108 | printf("rc from MQTT subscribe is %d\n", rc); |
pavleradojkovic | 2:4daed4780232 | 109 | |
pavleradojkovic | 2:4daed4780232 | 110 | while (true) { |
pavleradojkovic | 2:4daed4780232 | 111 | // Show that the loop is running by switching motherboard LED2: |
pavleradojkovic | 2:4daed4780232 | 112 | led2 = !led2; |
pavleradojkovic | 2:4daed4780232 | 113 | thread_sleep_for(BLINKING_RATE_MS); |
pavleradojkovic | 2:4daed4780232 | 114 | if (button_pressed==1) { |
pavleradojkovic | 2:4daed4780232 | 115 | button_pressed=0; |
pavleradojkovic | 2:4daed4780232 | 116 | // QoS 0 |
pavleradojkovic | 2:4daed4780232 | 117 | char buf[100]; |
pavleradojkovic | 2:4daed4780232 | 118 | sprintf(buf, "V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER); |
pavleradojkovic | 2:4daed4780232 | 119 | message.qos = MQTT::QOS0; |
pavleradojkovic | 2:4daed4780232 | 120 | message.retained = false; |
pavleradojkovic | 2:4daed4780232 | 121 | message.dup = false; |
pavleradojkovic | 2:4daed4780232 | 122 | message.payload = (void*)buf; |
pavleradojkovic | 2:4daed4780232 | 123 | message.payloadlen = strlen(buf)+1; |
pavleradojkovic | 2:4daed4780232 | 124 | client.publish(topic, message); |
pavleradojkovic | 2:4daed4780232 | 125 | } |
pavleradojkovic | 2:4daed4780232 | 126 | // Need to call yield API to maintain connection: |
pavleradojkovic | 2:4daed4780232 | 127 | client.yield(YIELD_TIMEOUT_MS); |
pavleradojkovic | 2:4daed4780232 | 128 | } |
pavleradojkovic | 2:4daed4780232 | 129 | } |
pavleradojkovic | 2:4daed4780232 | 130 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 131 | const char *sec2str(nsapi_security_t sec) |
pavleradojkovic | 0:cd8f25aaf6d7 | 132 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 133 | switch (sec) |
pavleradojkovic | 0:cd8f25aaf6d7 | 134 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 135 | case NSAPI_SECURITY_NONE: |
pavleradojkovic | 0:cd8f25aaf6d7 | 136 | return "None"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 137 | case NSAPI_SECURITY_WEP: |
pavleradojkovic | 0:cd8f25aaf6d7 | 138 | return "WEP"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 139 | case NSAPI_SECURITY_WPA: |
pavleradojkovic | 0:cd8f25aaf6d7 | 140 | return "WPA"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 141 | case NSAPI_SECURITY_WPA2: |
pavleradojkovic | 0:cd8f25aaf6d7 | 142 | return "WPA2"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 143 | case NSAPI_SECURITY_WPA_WPA2: |
pavleradojkovic | 0:cd8f25aaf6d7 | 144 | return "WPA/WPA2"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 145 | case NSAPI_SECURITY_UNKNOWN: |
pavleradojkovic | 0:cd8f25aaf6d7 | 146 | default: |
pavleradojkovic | 0:cd8f25aaf6d7 | 147 | return "Unknown"; |
pavleradojkovic | 0:cd8f25aaf6d7 | 148 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 149 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 150 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 151 | int scan_networks(WiFiInterface *wifi) |
pavleradojkovic | 0:cd8f25aaf6d7 | 152 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 153 | printf("Scan:\n"); |
pavleradojkovic | 0:cd8f25aaf6d7 | 154 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 155 | // Scan only for the number of networks, first parameter is NULL: |
pavleradojkovic | 0:cd8f25aaf6d7 | 156 | int count = wifi->scan(NULL, 0); |
pavleradojkovic | 0:cd8f25aaf6d7 | 157 | // If there are no networks, count == 0, if there is an error, counter < 0: |
pavleradojkovic | 0:cd8f25aaf6d7 | 158 | if (count <= 0) |
pavleradojkovic | 0:cd8f25aaf6d7 | 159 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 160 | printf("scan() failed with return value: %d\n", count); |
pavleradojkovic | 0:cd8f25aaf6d7 | 161 | return 0; |
pavleradojkovic | 0:cd8f25aaf6d7 | 162 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 163 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 164 | // Limit number of network arbitrary to some reasonable number: |
pavleradojkovic | 0:cd8f25aaf6d7 | 165 | count = count < MAX_NETWORKS ? count : MAX_NETWORKS; |
pavleradojkovic | 0:cd8f25aaf6d7 | 166 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 167 | // Create a local pointer to an object, which is an array of WiFi APs: |
pavleradojkovic | 0:cd8f25aaf6d7 | 168 | WiFiAccessPoint *ap = new WiFiAccessPoint[count]; |
pavleradojkovic | 0:cd8f25aaf6d7 | 169 | // Now scan again for 'count' networks and populate the array of APs: |
pavleradojkovic | 0:cd8f25aaf6d7 | 170 | count = wifi->scan(ap, count); |
pavleradojkovic | 0:cd8f25aaf6d7 | 171 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 172 | // This time, the number of entries to 'ap' is returned: |
pavleradojkovic | 0:cd8f25aaf6d7 | 173 | if (count <= 0) |
pavleradojkovic | 0:cd8f25aaf6d7 | 174 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 175 | printf("scan() failed with return value: %d\n", count); |
pavleradojkovic | 0:cd8f25aaf6d7 | 176 | return 0; |
pavleradojkovic | 0:cd8f25aaf6d7 | 177 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 178 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 179 | // Print out the parameters of each AP: |
pavleradojkovic | 0:cd8f25aaf6d7 | 180 | for (int i = 0; i < count; i++) |
pavleradojkovic | 0:cd8f25aaf6d7 | 181 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 182 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
pavleradojkovic | 0:cd8f25aaf6d7 | 183 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
pavleradojkovic | 0:cd8f25aaf6d7 | 184 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
pavleradojkovic | 0:cd8f25aaf6d7 | 185 | thread_sleep_for(PRINTF_DELAY_MS); |
pavleradojkovic | 0:cd8f25aaf6d7 | 186 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 187 | printf("%d networks available.\n", count); |
pavleradojkovic | 0:cd8f25aaf6d7 | 188 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 189 | // Since 'ap' is dynamically allocated pointer to the array of objects, it |
pavleradojkovic | 0:cd8f25aaf6d7 | 190 | // needs to be deleted: |
pavleradojkovic | 0:cd8f25aaf6d7 | 191 | delete[] ap; |
pavleradojkovic | 0:cd8f25aaf6d7 | 192 | return count; |
pavleradojkovic | 0:cd8f25aaf6d7 | 193 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 194 | |
pavleradojkovic | 0:cd8f25aaf6d7 | 195 | void messageArrived(MQTT::MessageData& md) |
pavleradojkovic | 0:cd8f25aaf6d7 | 196 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 197 | MQTT::Message &message = md.message; |
pavleradojkovic | 0:cd8f25aaf6d7 | 198 | //printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
pavleradojkovic | 0:cd8f25aaf6d7 | 199 | printf("Message from the browser: %.*s\r\n", message.payloadlen, (char*)message.payload); |
pavleradojkovic | 0:cd8f25aaf6d7 | 200 | ++arrivedcount; |
pavleradojkovic | 0:cd8f25aaf6d7 | 201 | } |
pavleradojkovic | 0:cd8f25aaf6d7 | 202 | |
pavleradojkovic | 3:68c83e94576f | 203 | void buttonFunction() |
pavleradojkovic | 3:68c83e94576f | 204 | { |
pavleradojkovic | 0:cd8f25aaf6d7 | 205 | button_pressed=1; |
pavleradojkovic | 2:4daed4780232 | 206 | } |