PMK_industrija_mikro3
Dependencies: proxy2 proxy2123 Servo
Diff: main.cpp
- Revision:
- 1:e73b7979a1a3
- Parent:
- 0:bfa464805a45
--- a/main.cpp Wed Jun 30 20:52:13 2021 +0000 +++ b/main.cpp Mon Jul 05 12:57:13 2021 +0000 @@ -20,15 +20,23 @@ // Blinking rate in milliseconds #define BLINKING_RATE_MS 500 Serial pc(USBTX,USBRX); + +Timeout T2; +void interruptT2(){ + printf("error2"); + } + +//varijable boje float r1[3]={1 ,109.0/399.0,133.0/399.0}; float g1[3]={206.0/411.0,399.0/411.0,1}; float b1[3]={120.0/490.0 ,209.0/490.0, 1}; - +DigitalOut colorLed(D4); +DigitalOut colorVout(D5); +int salji_boju=0; +int fleg_start=0; +//varijable za servo Servo servo2(D3); - -DigitalOut colorLed(D4); - -DigitalOut colorVout(D5); +int fleg_proxy1=0; InterruptIn button(USER_BUTTON); int arrivedcount = 0; TCPSocket socket; @@ -36,10 +44,11 @@ MQTT::Message message; int button_pressed=0; char* topic_pub1 = "PMK_industrija/micro1/proxy1"; - +char* topic_pub4 = "PMK_industrija/micro1/proxy2"; char* topic_pub3 = "PMK_industrija/micro1/color2"; +char* topic_pub2 = "PMK_industrija/micro1/echo/3"; char* topic_sub = "PMK_industrija/micro3/#"; -int salji_boju=0; +int fleg_proxy2=0; WiFiInterface *wifi; volatile int mems_event = 0; @@ -130,16 +139,17 @@ } void messageArrived(MQTT::MessageData& md) -{ +{ char buf[100]; MQTTString &topic = md.topicName; string topic_name = topic.lenstring.data; printf("Topic name %d : %s\r\n",topic.lenstring.len,topic_name.c_str() ); MQTT::Message &message = md.message; printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); - char* poruka = (char*)message.payload; + string poruka = (char*)message.payload; if(topic_name.find("servo2")!=std::string::npos) { - printf("AKTIVIRAN SERVO0\n"); + printf("AKTIVIRAN SERVO2\n"); + T2.detach(); for(float p=0; p<1.0; p += 0.1) { servo2 = p; @@ -149,11 +159,32 @@ servo2 = p; } + sprintf(buf, "servo2-AKTIVIRAN \r\n"); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub2, message); } - else if(topic_name.find("color3fleg")!=std::string::npos){ + else if(topic_name.find("color2")!=std::string::npos){ salji_boju=1; } - + else if(topic_name.find("echo")!=std::string::npos){ + if(poruka.find("connect")!=std::string::npos){ + fleg_start=1; + sprintf(buf, "echo3 \r\n"); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub2, message); + } + else if(poruka.find("echo")!=std::string::npos){ + T2.detach(); + } + } ++arrivedcount; } @@ -167,7 +198,7 @@ bool error; uint16_t data[3]; - error = rpr0521_read_data(&data[0]); + error = rpr0521_read_data(&data[0],0); if (!error) { pc.printf("PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]); } @@ -191,10 +222,19 @@ const char* hostname = "broker.mqttdashboard.com"; int port = 1883; + pc.printf("\nRPR0521 library test program.\n\r"); I2CCommonBegin(); - rpr0521_wait_until_found(); + + rpr0521_wait_until_found(0); pc.printf("\nSensor found.\n\r"); - rpr0521_initial_setup(); + rpr0521_initial_setup(0); + wait(1); + + rpr0521_wait_until_found(1); + pc.printf("\nSensor found.\n\r"); + rpr0521_initial_setup(1); + wait(1); + wifi = WiFiInterface::get_default_instance(); @@ -244,9 +284,11 @@ while (true) { + if(fleg_start==1){ led = !led; thread_sleep_for(BLINKING_RATE_MS); if (salji_boju==1) { + T2.detach(); salji_boju=0; printf("Publishing data\r\n"); // QoS 0 @@ -270,12 +312,12 @@ BojaScal[2]=Boja[2]/(strongCol*1.0); 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 ) { - sprintf(buf, "RGB: CRVENA \r\n");} + sprintf(buf, "Nije \r\n");} 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 ) { - sprintf(buf, "RGB: ZELENA \r\n");} + sprintf(buf, "Jeste \r\n");} 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 ) { - sprintf(buf, "RGB: PLAVA \r\n");} - else { sprintf(buf, "RGB: NEKA DRUGA BOJA \r\n");} + sprintf(buf, "Nije \r\n");} + else { sprintf(buf, "Nije \r\n");} printf("R: %.3f", BojaScal[0]); printf("G: %.3f", BojaScal[1]); @@ -293,7 +335,7 @@ message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; client.publish(topic_pub3, message); - + T2.attach(&interruptT2,5); } @@ -302,23 +344,46 @@ //printf(" -> Yielded\r\n"); //} - error = rpr0521_read_data(&dataProx[0]); + error = rpr0521_read_data(&dataProx[0],0); if (!error){ - if(dataProx[0] > 50) { + if(dataProx[0] > 50 && !fleg_proxy1) { + fleg_proxy1=1; printf("Pristigao je cep na lokaciju O.O O:'( \r\n "); char buf[100]; - sprintf(buf, "Cep je pristigao na proxy1 PS = %d",dataProx[0]); + sprintf(buf, "proxy1-AKTIV PS = %d",dataProx[0]); message.qos = MQTT::QOS0; message.retained = false; message.dup = false; message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; - client.publish(topic_pub1, message); + client.publish(topic_pub1, message); + T2.attach(&interruptT2,5); } + else if(dataProx[0]<50){fleg_proxy1=0;} client.yield(1000); } + error = rpr0521_read_data(&dataProx[0],1); + if (!error){ + if(dataProx[0] > 50 && !fleg_proxy2) { + fleg_proxy2=1; + printf("Pristigao je cep na lokaciju O.O O:'( \r\n "); + char buf[100]; + sprintf(buf, "proxy1-AKTIV PS = %d",dataProx[0]); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub4, message); + T2.attach(&interruptT2,5); + + } + else if(dataProx[0]<50){fleg_proxy2=0;} + client.yield(1000); + } + } - + } } \ No newline at end of file