Luka Ristic 2020/0331

Dependencies:   Adafruit_GFX 19E042PIM_MB_PINS

Committer:
filip_ste
Date:
Sat Dec 11 15:53:49 2021 +0000
Revision:
2:8dacbf975c41
Parent:
0:82908a60b720
Child:
3:522c779c5f29
Filip Stevanovic 2020/0039;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
filip_ste 0:82908a60b720 1 #include "mb_pins.h"
filip_ste 0:82908a60b720 2 #include "mbed.h"
filip_ste 0:82908a60b720 3 #include "platform/mbed_thread.h"
filip_ste 0:82908a60b720 4 #include "MQTTClientMbedOs.h"
filip_ste 0:82908a60b720 5
filip_ste 0:82908a60b720 6 #include "Adafruit_GFX.h"
filip_ste 0:82908a60b720 7 #include "Adafruit_GFX_Config.h"
filip_ste 0:82908a60b720 8 #include "Adafruit_SSD1306.h"
filip_ste 0:82908a60b720 9
filip_ste 2:8dacbf975c41 10 #include <string>
filip_ste 2:8dacbf975c41 11 using namespace std;
filip_ste 2:8dacbf975c41 12
filip_ste 0:82908a60b720 13 ///////////////////////////////////////////////////////////////
filip_ste 0:82908a60b720 14
filip_ste 0:82908a60b720 15 #define SMALL_WAIT_MS 10
filip_ste 2:8dacbf975c41 16 #define VOLTAGE_SCALER 3.3f
filip_ste 0:82908a60b720 17
filip_ste 0:82908a60b720 18 //I2C adress
filip_ste 0:82908a60b720 19 #define I2C_ADDRESS 0x3c
filip_ste 0:82908a60b720 20 #define I2C_ADD_MBED I2C_ADDRESS << 1
filip_ste 0:82908a60b720 21
filip_ste 0:82908a60b720 22 //OLED DIMENSIONS
filip_ste 0:82908a60b720 23 #define OLED_HEIGHT_PX 64
filip_ste 0:82908a60b720 24 #define OLED_WIDTH_PX 128
filip_ste 0:82908a60b720 25
filip_ste 0:82908a60b720 26 ///////////////////////////////////////////////////////////////
filip_ste 0:82908a60b720 27
filip_ste 0:82908a60b720 28 TCPSocket socket;
filip_ste 0:82908a60b720 29 MQTTClient client(&socket);
filip_ste 0:82908a60b720 30 MQTT::Message message;
filip_ste 0:82908a60b720 31 WiFiInterface *wifi;
filip_ste 0:82908a60b720 32
filip_ste 0:82908a60b720 33 I2C i2c_obj(MB_OLED_SDA, MB_OLED_SCL);
filip_ste 0:82908a60b720 34 Adafruit_SSD1306_I2c myOLED(i2c_obj, PB_5, I2C_ADD_MBED, OLED_HEIGHT_PX, OLED_WIDTH_PX);
filip_ste 0:82908a60b720 35
filip_ste 2:8dacbf975c41 36 const char* topic_sub = "subpim";
filip_ste 2:8dacbf975c41 37 const char* topic_pub = "pubpim";
filip_ste 0:82908a60b720 38 const char* hostname = "broker.hivemq.com";
filip_ste 0:82908a60b720 39 int port = 1883;
filip_ste 0:82908a60b720 40
filip_ste 2:8dacbf975c41 41 Timer t;
filip_ste 2:8dacbf975c41 42 AnalogIn pot1(MB_POT1);
filip_ste 2:8dacbf975c41 43
filip_ste 2:8dacbf975c41 44 char buf[100];
filip_ste 2:8dacbf975c41 45 string s;
filip_ste 2:8dacbf975c41 46
filip_ste 2:8dacbf975c41 47 string recv;
filip_ste 2:8dacbf975c41 48 string tran;
filip_ste 2:8dacbf975c41 49
filip_ste 2:8dacbf975c41 50 string chrToStr(char* a, int size){
filip_ste 2:8dacbf975c41 51 string s = "";
filip_ste 2:8dacbf975c41 52 for (int i = 0; i < size; i++) {
filip_ste 2:8dacbf975c41 53 s = s + a[i];
filip_ste 2:8dacbf975c41 54 }
filip_ste 2:8dacbf975c41 55 return s;
filip_ste 2:8dacbf975c41 56 }
filip_ste 2:8dacbf975c41 57
filip_ste 2:8dacbf975c41 58 void updateOLED()
filip_ste 2:8dacbf975c41 59 {
filip_ste 2:8dacbf975c41 60 myOLED.clearDisplay();
filip_ste 2:8dacbf975c41 61 myOLED.setTextCursor(0, 0);
filip_ste 2:8dacbf975c41 62 myOLED.printf("Recv: %s\n", recv.c_str());
filip_ste 2:8dacbf975c41 63 myOLED.printf("Tran: %s\n", tran.c_str());
filip_ste 2:8dacbf975c41 64 myOLED.display();
filip_ste 2:8dacbf975c41 65 }
filip_ste 2:8dacbf975c41 66
filip_ste 2:8dacbf975c41 67 void printMQTT()
filip_ste 2:8dacbf975c41 68 {
filip_ste 2:8dacbf975c41 69 message.qos = MQTT::QOS0;
filip_ste 2:8dacbf975c41 70 message.retained = false;
filip_ste 2:8dacbf975c41 71 message.dup = false;
filip_ste 2:8dacbf975c41 72 message.payload = (void*)buf;
filip_ste 2:8dacbf975c41 73 //message.payloadlen = strlen(buf)+1;
filip_ste 2:8dacbf975c41 74 message.payloadlen = strlen(buf);
filip_ste 2:8dacbf975c41 75 client.publish(topic_pub, message);
filip_ste 2:8dacbf975c41 76 }
filip_ste 2:8dacbf975c41 77
filip_ste 2:8dacbf975c41 78 void sendValue(){
filip_ste 2:8dacbf975c41 79 sprintf(buf, "V = %1.2f", pot1*VOLTAGE_SCALER);
filip_ste 2:8dacbf975c41 80 s = chrToStr(buf, sizeof(buf));
filip_ste 2:8dacbf975c41 81 tran = s;
filip_ste 2:8dacbf975c41 82 printMQTT();
filip_ste 2:8dacbf975c41 83 }
filip_ste 2:8dacbf975c41 84
filip_ste 0:82908a60b720 85 ///////////////////////////////////////////////////////////////
filip_ste 0:82908a60b720 86
filip_ste 2:8dacbf975c41 87
filip_ste 2:8dacbf975c41 88
filip_ste 0:82908a60b720 89 void messageArrived(MQTT::MessageData& md)
filip_ste 0:82908a60b720 90 {
filip_ste 0:82908a60b720 91 MQTT::Message &message = md.message;
filip_ste 0:82908a60b720 92 printf("Browser message: %.*s \n", message.payloadlen, (char*)message.payload);
filip_ste 0:82908a60b720 93
filip_ste 2:8dacbf975c41 94 //sprintf(buf, "%.*s %d", message.payloadlen, (char*)message.payload, message.payloadlen);
filip_ste 2:8dacbf975c41 95 //printMQTT();
filip_ste 2:8dacbf975c41 96
filip_ste 2:8dacbf975c41 97 s = chrToStr((char*)message.payload, message.payloadlen);
filip_ste 2:8dacbf975c41 98
filip_ste 2:8dacbf975c41 99 recv = s;
filip_ste 2:8dacbf975c41 100
filip_ste 2:8dacbf975c41 101 sprintf(buf, "%s", s.c_str());
filip_ste 2:8dacbf975c41 102 printMQTT();
filip_ste 2:8dacbf975c41 103
filip_ste 2:8dacbf975c41 104 if(s == "start")
filip_ste 2:8dacbf975c41 105 {
filip_ste 2:8dacbf975c41 106 sendValue();
filip_ste 2:8dacbf975c41 107 t.reset();
filip_ste 2:8dacbf975c41 108 t.start();
filip_ste 2:8dacbf975c41 109 }
filip_ste 2:8dacbf975c41 110 else if(s == "stop")
filip_ste 2:8dacbf975c41 111 {
filip_ste 2:8dacbf975c41 112 t.stop();
filip_ste 2:8dacbf975c41 113 }
filip_ste 2:8dacbf975c41 114 else if(s == "oled")
filip_ste 2:8dacbf975c41 115 {
filip_ste 2:8dacbf975c41 116 myOLED.begin();
filip_ste 2:8dacbf975c41 117 myOLED.clearDisplay();
filip_ste 2:8dacbf975c41 118 myOLED.display();
filip_ste 2:8dacbf975c41 119 }
filip_ste 2:8dacbf975c41 120
filip_ste 0:82908a60b720 121 }
filip_ste 0:82908a60b720 122
filip_ste 0:82908a60b720 123 ///////////////////////////////////////////////////////////////
filip_ste 0:82908a60b720 124
filip_ste 0:82908a60b720 125 int main()
filip_ste 0:82908a60b720 126 {
filip_ste 0:82908a60b720 127 wifi = WiFiInterface::get_default_instance();
filip_ste 0:82908a60b720 128
filip_ste 2:8dacbf975c41 129 //printf("Connecting to %s \n",MBED_CONF_APP_WIFI_SSID);
filip_ste 0:82908a60b720 130 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
filip_ste 2:8dacbf975c41 131 //if(ret != 0){
filip_ste 2:8dacbf975c41 132 // printf("Greska u povezivanju");
filip_ste 2:8dacbf975c41 133 // return -1;
filip_ste 2:8dacbf975c41 134 //}
filip_ste 2:8dacbf975c41 135 //printf("Connected\n");
filip_ste 0:82908a60b720 136
filip_ste 0:82908a60b720 137 socket.open(wifi);
filip_ste 0:82908a60b720 138 socket.connect(hostname,port);
filip_ste 0:82908a60b720 139
filip_ste 0:82908a60b720 140 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
filip_ste 0:82908a60b720 141 data.MQTTVersion = 3;
filip_ste 2:8dacbf975c41 142 data.clientID.cstring = "pim-60";
filip_ste 0:82908a60b720 143
filip_ste 0:82908a60b720 144 client.connect(data);
filip_ste 0:82908a60b720 145 client.subscribe(topic_sub, MQTT::QOS0, messageArrived);
filip_ste 0:82908a60b720 146
filip_ste 2:8dacbf975c41 147 i2c_obj.frequency(400000);
filip_ste 0:82908a60b720 148
filip_ste 0:82908a60b720 149
filip_ste 0:82908a60b720 150 while(1) {
filip_ste 2:8dacbf975c41 151 if (t.read() > 10) {
filip_ste 2:8dacbf975c41 152 sendValue();
filip_ste 2:8dacbf975c41 153 t.reset();
filip_ste 2:8dacbf975c41 154 }
filip_ste 0:82908a60b720 155 thread_sleep_for(SMALL_WAIT_MS);
filip_ste 2:8dacbf975c41 156 updateOLED();
filip_ste 0:82908a60b720 157 client.yield(1000);
filip_ste 0:82908a60b720 158 }
filip_ste 0:82908a60b720 159 }