NIKOLA ANICIC 2019/0099
Dependencies: Adafruit_GFX_PIM 19E042PIM_MB_PINS
main.cpp@0:32862e9c3b40, 2021-12-11 (annotated)
- Committer:
- an_thanik
- Date:
- Sat Dec 11 16:29:03 2021 +0000
- Revision:
- 0:32862e9c3b40
Exam.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
an_thanik | 0:32862e9c3b40 | 1 | /* |
an_thanik | 0:32862e9c3b40 | 2 | * Nikola Anicic |
an_thanik | 0:32862e9c3b40 | 3 | * 2019/0099 |
an_thanik | 0:32862e9c3b40 | 4 | * |
an_thanik | 0:32862e9c3b40 | 5 | * ETF Beograd |
an_thanik | 0:32862e9c3b40 | 6 | * napisano 11/12/2021 |
an_thanik | 0:32862e9c3b40 | 7 | * |
an_thanik | 0:32862e9c3b40 | 8 | * 15:22 - 17:22 |
an_thanik | 0:32862e9c3b40 | 9 | */ |
an_thanik | 0:32862e9c3b40 | 10 | |
an_thanik | 0:32862e9c3b40 | 11 | /* Libraries & Definitions */ |
an_thanik | 0:32862e9c3b40 | 12 | // Pin out diagram for motherboard |
an_thanik | 0:32862e9c3b40 | 13 | #include "mb_pins.h" |
an_thanik | 0:32862e9c3b40 | 14 | |
an_thanik | 0:32862e9c3b40 | 15 | // Standard needed for most functions - Moved to version 5.15.4 for compatibility through Revisions |
an_thanik | 0:32862e9c3b40 | 16 | #include "mbed.h" |
an_thanik | 0:32862e9c3b40 | 17 | |
an_thanik | 0:32862e9c3b40 | 18 | /* Headers needed for MQTT protocol communication */ |
an_thanik | 0:32862e9c3b40 | 19 | #include "platform/mbed_thread.h" |
an_thanik | 0:32862e9c3b40 | 20 | #include "MQTTClientMbedOs.h" |
an_thanik | 0:32862e9c3b40 | 21 | |
an_thanik | 0:32862e9c3b40 | 22 | /* Headers & Definitions needed for communication with OLED display */ |
an_thanik | 0:32862e9c3b40 | 23 | #include "Adafruit_GFX.h" |
an_thanik | 0:32862e9c3b40 | 24 | #include "Adafruit_GFX_Config.h" |
an_thanik | 0:32862e9c3b40 | 25 | #include "Adafruit_SSD1306.h" |
an_thanik | 0:32862e9c3b40 | 26 | // I2C bus |
an_thanik | 0:32862e9c3b40 | 27 | #define SCL PB_13 |
an_thanik | 0:32862e9c3b40 | 28 | #define SDA PB_14 |
an_thanik | 0:32862e9c3b40 | 29 | // I2C address |
an_thanik | 0:32862e9c3b40 | 30 | #define I2C_ADDRESS 0x3C |
an_thanik | 0:32862e9c3b40 | 31 | #define I2C_ADDRESS_MBED I2C_ADDRESS << 1 |
an_thanik | 0:32862e9c3b40 | 32 | // I2C frequency |
an_thanik | 0:32862e9c3b40 | 33 | #define FREQ 400000 // 400 kHz |
an_thanik | 0:32862e9c3b40 | 34 | // OLED dimensions |
an_thanik | 0:32862e9c3b40 | 35 | #define OLED_HEIGHT 64 |
an_thanik | 0:32862e9c3b40 | 36 | #define OLED_WIDTH 128 |
an_thanik | 0:32862e9c3b40 | 37 | |
an_thanik | 0:32862e9c3b40 | 38 | #define RESET_PIN PB_5 /* Needed for initializing the display */ |
an_thanik | 0:32862e9c3b40 | 39 | |
an_thanik | 0:32862e9c3b40 | 40 | I2C i2c_obj(SDA, SCL); |
an_thanik | 0:32862e9c3b40 | 41 | Adafruit_SSD1306_I2c myOLED(i2c_obj, /* Adafruit_SSD1306.cpp documentation */ |
an_thanik | 0:32862e9c3b40 | 42 | RESET_PIN, |
an_thanik | 0:32862e9c3b40 | 43 | I2C_ADDRESS_MBED, |
an_thanik | 0:32862e9c3b40 | 44 | OLED_HEIGHT, |
an_thanik | 0:32862e9c3b40 | 45 | OLED_WIDTH |
an_thanik | 0:32862e9c3b40 | 46 | ); |
an_thanik | 0:32862e9c3b40 | 47 | |
an_thanik | 0:32862e9c3b40 | 48 | /* Standard Definitions */ |
an_thanik | 0:32862e9c3b40 | 49 | #define WAIT_PERIOD_SEC 10 // s |
an_thanik | 0:32862e9c3b40 | 50 | #define YIELD_TIMEOUT_MS 1000 // ms |
an_thanik | 0:32862e9c3b40 | 51 | #define BLINK_PERIOD 250 // ms |
an_thanik | 0:32862e9c3b40 | 52 | #define SMALL_WAIT_MS 10 // ms |
an_thanik | 0:32862e9c3b40 | 53 | |
an_thanik | 0:32862e9c3b40 | 54 | DigitalOut LED (MB_LED2); |
an_thanik | 0:32862e9c3b40 | 55 | InterruptIn SW1(MB_SW1); |
an_thanik | 0:32862e9c3b40 | 56 | AnalogIn POT1 (MB_POT1); |
an_thanik | 0:32862e9c3b40 | 57 | |
an_thanik | 0:32862e9c3b40 | 58 | TCPSocket socket; |
an_thanik | 0:32862e9c3b40 | 59 | MQTTClient client(&socket); |
an_thanik | 0:32862e9c3b40 | 60 | MQTT::Message message; |
an_thanik | 0:32862e9c3b40 | 61 | |
an_thanik | 0:32862e9c3b40 | 62 | /* Variables */ |
an_thanik | 0:32862e9c3b40 | 63 | bool active = 0; |
an_thanik | 0:32862e9c3b40 | 64 | |
an_thanik | 0:32862e9c3b40 | 65 | // Wifi information |
an_thanik | 0:32862e9c3b40 | 66 | WiFiInterface *wifi; |
an_thanik | 0:32862e9c3b40 | 67 | // .json for network information |
an_thanik | 0:32862e9c3b40 | 68 | const char* hostname = "broker.hivemq.com"; |
an_thanik | 0:32862e9c3b40 | 69 | int port = 1883; |
an_thanik | 0:32862e9c3b40 | 70 | |
an_thanik | 0:32862e9c3b40 | 71 | char* topic_pub = "pubpim"; // Publication // Za potrebe testiranja broj hardvera, obrisi kada zavrsis |
an_thanik | 0:32862e9c3b40 | 72 | char* topic_sub = "subpim"; // Subscription |
an_thanik | 0:32862e9c3b40 | 73 | |
an_thanik | 0:32862e9c3b40 | 74 | /* Functions */ |
an_thanik | 0:32862e9c3b40 | 75 | const char *sec2str(nsapi_security_t sec) |
an_thanik | 0:32862e9c3b40 | 76 | { |
an_thanik | 0:32862e9c3b40 | 77 | switch (sec) |
an_thanik | 0:32862e9c3b40 | 78 | { |
an_thanik | 0:32862e9c3b40 | 79 | case NSAPI_SECURITY_NONE: |
an_thanik | 0:32862e9c3b40 | 80 | return "None"; |
an_thanik | 0:32862e9c3b40 | 81 | case NSAPI_SECURITY_WEP: |
an_thanik | 0:32862e9c3b40 | 82 | return "WEP"; |
an_thanik | 0:32862e9c3b40 | 83 | case NSAPI_SECURITY_WPA: |
an_thanik | 0:32862e9c3b40 | 84 | return "WPA"; |
an_thanik | 0:32862e9c3b40 | 85 | case NSAPI_SECURITY_WPA2: |
an_thanik | 0:32862e9c3b40 | 86 | return "WPA2"; |
an_thanik | 0:32862e9c3b40 | 87 | case NSAPI_SECURITY_WPA_WPA2: |
an_thanik | 0:32862e9c3b40 | 88 | return "WPA/WPA2"; |
an_thanik | 0:32862e9c3b40 | 89 | case NSAPI_SECURITY_UNKNOWN: |
an_thanik | 0:32862e9c3b40 | 90 | default: |
an_thanik | 0:32862e9c3b40 | 91 | return "Unknown"; |
an_thanik | 0:32862e9c3b40 | 92 | } |
an_thanik | 0:32862e9c3b40 | 93 | } |
an_thanik | 0:32862e9c3b40 | 94 | |
an_thanik | 0:32862e9c3b40 | 95 | int scan(WiFiInterface *wifi) |
an_thanik | 0:32862e9c3b40 | 96 | { |
an_thanik | 0:32862e9c3b40 | 97 | WiFiAccessPoint *access_point; |
an_thanik | 0:32862e9c3b40 | 98 | printf("Scan:\n"); |
an_thanik | 0:32862e9c3b40 | 99 | |
an_thanik | 0:32862e9c3b40 | 100 | int counter = wifi->scan(NULL, 0); |
an_thanik | 0:32862e9c3b40 | 101 | if (counter <= 0) |
an_thanik | 0:32862e9c3b40 | 102 | { |
an_thanik | 0:32862e9c3b40 | 103 | printf("Function 'scan()' failed."); |
an_thanik | 0:32862e9c3b40 | 104 | return 0; |
an_thanik | 0:32862e9c3b40 | 105 | } |
an_thanik | 0:32862e9c3b40 | 106 | |
an_thanik | 0:32862e9c3b40 | 107 | counter = counter < 10 ? counter : 10; |
an_thanik | 0:32862e9c3b40 | 108 | |
an_thanik | 0:32862e9c3b40 | 109 | access_point = new WiFiAccessPoint[counter]; |
an_thanik | 0:32862e9c3b40 | 110 | counter = wifi->scan(access_point, counter); |
an_thanik | 0:32862e9c3b40 | 111 | |
an_thanik | 0:32862e9c3b40 | 112 | if (counter <= 0) |
an_thanik | 0:32862e9c3b40 | 113 | { |
an_thanik | 0:32862e9c3b40 | 114 | printf("Second 'scan()' failed"); |
an_thanik | 0:32862e9c3b40 | 115 | return 0; |
an_thanik | 0:32862e9c3b40 | 116 | } |
an_thanik | 0:32862e9c3b40 | 117 | |
an_thanik | 0:32862e9c3b40 | 118 | for (int i = 0; i < counter; i++) |
an_thanik | 0:32862e9c3b40 | 119 | { |
an_thanik | 0:32862e9c3b40 | 120 | printf( |
an_thanik | 0:32862e9c3b40 | 121 | "Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", |
an_thanik | 0:32862e9c3b40 | 122 | access_point[i].get_ssid(), |
an_thanik | 0:32862e9c3b40 | 123 | sec2str(access_point[i].get_security()), |
an_thanik | 0:32862e9c3b40 | 124 | access_point[i].get_bssid()[0], |
an_thanik | 0:32862e9c3b40 | 125 | access_point[i].get_bssid()[1], |
an_thanik | 0:32862e9c3b40 | 126 | access_point[i].get_bssid()[2], |
an_thanik | 0:32862e9c3b40 | 127 | access_point[i].get_bssid()[3], |
an_thanik | 0:32862e9c3b40 | 128 | access_point[i].get_bssid()[4], |
an_thanik | 0:32862e9c3b40 | 129 | access_point[i].get_bssid()[5], |
an_thanik | 0:32862e9c3b40 | 130 | access_point[i].get_rssi(), |
an_thanik | 0:32862e9c3b40 | 131 | access_point[i].get_channel() |
an_thanik | 0:32862e9c3b40 | 132 | ); |
an_thanik | 0:32862e9c3b40 | 133 | thread_sleep_for(SMALL_WAIT_MS); |
an_thanik | 0:32862e9c3b40 | 134 | } |
an_thanik | 0:32862e9c3b40 | 135 | printf("%d networks found.\n", counter); |
an_thanik | 0:32862e9c3b40 | 136 | |
an_thanik | 0:32862e9c3b40 | 137 | delete[] access_point; |
an_thanik | 0:32862e9c3b40 | 138 | return counter; |
an_thanik | 0:32862e9c3b40 | 139 | } |
an_thanik | 0:32862e9c3b40 | 140 | |
an_thanik | 0:32862e9c3b40 | 141 | void messageArrived(MQTT::MessageData &md) |
an_thanik | 0:32862e9c3b40 | 142 | { |
an_thanik | 0:32862e9c3b40 | 143 | MQTT::Message &message = md.message; |
an_thanik | 0:32862e9c3b40 | 144 | printf("%.*s \r\n", message.payloadlen, (char*) message.payload); |
an_thanik | 0:32862e9c3b40 | 145 | // Browser message: izbaceno radi poredjenja |
an_thanik | 0:32862e9c3b40 | 146 | } |
an_thanik | 0:32862e9c3b40 | 147 | |
an_thanik | 0:32862e9c3b40 | 148 | int button_pressed = 0; |
an_thanik | 0:32862e9c3b40 | 149 | void ISR_SW1() |
an_thanik | 0:32862e9c3b40 | 150 | { |
an_thanik | 0:32862e9c3b40 | 151 | active = !active; |
an_thanik | 0:32862e9c3b40 | 152 | } |
an_thanik | 0:32862e9c3b40 | 153 | |
an_thanik | 0:32862e9c3b40 | 154 | /* Main program */ |
an_thanik | 0:32862e9c3b40 | 155 | int main () |
an_thanik | 0:32862e9c3b40 | 156 | { |
an_thanik | 0:32862e9c3b40 | 157 | |
an_thanik | 0:32862e9c3b40 | 158 | /* Initialization */ |
an_thanik | 0:32862e9c3b40 | 159 | SW1.fall(&ISR_SW1); // Debugging |
an_thanik | 0:32862e9c3b40 | 160 | |
an_thanik | 0:32862e9c3b40 | 161 | // Initializing the display |
an_thanik | 0:32862e9c3b40 | 162 | i2c_obj.frequency(FREQ); |
an_thanik | 0:32862e9c3b40 | 163 | myOLED.begin(); |
an_thanik | 0:32862e9c3b40 | 164 | |
an_thanik | 0:32862e9c3b40 | 165 | wifi = WiFiInterface::get_default_instance(); |
an_thanik | 0:32862e9c3b40 | 166 | |
an_thanik | 0:32862e9c3b40 | 167 | if(!wifi) |
an_thanik | 0:32862e9c3b40 | 168 | { |
an_thanik | 0:32862e9c3b40 | 169 | printf("ERROR 000: Connection not established.\n"); |
an_thanik | 0:32862e9c3b40 | 170 | return -1; |
an_thanik | 0:32862e9c3b40 | 171 | } |
an_thanik | 0:32862e9c3b40 | 172 | |
an_thanik | 0:32862e9c3b40 | 173 | int count = scan(wifi); |
an_thanik | 0:32862e9c3b40 | 174 | if (count == 0) |
an_thanik | 0:32862e9c3b40 | 175 | { |
an_thanik | 0:32862e9c3b40 | 176 | printf("ERROR 001: No Wifi access points found.\n"); |
an_thanik | 0:32862e9c3b40 | 177 | return -1; |
an_thanik | 0:32862e9c3b40 | 178 | } |
an_thanik | 0:32862e9c3b40 | 179 | |
an_thanik | 0:32862e9c3b40 | 180 | printf("\n Connecting to %s... \n", MBED_CONF_APP_WIFI_SSID); |
an_thanik | 0:32862e9c3b40 | 181 | |
an_thanik | 0:32862e9c3b40 | 182 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
an_thanik | 0:32862e9c3b40 | 183 | if (ret != 0) |
an_thanik | 0:32862e9c3b40 | 184 | { |
an_thanik | 0:32862e9c3b40 | 185 | printf("Error 002: Connection error.\n"); |
an_thanik | 0:32862e9c3b40 | 186 | return 1; |
an_thanik | 0:32862e9c3b40 | 187 | } |
an_thanik | 0:32862e9c3b40 | 188 | printf("Success!\n"); |
an_thanik | 0:32862e9c3b40 | 189 | printf("MAC: %s\n", wifi->get_mac_address()); |
an_thanik | 0:32862e9c3b40 | 190 | printf("IP: %s\n", wifi->get_ip_address()); |
an_thanik | 0:32862e9c3b40 | 191 | printf("Netmask: %s \n", wifi->get_netmask()); |
an_thanik | 0:32862e9c3b40 | 192 | printf("Gateway: %s \n", wifi->get_gateway()); |
an_thanik | 0:32862e9c3b40 | 193 | printf("RSSI: %d \n\n", wifi->get_rssi()); |
an_thanik | 0:32862e9c3b40 | 194 | |
an_thanik | 0:32862e9c3b40 | 195 | int rc = 0; |
an_thanik | 0:32862e9c3b40 | 196 | |
an_thanik | 0:32862e9c3b40 | 197 | socket.open(wifi); |
an_thanik | 0:32862e9c3b40 | 198 | socket.connect(hostname, port); |
an_thanik | 0:32862e9c3b40 | 199 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
an_thanik | 0:32862e9c3b40 | 200 | data.MQTTVersion = 3; |
an_thanik | 0:32862e9c3b40 | 201 | data.clientID.cstring = "pim-06"; |
an_thanik | 0:32862e9c3b40 | 202 | |
an_thanik | 0:32862e9c3b40 | 203 | if ((rc = client.connect(data)) != 0) |
an_thanik | 0:32862e9c3b40 | 204 | { |
an_thanik | 0:32862e9c3b40 | 205 | printf("rc from MQTT connect is %d\r\n", rc); |
an_thanik | 0:32862e9c3b40 | 206 | } |
an_thanik | 0:32862e9c3b40 | 207 | |
an_thanik | 0:32862e9c3b40 | 208 | if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) |
an_thanik | 0:32862e9c3b40 | 209 | { |
an_thanik | 0:32862e9c3b40 | 210 | printf("rc from MQTT subscribe is %d\r\n", rc); |
an_thanik | 0:32862e9c3b40 | 211 | } |
an_thanik | 0:32862e9c3b40 | 212 | |
an_thanik | 0:32862e9c3b40 | 213 | while (true) |
an_thanik | 0:32862e9c3b40 | 214 | { |
an_thanik | 0:32862e9c3b40 | 215 | LED = !LED; |
an_thanik | 0:32862e9c3b40 | 216 | thread_sleep_for(BLINK_PERIOD); |
an_thanik | 0:32862e9c3b40 | 217 | |
an_thanik | 0:32862e9c3b40 | 218 | if ( strcmp((char *) message.payload, "start") == 0 ) |
an_thanik | 0:32862e9c3b40 | 219 | active = 1; |
an_thanik | 0:32862e9c3b40 | 220 | |
an_thanik | 0:32862e9c3b40 | 221 | if ( strcmp((char *) message.payload, "stop") == 0 ) |
an_thanik | 0:32862e9c3b40 | 222 | active = 0; |
an_thanik | 0:32862e9c3b40 | 223 | |
an_thanik | 0:32862e9c3b40 | 224 | if ( active == 1 ) |
an_thanik | 0:32862e9c3b40 | 225 | { |
an_thanik | 0:32862e9c3b40 | 226 | char buffer[100]; |
an_thanik | 0:32862e9c3b40 | 227 | int bufferlen = strlen(buffer) + 1; |
an_thanik | 0:32862e9c3b40 | 228 | sprintf(buffer, "V(POT1) = %1.2f \r\n", POT1 * 3.3); // Where 3.3V is Vcc |
an_thanik | 0:32862e9c3b40 | 229 | |
an_thanik | 0:32862e9c3b40 | 230 | message.qos = MQTT::QOS0; |
an_thanik | 0:32862e9c3b40 | 231 | message.retained = false; |
an_thanik | 0:32862e9c3b40 | 232 | message.dup = false; |
an_thanik | 0:32862e9c3b40 | 233 | message.payload = (void*) buffer; |
an_thanik | 0:32862e9c3b40 | 234 | message.payloadlen = bufferlen; |
an_thanik | 0:32862e9c3b40 | 235 | client.publish(topic_pub, message); // Sending 'Message' to 'Topic for publishing' |
an_thanik | 0:32862e9c3b40 | 236 | thread_sleep_for(WAIT_PERIOD_SEC * 1000); |
an_thanik | 0:32862e9c3b40 | 237 | } |
an_thanik | 0:32862e9c3b40 | 238 | |
an_thanik | 0:32862e9c3b40 | 239 | client.yield(YIELD_TIMEOUT_MS); |
an_thanik | 0:32862e9c3b40 | 240 | } |
an_thanik | 0:32862e9c3b40 | 241 | } |
an_thanik | 0:32862e9c3b40 | 242 | |
an_thanik | 0:32862e9c3b40 | 243 | /* Debug code */ |
an_thanik | 0:32862e9c3b40 | 244 | /* |
an_thanik | 0:32862e9c3b40 | 245 | if ( button_pressed == 1 ) |
an_thanik | 0:32862e9c3b40 | 246 | { |
an_thanik | 0:32862e9c3b40 | 247 | button_pressed = 0; |
an_thanik | 0:32862e9c3b40 | 248 | char buffer[100]; |
an_thanik | 0:32862e9c3b40 | 249 | sprintf(buffer, "V(POT1) = %1.2f \r\n", POT1 * 3.3); // Where 3.3V is Vcc |
an_thanik | 0:32862e9c3b40 | 250 | |
an_thanik | 0:32862e9c3b40 | 251 | message.qos = MQTT::QOS0; |
an_thanik | 0:32862e9c3b40 | 252 | message.retained = false; |
an_thanik | 0:32862e9c3b40 | 253 | message.dup = false; |
an_thanik | 0:32862e9c3b40 | 254 | message.payload = (void*) buffer; |
an_thanik | 0:32862e9c3b40 | 255 | message.payloadlen = strlen(buffer) + 1; |
an_thanik | 0:32862e9c3b40 | 256 | client.publish(topic_pub, message); // Sending 'Message' to 'Topic for publishing' |
an_thanik | 0:32862e9c3b40 | 257 | } |
an_thanik | 0:32862e9c3b40 | 258 | */ |