vuk stijepovic 2020/0639

Dependencies:   Adafruit_GFX StrLib Util 19E042PIM_MB_PINS

Committer:
vukstijepovic
Date:
Sat Dec 11 16:25:57 2021 +0000
Revision:
0:0de9c1e5bad1
vuk stijepovic 2020/0639;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vukstijepovic 0:0de9c1e5bad1 1 #include "mb_pins.h"
vukstijepovic 0:0de9c1e5bad1 2 #include "mbed.h"
vukstijepovic 0:0de9c1e5bad1 3 #include "platform/mbed_thread.h"
vukstijepovic 0:0de9c1e5bad1 4 #include "MQTTClientMbedOs.h"
vukstijepovic 0:0de9c1e5bad1 5 #include "Adafruit_GFX.h"
vukstijepovic 0:0de9c1e5bad1 6 #include "Adafruit_GFX_Config.h"
vukstijepovic 0:0de9c1e5bad1 7 #include "Adafruit_SSD1306.h"
vukstijepovic 0:0de9c1e5bad1 8 #include<string>
vukstijepovic 0:0de9c1e5bad1 9 #include "stringUtils.h"
vukstijepovic 0:0de9c1e5bad1 10
vukstijepovic 0:0de9c1e5bad1 11 #define SMALL_WAIT_MS 10
vukstijepovic 0:0de9c1e5bad1 12 #define I2C_ADDRESS 0x3c
vukstijepovic 0:0de9c1e5bad1 13 #define I2C_ADD_MBED I2C_ADDRESS << 1
vukstijepovic 0:0de9c1e5bad1 14 #define OLED_HEIGHT_PX 64
vukstijepovic 0:0de9c1e5bad1 15 #define OLED_WIDTH_PX 128
vukstijepovic 0:0de9c1e5bad1 16 #define VOLTAGE_SCALER 3.3f
vukstijepovic 0:0de9c1e5bad1 17
vukstijepovic 0:0de9c1e5bad1 18 AnalogIn pot1(MB_POT1);
vukstijepovic 0:0de9c1e5bad1 19 TCPSocket socket;
vukstijepovic 0:0de9c1e5bad1 20 MQTTClient client(&socket);
vukstijepovic 0:0de9c1e5bad1 21 MQTT::Message message;
vukstijepovic 0:0de9c1e5bad1 22 WiFiInterface *wifi;
vukstijepovic 0:0de9c1e5bad1 23
vukstijepovic 0:0de9c1e5bad1 24 I2C i2c_obj(MB_OLED_SDA, MB_OLED_SCL);
vukstijepovic 0:0de9c1e5bad1 25 Adafruit_SSD1306_I2c myOLED(i2c_obj, PB_5, I2C_ADD_MBED, OLED_HEIGHT_PX, OLED_WIDTH_PX);
vukstijepovic 0:0de9c1e5bad1 26
vukstijepovic 0:0de9c1e5bad1 27 using namespace std;
vukstijepovic 0:0de9c1e5bad1 28 char* subpim = "mbed-sample-sub";
vukstijepovic 0:0de9c1e5bad1 29 char* pubpim = "mbed-sample-pub";
vukstijepovic 0:0de9c1e5bad1 30 const char* hostname = "broker.hivemq.com";
vukstijepovic 0:0de9c1e5bad1 31 int port = 1883;
vukstijepovic 0:0de9c1e5bad1 32
vukstijepovic 0:0de9c1e5bad1 33
vukstijepovic 0:0de9c1e5bad1 34
vukstijepovic 0:0de9c1e5bad1 35 void messageArrived(MQTT::MessageData& md)
vukstijepovic 0:0de9c1e5bad1 36 {
vukstijepovic 0:0de9c1e5bad1 37 char buf[100];
vukstijepovic 0:0de9c1e5bad1 38 sprintf(buf, "V(POT1) = %1.2f\r\n", pot1*VOLTAGE_SCALER);
vukstijepovic 0:0de9c1e5bad1 39 message.qos = MQTT::QOS0;
vukstijepovic 0:0de9c1e5bad1 40 message.retained = false;
vukstijepovic 0:0de9c1e5bad1 41 message.dup = false;
vukstijepovic 0:0de9c1e5bad1 42 message.payload = (void*)buf;
vukstijepovic 0:0de9c1e5bad1 43 message.payloadlen = strlen(buf)+1;
vukstijepovic 0:0de9c1e5bad1 44 client.publish(pubpim, message);
vukstijepovic 0:0de9c1e5bad1 45 MQTT::Message &message = md.message;
vukstijepovic 0:0de9c1e5bad1 46 printf("Browser message: %.*s \n", message.payloadlen, (char*)message.payload);
vukstijepovic 0:0de9c1e5bad1 47 myOLED.clearDisplay();
vukstijepovic 0:0de9c1e5bad1 48 myOLED.setTextCursor(0, 0);
vukstijepovic 0:0de9c1e5bad1 49 myOLED.printf("%.*s \r", message.payloadlen, (char*)message.payload);
vukstijepovic 0:0de9c1e5bad1 50 myOLED.display();
vukstijepovic 0:0de9c1e5bad1 51
vukstijepovic 0:0de9c1e5bad1 52 }
vukstijepovic 0:0de9c1e5bad1 53
vukstijepovic 0:0de9c1e5bad1 54
vukstijepovic 0:0de9c1e5bad1 55
vukstijepovic 0:0de9c1e5bad1 56 int main()
vukstijepovic 0:0de9c1e5bad1 57 {
vukstijepovic 0:0de9c1e5bad1 58 wifi = WiFiInterface::get_default_instance();
vukstijepovic 0:0de9c1e5bad1 59
vukstijepovic 0:0de9c1e5bad1 60 printf("Connecting to %s \n",MBED_CONF_APP_WIFI_SSID);
vukstijepovic 0:0de9c1e5bad1 61 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
vukstijepovic 0:0de9c1e5bad1 62 if(ret != 0){
vukstijepovic 0:0de9c1e5bad1 63 printf("Greska u povezivanju");
vukstijepovic 0:0de9c1e5bad1 64 return -1;
vukstijepovic 0:0de9c1e5bad1 65 }
vukstijepovic 0:0de9c1e5bad1 66 printf("Success\n");
vukstijepovic 0:0de9c1e5bad1 67
vukstijepovic 0:0de9c1e5bad1 68 socket.open(wifi);
vukstijepovic 0:0de9c1e5bad1 69 socket.connect(hostname,port);
vukstijepovic 0:0de9c1e5bad1 70
vukstijepovic 0:0de9c1e5bad1 71 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
vukstijepovic 0:0de9c1e5bad1 72 data.MQTTVersion = 3;
vukstijepovic 0:0de9c1e5bad1 73 data.clientID.cstring = "pim-11";
vukstijepovic 0:0de9c1e5bad1 74
vukstijepovic 0:0de9c1e5bad1 75 client.connect(data);
vukstijepovic 0:0de9c1e5bad1 76 client.subscribe(subpim, MQTT::QOS0, messageArrived);
vukstijepovic 0:0de9c1e5bad1 77
vukstijepovic 0:0de9c1e5bad1 78
vukstijepovic 0:0de9c1e5bad1 79
vukstijepovic 0:0de9c1e5bad1 80
vukstijepovic 0:0de9c1e5bad1 81
vukstijepovic 0:0de9c1e5bad1 82
vukstijepovic 0:0de9c1e5bad1 83 myOLED.begin();
vukstijepovic 0:0de9c1e5bad1 84 i2c_obj.frequency(400000);
vukstijepovic 0:0de9c1e5bad1 85
vukstijepovic 0:0de9c1e5bad1 86 myOLED.clearDisplay();
vukstijepovic 0:0de9c1e5bad1 87 myOLED.display();
vukstijepovic 0:0de9c1e5bad1 88
vukstijepovic 0:0de9c1e5bad1 89
vukstijepovic 0:0de9c1e5bad1 90 while(1) {
vukstijepovic 0:0de9c1e5bad1 91 thread_sleep_for(SMALL_WAIT_MS);
vukstijepovic 0:0de9c1e5bad1 92 client.yield(1000);
vukstijepovic 0:0de9c1e5bad1 93 }
vukstijepovic 0:0de9c1e5bad1 94 }