Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: rohm-rpr0521 rohm-sensor-hal Servo TextLCD
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 */ 00005 #include "Servo.h" 00006 #include "color.h" 00007 #include "mbed.h" 00008 #include "platform/mbed_thread.h" 00009 #include <string> 00010 #include <MQTTClientMbedOs.h> 00011 #include "TextLCD.h" 00012 #include "rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //mbed.h, types, DEBUG_print* 00013 #include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" 00014 00015 #include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h" 00016 #include "rohm-rpr0521/rohm-rpr0521/rpr0521.h" 00017 00018 // Blinking rate in milliseconds 00019 #define BLINKING_RATE_MS 00020 500 00021 int salji_boju=0; 00022 Serial pc(USBTX,USBRX); 00023 InterruptIn button(USER_BUTTON); 00024 int arrivedcount = 0; 00025 TCPSocket socket; 00026 MQTTClient client(&socket); 00027 MQTT::Message message; 00028 int button_pressed=0; 00029 00030 Timeout T3; 00031 00032 char* topic_sub = "PMK_industrija/micro4/#"; 00033 char* topic_pub = "PMK_industrija/micro1/proxy3"; 00034 char* topic_pub2 = "PMK_industrija/micro1/color3"; 00035 char* topic_pub1 = "PMK_industrija/micro1/echo/4"; 00036 Servo servo3(D7); 00037 00038 00039 WiFiInterface *wifi; 00040 00041 TextLCD lcd(D3,D4,D13,D12,D11,D10); 00042 DigitalOut vdd(A5); 00043 00044 volatile int mems_event = 0; 00045 uint32_t previous_tick = 0; 00046 uint32_t current_tick = 0; 00047 uint8_t high = 0, low = 0; 00048 float temperature = 0.0f; 00049 char buffer[32]; 00050 00051 static char *print_double(char *str, double v, int decimalDigits = 2) 00052 { 00053 int i = 1; 00054 int intPart, fractPart; 00055 int len; 00056 char *ptr; 00057 00058 /* prepare decimal digits multiplicator */ 00059 for (; decimalDigits != 0; i *= 10, decimalDigits--); 00060 00061 /* calculate integer & fractinal parts */ 00062 intPart = (int)v; 00063 fractPart = (int)((v - (double)(int)v) * i); 00064 00065 /* fill in integer part */ 00066 sprintf(str, "%i.", intPart); 00067 00068 /* prepare fill in of fractional part */ 00069 len = strlen(str); 00070 ptr = &str[len]; 00071 00072 /* fill in leading fractional zeros */ 00073 for (i /= 10; i > 1; i /= 10, ptr++) { 00074 if (fractPart >= i) { 00075 break; 00076 } 00077 *ptr = '0'; 00078 } 00079 00080 /* fill in (rest of) fractional part */ 00081 sprintf(ptr, "%i", fractPart); 00082 00083 return str; 00084 } 00085 00086 const char *sec2str(nsapi_security_t sec) 00087 { 00088 switch (sec) { 00089 case NSAPI_SECURITY_NONE: 00090 return "None"; 00091 case NSAPI_SECURITY_WEP: 00092 return "WEP"; 00093 case NSAPI_SECURITY_WPA: 00094 return "WPA"; 00095 case NSAPI_SECURITY_WPA2: 00096 return "WPA2"; 00097 case NSAPI_SECURITY_WPA_WPA2: 00098 return "WPA/WPA2"; 00099 case NSAPI_SECURITY_UNKNOWN: 00100 default: 00101 return "Unknown"; 00102 } 00103 } 00104 00105 int scan_demo(WiFiInterface *wifi) 00106 { 00107 WiFiAccessPoint *ap; 00108 printf("Scan:\n"); 00109 int count = wifi->scan(NULL,0); 00110 if (count <= 0) { 00111 printf("scan() failed with return value: %d\n", count); 00112 return 0; 00113 } 00114 /* Limit number of network arbitrary to 15 */ 00115 count = count < 15 ? count : 15; 00116 ap = new WiFiAccessPoint[count]; 00117 count = wifi->scan(ap, count); 00118 if (count <= 0) { 00119 printf("scan() failed with return value: %d\n", count); 00120 return 0; 00121 } 00122 for (int i = 0; i < count; i++) { 00123 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), 00124 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], 00125 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); 00126 } 00127 printf("%d networks available.\n", count); 00128 delete[] ap; 00129 return count; 00130 } 00131 00132 void messageArrived(MQTT::MessageData& md) 00133 { 00134 MQTTString &topic = md.topicName; 00135 string topic_name = topic.lenstring.data; 00136 printf("Topic name %d : %s\r\n",topic.lenstring.len,topic_name.c_str() ); 00137 MQTT::Message &message = md.message; 00138 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00139 printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00140 char* poruka = (char*)message.payload; 00141 if(topic_name.find("servo3")!=std::string::npos) { 00142 T3.detach(); 00143 printf("AKTIVIRAN SERVO3\n"); 00144 for(float p=0; p<1.0; p += 0.1) { 00145 servo3 = p; 00146 00147 } 00148 wait(2); 00149 for(float p=1.0; p>0.0; p -= 0.1) { 00150 servo3 = p; 00151 00152 } 00153 sprintf(buf, "servo3-AKTIVIRAN \r\n"); 00154 message.qos = MQTT::QOS0; 00155 message.retained = false; 00156 message.dup = false; 00157 message.payload = (void*)buf; 00158 message.payloadlen = strlen(buf)+1; 00159 client.publish(topic_pub1, message); 00160 } 00161 else if(topic_name.find("color3")!=std::string::npos){ 00162 salji_boju=1; 00163 } 00164 00165 00166 else if(topic_name.find("LCD")!=std::string::npos){ 00167 00168 lcd.cls(); 00169 lcd.printf("%s ",poruka); 00170 00171 00172 } 00173 else if(topic_name.find("echo")!=std::string::npos){ 00174 if(poruka.find("connect")!=std::string::npos){ 00175 fleg_start=1; 00176 sprintf(buf, "echo4 \r\n"); 00177 message.qos = MQTT::QOS0; 00178 message.retained = false; 00179 message.dup = false; 00180 message.payload = (void*)buf; 00181 message.payloadlen = strlen(buf)+1; 00182 client.publish(topic_pub1, message); 00183 } 00184 else if(poruka.find("echo")!=std::string::npos){ 00185 T3.detach(); 00186 } 00187 } 00188 00189 ++arrivedcount; 00190 } 00191 00192 00193 void buttonFunction() { 00194 button_pressed=1; 00195 } 00196 00197 void rpr0521_print_one_value(){ 00198 bool error; 00199 uint16_t data[3]; 00200 00201 error = rpr0521_read_data(&data[0]); 00202 if (!error) { 00203 printf("PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]); 00204 } 00205 else { 00206 printf("\n\r"); 00207 } 00208 } 00209 int main() 00210 { vdd=1; 00211 // Initialise the digital pin LED1 as an output 00212 bool error; 00213 //ColorSensor cs(PA_5,PA_6,PA_7,PB_6,PC_7); 00214 uint16_t dataProx[3]; 00215 DigitalOut led(LED1); 00216 button.rise(&buttonFunction); 00217 const char* hostname = "broker.mqttdashboard.com"; 00218 int port = 1883; 00219 00220 I2CCommonBegin(); 00221 rpr0521_wait_until_found(); 00222 pc.printf("\nSensor found.\n\r"); 00223 rpr0521_initial_setup(); 00224 00225 wifi = WiFiInterface::get_default_instance(); 00226 if (!wifi) { 00227 printf("ERROR: No WiFiInterface found.\n"); 00228 return -1; 00229 } 00230 00231 int count = scan_demo(wifi); 00232 if (count == 0) { 00233 printf("No WIFI APs found - can't continue further.\n"); 00234 return -1; 00235 } 00236 00237 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00238 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00239 if (ret != 0) { 00240 printf("\nConnection error: %d\n", ret); 00241 return -1; 00242 } 00243 00244 printf("Success\n\n"); 00245 printf("MAC: %s\n", wifi->get_mac_address()); 00246 printf("IP: %s\n", wifi->get_ip_address()); 00247 printf("Netmask: %s\n", wifi->get_netmask()); 00248 printf("Gateway: %s\n", wifi->get_gateway()); 00249 printf("RSSI: %d\n\n", wifi->get_rssi()); 00250 00251 socket.open(wifi); 00252 socket.connect(hostname, port); 00253 00254 int rc=0; 00255 00256 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00257 data.MQTTVersion = 3; 00258 data.clientID.cstring = "PMK-client4"; 00259 //data.username.cstring = "testuser"; 00260 //data.password.cstring = "testpassword"; 00261 00262 if ((rc = client.connect(data)) != 0) 00263 printf("rc from MQTT connect is %d\r\n", rc); 00264 00265 00266 if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) 00267 printf("rc from MQTT subscribe is %d\r\n", rc); 00268 00269 00270 00271 while (true) { 00272 led = !led; 00273 thread_sleep_for(BLINKING_RATE_MS); 00274 00275 00276 error = rpr0521_read_data(&dataProx[0]); 00277 if (!error){ 00278 T3.attach(&interuptT3,5); 00279 if(dataProx[0] > 50 && !fleg_proxy) { 00280 fleg_proxy1=1; 00281 printf("Pristigao je cep na lokaciju O.O O:'( \r\n "); 00282 char buf[100]; 00283 sprintf(buf, "proxy3-AKTIV PS = %d",dataProx[0]); 00284 message.qos = MQTT::QOS0; 00285 message.retained = false; 00286 message.dup = false; 00287 message.payload = (void*)buf; 00288 message.payloadlen = strlen(buf)+1; 00289 client.publish(topic_pub, message); 00290 //salji_boju=1; 00291 } 00292 else{fleg_proxy=0;} 00293 00294 } 00295 if (salji_boju==1) { 00296 T3.detach(); 00297 //salji_boju=0; 00298 //printf("Publishing data\r\n"); 00299 // QoS 0 00300 //char buf[100]; 00301 //float treshold=0.1; 00302 00303 //int Boja[3]={cs.getRed(), cs.getGreen(),cs.getBlue()}; 00304 00305 //while(Boja[0]<30 && Boja[1]<30 && Boja[2]<30){ 00306 //wait(1); 00307 //Boja[0]=cs.getRed(); 00308 //Boja[1]=cs.getGreen(); 00309 //Boja[2]=cs.getBlue(); 00310 //printf("LOSE MERENJE \r\n"); 00311 //printf("R:%d , G:%d , B:%d",Boja[0],Boja[1],Boja[2]); 00312 // } 00313 //int strongCol=max(Boja[0],max(Boja[1],Boja[2])); 00314 //float BojaScal[3]; 00315 //BojaScal[0]=Boja[0]/(strongCol*1.0); 00316 //BojaScal[1]=Boja[1]/(strongCol*1.0); 00317 //BojaScal[2]=Boja[2]/(strongCol*1.0); 00318 00319 //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 ) { 00320 // sprintf(buf, "RGB: CRVENA \r\n");} 00321 //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 ) { 00322 //sprintf(buf, "RGB: ZELENA \r\n");} 00323 //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 ) { 00324 //sprintf(buf, "RGB: PLAVA \r\n");} 00325 // else { sprintf(buf, "RGB: NEKA DRUGA BOJA \r\n");} 00326 00327 //printf("R: %.3f", BojaScal[0]); 00328 //printf("G: %.3f", BojaScal[1]); 00329 // printf("B: %.3f\r\n", BojaScal[2]); 00330 00331 //printf(" Crvena R: %.3f", r1[0]); 00332 //printf("G: %.3f", r1[1]); 00333 //printf("B: %.3f\r\n", r1[2]); 00334 00335 00336 //message.qos = MQTT::QOS0; 00337 //message.retained = false; 00338 //message.dup = false; 00339 //message.payload = (void*)buf; 00340 //message.payloadlen = strlen(buf)+1; 00341 //client.publish(topic_pub, message); 00342 T3.attach(&interruptT3,5); 00343 salji_boju=0; 00344 } 00345 00346 00347 if (button_pressed==1) { 00348 button_pressed=0; 00349 printf("Publishing data\r\n"); 00350 // QoS 0 00351 char buf[100]; 00352 00353 //sprintf(buf, "RGB: %7s C\r\n", print_double(buffer, cs.getRed()*100000000+cs.getGreen()*10000+cs.getBlue())); 00354 00355 //message.qos = MQTT::QOS0; 00356 //message.retained = false; 00357 //message.dup = false; 00358 //message.payload = (void*)buf; 00359 //message.payloadlen = strlen(buf)+1; 00360 //client.publish(topic_pub, message); 00361 00362 } 00363 // printf("Yielding"); 00364 client.yield(1000); 00365 //printf(" -> Yielded\r\n"); 00366 } 00367 }
Generated on Mon Aug 8 2022 17:45:34 by
1.7.2