![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mihajlo Djordjevic 2020/438
Dependencies: Adafruit_GFX 19E042PIM_MB_PINS
main.cpp@2:c1c5cb1af5b0, 2021-12-11 (annotated)
- Committer:
- mixidj
- Date:
- Sat Dec 11 16:27:19 2021 +0000
- Revision:
- 2:c1c5cb1af5b0
- Parent:
- 1:a1bf28d2dbdb
Mihajlo Djordjevic
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 | |
mixidj | 1:a1bf28d2dbdb | 29 | char* topic_sub = "subpim05"; |
mixidj | 1:a1bf28d2dbdb | 30 | char* topic_pub = "pubpim05"; |
makotolagano | 0:3e4e1cfbfd0d | 31 | const char* hostname = "broker.hivemq.com"; |
makotolagano | 0:3e4e1cfbfd0d | 32 | int port = 1883; |
makotolagano | 0:3e4e1cfbfd0d | 33 | |
makotolagano | 0:3e4e1cfbfd0d | 34 | void messageArrived(MQTT::MessageData& md) |
makotolagano | 0:3e4e1cfbfd0d | 35 | { |
makotolagano | 0:3e4e1cfbfd0d | 36 | MQTT::Message &message = md.message; |
makotolagano | 0:3e4e1cfbfd0d | 37 | printf("Browser message: %.*s \n", message.payloadlen, (char*)message.payload); |
makotolagano | 0:3e4e1cfbfd0d | 38 | |
makotolagano | 0:3e4e1cfbfd0d | 39 | myOLED.clearDisplay(); |
makotolagano | 0:3e4e1cfbfd0d | 40 | myOLED.setTextCursor(0, 0); |
makotolagano | 0:3e4e1cfbfd0d | 41 | myOLED.printf("%.*s \r", message.payloadlen, (char*)message.payload); |
makotolagano | 0:3e4e1cfbfd0d | 42 | myOLED.display(); |
makotolagano | 0:3e4e1cfbfd0d | 43 | } |
makotolagano | 0:3e4e1cfbfd0d | 44 | |
makotolagano | 0:3e4e1cfbfd0d | 45 | int main() |
makotolagano | 0:3e4e1cfbfd0d | 46 | { |
makotolagano | 0:3e4e1cfbfd0d | 47 | wifi = WiFiInterface::get_default_instance(); |
makotolagano | 0:3e4e1cfbfd0d | 48 | |
makotolagano | 0:3e4e1cfbfd0d | 49 | printf("Connecting to %s... \n",MBED_CONF_APP_WIFI_SSID); |
makotolagano | 0:3e4e1cfbfd0d | 50 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
makotolagano | 0:3e4e1cfbfd0d | 51 | if(ret != 0){ |
makotolagano | 0:3e4e1cfbfd0d | 52 | printf("Greska u povezivanju"); |
makotolagano | 0:3e4e1cfbfd0d | 53 | return -1; |
makotolagano | 0:3e4e1cfbfd0d | 54 | } |
makotolagano | 0:3e4e1cfbfd0d | 55 | printf("Success!\n"); |
makotolagano | 0:3e4e1cfbfd0d | 56 | |
makotolagano | 0:3e4e1cfbfd0d | 57 | socket.open(wifi); |
makotolagano | 0:3e4e1cfbfd0d | 58 | socket.connect(hostname,port); |
makotolagano | 0:3e4e1cfbfd0d | 59 | |
makotolagano | 0:3e4e1cfbfd0d | 60 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
makotolagano | 0:3e4e1cfbfd0d | 61 | data.MQTTVersion = 3; |
mixidj | 1:a1bf28d2dbdb | 62 | data.clientID.cstring = "miksi"; |
makotolagano | 0:3e4e1cfbfd0d | 63 | |
makotolagano | 0:3e4e1cfbfd0d | 64 | client.connect(data); |
makotolagano | 0:3e4e1cfbfd0d | 65 | client.subscribe(topic_sub, MQTT::QOS0, messageArrived); |
makotolagano | 0:3e4e1cfbfd0d | 66 | |
mixidj | 1:a1bf28d2dbdb | 67 | |
makotolagano | 0:3e4e1cfbfd0d | 68 | myOLED.begin(); |
makotolagano | 0:3e4e1cfbfd0d | 69 | i2c_obj.frequency(400000); |
makotolagano | 0:3e4e1cfbfd0d | 70 | |
makotolagano | 0:3e4e1cfbfd0d | 71 | myOLED.clearDisplay(); |
makotolagano | 0:3e4e1cfbfd0d | 72 | myOLED.display(); |
makotolagano | 0:3e4e1cfbfd0d | 73 | |
makotolagano | 0:3e4e1cfbfd0d | 74 | |
makotolagano | 0:3e4e1cfbfd0d | 75 | while(1) { |
makotolagano | 0:3e4e1cfbfd0d | 76 | thread_sleep_for(WAIT_MS); |
mixidj | 2:c1c5cb1af5b0 | 77 | /* if ((char*)message.payload == "start"){ |
mixidj | 2:c1c5cb1af5b0 | 78 | char buf[100]; |
mixidj | 2:c1c5cb1af5b0 | 79 | sprintf(buf,"V{POT1} = %1.2f \r\n",pot1 * (3.3)); |
mixidj | 2:c1c5cb1af5b0 | 80 | message.qos = MQTT::QOS0; |
mixidj | 2:c1c5cb1af5b0 | 81 | message.retained = false; |
mixidj | 2:c1c5cb1af5b0 | 82 | message.dup = false; |
mixidj | 2:c1c5cb1af5b0 | 83 | message.payload = (void*)buf; |
mixidj | 2:c1c5cb1af5b0 | 84 | message.payloadlen=strlen(buf+1); |
mixidj | 2:c1c5cb1af5b0 | 85 | client.publish(topic_pub, message); |
mixidj | 2:c1c5cb1af5b0 | 86 | } nesto sam se pogubio ovde :(*/ |
mixidj | 1:a1bf28d2dbdb | 87 | client.yield(5000); |
makotolagano | 0:3e4e1cfbfd0d | 88 | } |
makotolagano | 0:3e4e1cfbfd0d | 89 | } |