2020/0214 Isidora Milivojevic
Dependencies: Adafruit_GFX 19E042PIM_MB_PINS
main.cpp@0:f74d93dfa17f, 2021-12-11 (annotated)
- Committer:
- soconstruct
- Date:
- Sat Dec 11 16:25:57 2021 +0000
- Revision:
- 0:f74d93dfa17f
2020/0214 Isidora Milivojevic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soconstruct | 0:f74d93dfa17f | 1 | //MQTT i mbed biblioteke |
soconstruct | 0:f74d93dfa17f | 2 | #include "mb_pins.h" |
soconstruct | 0:f74d93dfa17f | 3 | #include "mbed.h" |
soconstruct | 0:f74d93dfa17f | 4 | #include "platform/mbed_thread.h" |
soconstruct | 0:f74d93dfa17f | 5 | #include "MQTTClientMbedOs.h" |
soconstruct | 0:f74d93dfa17f | 6 | //Biblioteke za OLED |
soconstruct | 0:f74d93dfa17f | 7 | #include "Adafruit_GFX.h" |
soconstruct | 0:f74d93dfa17f | 8 | #include "Adafruit_GFX_Config.h" |
soconstruct | 0:f74d93dfa17f | 9 | #include "Adafruit_SSD1306.h" |
soconstruct | 0:f74d93dfa17f | 10 | //Opste biblioteke |
soconstruct | 0:f74d93dfa17f | 11 | #include <string.h> |
soconstruct | 0:f74d93dfa17f | 12 | //I2C makroi |
soconstruct | 0:f74d93dfa17f | 13 | #define I2C_ADDRESS 0x3c |
soconstruct | 0:f74d93dfa17f | 14 | #define I2C_ADD_MBED I2C_ADDRESS << 1 |
soconstruct | 0:f74d93dfa17f | 15 | #define OLED_HEIGHT_PX 64 |
soconstruct | 0:f74d93dfa17f | 16 | #define OLED_WIDTH_PX 128 |
soconstruct | 0:f74d93dfa17f | 17 | |
soconstruct | 0:f74d93dfa17f | 18 | #define WAIT_MS 10 |
soconstruct | 0:f74d93dfa17f | 19 | |
soconstruct | 0:f74d93dfa17f | 20 | TCPSocket socket; |
soconstruct | 0:f74d93dfa17f | 21 | MQTTClient client(&socket); |
soconstruct | 0:f74d93dfa17f | 22 | MQTT::Message message; |
soconstruct | 0:f74d93dfa17f | 23 | WiFiInterface *wifi; |
soconstruct | 0:f74d93dfa17f | 24 | |
soconstruct | 0:f74d93dfa17f | 25 | I2C i2c_obj(MB_OLED_SDA, MB_OLED_SCL); |
soconstruct | 0:f74d93dfa17f | 26 | Adafruit_SSD1306_I2c oled(i2c_obj, PB_5, I2C_ADD_MBED, OLED_HEIGHT_PX, OLED_WIDTH_PX); |
soconstruct | 0:f74d93dfa17f | 27 | AnalogIn pot1(MB_POT1); |
soconstruct | 0:f74d93dfa17f | 28 | |
soconstruct | 0:f74d93dfa17f | 29 | char* topic_sub = "pubpim"; |
soconstruct | 0:f74d93dfa17f | 30 | char* topic_pub = "subpim"; |
soconstruct | 0:f74d93dfa17f | 31 | const char* hostname = "broker.hivemq.com"; |
soconstruct | 0:f74d93dfa17f | 32 | int port = 1883; |
soconstruct | 0:f74d93dfa17f | 33 | |
soconstruct | 0:f74d93dfa17f | 34 | void messageArrived(MQTT::MessageData& md) |
soconstruct | 0:f74d93dfa17f | 35 | { |
soconstruct | 0:f74d93dfa17f | 36 | MQTT::Message &message = md.message; |
soconstruct | 0:f74d93dfa17f | 37 | printf("Browser message: %.*s \n", message.payloadlen, (char*)message.payload); |
soconstruct | 0:f74d93dfa17f | 38 | |
soconstruct | 0:f74d93dfa17f | 39 | oled.clearDisplay(); |
soconstruct | 0:f74d93dfa17f | 40 | oled.setTextCursor(0, 0); |
soconstruct | 0:f74d93dfa17f | 41 | oled.printf("%.*s \r", message.payloadlen, (char*)message.payload); |
soconstruct | 0:f74d93dfa17f | 42 | oled.display(); |
soconstruct | 0:f74d93dfa17f | 43 | oled.clearDisplay(); |
soconstruct | 0:f74d93dfa17f | 44 | oled.display(); |
soconstruct | 0:f74d93dfa17f | 45 | } |
soconstruct | 0:f74d93dfa17f | 46 | |
soconstruct | 0:f74d93dfa17f | 47 | void pot1Mess(MQTT::MessageData& md){ |
soconstruct | 0:f74d93dfa17f | 48 | char buf(100); |
soconstruct | 0:f74d93dfa17f | 49 | printf(buf, "V(POT1) = %1.2f \r\n", pot1*(3.3f)); |
soconstruct | 0:f74d93dfa17f | 50 | message.qos = MQTT::QOS0; |
soconstruct | 0:f74d93dfa17f | 51 | message.retained = false; |
soconstruct | 0:f74d93dfa17f | 52 | message.dup = false; |
soconstruct | 0:f74d93dfa17f | 53 | message.payload = (void *)buf; |
soconstruct | 0:f74d93dfa17f | 54 | message.payloadlen = strlen(buf) + 1; |
soconstruct | 0:f74d93dfa17f | 55 | client.publish(topic_pub, message); |
soconstruct | 0:f74d93dfa17f | 56 | } |
soconstruct | 0:f74d93dfa17f | 57 | |
soconstruct | 0:f74d93dfa17f | 58 | void oledPrint(MQTT::MessageData& md){ |
soconstruct | 0:f74d93dfa17f | 59 | oled.clearDisplay(); |
soconstruct | 0:f74d93dfa17f | 60 | oled.setTextCursor(0, 0); |
soconstruct | 0:f74d93dfa17f | 61 | oled.printf("%.*s \r", message.payloadlen, (char*)message.payload); |
soconstruct | 0:f74d93dfa17f | 62 | oled.display(); |
soconstruct | 0:f74d93dfa17f | 63 | } |
soconstruct | 0:f74d93dfa17f | 64 | |
soconstruct | 0:f74d93dfa17f | 65 | int main(){ |
soconstruct | 0:f74d93dfa17f | 66 | wifi = WiFiInterface::get_default_instance(); |
soconstruct | 0:f74d93dfa17f | 67 | |
soconstruct | 0:f74d93dfa17f | 68 | printf("Connecting to %s... \n",MBED_CONF_APP_WIFI_SSID); |
soconstruct | 0:f74d93dfa17f | 69 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
soconstruct | 0:f74d93dfa17f | 70 | if(ret != 0){ |
soconstruct | 0:f74d93dfa17f | 71 | printf("Greska u povezivanju"); |
soconstruct | 0:f74d93dfa17f | 72 | return -1; |
soconstruct | 0:f74d93dfa17f | 73 | } |
soconstruct | 0:f74d93dfa17f | 74 | printf("Success!\n"); |
soconstruct | 0:f74d93dfa17f | 75 | |
soconstruct | 0:f74d93dfa17f | 76 | socket.open(wifi); |
soconstruct | 0:f74d93dfa17f | 77 | socket.connect(hostname,port); |
soconstruct | 0:f74d93dfa17f | 78 | |
soconstruct | 0:f74d93dfa17f | 79 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
soconstruct | 0:f74d93dfa17f | 80 | data.MQTTVersion = 3; |
soconstruct | 0:f74d93dfa17f | 81 | data.clientID.cstring = "pim-12"; |
soconstruct | 0:f74d93dfa17f | 82 | |
soconstruct | 0:f74d93dfa17f | 83 | client.connect(data); |
soconstruct | 0:f74d93dfa17f | 84 | client.subscribe(topic_sub, MQTT::QOS0, messageArrived); |
soconstruct | 0:f74d93dfa17f | 85 | |
soconstruct | 0:f74d93dfa17f | 86 | while(true) { |
soconstruct | 0:f74d93dfa17f | 87 | thread_sleep_for(WAIT_MS); |
soconstruct | 0:f74d93dfa17f | 88 | client.yield(1000); |
soconstruct | 0:f74d93dfa17f | 89 | } |
soconstruct | 0:f74d93dfa17f | 90 | |
soconstruct | 0:f74d93dfa17f | 91 | char* startMess = "start"; |
soconstruct | 0:f74d93dfa17f | 92 | char* stopMess = "stop"; |
soconstruct | 0:f74d93dfa17f | 93 | char* oledMess = "oled"; |
soconstruct | 0:f74d93dfa17f | 94 | |
soconstruct | 0:f74d93dfa17f | 95 | while(!strcmp((char*)message.payload, startMess)){ |
soconstruct | 0:f74d93dfa17f | 96 | client.subscribe(topic_sub, MQTT::QOS0, pot1Mess()); |
soconstruct | 0:f74d93dfa17f | 97 | if(!strcmp((char*)message.payload, stopMess)){ |
soconstruct | 0:f74d93dfa17f | 98 | break; |
soconstruct | 0:f74d93dfa17f | 99 | } |
soconstruct | 0:f74d93dfa17f | 100 | } |
soconstruct | 0:f74d93dfa17f | 101 | |
soconstruct | 0:f74d93dfa17f | 102 | if(!strcmp((char*)message.payload, oledMess)){ |
soconstruct | 0:f74d93dfa17f | 103 | oled.begin(); |
soconstruct | 0:f74d93dfa17f | 104 | i2c_obj.frequency(400000); |
soconstruct | 0:f74d93dfa17f | 105 | client.subscribe(topic_sub, MQTT::QOS0, oledPrint); |
soconstruct | 0:f74d93dfa17f | 106 | } |
soconstruct | 0:f74d93dfa17f | 107 | |
soconstruct | 0:f74d93dfa17f | 108 | |
soconstruct | 0:f74d93dfa17f | 109 | |
soconstruct | 0:f74d93dfa17f | 110 | } |