Masa Kovacevic 2020/0229
Dependencies: 19E042PIM_MB_PINS
Diff: main.cpp
- Revision:
- 2:c7fcce20f023
- Parent:
- 1:c994530bdb3d
diff -r c994530bdb3d -r c7fcce20f023 main.cpp --- a/main.cpp Sat Nov 27 20:33:49 2021 +0000 +++ b/main.cpp Sat Dec 11 16:26:54 2021 +0000 @@ -1,23 +1,21 @@ -/* - * Program implements MQTT client on a NUCLEO-L476RG board - * using arm mbed-mqtt library and ESP-WROOM-02 WiFi modem. - * - * University of Belgrade - School of Electrical Engineering - * Department of Electronics - * Bulevar Kralja Aleksandra 73, 11120 Belgrade, Serbia - * - * November 2021. - * - */ +// Masa Kovacevic 2020/0229 +// Decembar 2021 + +#include "Adafruit_GFX.h" +#include "Adafruit_GFX_Config.h" +#include "Adafruit_SSD1306.h" #include "mbed.h" #include "mb_pins.h" #include "platform/mbed_thread.h" #include "MQTTClientMbedOs.h" +#define I2C_ADDRESS 0x3c<<1 +#define OLED_HEIGHT_PX 64 +#define OLED_WIDTH_PX 128 +#define I2C_FREQUENCY 400000 -// LED2 blinking rate: -#define BLINKING_RATE_MS 250 + // Scaler to 3v3L #define VOLTAGE_SCALER 3.3f // Client yield timeout in miliseconds: @@ -32,8 +30,6 @@ AnalogIn pot1(MB_POT1); // Left button on the motherboard: InterruptIn sw1(MB_SW1); -// Right LED on the motherboard: -DigitalOut led2(MB_LED2); // Pointer to a WiFi network object: WiFiInterface *wifi; // Creating TCP socket: @@ -43,8 +39,19 @@ // Message handler: MQTT::Message message; -char* topic = "mbed-sample-pub"; -char* topic_sub = "mbed-sample-sub"; +I2C i2c(PB_14, PB_13); +Adafruit_SSD1306_I2c myOled(i2c,PB_5,I2C_ADDRESS,OLED_HEIGHT_PX,OLED_WIDTH_PX); + + +// Strings for start and stop +char str11[] = "start"; +char str22[] = "stop"; +// Indicator of start and stop: +int start; +int stop; +// Topic and subscription: +char* topic = "pubpim"; +char* topic_sub = "subpim"; // Counter of arrived messages: int arrivedcount = 0; // Flag indicating that button is not pressed: @@ -117,24 +124,54 @@ return count; } +// Comparing strings +bool compare_start(char *str1, char *str2) +{ + for (int i = 0, i < 6, i++){ + + if (*str1 == *str2 && *str1 == '\0' && *str2 == '\0'){ + return true; + str1++; + str2++; + } + } + + return false; +} + +bool compare_stop(char *str1, char *str2) +{ + for (int i = 0, i < 5, i++){ + + if (*str1 == *str2 && *str1 == '\0' && *str2 == '\0'){ + return true; + str1++; + str2++; + } + } + + return false; +} + 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); ++arrivedcount; + + + myOled.clearDisplay(); + myOled.printf("%.*s \r", message.payloadlen, (char*)message.payload); + myOled.display(); } -void buttonFunction() { - - button_pressed=1; - -} + int main() { - // Set the interrupt event: - sw1.fall(&buttonFunction); + + // Create a default network interface: wifi = WiFiInterface::get_default_instance(); @@ -158,13 +195,6 @@ 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); @@ -176,21 +206,17 @@ 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); + client.connect(data); + client.subscribe(topic_sub, MQTT::QOS0, messageArrived); while (true) { - // Show that the loop is running by switching motherboard LED2: - led2 = !led2; - thread_sleep_for(BLINKING_RATE_MS); - if (button_pressed==1) { - button_pressed=0; - // QoS 0 + myOLED.begin(); + i2c_obj.frequency(I2C_FREQUENCY); + myOled.clearDisplay(); + myOled.display(); + + while (start) { + char buf[100]; sprintf(buf, "V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER); message.qos = MQTT::QOS0; @@ -199,7 +225,11 @@ message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; client.publish(topic, message); + thread_sleep_for(PRINTF_DELAY_MS); + } + + // Need to call yield API to maintain connection: client.yield(YIELD_TIMEOUT_MS); }