PMKIND
Dependencies: rohm-rpr0521 rohm-sensor-hal Servo TextLCD
Revision 1:7b52bdcdac4e, committed 2021-06-27
- Comitter:
- pavledjo
- Date:
- Sun Jun 27 17:38:31 2021 +0000
- Parent:
- 0:51001d8fdeff
- Commit message:
- PMK_m1;
Changed in this revision
--- a/main.cpp Thu Jun 24 15:11:30 2021 +0000 +++ b/main.cpp Sun Jun 27 17:38:31 2021 +0000 @@ -5,32 +5,46 @@ #include "mbed.h" #include "platform/mbed_thread.h" -#include "Servo.h" -#include "color.h" #include <string> #include <MQTTClientMbedOs.h> +#include "TextLCD.h" +#include "rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h" //mbed.h, types, DEBUG_print* +#include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" + +#include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h" +#include "rohm-rpr0521/rohm-rpr0521/rpr0521.h" // Blinking rate in milliseconds #define BLINKING_RATE_MS 500 -Servo servo0(D3); -Servo servo1(D4); +Serial pc(USBTX,USBRX); InterruptIn button(USER_BUTTON); int arrivedcount = 0; TCPSocket socket; MQTTClient client(&socket); MQTT::Message message; int button_pressed=0; -char* topic_pub = "PMK-client-temperature"; -char* topic_sub = "PMK_industrija/micro2/#"; +char* topic_sub = "PMK_industrija/micro1/#"; +char* topic_pub1 = "PMK_industrija/micro1/proxy1"; +char* topic_pub2 = "PMK_industrija/micro2/proxy1"; +char* topic_pub3 = "PMK_industrija/micro2/servo0"; WiFiInterface *wifi; + +TextLCD lcd(D2,D3,D4,D5,D6,D7); +DigitalOut vdd(D8); + volatile int mems_event = 0; uint32_t previous_tick = 0; uint32_t current_tick = 0; uint8_t high = 0, low = 0; float temperature = 0.0f; char buffer[32]; +int plava = 0; +int zuta = 0; +int zelena = 0; +int crvena = 0; +int drugaBoja = 0; static char *print_double(char *str, double v, int decimalDigits = 2) { int i = 1; @@ -120,18 +134,50 @@ 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("blblabla\n"); - for(float p=0; p<1.0; p += 0.1) { - servo0 = p; + string poruka = (char*)message.payload; + if(topic_name.find("proxy1")!=std::string::npos) { + printf("Ima cepa!!! \r\n"); + + char buf[100]; + sprintf(buf, "Daj mi boju tu je cep"); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub2, message); + + } + if(topic_name.find("color")!=std::string::npos) { + printf("Vidi boje!!! \r\n"); + bool levo = false; + if (poruka.find("CRVENA") !=std::string::npos){ + levo = true; + crvena++; } - wait(2); - for(float p=1.0; p>0.0; p -= 0.1) { - servo0 = p; - + else if (poruka.find("ZELENA") !=std::string::npos){ + zelena++; + } + else if (poruka.find("PLAVA") !=std::string::npos){ + plava++; } + else if (poruka.find("ZUTA") !=std::string::npos){ + zuta++; + }else { + drugaBoja++; + } + char buf[100]; + + sprintf(buf, "Otvaraj taj servo PS =%b",levo ); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub3, message); + lcd.cls(); + lcd.printf("c:%d p:%d z:%d\ny:%d dr:%d",crvena,plava,zelena,zuta,drugaBoja); } ++arrivedcount; } @@ -141,16 +187,38 @@ button_pressed=1; } +void rpr0521_print_one_value(){ + bool error; + uint16_t data[3]; + + error = rpr0521_read_data(&data[0]); + if (!error) { + printf("PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]); + } + else { + printf("\n\r"); + } +} int main() { // Initialise the digital pin LED1 as an output - - ColorSensor cs(PA_5,PA_6,PA_7,PB_6,PC_7); - DigitalOut led(LED1); - servo0.calibrate(0.0005,90); + bool error; + uint16_t dataProx[3]; + DigitalOut led(LED1); button.rise(&buttonFunction); const char* hostname = "broker.mqttdashboard.com"; int port = 1883; + DigitalOut d8(D8); + d8 = 1; + DigitalOut d12(D12); + d12 = 0; + DigitalOut d13(D13); + d13 = 1; + + I2CCommonBegin(); + rpr0521_wait_until_found(); + pc.printf("\nSensor found.\n\r"); + rpr0521_initial_setup(); wifi = WiFiInterface::get_default_instance(); if (!wifi) { @@ -201,18 +269,33 @@ while (true) { led = !led; thread_sleep_for(BLINKING_RATE_MS); + + + error = rpr0521_read_data(&dataProx[0]); + if (!error){ + if(dataProx[0] > 50) { + 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]); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub1, message); + + } + + } if (button_pressed==1) { button_pressed=0; printf("Publishing data\r\n"); // QoS 0 char buf[100]; - printf("r: %d", cs.getRed()); - - printf("g: %d", cs.getGreen()); - printf("b: %d", cs.getBlue()); + //sprintf(buf, "RGB: %7s C\r\n", print_double(buffer, cs.getRed()*100000000+cs.getGreen()*10000+cs.getBlue())); - //message.qos = MQTT::QOS0; + //message.qos = MQTT::QOS0; //message.retained = false; //message.dup = false; //message.payload = (void*)buf; @@ -220,8 +303,8 @@ //client.publish(topic_pub, message); } - printf("Yielding"); + // printf("Yielding"); client.yield(1000); - printf(" -> Yielded\r\n"); + //printf(" -> Yielded\r\n"); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rohm-rpr0521.lib Sun Jun 27 17:38:31 2021 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/Rohm/code/rohm-rpr0521/#145f11782373
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rohm-sensor-hal.lib Sun Jun 27 17:38:31 2021 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/Rohm/code/rohm-sensor-hal/#38c206f19bb7