Nenad Tepavac 2020/0028
Dependencies: mbed Adafruit_GFX 19E042PIM_MB_PINS
Revision 0:981049ce9511, committed 2022-01-17
- Comitter:
- ostoja
- Date:
- Mon Jan 17 16:01:04 2022 +0000
- Commit message:
- Nenad Tepavac 2020/0028;
Changed in this revision
diff -r 000000000000 -r 981049ce9511 19E042PIM_MB_PINS.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/19E042PIM_MB_PINS.lib Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/tzwell/code/19E042PIM_MB_PINS/#5722459a201f
diff -r 000000000000 -r 981049ce9511 Adafruit_GFX.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Adafruit_GFX.lib Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/ostoja/code/Adafruit_GFX/#1f77bb01df7e
diff -r 000000000000 -r 981049ce9511 main1.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main1.txt Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,91 @@ +/* +* Nenad Tepavac 2020/0028 +* Etf Beograd +*/ + +/* +* Biblioteke +*/ +#include "mbed.h" + +/* +* Makroi +*/ +//11000000 +#define NULA 0xC0 +#define KEC 0xF9 +#define DVA 0xA4 +#define TRI 0xB0 +#define CTR 0x99 +#define PET 0x92 +#define SES 0x82 +#define SEAM 0xF8 +#define OSAM 0x80 +#define DE 0x90 + + +BusOut display(PA_10, PA_9, PA_8,PB_10,PB_5, PB_4, PB_3, PB_6, PC_7); +InterruptIn sw(PC_8); +InterruptIn btn(BUTTON1); + +int c = 0; +int b = 0; +int b2 = 0; +int s = 0; + +void increment(){ + c++; + if(c > 10) + c = 0; + + switch(c){ + case 0: + display.write(NULA); + break; + case 1: + display.write(KEC); + break; + case 2: + display.write(DVA); + break; + case 3: + display.write(TRI); + break; + case 4: + display.write(CTR); + break; + case 5: + display.write(PET); + break; + case 6: + display.write(SES); + break; + case 7: + display.write(SEAM); + break; + case 8: + display.write(OSAM); + break; + case 9: + display.write(DE); + break; + } +} + +int main(){ + //podizanje broja mogu da postignu i dugme na nucleo ploci i sw2 na maticnoj + //ne koristim sw1 jer je problematican + + sw.rise(&increment); + btn.rise(&increment); + + while(true){ + if(c > 9){ + display.write(0b101111001); + wait_ms(5); + display.write(NULA); + wait_ms(5); + } + } + +} \ No newline at end of file
diff -r 000000000000 -r 981049ce9511 main2.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main2.txt Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,95 @@ +/* +* Nenad Tepavac 2020/0028 +* Etf Beograd +*/ + +/* +* Biblioteke +*/ +#include "mbed.h" +#include "mb_pins.h" +/* +* Makroi +*/ +//11000000 +#define NULA 0xC0 +#define KEC 0xF9 +#define DVA 0xA4 +#define TRI 0xB0 +#define CTR 0x99 +#define PET 0x92 +#define SES 0x82 +#define SEAM 0xF8 +#define OSAM 0x80 +#define DE 0x90 + + +BusOut display(PA_10, PA_9, PA_8,PB_10,PB_5, PB_4, PB_3, PB_6, PC_7); +PwmOut buzzer(MB_BUZZ); +InterruptIn sw(BUTTON1); +//sw2 se koristi jer je sw1 vezan za buzzer na ploci +InterruptIn btn(PC_8); + +AnalogIn pot1(MB_POT1); +AnalogIn pot2(MB_POT2); + +//AnalogOut b5(PA_11); + +//DigitalIn swd(PC_8); + +int c = 0; +int b = 0; +int b2 = 0; +int s = 0; + +void buzz(){ + buzzer.period((float)(pot2 + 1) / 1000); + buzzer.write(1 - pot1 * 0.1); + wait_ms(500); + buzzer.write(0); +} + +void displaywrite(){ + c = (int)(pot1 * 10); + switch(c){ + case 0: + display.write(NULA); + break; + case 1: + display.write(KEC); + break; + case 2: + display.write(DVA); + break; + case 3: + display.write(TRI); + break; + case 4: + display.write(CTR); + break; + case 5: + display.write(PET); + break; + case 6: + display.write(SES); + break; + case 7: + display.write(SEAM); + break; + case 8: + display.write(OSAM); + break; + case 9: + display.write(DE); + break; + } +} + +int main(){ + display.write(NULA); + + sw.rise(&buzz); + btn.rise(&displaywrite); + + while(true){} +} \ No newline at end of file
diff -r 000000000000 -r 981049ce9511 main3.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main3.txt Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,254 @@ +#include "mbed.h" +#include "Adafruit_GFX.h" +#include "Adafruit_GFX_Config.h" +#include "Adafruit_SSD1306.h" +#include "mb_pins.h" +#include "platform/mbed_thread.h" +#include "MQTTClientMbedOs.h" + +// I2C bus pins: +#define D_SDA PB_14 +#define D_SCL PB_13 +// I2C address, 60d or 0x3c: +#define I2C_REAL_ADD 0x3c +#define I2C_ADDRESS I2C_REAL_ADD << 1 +// Set OLED width and heigth [pixel]: +#define OLED_WIDTH_PX 128 +#define OLED_HEIGHT_PX 64 +// I2C frequency: +#define I2C_FREQUENCY 400000 +// Multipliers of POT1 and POT2 for OLED rectangle position: +#define WIDTH_SCALER 128 +#define HEIGHT_SCALER 64 +// Initial rectangle position: +#define INITIAL_X_POSITION OLED_WIDTH_PX / 2 +#define INITIAL_Y_POSITION OLED_HEIGHT_PX / 2 +// Refresh rate: +#define REFRESH_RATE_MS 5 +// Half of the potentiometer return value: +#define HALF_INTERVAL 0.5f +// LED2 blinking rate: +#define BLINKING_RATE_MS 250 +// Scaler to 3v3L +#define VOLTAGE_SCALER 3.3f +// Client yield timeout in miliseconds: +#define YIELD_TIMEOUT_MS 1000 +// Maximum number of networks to scan for: +#define MAX_NETWORKS 15 +// Small delay for network information printing: +#define PRINTF_DELAY_MS 10 + +// Initialize I2C: +I2C i2c(PB_14,PB_13); + +// Initialize OLED display: +Adafruit_SSD1306_I2c myOled(i2c,PB_5,I2C_ADDRESS,OLED_HEIGHT_PX,OLED_WIDTH_PX); + +// Left potentiometer: +AnalogIn pot1(MB_POT1); +// Left button on the motherboard: +InterruptIn sw1(MB_SW1); +// Right LED on the motherboard: +DigitalOut led2(MB_LED2); +DigitalIn sw2(MB_SW2); +int sw2_state; +// Pointer to a WiFi network object: +WiFiInterface *wifi; +// Creating TCP socket: +TCPSocket socket; +// Creating MQTT client using the TCP socket; +MQTTClient client(&socket); +// Message handler: +MQTT::Message message; + +char* topic = "pubpim"; +char* topic_sub = "subpim"; +// Counter of arrived messages: +int arrivedcount = 0; +int send_voltage = 0; +// Flag indicating that button is not pressed: +int button_pressed=0; +// HiveMQ broker connectivity information: +const char* hostname = "broker.hivemq.com"; +int port = 1883; +// Returning a string for a provided network encryption: +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_networks(WiFiInterface *wifi) +{ + printf("Scan:\n"); + + // Scan only for the number of networks, first parameter is NULL: + int count = wifi->scan(NULL, 0); + // If there are no networks, count == 0, if there is an error, counter < 0: + if (count <= 0) + { + printf("scan() failed with return value: %d\n", count); + return 0; + } + + // Limit number of network arbitrary to some reasonable number: + count = count < MAX_NETWORKS ? count : MAX_NETWORKS; + + // Create a local pointer to an object, which is an array of WiFi APs: + WiFiAccessPoint *ap = new WiFiAccessPoint[count]; + // Now scan again for 'count' networks and populate the array of APs: + count = wifi->scan(ap, count); + + // This time, the number of entries to 'ap' is returned: + if (count <= 0) + { + printf("scan() failed with return value: %d\n", count); + return 0; + } + + // Print out the parameters of each AP: + 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()); + thread_sleep_for(PRINTF_DELAY_MS); + } + printf("%d networks available.\n", count); + + // Since 'ap' is dynamically allocated pointer to the array of objects, it + // needs to be deleted: + delete[] ap; + return count; +} + +void print_text(int len, char buf[100]){ + if(arrivedcount > 5){ + arrivedcount = 0; + myOled.clearDisplay(); + } + for(int i = 0;i < len; i++){ + myOled.drawChar(10 + i * 6,1 + 10 * arrivedcount,buf[i],1,0,1); + } + myOled.display(); +} + +void messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + //printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); + printf("Message from the browser: %.*s\r\n", message.payloadlen, (char*)message.payload); + char buf[100]; + sprintf(buf,"%.*s", message.payloadlen, (char*)message.payload); + + printf("%s\r\n",buf); + + ++arrivedcount; + print_text(message.payloadlen,(char*)message.payload); + + if(strcmp(buf,"start")==0){ + send_voltage = 1; + } + + if(strcmp(buf,"stop")==0){ + send_voltage = 0; + } +} + +void buttonFunction() { + button_pressed=1; +} + +int main() +{ + myOled.begin(); + i2c.frequency(I2C_FREQUENCY); + // Set the interrupt event: + sw1.fall(&buttonFunction); + + // Create a default network interface: + wifi = WiFiInterface::get_default_instance(); + if (!wifi) { + printf("ERROR: No WiFiInterface found.\n"); + return -1; + } + + // Scan for available networks and aquire information about Access Points: + int count = scan_networks(wifi); + if (count == 0) { + printf("No WIFI APs found - can't continue further.\n"); + return -1; + } + + // Connect to the network with the parameters specified in 'mbed_app.json': + 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; + } + + // Print out the information aquired: + 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()); + + // Open TCP socket using WiFi network interface: + socket.open(wifi); + // Connect to the HiveMQ broker: + socket.connect(hostname, port); + // Fill connect data with default values: + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + // Change only ID and protocol version: + data.MQTTVersion = 3; + data.clientID.cstring = "NUCLEO-L476RG-60"; + + // Connect the + int rc = 0; + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\r\n", rc); + + if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) + printf("rc from MQTT subscribe is %d\r\n", rc); + + while (true) { + + // Show that the loop is running by switching motherboard LED2: + led2 = !led2; + //thread_sleep_for(BLINKING_RATE_MS); + if (button_pressed==1 && send_voltage == 1) { + button_pressed=0; + // QoS 0 + char buf[100]; + sprintf(buf, "V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic, message); + ++arrivedcount; + print_text(14,buf); + + } + // Need to call yield API to maintain connection: + client.yield(YIELD_TIMEOUT_MS); + } +} \ No newline at end of file
diff -r 000000000000 -r 981049ce9511 mbed-mqtt.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-mqtt.lib Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-mqtt/#7fa219e87b3355e8a6fd281659697fe3a0c87630
diff -r 000000000000 -r 981049ce9511 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#e4b81f67f939a0c0b11c147ce74aa367271e1279
diff -r 000000000000 -r 981049ce9511 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
diff -r 000000000000 -r 981049ce9511 mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Mon Jan 17 16:01:04 2022 +0000 @@ -0,0 +1,25 @@ +{ + "config": { + "wifi-ssid": { + "help": "WIFI SSID", + "value": "\"cvele97\"" + }, + "wifi-password": { + "help": "Wifi Password", + "value": "\"ekonomskitigar2022\"" + } + }, + "target_overrides": { + "*": { + "platform.stdio-convert-newlines": true + }, + "NUCLEO_L476RG": { + "target.network-default-interface-type": "WIFI", + "esp8266.tx" : "PC_4", + "esp8266.rx" : "PC_5", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + } + } +} \ No newline at end of file