Andrija Mijailovic 2020/0388

Dependencies:   19E042PIM_MB_PINS

Files at this revision

API Documentation at this revision

Comitter:
ma200388
Date:
Sat Dec 11 16:22:16 2021 +0000
Parent:
3:bd632f30b46b
Commit message:
Andrija Mijailovic 2020/0388

Changed in this revision

19E042PIM_MB_PINS.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-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.bld Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r bd632f30b46b -r 3289b25dc1db 19E042PIM_MB_PINS.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/19E042PIM_MB_PINS.lib	Sat Dec 11 16:22:16 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/tzwell/code/19E042PIM_MB_PINS/#5722459a201f
diff -r bd632f30b46b -r 3289b25dc1db main.cpp
--- a/main.cpp	Sat Nov 13 09:17:44 2021 +0000
+++ b/main.cpp	Sat Dec 11 16:22:16 2021 +0000
@@ -1,174 +1,235 @@
 /*
  * Biblioteke za uvoz:
  */
+ 
+//#include <Adafruit_GFX.h>
+//#include <Adafruit_SSD1306.h>
+//#include <Adafruit_GFX_Config.h>
+#include "string.h"
 #include "mbed.h"
+#include "mb_pins.h"
+#include "platform/mbed_thread.h"
+#include "MQTTClientMbedOs.h"
 
-/*
- * Definisanje makroa:
- */
- 
-#define PERIOD 100
-#define DUTY_CYCLE 0.75f
-#define ZERO 64
-#define ONE 121
-#define TWO 36
-#define THREE 48
-#define FOUR 25
-#define FIVE 18
-#define SIX 2
-#define SEVEN 120
-#define EIGHT 0
-#define NINE 16
-/*
- * Globalne promenljive:
- */
- 
-PwmOut buzz(PA_11);
-AnalogIn pot1(PA_0);
-AnalogIn pot2(PA_1);
-InterruptIn sw1(PC_9);
-DigitalOut sel1(PB_6);
-DigitalOut sel2(PC_7);
-BusOut display(PA_10,PA_9,PA_8,PB_10,PB_5,PB_4,PB_3);
-static int update=0;
-static float br=0;
-/*
- * Deklaracija funkcija:
- */
+// 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                                                    9750
+// Maximum number of networks to scan for:
+#define MAX_NETWORKS                                                          15
+// Small delay for network information printing:
+#define PRINTF_DELAY_MS                                                       10
 
-void ISR_sw1(void);
-void screen(int percent);
-/*
- * Glavna funkcija:
- */
-int main()
-{
-    sw1.fall(&ISR_sw1);
-    while(true)
-    {
-        
-        buzz.period(pot2.read()/200+0.00005);
-        buzz.write(pot1.read());
-        screen((int)br);
-        wait_ms(4);
-    }    
-}
-
+#define I2C_ADD 0x3c
+#define I2C_ADD_MBED I2C_ADD<<1
+#define OLED_HEIGHT_PX 64
+#define OLED_WIDTH_PX 128
+#define REFRESH_RATE_MS 5
+#define OFFSET 0.5f
+#define I2C_FREQ 400000
+#define SCL PB_13
+#define SDA PB_14
 
 /*
- * Definicija funkcija:
+ *Globalne promenjive:
  */
- void screen(int percent){
-     
-     switch(percent-percent/10*10){
-            case 0:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(ZERO);
-                break;
-            case 1:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(ONE);
-                break;
-            case 2:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(TWO);
-                break;
-            case 3:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(THREE);
-                break;
-            case 4:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(FOUR);
-                break;
-            case 5:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(FIVE);
-                break;
-            case 6:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(SIX);
-                break;
-            case 7:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(SEVEN);
-                break;
-            case 8:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(EIGHT);
-                break;
-            case 9:
-                sel1.write(1);
-                sel2.write(0);
-                display.write(NINE);
-                break;
-     }
-     wait_ms(8);
-    switch(percent/10){
-            case 0:
-                break;
-            case 1:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(ONE);
-                break;
-            case 2:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(TWO);
-                break;
-            case 3:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(THREE);
-                break;
-            case 4:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(FOUR);
-                break;
-            case 5:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(FIVE);
-                break;
-            case 6:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(SIX);
-                break;
-            case 7:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(SEVEN);
-                break;
-            case 8:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(EIGHT);
-                break;
-            case 9:
-                sel1.write(0);
-                sel2.write(1);
-                display.write(NINE);
-                break;
-     }
-     wait_ms(4);
-     
-     
+ 
+
+// Left potentiometer:
+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:
+TCPSocket socket;
+// Creating MQTT client using the TCP socket;
+MQTTClient client(&socket);
+// Message handler:
+MQTT::Message message;
+
+
+//I2C i2c_obj(SDA, SCL);
+//Adafruit_SSD1306_I2c myOLED(i2c_obj,PB_5,I2C_ADD_MBED,OLED_HEIGHT_PX,OLED_WIDTH_PX);
+char* topic = "pubpim";
+char* topic_sub = "subpim";
+int arrivedcount = 0;
+int button_pressed=0;
+const char* hostname = "broker.hivemq.com";
+int port = 1883;
+char* message_input;
+int oled_print=0;
+int voltage_print=0;
+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 ISR_sw1(){
-     br=pot1.read();
-     }
+
+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;
+    //pretvaranje poruke u string
+    message_input=(char*)message.payload;
+    message_input[message.payloadlen]='\0';
+    //proveravanje poruke
+    if(!strcmp(message_input,"start"))voltage_print=1;
+    if(!strcmp(message_input,"stop"))voltage_print=0;
+    if(!strcmp(message_input,"oled"))oled_print=1;
+}
+
+void buttonFunction() {
+    
+    button_pressed=1;
+   
+}
+
+int main()
+{
+    //myOLED.begin();
+    // 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 = "pim-35";
+    
+    // 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 (voltage_print) {      
+            // 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);
+        }
+        /*if(oled_print)
+        {
+            display.setTextSize(2);
+            display.setTextColor(WHITE);
+            display.setCursor(0, 0);
+            display.println("Welcome");
+            myOLED.display;
+            }
+        */
+        // Need to call yield API to maintain connection:
+        client.yield(YIELD_TIMEOUT_MS);
+    }
+}
+ 
\ No newline at end of file
diff -r bd632f30b46b -r 3289b25dc1db mbed-mqtt.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-mqtt.lib	Sat Dec 11 16:22:16 2021 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-mqtt/#7fa219e87b3355e8a6fd281659697fe3a0c87630
diff -r bd632f30b46b -r 3289b25dc1db mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Sat Dec 11 16:22:16 2021 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e4b81f67f939a0c0b11c147ce74aa367271e1279
diff -r bd632f30b46b -r 3289b25dc1db mbed.bld
--- a/mbed.bld	Sat Nov 13 09:17:44 2021 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
diff -r bd632f30b46b -r 3289b25dc1db mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Sat Dec 11 16:22:16 2021 +0000
@@ -0,0 +1,25 @@
+{
+    "config": {
+        "wifi-ssid": {
+            "help": "WIFI SSID",
+            "value": "\"cvele97\""
+        },
+        "wifi-password": {
+            "help": "Wifi Password",
+            "value": "\"ekonomskitigar2021\""
+        }
+    },
+    "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