MQTT to OLED

Dependencies:   Adafruit_GFX 19E042PIM_MB_PINS

Files at this revision

API Documentation at this revision

Comitter:
filip_ste
Date:
Fri Dec 10 18:13:38 2021 +0000
Child:
1:5992d7483ab2
Commit message:
MQTT to OLED;

Changed in this revision

19E042PIM_MB_PINS.lib Show annotated file Show diff for this revision Revisions of this file
Adafruit_GFX.lib Show annotated file Show diff for this revision Revisions of this file
Adafruit_GFX_Config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-mqtt.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/19E042PIM_MB_PINS.lib	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/tzwell/code/19E042PIM_MB_PINS/#5722459a201f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_GFX.lib	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/nkhorman/code/Adafruit_GFX/#7fb1d4d3525d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_GFX_Config.h	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,1 @@
+#define GFX_WANT_ABSTRACTS
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,84 @@
+#include "mb_pins.h"
+#include "mbed.h"
+#include "platform/mbed_thread.h"
+#include "MQTTClientMbedOs.h"
+
+#include "Adafruit_GFX.h"
+#include "Adafruit_GFX_Config.h"
+#include "Adafruit_SSD1306.h"
+
+///////////////////////////////////////////////////////////////
+
+#define SMALL_WAIT_MS 10
+
+//I2C adress
+#define I2C_ADDRESS         0x3c
+#define I2C_ADD_MBED    I2C_ADDRESS << 1
+
+//OLED DIMENSIONS
+#define OLED_HEIGHT_PX 64
+#define OLED_WIDTH_PX 128
+
+///////////////////////////////////////////////////////////////
+
+TCPSocket socket;
+MQTTClient client(&socket);
+MQTT::Message message;
+WiFiInterface *wifi;
+
+I2C i2c_obj(MB_OLED_SDA, MB_OLED_SCL);
+Adafruit_SSD1306_I2c myOLED(i2c_obj, PB_5, I2C_ADD_MBED, OLED_HEIGHT_PX, OLED_WIDTH_PX);
+
+char* topic_sub = "mbed-sample-sub";
+const char* hostname = "broker.hivemq.com";
+int port = 1883;
+
+///////////////////////////////////////////////////////////////
+
+void messageArrived(MQTT::MessageData& md)
+{
+    MQTT::Message &message = md.message;
+    printf("Browser message: %.*s \n", message.payloadlen, (char*)message.payload);
+    
+    myOLED.clearDisplay();
+    //myOLED.setTextCursor(0, 0);
+    myOLED.printf("%.*s \r", message.payloadlen, (char*)message.payload);
+    myOLED.display();
+}
+
+///////////////////////////////////////////////////////////////
+
+int main()
+{
+    wifi = WiFiInterface::get_default_instance();
+    
+    printf("Connecting 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("Greska u povezivanju");
+        return -1;
+    }
+    printf("Success\n");
+    
+    socket.open(wifi);
+    socket.connect(hostname,port);
+    
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = "Mustafa";
+
+    client.connect(data);
+    client.subscribe(topic_sub, MQTT::QOS0, messageArrived);
+    
+    myOLED.begin(); //pocetak
+    i2c_obj.frequency(400000); //veca frekvencija slanja da ne bi ghostovalo
+    
+    myOLED.clearDisplay();
+    myOLED.display();
+    
+    
+    while(1) {
+        thread_sleep_for(SMALL_WAIT_MS);
+        client.yield(1000);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-mqtt.lib	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-mqtt/#7fa219e87b3355e8a6fd281659697fe3a0c87630
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e4b81f67f939a0c0b11c147ce74aa367271e1279
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Fri Dec 10 18:13:38 2021 +0000
@@ -0,0 +1,25 @@
+{
+    "config": {
+        "wifi-ssid": {
+            "help": "WIFI SSID",
+            "value": "\"Fidra-wireless\""
+        },
+        "wifi-password": {
+            "help": "Wifi Password",
+            "value": "\"fiki2002tea2006\""
+        }
+    },
+    "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