PMKIND

Dependencies:   Servo TextLCD

Revision:
2:5918b8bad3a7
Parent:
1:d3bb978c1ff7
--- a/main.cpp	Wed Jun 30 20:50:16 2021 +0000
+++ b/main.cpp	Mon Jul 05 12:56:18 2021 +0000
@@ -14,24 +14,37 @@
 // Blinking rate in milliseconds
 #define BLINKING_RATE_MS                                                    500
 
+// varijable za boju
+
 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;
+Serial pc(USBTX,USBRX);
+// varijable za servo
 Servo servo0(D3);
 Servo servo1(D6);
-DigitalOut colorLed(D4);
 DigitalOut Servo1Napajanje(D7);
-DigitalOut colorVout(D5);
+
 InterruptIn button(USER_BUTTON);
 int arrivedcount = 0;
 TCPSocket socket;
 MQTTClient client(&socket);
 MQTT::Message message;
 int button_pressed=0;
-char* topic_pub = "PMK_industrija/micro1/color";
+
+//Timeout T1;
+//void interruptT1(){
+  //  printf("error1");
+   // }
+
+char* topic_pub = "PMK_industrija/micro1/color1";
+char* topic_pub1 = "PMK_industrija/micro1/echo/2";
 char* topic_sub = "PMK_industrija/micro2/#";
-int salji_boju=0;
+
 
 WiFiInterface *wifi;
 volatile int mems_event = 0;
@@ -40,6 +53,7 @@
 uint8_t high = 0, low = 0;
 float temperature = 0.0f;
 char buffer[32];
+
 static char *print_double(char *str, double v, int decimalDigits = 2)
 {
     int i = 1;
@@ -122,67 +136,101 @@
 }
 
 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;
-    if(topic_name.find("servo0")!=std::string::npos) {
-         printf("AKTIVIRAN SERVO0\n"); 
-         for(float p=0; p<1.0; p += 0.1) {
-        servo0 = p;
+    string poruka = (char*)message.payload;
+    
+    //if(topic_name.find("servo0")!=std::string::npos) {
+      //   printf("AKTIVIRAN SERVO0\n"); 
+        // for(float p=0; p<1.0; p += 0.1) {
+       // servo0 = p;
         
-        }
-        wait(2);
-        for(float p=1.0; p>0.0; p -= 0.1) {
-        servo0 = p;
+       // }
+       // wait(2);
+       // for(float p=1.0; p>0.0; p -= 0.1) {
+       // servo0 = p;
+        
+        //}
         
-        }
-    }
-    else if(topic_name.find("proxy1")!=std::string::npos){
-    salji_boju=1;
-    }
-    else if(topic_name.find("servo1")!=std::string::npos){
-        printf("AKTIVIRAN SERVO1\n"); 
-         for(float p=0; p<1.0; p += 0.1) {
-        servo1 = p;
+        //sprintf(buf, "servo0-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_pub1, message); 
+    //}
+    //else if(topic_name.find("color1")!=std::string::npos){
+    //salji_boju=1;
+    //}
+    //else if(topic_name.find("servo1")!=std::string::npos){
+     //   T1.detach();
+      //  printf("AKTIVIRAN SERVO1\n"); 
+        // for(float p=0; p<1.0; p += 0.1) {
+        //servo1 = p;
+      // da li na 45        
+        //}
+        //wait(2);
+        //for(float p=1.0; p>0.0; p -= 0.1) {
+        //servo1 = p;
+        
+        //}  
         
-        }
-        wait(2);
-        for(float p=1.0; p>0.0; p -= 0.1) {
-        servo1 = p;
-        
-        }
+          //  sprintf(buf, "servo1-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_pub1, message); 
+
         
-        
-        
-        }
+        //}
+        //else 
+        if(topic_name.find("echo")!=std::string::npos){
+            if(poruka.find("connect")!=std::string::npos){
+            fleg_start=1;
+            sprintf(buf, "echo2 \r\n");
+            message.qos = MQTT::QOS0;
+            message.retained = false;
+            message.dup = false;
+            message.payload = (void*)buf;
+            message.payloadlen = strlen(buf)+1;
+            client.publish(topic_pub1, message); 
+            }
+            else if(poruka.find("echo")!=std::string::npos){
+       //         T1.detach();
+            }
+            }
     ++arrivedcount;
+
 }
 
 
-
 void buttonFunction() {    
     button_pressed=1;   
 }
 
 int main()
-{
+{   
     // Initialise the digital pin LED1 as an output
     colorLed=1;
     colorVout=1;
-    Servo1Napajanje=1;
+    
     ColorSensor cs(PA_5,PA_6,PA_7,PB_6,PC_7);
     DigitalOut led(LED1);
+    Servo1Napajanje=1;
     servo0.calibrate(0.0005,90);
     servo1.calibrate(0.0005,180);
     button.rise(&buttonFunction);
+    
     const char* hostname = "broker.mqttdashboard.com";
     int port = 1883;    
-    
     wifi = WiFiInterface::get_default_instance();
     if (!wifi) {
         printf("ERROR: No WiFiInterface found.\n");
@@ -228,16 +276,18 @@
         printf("rc from MQTT subscribe is %d\r\n", rc);    
     
     
-    
+     char buf[100];
+            float treshold=0.1;  
     while (true) {
+        client.yield(1000);
+        if (fleg_start==1){
         led = !led;
         thread_sleep_for(BLINKING_RATE_MS);
         if (salji_boju==1) {
             salji_boju=0;    
             printf("Publishing data\r\n");    
             // QoS 0
-            char buf[100];
-            float treshold=0.1;
+           
             
             int Boja[3]={cs.getRed(), cs.getGreen(),cs.getBlue()};
             
@@ -256,12 +306,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, "Jeste \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, "Nije \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]);
@@ -279,10 +329,12 @@
             message.payload = (void*)buf;
             message.payloadlen = strlen(buf)+1;
             client.publish(topic_pub, message);
-            
+         //   T1.attach(&interruptT1,5);
         }
         //printf("Yielding"); 
         client.yield(1000);
         //printf(" -> Yielded\r\n"); 
     }
+    }
+    
 }