Petar Djuric 2018/0002
Dependencies: mbed Adafruit_GFX 19E042PIM_MB_PINS
Revision 0:a2c1c8bc2b0f, committed 2021-12-11
- Comitter:
- petardjuric180002
- Date:
- Sat Dec 11 16:28:28 2021 +0000
- Commit message:
- Petar Djuric 2018/0002
Changed in this revision
diff -r 000000000000 -r a2c1c8bc2b0f 19E042PIM_MB_PINS.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/19E042PIM_MB_PINS.lib Sat Dec 11 16:28:28 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/tzwell/code/19E042PIM_MB_PINS/#5722459a201f
diff -r 000000000000 -r a2c1c8bc2b0f Adafruit_GFX.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Adafruit_GFX.lib Sat Dec 11 16:28:28 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/nkhorman/code/Adafruit_GFX/#7fb1d4d3525d
diff -r 000000000000 -r a2c1c8bc2b0f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Dec 11 16:28:28 2021 +0000 @@ -0,0 +1,133 @@ +#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" +#include <cstring> + +#define SMALL_WAIT_MS 10 + +#define BLINK_PERIOD_MS 250 + +//OLED DIMENSIONS +#define OLED_HEIGHT_PX 64 +#define OLED_WIDTH_PX 128 + +//I2C adress +#define I2C_ADDRESS 0x3c +#define I2C_ADD_MBED I2C_ADDRESS << 1 + +AnalogIn POT1(MB_POT1); +DigitalOut led2(MB_LED2); + +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_pub = "pubpim"; +char* topic_sub = "subpim"; +const char* hostname = "broker.hivemq.com"; +int port = 1883; + + + +void messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + printf("Browser message: %.*s \r\n", message.payloadlen, (char*)message.payload); + + myOLED.clearDisplay(); + myOLED.printf("%.*s \r", message.payloadlen, (char*)message.payload); + myOLED.display(); +} + + + +int main() +{ + wifi = WiFiInterface::get_default_instance(); + if(!wifi) + { + printf("ERROR: Nema WiFija"); + return -1; + } + + printf("\n 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 \n"); + return -1; + } + + printf("Success\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()); + + socket.open(wifi); + socket.connect(hostname, port); + + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "YUSUF"; + + 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) //ovako se on subscribuje na temu + printf("rc from MQTT subscribe is %d \r\n", rc); + + + char dr[4] = {'o','l','e','d'}; + char strt[5] = {'s','t','a','r','t'}; + char stp[4] = {'s','t','o','p'}; + while(true) + { + led2 =! led2; + thread_sleep_for(BLINK_PERIOD_MS); + char buf[100]; + sprintf(buf, "rec je"); + + + /* if (std::strcmp(buf, strt) == 0 && m==1) { + sprintf(buf, "V(POT1) = %1.2f \r\n", POT1 * (3.3f)); + + + if (std::strcmp(buf, stp) == 0) { + sprintf(buf, "rec je"); + + } } + */ + + if (std::strcmp(buf, dr) == 0) { + //ako jes + myOLED.begin(); //pocetak + i2c_obj.frequency(400000); //veca frekvencija slanja da ne bi ghostovalo + + myOLED.clearDisplay(); + myOLED.display(); + + } + + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub, message); + + client.yield(1000); + + } +} \ No newline at end of file
diff -r 000000000000 -r a2c1c8bc2b0f mbed-mqtt.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-mqtt.lib Sat Dec 11 16:28:28 2021 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-mqtt/#7fa219e87b3355e8a6fd281659697fe3a0c87630
diff -r 000000000000 -r a2c1c8bc2b0f mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Sat Dec 11 16:28:28 2021 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#e4b81f67f939a0c0b11c147ce74aa367271e1279
diff -r 000000000000 -r a2c1c8bc2b0f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Dec 11 16:28:28 2021 +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 a2c1c8bc2b0f mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Sat Dec 11 16:28:28 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