PMKIND

Dependencies:   rohm-rpr0521 rohm-sensor-hal Servo TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include "mbed.h"
00007 #include "platform/mbed_thread.h"
00008 #include <string>
00009 #include <MQTTClientMbedOs.h>
00010 #include "TextLCD.h"
00011 #include "rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"       //mbed.h, types, DEBUG_print*
00012 #include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"
00013 
00014 #include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h"
00015 #include "rohm-rpr0521/rohm-rpr0521/rpr0521.h"
00016 
00017 // Blinking rate in milliseconds
00018 #define BLINKING_RATE_MS                                                    500
00019 
00020 Serial pc(USBTX,USBRX);
00021 InterruptIn button(USER_BUTTON);
00022 int arrivedcount = 0;
00023 TCPSocket socket;
00024 MQTTClient client(&socket);
00025 MQTT::Message message;
00026 int button_pressed=0;
00027 
00028 char* topic_sub = "PMK_industrija/micro1/#";
00029 char* topic_pub1 = "PMK_industrija/micro1/proxy1";
00030 char* topic_pub2 = "PMK_industrija/micro2/proxy1";
00031 char* topic_pub3 = "PMK_industrija/micro2/servo0";
00032 WiFiInterface *wifi;
00033 
00034 TextLCD lcd(D2,D3,D4,D5,D6,D7);
00035 DigitalOut vdd(D8);
00036 
00037 volatile int mems_event = 0;
00038 uint32_t previous_tick = 0;
00039 uint32_t current_tick = 0;
00040 uint8_t high = 0, low = 0;
00041 float temperature = 0.0f;
00042 char buffer[32];
00043 int plava = 0;
00044 int zuta = 0;
00045 int zelena = 0;
00046 int crvena = 0;
00047 int drugaBoja = 0;
00048 static char *print_double(char *str, double v, int decimalDigits = 2)
00049 {
00050     int i = 1;
00051     int intPart, fractPart;
00052     int len;
00053     char *ptr;
00054  
00055     /* prepare decimal digits multiplicator */
00056     for (; decimalDigits != 0; i *= 10, decimalDigits--);
00057  
00058     /* calculate integer & fractinal parts */
00059     intPart = (int)v;
00060     fractPart = (int)((v - (double)(int)v) * i);
00061  
00062     /* fill in integer part */
00063     sprintf(str, "%i.", intPart);
00064  
00065     /* prepare fill in of fractional part */
00066     len = strlen(str);
00067     ptr = &str[len];
00068  
00069     /* fill in leading fractional zeros */
00070     for (i /= 10; i > 1; i /= 10, ptr++) {
00071         if (fractPart >= i) {
00072             break;
00073         }
00074         *ptr = '0';
00075     }
00076  
00077     /* fill in (rest of) fractional part */
00078     sprintf(ptr, "%i", fractPart);
00079  
00080     return str;
00081 }
00082 
00083 const char *sec2str(nsapi_security_t sec)
00084 {
00085     switch (sec) {
00086         case NSAPI_SECURITY_NONE:
00087             return "None";
00088         case NSAPI_SECURITY_WEP:
00089             return "WEP";
00090         case NSAPI_SECURITY_WPA:
00091             return "WPA";
00092         case NSAPI_SECURITY_WPA2:
00093             return "WPA2";
00094         case NSAPI_SECURITY_WPA_WPA2:
00095             return "WPA/WPA2";
00096         case NSAPI_SECURITY_UNKNOWN:
00097         default:
00098             return "Unknown";
00099     }
00100 }
00101 
00102 int scan_demo(WiFiInterface *wifi)
00103 {
00104     WiFiAccessPoint *ap;
00105     printf("Scan:\n");
00106     int count = wifi->scan(NULL,0);
00107     if (count <= 0) {
00108         printf("scan() failed with return value: %d\n", count);
00109         return 0;
00110     }
00111     /* Limit number of network arbitrary to 15 */
00112     count = count < 15 ? count : 15;
00113     ap = new WiFiAccessPoint[count];
00114     count = wifi->scan(ap, count);
00115     if (count <= 0) {
00116         printf("scan() failed with return value: %d\n", count);
00117         return 0;
00118     }
00119     for (int i = 0; i < count; i++) {
00120         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00121                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00122                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00123     }
00124     printf("%d networks available.\n", count);
00125     delete[] ap;
00126     return count;
00127 }
00128 
00129 void messageArrived(MQTT::MessageData& md)
00130 {
00131     MQTTString &topic = md.topicName;
00132     string topic_name = topic.lenstring.data;
00133     printf("Topic name %d : %s\r\n",topic.lenstring.len,topic_name.c_str() );
00134     MQTT::Message &message = md.message;
00135     printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
00136     printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
00137     string poruka = (char*)message.payload;
00138     if(topic_name.find("proxy1")!=std::string::npos) {
00139         printf("Ima cepa!!! \r\n"); 
00140         
00141         char buf[100];
00142         sprintf(buf, "Daj mi boju tu je cep");
00143         message.qos = MQTT::QOS0;
00144         message.retained = false;
00145         message.dup = false;
00146         message.payload = (void*)buf;
00147         message.payloadlen = strlen(buf)+1;
00148         client.publish(topic_pub2, message);    
00149                 
00150     }
00151     if(topic_name.find("color")!=std::string::npos) {
00152         printf("Vidi boje!!! \r\n"); 
00153         bool levo = false;
00154         
00155         if (poruka.find("CRVENA") !=std::string::npos){
00156                 levo = true;
00157                 crvena++;
00158         }
00159         else if (poruka.find("ZELENA") !=std::string::npos){
00160                 zelena++;
00161         }
00162         else if (poruka.find("PLAVA") !=std::string::npos){
00163                 plava++;
00164         }
00165         else if (poruka.find("ZUTA") !=std::string::npos){
00166                 zuta++;
00167         }else {
00168                 drugaBoja++;
00169             }
00170         char buf[100];
00171                 
00172         sprintf(buf, "Otvaraj taj servo PS =%b",levo );
00173         message.qos = MQTT::QOS0;
00174         message.retained = false;
00175         message.dup = false;
00176         message.payload = (void*)buf;
00177         message.payloadlen = strlen(buf)+1;
00178         client.publish(topic_pub3, message);    
00179         lcd.cls();
00180         lcd.printf("c:%d  p:%d z:%d\ny:%d dr:%d",crvena,plava,zelena,zuta,drugaBoja);
00181     }
00182     ++arrivedcount;
00183 }
00184 
00185 
00186 void buttonFunction() {    
00187     button_pressed=1;   
00188 }
00189 
00190 void rpr0521_print_one_value(){
00191     bool error;
00192     uint16_t data[3];
00193     
00194     error = rpr0521_read_data(&data[0]);
00195     if (!error) {
00196         printf("PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]);
00197         }
00198     else {
00199         printf("\n\r");
00200         }
00201 }
00202 int main()
00203 {
00204     // Initialise the digital pin LED1 as an output
00205     bool error;
00206     uint16_t dataProx[3];
00207     DigitalOut led(LED1); 
00208     button.rise(&buttonFunction);
00209     const char* hostname = "broker.mqttdashboard.com";
00210     int port = 1883;    
00211     DigitalOut d8(D8);
00212     d8 = 1;
00213     DigitalOut d12(D12);
00214     d12 = 0;
00215     DigitalOut d13(D13);
00216     d13 = 1;
00217     
00218     I2CCommonBegin();
00219     rpr0521_wait_until_found();
00220     pc.printf("\nSensor found.\n\r");
00221     rpr0521_initial_setup();
00222     
00223     wifi = WiFiInterface::get_default_instance();
00224     if (!wifi) {
00225         printf("ERROR: No WiFiInterface found.\n");
00226         return -1;
00227     }
00228 
00229     int count = scan_demo(wifi);
00230     if (count == 0) {
00231         printf("No WIFI APs found - can't continue further.\n");
00232         return -1;
00233     }
00234 
00235     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00236     int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00237     if (ret != 0) {
00238         printf("\nConnection error: %d\n", ret);
00239         return -1;
00240     }
00241 
00242     printf("Success\n\n");
00243     printf("MAC: %s\n", wifi->get_mac_address());
00244     printf("IP: %s\n", wifi->get_ip_address());
00245     printf("Netmask: %s\n", wifi->get_netmask());
00246     printf("Gateway: %s\n", wifi->get_gateway());
00247     printf("RSSI: %d\n\n", wifi->get_rssi());
00248 
00249     socket.open(wifi);
00250     socket.connect(hostname, port);
00251     
00252     int rc=0;
00253     
00254     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00255     data.MQTTVersion = 3;
00256     data.clientID.cstring = "PMK-client2";
00257     //data.username.cstring = "testuser";
00258     //data.password.cstring = "testpassword";
00259     
00260     if ((rc = client.connect(data)) != 0)
00261         printf("rc from MQTT connect is %d\r\n", rc);
00262 
00263     
00264     if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0)
00265         printf("rc from MQTT subscribe is %d\r\n", rc);    
00266     
00267     
00268     
00269     while (true) {
00270         led = !led;
00271         thread_sleep_for(BLINKING_RATE_MS);
00272      
00273     
00274         error = rpr0521_read_data(&dataProx[0]);
00275         if (!error){
00276             if(dataProx[0] > 50) {
00277                 printf("Pristigao je cep na lokaciju O.O O:'( \r\n ");
00278                 char buf[100];
00279                 sprintf(buf, "Cep je pristigao na proxy1 PS = %d",dataProx[0]);
00280                 message.qos = MQTT::QOS0;
00281                 message.retained = false;
00282                 message.dup = false;
00283                 message.payload = (void*)buf;
00284                 message.payloadlen = strlen(buf)+1;
00285                 client.publish(topic_pub1, message);    
00286                 
00287                 }
00288         
00289         }
00290         if (button_pressed==1) {
00291             button_pressed=0;    
00292             printf("Publishing data\r\n");    
00293             // QoS 0
00294             char buf[100];
00295           
00296             //sprintf(buf, "RGB: %7s C\r\n", print_double(buffer, cs.getRed()*100000000+cs.getGreen()*10000+cs.getBlue()));
00297             
00298            //message.qos = MQTT::QOS0;
00299             //message.retained = false;
00300             //message.dup = false;
00301             //message.payload = (void*)buf;
00302             //message.payloadlen = strlen(buf)+1;
00303             //client.publish(topic_pub, message);
00304             
00305         }
00306        // printf("Yielding"); 
00307         client.yield(1000);
00308         //printf(" -> Yielded\r\n"); 
00309     }
00310 }