Emilija Stankovic
/
PMK_industrija_mikro2
PMKIND
main.cpp@0:51001d8fdeff, 2021-06-24 (annotated)
- Committer:
- emilija
- Date:
- Thu Jun 24 15:11:30 2021 +0000
- Revision:
- 0:51001d8fdeff
- Child:
- 1:d3bb978c1ff7
pmkINDUSTRIJA1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilija | 0:51001d8fdeff | 1 | /* mbed Microcontroller Library |
emilija | 0:51001d8fdeff | 2 | * Copyright (c) 2019 ARM Limited |
emilija | 0:51001d8fdeff | 3 | * SPDX-License-Identifier: Apache-2.0 |
emilija | 0:51001d8fdeff | 4 | */ |
emilija | 0:51001d8fdeff | 5 | |
emilija | 0:51001d8fdeff | 6 | #include "mbed.h" |
emilija | 0:51001d8fdeff | 7 | #include "platform/mbed_thread.h" |
emilija | 0:51001d8fdeff | 8 | #include "Servo.h" |
emilija | 0:51001d8fdeff | 9 | #include "color.h" |
emilija | 0:51001d8fdeff | 10 | #include <string> |
emilija | 0:51001d8fdeff | 11 | #include <MQTTClientMbedOs.h> |
emilija | 0:51001d8fdeff | 12 | |
emilija | 0:51001d8fdeff | 13 | // Blinking rate in milliseconds |
emilija | 0:51001d8fdeff | 14 | #define BLINKING_RATE_MS 500 |
emilija | 0:51001d8fdeff | 15 | |
emilija | 0:51001d8fdeff | 16 | Servo servo0(D3); |
emilija | 0:51001d8fdeff | 17 | Servo servo1(D4); |
emilija | 0:51001d8fdeff | 18 | InterruptIn button(USER_BUTTON); |
emilija | 0:51001d8fdeff | 19 | int arrivedcount = 0; |
emilija | 0:51001d8fdeff | 20 | TCPSocket socket; |
emilija | 0:51001d8fdeff | 21 | MQTTClient client(&socket); |
emilija | 0:51001d8fdeff | 22 | MQTT::Message message; |
emilija | 0:51001d8fdeff | 23 | int button_pressed=0; |
emilija | 0:51001d8fdeff | 24 | char* topic_pub = "PMK-client-temperature"; |
emilija | 0:51001d8fdeff | 25 | char* topic_sub = "PMK_industrija/micro2/#"; |
emilija | 0:51001d8fdeff | 26 | |
emilija | 0:51001d8fdeff | 27 | WiFiInterface *wifi; |
emilija | 0:51001d8fdeff | 28 | volatile int mems_event = 0; |
emilija | 0:51001d8fdeff | 29 | uint32_t previous_tick = 0; |
emilija | 0:51001d8fdeff | 30 | uint32_t current_tick = 0; |
emilija | 0:51001d8fdeff | 31 | uint8_t high = 0, low = 0; |
emilija | 0:51001d8fdeff | 32 | float temperature = 0.0f; |
emilija | 0:51001d8fdeff | 33 | char buffer[32]; |
emilija | 0:51001d8fdeff | 34 | static char *print_double(char *str, double v, int decimalDigits = 2) |
emilija | 0:51001d8fdeff | 35 | { |
emilija | 0:51001d8fdeff | 36 | int i = 1; |
emilija | 0:51001d8fdeff | 37 | int intPart, fractPart; |
emilija | 0:51001d8fdeff | 38 | int len; |
emilija | 0:51001d8fdeff | 39 | char *ptr; |
emilija | 0:51001d8fdeff | 40 | |
emilija | 0:51001d8fdeff | 41 | /* prepare decimal digits multiplicator */ |
emilija | 0:51001d8fdeff | 42 | for (; decimalDigits != 0; i *= 10, decimalDigits--); |
emilija | 0:51001d8fdeff | 43 | |
emilija | 0:51001d8fdeff | 44 | /* calculate integer & fractinal parts */ |
emilija | 0:51001d8fdeff | 45 | intPart = (int)v; |
emilija | 0:51001d8fdeff | 46 | fractPart = (int)((v - (double)(int)v) * i); |
emilija | 0:51001d8fdeff | 47 | |
emilija | 0:51001d8fdeff | 48 | /* fill in integer part */ |
emilija | 0:51001d8fdeff | 49 | sprintf(str, "%i.", intPart); |
emilija | 0:51001d8fdeff | 50 | |
emilija | 0:51001d8fdeff | 51 | /* prepare fill in of fractional part */ |
emilija | 0:51001d8fdeff | 52 | len = strlen(str); |
emilija | 0:51001d8fdeff | 53 | ptr = &str[len]; |
emilija | 0:51001d8fdeff | 54 | |
emilija | 0:51001d8fdeff | 55 | /* fill in leading fractional zeros */ |
emilija | 0:51001d8fdeff | 56 | for (i /= 10; i > 1; i /= 10, ptr++) { |
emilija | 0:51001d8fdeff | 57 | if (fractPart >= i) { |
emilija | 0:51001d8fdeff | 58 | break; |
emilija | 0:51001d8fdeff | 59 | } |
emilija | 0:51001d8fdeff | 60 | *ptr = '0'; |
emilija | 0:51001d8fdeff | 61 | } |
emilija | 0:51001d8fdeff | 62 | |
emilija | 0:51001d8fdeff | 63 | /* fill in (rest of) fractional part */ |
emilija | 0:51001d8fdeff | 64 | sprintf(ptr, "%i", fractPart); |
emilija | 0:51001d8fdeff | 65 | |
emilija | 0:51001d8fdeff | 66 | return str; |
emilija | 0:51001d8fdeff | 67 | } |
emilija | 0:51001d8fdeff | 68 | |
emilija | 0:51001d8fdeff | 69 | const char *sec2str(nsapi_security_t sec) |
emilija | 0:51001d8fdeff | 70 | { |
emilija | 0:51001d8fdeff | 71 | switch (sec) { |
emilija | 0:51001d8fdeff | 72 | case NSAPI_SECURITY_NONE: |
emilija | 0:51001d8fdeff | 73 | return "None"; |
emilija | 0:51001d8fdeff | 74 | case NSAPI_SECURITY_WEP: |
emilija | 0:51001d8fdeff | 75 | return "WEP"; |
emilija | 0:51001d8fdeff | 76 | case NSAPI_SECURITY_WPA: |
emilija | 0:51001d8fdeff | 77 | return "WPA"; |
emilija | 0:51001d8fdeff | 78 | case NSAPI_SECURITY_WPA2: |
emilija | 0:51001d8fdeff | 79 | return "WPA2"; |
emilija | 0:51001d8fdeff | 80 | case NSAPI_SECURITY_WPA_WPA2: |
emilija | 0:51001d8fdeff | 81 | return "WPA/WPA2"; |
emilija | 0:51001d8fdeff | 82 | case NSAPI_SECURITY_UNKNOWN: |
emilija | 0:51001d8fdeff | 83 | default: |
emilija | 0:51001d8fdeff | 84 | return "Unknown"; |
emilija | 0:51001d8fdeff | 85 | } |
emilija | 0:51001d8fdeff | 86 | } |
emilija | 0:51001d8fdeff | 87 | |
emilija | 0:51001d8fdeff | 88 | int scan_demo(WiFiInterface *wifi) |
emilija | 0:51001d8fdeff | 89 | { |
emilija | 0:51001d8fdeff | 90 | WiFiAccessPoint *ap; |
emilija | 0:51001d8fdeff | 91 | printf("Scan:\n"); |
emilija | 0:51001d8fdeff | 92 | int count = wifi->scan(NULL,0); |
emilija | 0:51001d8fdeff | 93 | if (count <= 0) { |
emilija | 0:51001d8fdeff | 94 | printf("scan() failed with return value: %d\n", count); |
emilija | 0:51001d8fdeff | 95 | return 0; |
emilija | 0:51001d8fdeff | 96 | } |
emilija | 0:51001d8fdeff | 97 | /* Limit number of network arbitrary to 15 */ |
emilija | 0:51001d8fdeff | 98 | count = count < 15 ? count : 15; |
emilija | 0:51001d8fdeff | 99 | ap = new WiFiAccessPoint[count]; |
emilija | 0:51001d8fdeff | 100 | count = wifi->scan(ap, count); |
emilija | 0:51001d8fdeff | 101 | if (count <= 0) { |
emilija | 0:51001d8fdeff | 102 | printf("scan() failed with return value: %d\n", count); |
emilija | 0:51001d8fdeff | 103 | return 0; |
emilija | 0:51001d8fdeff | 104 | } |
emilija | 0:51001d8fdeff | 105 | for (int i = 0; i < count; i++) { |
emilija | 0:51001d8fdeff | 106 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
emilija | 0:51001d8fdeff | 107 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
emilija | 0:51001d8fdeff | 108 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
emilija | 0:51001d8fdeff | 109 | } |
emilija | 0:51001d8fdeff | 110 | printf("%d networks available.\n", count); |
emilija | 0:51001d8fdeff | 111 | delete[] ap; |
emilija | 0:51001d8fdeff | 112 | return count; |
emilija | 0:51001d8fdeff | 113 | } |
emilija | 0:51001d8fdeff | 114 | |
emilija | 0:51001d8fdeff | 115 | void messageArrived(MQTT::MessageData& md) |
emilija | 0:51001d8fdeff | 116 | { |
emilija | 0:51001d8fdeff | 117 | MQTTString &topic = md.topicName; |
emilija | 0:51001d8fdeff | 118 | string topic_name = topic.lenstring.data; |
emilija | 0:51001d8fdeff | 119 | printf("Topic name %d : %s\r\n",topic.lenstring.len,topic_name.c_str() ); |
emilija | 0:51001d8fdeff | 120 | MQTT::Message &message = md.message; |
emilija | 0:51001d8fdeff | 121 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
emilija | 0:51001d8fdeff | 122 | printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
emilija | 0:51001d8fdeff | 123 | char* poruka = (char*)message.payload; |
emilija | 0:51001d8fdeff | 124 | if(topic_name.find("servo0")!=std::string::npos) { |
emilija | 0:51001d8fdeff | 125 | printf("blblabla\n"); |
emilija | 0:51001d8fdeff | 126 | for(float p=0; p<1.0; p += 0.1) { |
emilija | 0:51001d8fdeff | 127 | servo0 = p; |
emilija | 0:51001d8fdeff | 128 | |
emilija | 0:51001d8fdeff | 129 | } |
emilija | 0:51001d8fdeff | 130 | wait(2); |
emilija | 0:51001d8fdeff | 131 | for(float p=1.0; p>0.0; p -= 0.1) { |
emilija | 0:51001d8fdeff | 132 | servo0 = p; |
emilija | 0:51001d8fdeff | 133 | |
emilija | 0:51001d8fdeff | 134 | } |
emilija | 0:51001d8fdeff | 135 | } |
emilija | 0:51001d8fdeff | 136 | ++arrivedcount; |
emilija | 0:51001d8fdeff | 137 | } |
emilija | 0:51001d8fdeff | 138 | |
emilija | 0:51001d8fdeff | 139 | |
emilija | 0:51001d8fdeff | 140 | void buttonFunction() { |
emilija | 0:51001d8fdeff | 141 | button_pressed=1; |
emilija | 0:51001d8fdeff | 142 | } |
emilija | 0:51001d8fdeff | 143 | |
emilija | 0:51001d8fdeff | 144 | int main() |
emilija | 0:51001d8fdeff | 145 | { |
emilija | 0:51001d8fdeff | 146 | // Initialise the digital pin LED1 as an output |
emilija | 0:51001d8fdeff | 147 | |
emilija | 0:51001d8fdeff | 148 | ColorSensor cs(PA_5,PA_6,PA_7,PB_6,PC_7); |
emilija | 0:51001d8fdeff | 149 | DigitalOut led(LED1); |
emilija | 0:51001d8fdeff | 150 | servo0.calibrate(0.0005,90); |
emilija | 0:51001d8fdeff | 151 | button.rise(&buttonFunction); |
emilija | 0:51001d8fdeff | 152 | const char* hostname = "broker.mqttdashboard.com"; |
emilija | 0:51001d8fdeff | 153 | int port = 1883; |
emilija | 0:51001d8fdeff | 154 | |
emilija | 0:51001d8fdeff | 155 | wifi = WiFiInterface::get_default_instance(); |
emilija | 0:51001d8fdeff | 156 | if (!wifi) { |
emilija | 0:51001d8fdeff | 157 | printf("ERROR: No WiFiInterface found.\n"); |
emilija | 0:51001d8fdeff | 158 | return -1; |
emilija | 0:51001d8fdeff | 159 | } |
emilija | 0:51001d8fdeff | 160 | |
emilija | 0:51001d8fdeff | 161 | int count = scan_demo(wifi); |
emilija | 0:51001d8fdeff | 162 | if (count == 0) { |
emilija | 0:51001d8fdeff | 163 | printf("No WIFI APs found - can't continue further.\n"); |
emilija | 0:51001d8fdeff | 164 | return -1; |
emilija | 0:51001d8fdeff | 165 | } |
emilija | 0:51001d8fdeff | 166 | |
emilija | 0:51001d8fdeff | 167 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
emilija | 0:51001d8fdeff | 168 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
emilija | 0:51001d8fdeff | 169 | if (ret != 0) { |
emilija | 0:51001d8fdeff | 170 | printf("\nConnection error: %d\n", ret); |
emilija | 0:51001d8fdeff | 171 | return -1; |
emilija | 0:51001d8fdeff | 172 | } |
emilija | 0:51001d8fdeff | 173 | |
emilija | 0:51001d8fdeff | 174 | printf("Success\n\n"); |
emilija | 0:51001d8fdeff | 175 | printf("MAC: %s\n", wifi->get_mac_address()); |
emilija | 0:51001d8fdeff | 176 | printf("IP: %s\n", wifi->get_ip_address()); |
emilija | 0:51001d8fdeff | 177 | printf("Netmask: %s\n", wifi->get_netmask()); |
emilija | 0:51001d8fdeff | 178 | printf("Gateway: %s\n", wifi->get_gateway()); |
emilija | 0:51001d8fdeff | 179 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
emilija | 0:51001d8fdeff | 180 | |
emilija | 0:51001d8fdeff | 181 | socket.open(wifi); |
emilija | 0:51001d8fdeff | 182 | socket.connect(hostname, port); |
emilija | 0:51001d8fdeff | 183 | |
emilija | 0:51001d8fdeff | 184 | int rc=0; |
emilija | 0:51001d8fdeff | 185 | |
emilija | 0:51001d8fdeff | 186 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
emilija | 0:51001d8fdeff | 187 | data.MQTTVersion = 3; |
emilija | 0:51001d8fdeff | 188 | data.clientID.cstring = "PMK-client2"; |
emilija | 0:51001d8fdeff | 189 | //data.username.cstring = "testuser"; |
emilija | 0:51001d8fdeff | 190 | //data.password.cstring = "testpassword"; |
emilija | 0:51001d8fdeff | 191 | |
emilija | 0:51001d8fdeff | 192 | if ((rc = client.connect(data)) != 0) |
emilija | 0:51001d8fdeff | 193 | printf("rc from MQTT connect is %d\r\n", rc); |
emilija | 0:51001d8fdeff | 194 | |
emilija | 0:51001d8fdeff | 195 | |
emilija | 0:51001d8fdeff | 196 | if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) |
emilija | 0:51001d8fdeff | 197 | printf("rc from MQTT subscribe is %d\r\n", rc); |
emilija | 0:51001d8fdeff | 198 | |
emilija | 0:51001d8fdeff | 199 | |
emilija | 0:51001d8fdeff | 200 | |
emilija | 0:51001d8fdeff | 201 | while (true) { |
emilija | 0:51001d8fdeff | 202 | led = !led; |
emilija | 0:51001d8fdeff | 203 | thread_sleep_for(BLINKING_RATE_MS); |
emilija | 0:51001d8fdeff | 204 | if (button_pressed==1) { |
emilija | 0:51001d8fdeff | 205 | button_pressed=0; |
emilija | 0:51001d8fdeff | 206 | printf("Publishing data\r\n"); |
emilija | 0:51001d8fdeff | 207 | // QoS 0 |
emilija | 0:51001d8fdeff | 208 | char buf[100]; |
emilija | 0:51001d8fdeff | 209 | printf("r: %d", cs.getRed()); |
emilija | 0:51001d8fdeff | 210 | |
emilija | 0:51001d8fdeff | 211 | printf("g: %d", cs.getGreen()); |
emilija | 0:51001d8fdeff | 212 | printf("b: %d", cs.getBlue()); |
emilija | 0:51001d8fdeff | 213 | //sprintf(buf, "RGB: %7s C\r\n", print_double(buffer, cs.getRed()*100000000+cs.getGreen()*10000+cs.getBlue())); |
emilija | 0:51001d8fdeff | 214 | |
emilija | 0:51001d8fdeff | 215 | //message.qos = MQTT::QOS0; |
emilija | 0:51001d8fdeff | 216 | //message.retained = false; |
emilija | 0:51001d8fdeff | 217 | //message.dup = false; |
emilija | 0:51001d8fdeff | 218 | //message.payload = (void*)buf; |
emilija | 0:51001d8fdeff | 219 | //message.payloadlen = strlen(buf)+1; |
emilija | 0:51001d8fdeff | 220 | //client.publish(topic_pub, message); |
emilija | 0:51001d8fdeff | 221 | |
emilija | 0:51001d8fdeff | 222 | } |
emilija | 0:51001d8fdeff | 223 | printf("Yielding"); |
emilija | 0:51001d8fdeff | 224 | client.yield(1000); |
emilija | 0:51001d8fdeff | 225 | printf(" -> Yielded\r\n"); |
emilija | 0:51001d8fdeff | 226 | } |
emilija | 0:51001d8fdeff | 227 | } |