![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mihajlo Djordjevic 2020/438
Dependencies: Adafruit_GFX 19E042PIM_MB_PINS
main.cpp@0:3e4e1cfbfd0d, 2021-12-11 (annotated)
- Committer:
- makotolagano
- Date:
- Sat Dec 11 10:15:42 2021 +0000
- Revision:
- 0:3e4e1cfbfd0d
- Child:
- 1:a1bf28d2dbdb
Samo jak gas ljubi brat ustima
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
makotolagano | 0:3e4e1cfbfd0d | 1 | //MQTT & mbed libraries |
makotolagano | 0:3e4e1cfbfd0d | 2 | #include "mb_pins.h" |
makotolagano | 0:3e4e1cfbfd0d | 3 | #include "mbed.h" |
makotolagano | 0:3e4e1cfbfd0d | 4 | #include "platform/mbed_thread.h" |
makotolagano | 0:3e4e1cfbfd0d | 5 | #include "MQTTClientMbedOs.h" |
makotolagano | 0:3e4e1cfbfd0d | 6 | //Oled libraries |
makotolagano | 0:3e4e1cfbfd0d | 7 | #include "Adafruit_GFX.h" |
makotolagano | 0:3e4e1cfbfd0d | 8 | #include "Adafruit_GFX_Config.h" |
makotolagano | 0:3e4e1cfbfd0d | 9 | #include "Adafruit_SSD1306.h" |
makotolagano | 0:3e4e1cfbfd0d | 10 | |
makotolagano | 0:3e4e1cfbfd0d | 11 | //Macros |
makotolagano | 0:3e4e1cfbfd0d | 12 | #define WAIT_MS 10 |
makotolagano | 0:3e4e1cfbfd0d | 13 | |
makotolagano | 0:3e4e1cfbfd0d | 14 | //I2C |
makotolagano | 0:3e4e1cfbfd0d | 15 | #define I2C_ADDRESS 0x3c |
makotolagano | 0:3e4e1cfbfd0d | 16 | #define I2C_ADD_MBED I2C_ADDRESS << 1 |
makotolagano | 0:3e4e1cfbfd0d | 17 | #define OLED_HEIGHT_PX 64 |
makotolagano | 0:3e4e1cfbfd0d | 18 | #define OLED_WIDTH_PX 128 |
makotolagano | 0:3e4e1cfbfd0d | 19 | |
makotolagano | 0:3e4e1cfbfd0d | 20 | |
makotolagano | 0:3e4e1cfbfd0d | 21 | TCPSocket socket; |
makotolagano | 0:3e4e1cfbfd0d | 22 | MQTTClient client(&socket); |
makotolagano | 0:3e4e1cfbfd0d | 23 | MQTT::Message message; |
makotolagano | 0:3e4e1cfbfd0d | 24 | WiFiInterface *wifi; |
makotolagano | 0:3e4e1cfbfd0d | 25 | |
makotolagano | 0:3e4e1cfbfd0d | 26 | I2C i2c_obj(MB_OLED_SDA, MB_OLED_SCL); |
makotolagano | 0:3e4e1cfbfd0d | 27 | Adafruit_SSD1306_I2c myOLED(i2c_obj, PB_5, I2C_ADD_MBED, OLED_HEIGHT_PX, OLED_WIDTH_PX); |
makotolagano | 0:3e4e1cfbfd0d | 28 | |
makotolagano | 0:3e4e1cfbfd0d | 29 | char* topic_sub = "samo-jak-gas"; |
makotolagano | 0:3e4e1cfbfd0d | 30 | const char* hostname = "broker.hivemq.com"; |
makotolagano | 0:3e4e1cfbfd0d | 31 | int port = 1883; |
makotolagano | 0:3e4e1cfbfd0d | 32 | |
makotolagano | 0:3e4e1cfbfd0d | 33 | void messageArrived(MQTT::MessageData& md) |
makotolagano | 0:3e4e1cfbfd0d | 34 | { |
makotolagano | 0:3e4e1cfbfd0d | 35 | MQTT::Message &message = md.message; |
makotolagano | 0:3e4e1cfbfd0d | 36 | printf("Browser message: %.*s \n", message.payloadlen, (char*)message.payload); |
makotolagano | 0:3e4e1cfbfd0d | 37 | |
makotolagano | 0:3e4e1cfbfd0d | 38 | myOLED.clearDisplay(); |
makotolagano | 0:3e4e1cfbfd0d | 39 | myOLED.setTextCursor(0, 0); |
makotolagano | 0:3e4e1cfbfd0d | 40 | myOLED.printf("%.*s \r", message.payloadlen, (char*)message.payload); |
makotolagano | 0:3e4e1cfbfd0d | 41 | myOLED.display(); |
makotolagano | 0:3e4e1cfbfd0d | 42 | } |
makotolagano | 0:3e4e1cfbfd0d | 43 | |
makotolagano | 0:3e4e1cfbfd0d | 44 | int main() |
makotolagano | 0:3e4e1cfbfd0d | 45 | { |
makotolagano | 0:3e4e1cfbfd0d | 46 | wifi = WiFiInterface::get_default_instance(); |
makotolagano | 0:3e4e1cfbfd0d | 47 | |
makotolagano | 0:3e4e1cfbfd0d | 48 | printf("Connecting to %s... \n",MBED_CONF_APP_WIFI_SSID); |
makotolagano | 0:3e4e1cfbfd0d | 49 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
makotolagano | 0:3e4e1cfbfd0d | 50 | if(ret != 0){ |
makotolagano | 0:3e4e1cfbfd0d | 51 | printf("Greska u povezivanju"); |
makotolagano | 0:3e4e1cfbfd0d | 52 | return -1; |
makotolagano | 0:3e4e1cfbfd0d | 53 | } |
makotolagano | 0:3e4e1cfbfd0d | 54 | printf("Success!\n"); |
makotolagano | 0:3e4e1cfbfd0d | 55 | |
makotolagano | 0:3e4e1cfbfd0d | 56 | socket.open(wifi); |
makotolagano | 0:3e4e1cfbfd0d | 57 | socket.connect(hostname,port); |
makotolagano | 0:3e4e1cfbfd0d | 58 | |
makotolagano | 0:3e4e1cfbfd0d | 59 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
makotolagano | 0:3e4e1cfbfd0d | 60 | data.MQTTVersion = 3; |
makotolagano | 0:3e4e1cfbfd0d | 61 | data.clientID.cstring = "Zolica"; |
makotolagano | 0:3e4e1cfbfd0d | 62 | |
makotolagano | 0:3e4e1cfbfd0d | 63 | client.connect(data); |
makotolagano | 0:3e4e1cfbfd0d | 64 | client.subscribe(topic_sub, MQTT::QOS0, messageArrived); |
makotolagano | 0:3e4e1cfbfd0d | 65 | |
makotolagano | 0:3e4e1cfbfd0d | 66 | myOLED.begin(); |
makotolagano | 0:3e4e1cfbfd0d | 67 | i2c_obj.frequency(400000); |
makotolagano | 0:3e4e1cfbfd0d | 68 | |
makotolagano | 0:3e4e1cfbfd0d | 69 | myOLED.clearDisplay(); |
makotolagano | 0:3e4e1cfbfd0d | 70 | myOLED.display(); |
makotolagano | 0:3e4e1cfbfd0d | 71 | |
makotolagano | 0:3e4e1cfbfd0d | 72 | |
makotolagano | 0:3e4e1cfbfd0d | 73 | while(1) { |
makotolagano | 0:3e4e1cfbfd0d | 74 | thread_sleep_for(WAIT_MS); |
makotolagano | 0:3e4e1cfbfd0d | 75 | client.yield(1000); |
makotolagano | 0:3e4e1cfbfd0d | 76 | } |
makotolagano | 0:3e4e1cfbfd0d | 77 | } |