PMK_industrija_mikro3
Dependencies: proxy2 proxy2123 Servo
main.cpp@1:e73b7979a1a3, 2021-07-05 (annotated)
- Committer:
- emilija
- Date:
- Mon Jul 05 12:57:13 2021 +0000
- Revision:
- 1:e73b7979a1a3
- Parent:
- 0:bfa464805a45
PMK_INDUSTRIJA_MICRO3 VERZIJA2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilija | 0:bfa464805a45 | 1 | /* mbed Microcontroller Library |
emilija | 0:bfa464805a45 | 2 | * Copyright (c) 2019 ARM Limited |
emilija | 0:bfa464805a45 | 3 | * SPDX-License-Identifier: Apache-2.0 |
emilija | 0:bfa464805a45 | 4 | */ |
emilija | 0:bfa464805a45 | 5 | |
emilija | 0:bfa464805a45 | 6 | #include "mbed.h" |
emilija | 0:bfa464805a45 | 7 | #include "platform/mbed_thread.h" |
emilija | 0:bfa464805a45 | 8 | #include "Servo.h" |
emilija | 0:bfa464805a45 | 9 | #include "color.h" |
emilija | 0:bfa464805a45 | 10 | #include <string> |
emilija | 0:bfa464805a45 | 11 | #include <MQTTClientMbedOs.h> |
emilija | 0:bfa464805a45 | 12 | #include <algorithm> |
emilija | 0:bfa464805a45 | 13 | |
emilija | 0:bfa464805a45 | 14 | #include "rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //mbed.h, types, DEBUG_print* |
emilija | 0:bfa464805a45 | 15 | #include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" |
emilija | 0:bfa464805a45 | 16 | |
emilija | 0:bfa464805a45 | 17 | #include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h" |
emilija | 0:bfa464805a45 | 18 | #include "rohm-rpr0521/rohm-rpr0521/rpr0521.h" |
emilija | 0:bfa464805a45 | 19 | |
emilija | 0:bfa464805a45 | 20 | // Blinking rate in milliseconds |
emilija | 0:bfa464805a45 | 21 | #define BLINKING_RATE_MS 500 |
emilija | 0:bfa464805a45 | 22 | Serial pc(USBTX,USBRX); |
emilija | 1:e73b7979a1a3 | 23 | |
emilija | 1:e73b7979a1a3 | 24 | Timeout T2; |
emilija | 1:e73b7979a1a3 | 25 | void interruptT2(){ |
emilija | 1:e73b7979a1a3 | 26 | printf("error2"); |
emilija | 1:e73b7979a1a3 | 27 | } |
emilija | 1:e73b7979a1a3 | 28 | |
emilija | 1:e73b7979a1a3 | 29 | //varijable boje |
emilija | 0:bfa464805a45 | 30 | float r1[3]={1 ,109.0/399.0,133.0/399.0}; |
emilija | 0:bfa464805a45 | 31 | float g1[3]={206.0/411.0,399.0/411.0,1}; |
emilija | 0:bfa464805a45 | 32 | float b1[3]={120.0/490.0 ,209.0/490.0, 1}; |
emilija | 1:e73b7979a1a3 | 33 | DigitalOut colorLed(D4); |
emilija | 1:e73b7979a1a3 | 34 | DigitalOut colorVout(D5); |
emilija | 1:e73b7979a1a3 | 35 | int salji_boju=0; |
emilija | 1:e73b7979a1a3 | 36 | int fleg_start=0; |
emilija | 1:e73b7979a1a3 | 37 | //varijable za servo |
emilija | 0:bfa464805a45 | 38 | Servo servo2(D3); |
emilija | 1:e73b7979a1a3 | 39 | int fleg_proxy1=0; |
emilija | 0:bfa464805a45 | 40 | InterruptIn button(USER_BUTTON); |
emilija | 0:bfa464805a45 | 41 | int arrivedcount = 0; |
emilija | 0:bfa464805a45 | 42 | TCPSocket socket; |
emilija | 0:bfa464805a45 | 43 | MQTTClient client(&socket); |
emilija | 0:bfa464805a45 | 44 | MQTT::Message message; |
emilija | 0:bfa464805a45 | 45 | int button_pressed=0; |
emilija | 0:bfa464805a45 | 46 | char* topic_pub1 = "PMK_industrija/micro1/proxy1"; |
emilija | 1:e73b7979a1a3 | 47 | char* topic_pub4 = "PMK_industrija/micro1/proxy2"; |
emilija | 0:bfa464805a45 | 48 | char* topic_pub3 = "PMK_industrija/micro1/color2"; |
emilija | 1:e73b7979a1a3 | 49 | char* topic_pub2 = "PMK_industrija/micro1/echo/3"; |
emilija | 0:bfa464805a45 | 50 | char* topic_sub = "PMK_industrija/micro3/#"; |
emilija | 1:e73b7979a1a3 | 51 | int fleg_proxy2=0; |
emilija | 0:bfa464805a45 | 52 | |
emilija | 0:bfa464805a45 | 53 | WiFiInterface *wifi; |
emilija | 0:bfa464805a45 | 54 | volatile int mems_event = 0; |
emilija | 0:bfa464805a45 | 55 | uint32_t previous_tick = 0; |
emilija | 0:bfa464805a45 | 56 | uint32_t current_tick = 0; |
emilija | 0:bfa464805a45 | 57 | uint8_t high = 0, low = 0; |
emilija | 0:bfa464805a45 | 58 | float temperature = 0.0f; |
emilija | 0:bfa464805a45 | 59 | char buffer[32]; |
emilija | 0:bfa464805a45 | 60 | static char *print_double(char *str, double v, int decimalDigits = 2) |
emilija | 0:bfa464805a45 | 61 | { |
emilija | 0:bfa464805a45 | 62 | int i = 1; |
emilija | 0:bfa464805a45 | 63 | int intPart, fractPart; |
emilija | 0:bfa464805a45 | 64 | int len; |
emilija | 0:bfa464805a45 | 65 | char *ptr; |
emilija | 0:bfa464805a45 | 66 | |
emilija | 0:bfa464805a45 | 67 | /* prepare decimal digits multiplicator */ |
emilija | 0:bfa464805a45 | 68 | for (; decimalDigits != 0; i *= 10, decimalDigits--); |
emilija | 0:bfa464805a45 | 69 | |
emilija | 0:bfa464805a45 | 70 | /* calculate integer & fractinal parts */ |
emilija | 0:bfa464805a45 | 71 | intPart = (int)v; |
emilija | 0:bfa464805a45 | 72 | fractPart = (int)((v - (double)(int)v) * i); |
emilija | 0:bfa464805a45 | 73 | |
emilija | 0:bfa464805a45 | 74 | /* fill in integer part */ |
emilija | 0:bfa464805a45 | 75 | sprintf(str, "%i.", intPart); |
emilija | 0:bfa464805a45 | 76 | |
emilija | 0:bfa464805a45 | 77 | /* prepare fill in of fractional part */ |
emilija | 0:bfa464805a45 | 78 | len = strlen(str); |
emilija | 0:bfa464805a45 | 79 | ptr = &str[len]; |
emilija | 0:bfa464805a45 | 80 | |
emilija | 0:bfa464805a45 | 81 | /* fill in leading fractional zeros */ |
emilija | 0:bfa464805a45 | 82 | for (i /= 10; i > 1; i /= 10, ptr++) { |
emilija | 0:bfa464805a45 | 83 | if (fractPart >= i) { |
emilija | 0:bfa464805a45 | 84 | break; |
emilija | 0:bfa464805a45 | 85 | } |
emilija | 0:bfa464805a45 | 86 | *ptr = '0'; |
emilija | 0:bfa464805a45 | 87 | } |
emilija | 0:bfa464805a45 | 88 | |
emilija | 0:bfa464805a45 | 89 | /* fill in (rest of) fractional part */ |
emilija | 0:bfa464805a45 | 90 | sprintf(ptr, "%i", fractPart); |
emilija | 0:bfa464805a45 | 91 | |
emilija | 0:bfa464805a45 | 92 | return str; |
emilija | 0:bfa464805a45 | 93 | } |
emilija | 0:bfa464805a45 | 94 | |
emilija | 0:bfa464805a45 | 95 | const char *sec2str(nsapi_security_t sec) |
emilija | 0:bfa464805a45 | 96 | { |
emilija | 0:bfa464805a45 | 97 | switch (sec) { |
emilija | 0:bfa464805a45 | 98 | case NSAPI_SECURITY_NONE: |
emilija | 0:bfa464805a45 | 99 | return "None"; |
emilija | 0:bfa464805a45 | 100 | case NSAPI_SECURITY_WEP: |
emilija | 0:bfa464805a45 | 101 | return "WEP"; |
emilija | 0:bfa464805a45 | 102 | case NSAPI_SECURITY_WPA: |
emilija | 0:bfa464805a45 | 103 | return "WPA"; |
emilija | 0:bfa464805a45 | 104 | case NSAPI_SECURITY_WPA2: |
emilija | 0:bfa464805a45 | 105 | return "WPA2"; |
emilija | 0:bfa464805a45 | 106 | case NSAPI_SECURITY_WPA_WPA2: |
emilija | 0:bfa464805a45 | 107 | return "WPA/WPA2"; |
emilija | 0:bfa464805a45 | 108 | case NSAPI_SECURITY_UNKNOWN: |
emilija | 0:bfa464805a45 | 109 | default: |
emilija | 0:bfa464805a45 | 110 | return "Unknown"; |
emilija | 0:bfa464805a45 | 111 | } |
emilija | 0:bfa464805a45 | 112 | } |
emilija | 0:bfa464805a45 | 113 | |
emilija | 0:bfa464805a45 | 114 | int scan_demo(WiFiInterface *wifi) |
emilija | 0:bfa464805a45 | 115 | { |
emilija | 0:bfa464805a45 | 116 | WiFiAccessPoint *ap; |
emilija | 0:bfa464805a45 | 117 | printf("Scan:\n"); |
emilija | 0:bfa464805a45 | 118 | int count = wifi->scan(NULL,0); |
emilija | 0:bfa464805a45 | 119 | if (count <= 0) { |
emilija | 0:bfa464805a45 | 120 | printf("scan() failed with return value: %d\n", count); |
emilija | 0:bfa464805a45 | 121 | return 0; |
emilija | 0:bfa464805a45 | 122 | } |
emilija | 0:bfa464805a45 | 123 | /* Limit number of network arbitrary to 15 */ |
emilija | 0:bfa464805a45 | 124 | count = count < 15 ? count : 15; |
emilija | 0:bfa464805a45 | 125 | ap = new WiFiAccessPoint[count]; |
emilija | 0:bfa464805a45 | 126 | count = wifi->scan(ap, count); |
emilija | 0:bfa464805a45 | 127 | if (count <= 0) { |
emilija | 0:bfa464805a45 | 128 | printf("scan() failed with return value: %d\n", count); |
emilija | 0:bfa464805a45 | 129 | return 0; |
emilija | 0:bfa464805a45 | 130 | } |
emilija | 0:bfa464805a45 | 131 | for (int i = 0; i < count; i++) { |
emilija | 0:bfa464805a45 | 132 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
emilija | 0:bfa464805a45 | 133 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
emilija | 0:bfa464805a45 | 134 | 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:bfa464805a45 | 135 | } |
emilija | 0:bfa464805a45 | 136 | printf("%d networks available.\n", count); |
emilija | 0:bfa464805a45 | 137 | delete[] ap; |
emilija | 0:bfa464805a45 | 138 | return count; |
emilija | 0:bfa464805a45 | 139 | } |
emilija | 0:bfa464805a45 | 140 | |
emilija | 0:bfa464805a45 | 141 | void messageArrived(MQTT::MessageData& md) |
emilija | 1:e73b7979a1a3 | 142 | { char buf[100]; |
emilija | 0:bfa464805a45 | 143 | MQTTString &topic = md.topicName; |
emilija | 0:bfa464805a45 | 144 | string topic_name = topic.lenstring.data; |
emilija | 0:bfa464805a45 | 145 | printf("Topic name %d : %s\r\n",topic.lenstring.len,topic_name.c_str() ); |
emilija | 0:bfa464805a45 | 146 | MQTT::Message &message = md.message; |
emilija | 0:bfa464805a45 | 147 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
emilija | 0:bfa464805a45 | 148 | printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
emilija | 1:e73b7979a1a3 | 149 | string poruka = (char*)message.payload; |
emilija | 0:bfa464805a45 | 150 | if(topic_name.find("servo2")!=std::string::npos) { |
emilija | 1:e73b7979a1a3 | 151 | printf("AKTIVIRAN SERVO2\n"); |
emilija | 1:e73b7979a1a3 | 152 | T2.detach(); |
emilija | 0:bfa464805a45 | 153 | for(float p=0; p<1.0; p += 0.1) { |
emilija | 0:bfa464805a45 | 154 | servo2 = p; |
emilija | 0:bfa464805a45 | 155 | |
emilija | 0:bfa464805a45 | 156 | } |
emilija | 0:bfa464805a45 | 157 | wait(2); |
emilija | 0:bfa464805a45 | 158 | for(float p=1.0; p>0.0; p -= 0.1) { |
emilija | 0:bfa464805a45 | 159 | servo2 = p; |
emilija | 0:bfa464805a45 | 160 | |
emilija | 0:bfa464805a45 | 161 | } |
emilija | 1:e73b7979a1a3 | 162 | sprintf(buf, "servo2-AKTIVIRAN \r\n"); |
emilija | 1:e73b7979a1a3 | 163 | message.qos = MQTT::QOS0; |
emilija | 1:e73b7979a1a3 | 164 | message.retained = false; |
emilija | 1:e73b7979a1a3 | 165 | message.dup = false; |
emilija | 1:e73b7979a1a3 | 166 | message.payload = (void*)buf; |
emilija | 1:e73b7979a1a3 | 167 | message.payloadlen = strlen(buf)+1; |
emilija | 1:e73b7979a1a3 | 168 | client.publish(topic_pub2, message); |
emilija | 0:bfa464805a45 | 169 | } |
emilija | 1:e73b7979a1a3 | 170 | else if(topic_name.find("color2")!=std::string::npos){ |
emilija | 0:bfa464805a45 | 171 | salji_boju=1; |
emilija | 0:bfa464805a45 | 172 | } |
emilija | 1:e73b7979a1a3 | 173 | else if(topic_name.find("echo")!=std::string::npos){ |
emilija | 1:e73b7979a1a3 | 174 | if(poruka.find("connect")!=std::string::npos){ |
emilija | 1:e73b7979a1a3 | 175 | fleg_start=1; |
emilija | 1:e73b7979a1a3 | 176 | sprintf(buf, "echo3 \r\n"); |
emilija | 1:e73b7979a1a3 | 177 | message.qos = MQTT::QOS0; |
emilija | 1:e73b7979a1a3 | 178 | message.retained = false; |
emilija | 1:e73b7979a1a3 | 179 | message.dup = false; |
emilija | 1:e73b7979a1a3 | 180 | message.payload = (void*)buf; |
emilija | 1:e73b7979a1a3 | 181 | message.payloadlen = strlen(buf)+1; |
emilija | 1:e73b7979a1a3 | 182 | client.publish(topic_pub2, message); |
emilija | 1:e73b7979a1a3 | 183 | } |
emilija | 1:e73b7979a1a3 | 184 | else if(poruka.find("echo")!=std::string::npos){ |
emilija | 1:e73b7979a1a3 | 185 | T2.detach(); |
emilija | 1:e73b7979a1a3 | 186 | } |
emilija | 1:e73b7979a1a3 | 187 | } |
emilija | 0:bfa464805a45 | 188 | ++arrivedcount; |
emilija | 0:bfa464805a45 | 189 | } |
emilija | 0:bfa464805a45 | 190 | |
emilija | 0:bfa464805a45 | 191 | |
emilija | 0:bfa464805a45 | 192 | |
emilija | 0:bfa464805a45 | 193 | void buttonFunction() { |
emilija | 0:bfa464805a45 | 194 | button_pressed=1; |
emilija | 0:bfa464805a45 | 195 | } |
emilija | 0:bfa464805a45 | 196 | |
emilija | 0:bfa464805a45 | 197 | void rpr0521_print_one_value(){ |
emilija | 0:bfa464805a45 | 198 | bool error; |
emilija | 0:bfa464805a45 | 199 | uint16_t data[3]; |
emilija | 0:bfa464805a45 | 200 | |
emilija | 1:e73b7979a1a3 | 201 | error = rpr0521_read_data(&data[0],0); |
emilija | 0:bfa464805a45 | 202 | if (!error) { |
emilija | 0:bfa464805a45 | 203 | pc.printf("PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]); |
emilija | 0:bfa464805a45 | 204 | } |
emilija | 0:bfa464805a45 | 205 | else { |
emilija | 0:bfa464805a45 | 206 | pc.printf("\n\r"); |
emilija | 0:bfa464805a45 | 207 | } |
emilija | 0:bfa464805a45 | 208 | } |
emilija | 0:bfa464805a45 | 209 | |
emilija | 0:bfa464805a45 | 210 | int main() |
emilija | 0:bfa464805a45 | 211 | { bool error; |
emilija | 0:bfa464805a45 | 212 | uint16_t dataProx[3]; |
emilija | 0:bfa464805a45 | 213 | // Initialise the digital pin LED1 as an output |
emilija | 0:bfa464805a45 | 214 | colorLed=1; |
emilija | 0:bfa464805a45 | 215 | colorVout=1; |
emilija | 0:bfa464805a45 | 216 | //Servo1Napajanje=1; |
emilija | 0:bfa464805a45 | 217 | ColorSensor cs(PA_5,PA_6,PA_7,PB_6,PC_7); |
emilija | 0:bfa464805a45 | 218 | DigitalOut led(LED1); |
emilija | 0:bfa464805a45 | 219 | servo2.calibrate(0.0005,90); |
emilija | 0:bfa464805a45 | 220 | //servo1.calibrate(0.0005,180); |
emilija | 0:bfa464805a45 | 221 | button.rise(&buttonFunction); |
emilija | 0:bfa464805a45 | 222 | const char* hostname = "broker.mqttdashboard.com"; |
emilija | 0:bfa464805a45 | 223 | int port = 1883; |
emilija | 0:bfa464805a45 | 224 | |
emilija | 1:e73b7979a1a3 | 225 | pc.printf("\nRPR0521 library test program.\n\r"); |
emilija | 0:bfa464805a45 | 226 | I2CCommonBegin(); |
emilija | 1:e73b7979a1a3 | 227 | |
emilija | 1:e73b7979a1a3 | 228 | rpr0521_wait_until_found(0); |
emilija | 0:bfa464805a45 | 229 | pc.printf("\nSensor found.\n\r"); |
emilija | 1:e73b7979a1a3 | 230 | rpr0521_initial_setup(0); |
emilija | 1:e73b7979a1a3 | 231 | wait(1); |
emilija | 1:e73b7979a1a3 | 232 | |
emilija | 1:e73b7979a1a3 | 233 | rpr0521_wait_until_found(1); |
emilija | 1:e73b7979a1a3 | 234 | pc.printf("\nSensor found.\n\r"); |
emilija | 1:e73b7979a1a3 | 235 | rpr0521_initial_setup(1); |
emilija | 1:e73b7979a1a3 | 236 | wait(1); |
emilija | 1:e73b7979a1a3 | 237 | |
emilija | 0:bfa464805a45 | 238 | |
emilija | 0:bfa464805a45 | 239 | |
emilija | 0:bfa464805a45 | 240 | wifi = WiFiInterface::get_default_instance(); |
emilija | 0:bfa464805a45 | 241 | if (!wifi) { |
emilija | 0:bfa464805a45 | 242 | printf("ERROR: No WiFiInterface found.\n"); |
emilija | 0:bfa464805a45 | 243 | return -1; |
emilija | 0:bfa464805a45 | 244 | } |
emilija | 0:bfa464805a45 | 245 | |
emilija | 0:bfa464805a45 | 246 | int count = scan_demo(wifi); |
emilija | 0:bfa464805a45 | 247 | if (count == 0) { |
emilija | 0:bfa464805a45 | 248 | printf("No WIFI APs found - can't continue further.\n"); |
emilija | 0:bfa464805a45 | 249 | return -1; |
emilija | 0:bfa464805a45 | 250 | } |
emilija | 0:bfa464805a45 | 251 | |
emilija | 0:bfa464805a45 | 252 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
emilija | 0:bfa464805a45 | 253 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
emilija | 0:bfa464805a45 | 254 | if (ret != 0) { |
emilija | 0:bfa464805a45 | 255 | printf("\nConnection error: %d\n", ret); |
emilija | 0:bfa464805a45 | 256 | return -1; |
emilija | 0:bfa464805a45 | 257 | } |
emilija | 0:bfa464805a45 | 258 | |
emilija | 0:bfa464805a45 | 259 | printf("Success\n\n"); |
emilija | 0:bfa464805a45 | 260 | printf("MAC: %s\n", wifi->get_mac_address()); |
emilija | 0:bfa464805a45 | 261 | printf("IP: %s\n", wifi->get_ip_address()); |
emilija | 0:bfa464805a45 | 262 | printf("Netmask: %s\n", wifi->get_netmask()); |
emilija | 0:bfa464805a45 | 263 | printf("Gateway: %s\n", wifi->get_gateway()); |
emilija | 0:bfa464805a45 | 264 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
emilija | 0:bfa464805a45 | 265 | |
emilija | 0:bfa464805a45 | 266 | socket.open(wifi); |
emilija | 0:bfa464805a45 | 267 | socket.connect(hostname, port); |
emilija | 0:bfa464805a45 | 268 | |
emilija | 0:bfa464805a45 | 269 | int rc=0; |
emilija | 0:bfa464805a45 | 270 | |
emilija | 0:bfa464805a45 | 271 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
emilija | 0:bfa464805a45 | 272 | data.MQTTVersion = 3; |
emilija | 0:bfa464805a45 | 273 | data.clientID.cstring = "PMK-client3"; |
emilija | 0:bfa464805a45 | 274 | //data.username.cstring = "testuser"; |
emilija | 0:bfa464805a45 | 275 | //data.password.cstring = "testpassword"; |
emilija | 0:bfa464805a45 | 276 | |
emilija | 0:bfa464805a45 | 277 | if ((rc = client.connect(data)) != 0) |
emilija | 0:bfa464805a45 | 278 | printf("rc from MQTT connect is %d\r\n", rc); |
emilija | 0:bfa464805a45 | 279 | |
emilija | 0:bfa464805a45 | 280 | |
emilija | 0:bfa464805a45 | 281 | if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) |
emilija | 0:bfa464805a45 | 282 | printf("rc from MQTT subscribe is %d\r\n", rc); |
emilija | 0:bfa464805a45 | 283 | |
emilija | 0:bfa464805a45 | 284 | |
emilija | 0:bfa464805a45 | 285 | |
emilija | 0:bfa464805a45 | 286 | while (true) { |
emilija | 1:e73b7979a1a3 | 287 | if(fleg_start==1){ |
emilija | 0:bfa464805a45 | 288 | led = !led; |
emilija | 0:bfa464805a45 | 289 | thread_sleep_for(BLINKING_RATE_MS); |
emilija | 0:bfa464805a45 | 290 | if (salji_boju==1) { |
emilija | 1:e73b7979a1a3 | 291 | T2.detach(); |
emilija | 0:bfa464805a45 | 292 | salji_boju=0; |
emilija | 0:bfa464805a45 | 293 | printf("Publishing data\r\n"); |
emilija | 0:bfa464805a45 | 294 | // QoS 0 |
emilija | 0:bfa464805a45 | 295 | char buf[100]; |
emilija | 0:bfa464805a45 | 296 | float treshold=0.1; |
emilija | 0:bfa464805a45 | 297 | |
emilija | 0:bfa464805a45 | 298 | int Boja[3]={cs.getRed(), cs.getGreen(),cs.getBlue()}; |
emilija | 0:bfa464805a45 | 299 | |
emilija | 0:bfa464805a45 | 300 | while(Boja[0]<30 && Boja[1]<30 && Boja[2]<30){ |
emilija | 0:bfa464805a45 | 301 | wait(1); |
emilija | 0:bfa464805a45 | 302 | Boja[0]=cs.getRed(); |
emilija | 0:bfa464805a45 | 303 | Boja[1]=cs.getGreen(); |
emilija | 0:bfa464805a45 | 304 | Boja[2]=cs.getBlue(); |
emilija | 0:bfa464805a45 | 305 | printf("LOSE MERENJE \r\n"); |
emilija | 0:bfa464805a45 | 306 | printf("R:%d , G:%d , B:%d",Boja[0],Boja[1],Boja[2]); |
emilija | 0:bfa464805a45 | 307 | } |
emilija | 0:bfa464805a45 | 308 | int strongCol=max(Boja[0],max(Boja[1],Boja[2])); |
emilija | 0:bfa464805a45 | 309 | float BojaScal[3]; |
emilija | 0:bfa464805a45 | 310 | BojaScal[0]=Boja[0]/(strongCol*1.0); |
emilija | 0:bfa464805a45 | 311 | BojaScal[1]=Boja[1]/(strongCol*1.0); |
emilija | 0:bfa464805a45 | 312 | BojaScal[2]=Boja[2]/(strongCol*1.0); |
emilija | 0:bfa464805a45 | 313 | |
emilija | 0:bfa464805a45 | 314 | if(BojaScal[1]<r1[1]+treshold && BojaScal[1]>r1[1]-treshold && BojaScal[2]<r1[2]+treshold && BojaScal[0]>r1[0]-treshold && BojaScal[0]<r1[0]+treshold && BojaScal[2]>r1[2]-treshold ) { |
emilija | 1:e73b7979a1a3 | 315 | sprintf(buf, "Nije \r\n");} |
emilija | 0:bfa464805a45 | 316 | else if(BojaScal[1]<g1[1]+treshold && BojaScal[1]>g1[1]-treshold && BojaScal[2]<g1[2]+treshold && BojaScal[0]>g1[0]-treshold && BojaScal[0]<g1[0]+treshold && BojaScal[2]>g1[2]-treshold ) { |
emilija | 1:e73b7979a1a3 | 317 | sprintf(buf, "Jeste \r\n");} |
emilija | 0:bfa464805a45 | 318 | else if(BojaScal[1]<b1[1]+treshold && BojaScal[1]>b1[1]-treshold && BojaScal[2]<b1[2]+treshold && BojaScal[0]>b1[0]-treshold && BojaScal[0]<b1[0]+treshold && BojaScal[2]>b1[2]-treshold ) { |
emilija | 1:e73b7979a1a3 | 319 | sprintf(buf, "Nije \r\n");} |
emilija | 1:e73b7979a1a3 | 320 | else { sprintf(buf, "Nije \r\n");} |
emilija | 0:bfa464805a45 | 321 | |
emilija | 0:bfa464805a45 | 322 | printf("R: %.3f", BojaScal[0]); |
emilija | 0:bfa464805a45 | 323 | printf("G: %.3f", BojaScal[1]); |
emilija | 0:bfa464805a45 | 324 | printf("B: %.3f\r\n", BojaScal[2]); |
emilija | 0:bfa464805a45 | 325 | |
emilija | 0:bfa464805a45 | 326 | printf(" Crvena R: %.3f", r1[0]); |
emilija | 0:bfa464805a45 | 327 | printf("G: %.3f", r1[1]); |
emilija | 0:bfa464805a45 | 328 | printf("B: %.3f\r\n", r1[2]); |
emilija | 0:bfa464805a45 | 329 | |
emilija | 0:bfa464805a45 | 330 | //sprintf(buf, "RGB: %7s C\r\n", print_double(buffer, cs.getRed()*100000000+cs.getGreen()*10000+cs.getBlue())); |
emilija | 0:bfa464805a45 | 331 | |
emilija | 0:bfa464805a45 | 332 | message.qos = MQTT::QOS0; |
emilija | 0:bfa464805a45 | 333 | message.retained = false; |
emilija | 0:bfa464805a45 | 334 | message.dup = false; |
emilija | 0:bfa464805a45 | 335 | message.payload = (void*)buf; |
emilija | 0:bfa464805a45 | 336 | message.payloadlen = strlen(buf)+1; |
emilija | 0:bfa464805a45 | 337 | client.publish(topic_pub3, message); |
emilija | 1:e73b7979a1a3 | 338 | T2.attach(&interruptT2,5); |
emilija | 0:bfa464805a45 | 339 | } |
emilija | 0:bfa464805a45 | 340 | |
emilija | 0:bfa464805a45 | 341 | |
emilija | 0:bfa464805a45 | 342 | //printf("Yielding"); |
emilija | 0:bfa464805a45 | 343 | client.yield(1000); |
emilija | 0:bfa464805a45 | 344 | //printf(" -> Yielded\r\n"); |
emilija | 0:bfa464805a45 | 345 | //} |
emilija | 0:bfa464805a45 | 346 | |
emilija | 1:e73b7979a1a3 | 347 | error = rpr0521_read_data(&dataProx[0],0); |
emilija | 0:bfa464805a45 | 348 | if (!error){ |
emilija | 1:e73b7979a1a3 | 349 | if(dataProx[0] > 50 && !fleg_proxy1) { |
emilija | 1:e73b7979a1a3 | 350 | fleg_proxy1=1; |
emilija | 0:bfa464805a45 | 351 | printf("Pristigao je cep na lokaciju O.O O:'( \r\n "); |
emilija | 0:bfa464805a45 | 352 | char buf[100]; |
emilija | 1:e73b7979a1a3 | 353 | sprintf(buf, "proxy1-AKTIV PS = %d",dataProx[0]); |
emilija | 0:bfa464805a45 | 354 | message.qos = MQTT::QOS0; |
emilija | 0:bfa464805a45 | 355 | message.retained = false; |
emilija | 0:bfa464805a45 | 356 | message.dup = false; |
emilija | 0:bfa464805a45 | 357 | message.payload = (void*)buf; |
emilija | 0:bfa464805a45 | 358 | message.payloadlen = strlen(buf)+1; |
emilija | 1:e73b7979a1a3 | 359 | client.publish(topic_pub1, message); |
emilija | 1:e73b7979a1a3 | 360 | T2.attach(&interruptT2,5); |
emilija | 0:bfa464805a45 | 361 | |
emilija | 0:bfa464805a45 | 362 | } |
emilija | 1:e73b7979a1a3 | 363 | else if(dataProx[0]<50){fleg_proxy1=0;} |
emilija | 0:bfa464805a45 | 364 | client.yield(1000); |
emilija | 0:bfa464805a45 | 365 | } |
emilija | 1:e73b7979a1a3 | 366 | error = rpr0521_read_data(&dataProx[0],1); |
emilija | 1:e73b7979a1a3 | 367 | if (!error){ |
emilija | 1:e73b7979a1a3 | 368 | if(dataProx[0] > 50 && !fleg_proxy2) { |
emilija | 1:e73b7979a1a3 | 369 | fleg_proxy2=1; |
emilija | 1:e73b7979a1a3 | 370 | printf("Pristigao je cep na lokaciju O.O O:'( \r\n "); |
emilija | 1:e73b7979a1a3 | 371 | char buf[100]; |
emilija | 1:e73b7979a1a3 | 372 | sprintf(buf, "proxy1-AKTIV PS = %d",dataProx[0]); |
emilija | 1:e73b7979a1a3 | 373 | message.qos = MQTT::QOS0; |
emilija | 1:e73b7979a1a3 | 374 | message.retained = false; |
emilija | 1:e73b7979a1a3 | 375 | message.dup = false; |
emilija | 1:e73b7979a1a3 | 376 | message.payload = (void*)buf; |
emilija | 1:e73b7979a1a3 | 377 | message.payloadlen = strlen(buf)+1; |
emilija | 1:e73b7979a1a3 | 378 | client.publish(topic_pub4, message); |
emilija | 1:e73b7979a1a3 | 379 | T2.attach(&interruptT2,5); |
emilija | 1:e73b7979a1a3 | 380 | |
emilija | 1:e73b7979a1a3 | 381 | } |
emilija | 1:e73b7979a1a3 | 382 | else if(dataProx[0]<50){fleg_proxy2=0;} |
emilija | 1:e73b7979a1a3 | 383 | client.yield(1000); |
emilija | 1:e73b7979a1a3 | 384 | } |
emilija | 1:e73b7979a1a3 | 385 | |
emilija | 0:bfa464805a45 | 386 | |
emilija | 0:bfa464805a45 | 387 | } |
emilija | 1:e73b7979a1a3 | 388 | } |
emilija | 0:bfa464805a45 | 389 | } |