Milos Novakovic 2020/0425

Dependencies:   Adafruit_GFX 19E042PIM_MB_PINS

Files at this revision

API Documentation at this revision

Comitter:
mlos55
Date:
Sat Dec 11 16:27:25 2021 +0000
Parent:
1:c994530bdb3d
Commit message:
Milos Novakovic 2020/0425

Changed in this revision

Adafruit_GFX.lib 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_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/Adafruit_GFX.lib	Sat Dec 11 16:27:25 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mlos55/code/Adafruit_GFX/#29b3b8d8040d
--- a/main.cpp	Sat Nov 27 20:33:49 2021 +0000
+++ b/main.cpp	Sat Dec 11 16:27:25 2021 +0000
@@ -1,20 +1,27 @@
-/*
- * 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.
- *
- */
+//Milos Novakovic 2020/0425
 
+
+#include <string.h>
 #include "mbed.h"
 #include "mb_pins.h"
 #include "platform/mbed_thread.h"
 #include "MQTTClientMbedOs.h"
 
+#include "Adafruit_GFX.h"
+#include "Adafruit_GFX_Config.h"
+#include "Adafruit_SSD1306.h"
+
+//I2C za OLED
+#define SCL PB_13
+#define SDA PB_14
+
+//OLED DIMENSIONS
+#define OLED_HEIGHT_PX 64
+#define OLED_WIDTH_PX 128
+
+//I2C address
+#define I2C_ADDRESS         0x3c
+#define I2C_ADD_MBED    I2C_ADDRESS << 1
 
 // LED2 blinking rate:
 #define BLINKING_RATE_MS                                                     250
@@ -27,6 +34,9 @@
 // Small delay for network information printing:
 #define PRINTF_DELAY_MS                                                       10
 
+//Oledove promjenljive
+I2C i2c_obj(SDA, SCL);
+Adafruit_SSD1306_I2c myOLED(i2c_obj, PB_5, I2C_ADD_MBED, OLED_HEIGHT_PX, OLED_WIDTH_PX);
 
 // Left potentiometer:
 AnalogIn pot1(MB_POT1);
@@ -43,8 +53,13 @@
 // Message handler:
 MQTT::Message message;
 
-char* topic = "mbed-sample-pub";
-char* topic_sub = "mbed-sample-sub";
+int start, stop;
+char* startc = "start";
+char* stopc = "stop";
+
+char* topic = "pubpim";
+char* topic_sub = "subpim";
+
 // Counter of arrived messages:
 int arrivedcount = 0;
 // Flag indicating that button is not pressed:
@@ -111,8 +126,6 @@
     }
     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;
 }
@@ -120,9 +133,36 @@
 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;
+    
+    if(strcmp((char*)message.payload, startc) == 0) start = 1;
+    if(strcmp((char*)message.payload, stopc) == 0)  start = 0;
+    
+    if(start == 1){
+            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);
+            
+            //Ispis odlaznih poruka na OLED
+            myOLED.clearDisplay();
+            myOLED.setTextCursor(0, 0);
+            myOLED.printf("Odlazna poruka:\r\n V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER);
+            myOLED.display();
+            }
+    
+
+    //Ispis dolaznih poruka na OLED
+    myOLED.clearDisplay();
+    myOLED.setTextCursor(0, 0);
+    myOLED.printf("Dolazna poruka:\r\n %.*s \r", message.payloadlen, (char*)message.payload);
+    myOLED.display();
+    
 }
 
 void buttonFunction() {
@@ -133,6 +173,12 @@
 
 int main()
 {
+    
+    myOLED.begin(); //startovanje OLEDA
+    myOLED.clearDisplay();
+    myOLED.setTextCursor(0, 0);
+
+
     // Set the interrupt event:
     sw1.fall(&buttonFunction); 
     
@@ -174,9 +220,9 @@
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
     // Change only ID and protocol version:
     data.MQTTVersion = 3;
-    data.clientID.cstring = "NUCLEO-L476RG-60";
+    data.clientID.cstring = " pim-31";
     
-    // Connect the 
+     
     int rc = 0;
     if ((rc = client.connect(data)) != 0)
         printf("rc from MQTT connect is %d\r\n", rc);
@@ -188,9 +234,10 @@
         // 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
+        
+        if (button_pressed == 1) {
+            button_pressed = 0;      
+            
             char buf[100];
             sprintf(buf, "V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER);
             message.qos = MQTT::QOS0;
@@ -199,8 +246,15 @@
             message.payload = (void*)buf;
             message.payloadlen = strlen(buf)+1;
             client.publish(topic, message);
+            
+            //Ispis odlaznih poruka na OLED
+            myOLED.clearDisplay();
+            myOLED.setTextCursor(0, 0);
+            myOLED.printf("Odlazna poruka:\r\n V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER);
+            myOLED.display();
         }
-        // Need to call yield API to maintain connection:
+        
+        
         client.yield(YIELD_TIMEOUT_MS);
     }
 }
\ No newline at end of file
--- a/mbed_app.json	Sat Nov 27 20:33:49 2021 +0000
+++ b/mbed_app.json	Sat Dec 11 16:27:25 2021 +0000
@@ -2,11 +2,11 @@
     "config": {
         "wifi-ssid": {
             "help": "WIFI SSID",
-            "value": "\"testing1\""
+            "value": "\"cvele97\""
         },
         "wifi-password": {
             "help": "Wifi Password",
-            "value": "\"testing1\""
+            "value": "\"ekonomskitigar2021\""
         }
     },
     "target_overrides": {