Concurso Casa Conectada - PetIoTFreescale Por: Joao Henrique B. Gomes

Dependencies:   DHT EthernetInterface HCSR04 MbedJSONValue WebSocketClient mbed-rtos mbed

Concurso Casa Conectada

PROJETO: PETIOT /media/uploads/cloudhome/concurso_casa_conectada_joao_henrique_b_gomes.pdf

Files at this revision

API Documentation at this revision

Comitter:
cloudhome
Date:
Sat Aug 29 01:01:13 2015 +0000
Commit message:
Concurso Casa Conectada - PetIotFreescale; Por: Jo?o Henrique B. Gomes

Changed in this revision

DHT.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 357c3cc612ca DHT.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT.lib	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Wimpie/code/DHT/#9b5b3200688f
diff -r 000000000000 -r 357c3cc612ca EthernetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
diff -r 000000000000 -r 357c3cc612ca HCSR04.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/prabhuvd/code/HCSR04/#71da0dbf4400
diff -r 000000000000 -r 357c3cc612ca MbedJSONValue.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 000000000000 -r 357c3cc612ca WebSocketClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
diff -r 000000000000 -r 357c3cc612ca main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,262 @@
+#include "mbed.h"
+#include "DHT.h"
+#include "hcsr04.h"
+#include "EthernetInterface.h"
+#include "Websocket.h"
+#include "MbedJSONValue.h"
+#include <string>
+
+//### Declaracao do sensor de movimento
+InterruptIn motion(PTC2);
+int motion_detected = 0;
+void irq_handler(void)
+{
+    motion_detected = 1;
+}
+
+//### Declaracao do sensor de distancia
+HCSR04 usensor(PTA0, PTC3); //usensor(TRIGGER, ECHO)
+unsigned int dist;
+
+//### Inicio do programa
+int main() {
+    //### Inicio da interface de rede
+    EthernetInterface eth;
+    //### Declaracao do LED para indicar conexao de WS
+    DigitalOut ledON(PTA2);
+    //### Declaracao do LED para indicar erro de conexao de WS
+    DigitalOut ledOFF(PTB23);
+    //### Declaracao do SERVO que ira liberar alimento
+    DigitalOut servo(PTC10);
+    //### Declaracao de sensor de temperatura
+    DHT sensor(PTA1,SEN11301P);
+    //### Inicializacao do sensor de movimento
+    motion.rise(&irq_handler);
+    //### Declaracao do buzzer
+    DigitalOut buzzer(PTC16);
+    
+    //### Variavel para armazener sinal de erro
+    int err;
+    
+    printf("Inciando programa Pet-iot-ws\r\n");
+    //### Iniciar inteface de rede
+    eth.init(); //Use DHCP
+    eth.connect();
+    printf("IP adquirido %s\n\r", eth.getIPAddress());
+    
+    //### Tenta se conectar ao socket server
+    printf("Inciando conexao com o servidor Websocket\r\n");
+    Websocket ws("ws://192.168.100.115:3000/ws/freescale");
+    
+    if(ws.connect()){
+        //### Variavel para armazenar as mensagems JSON
+        MbedJSONValue mensagemJSON;
+        //### Variavel para os parses do JSON
+        std::string tipo;
+        std::string mensagem;
+
+        printf("Conectado com sucesso\r\n");
+        
+        //### Liga o LED verde
+        ledON  = 1;
+        //### Desliga o LED vermelho
+        ledOFF = 0;
+        
+        char recv[256];
+        int res = ws.send("{\"tipo\": \"conexao\", \"mensagem\": \"Freescale Pet IoT - Conectado\", \"destino\": \"ws/cliente\" }");
+        while (1) {
+            if (ws.read(recv)) {
+                printf("Dados recebidos: %s\r\n", recv);
+                parse(mensagemJSON, recv);
+                tipo      = mensagemJSON["tipo"].get<std::string>();
+                mensagem  = mensagemJSON["mensagem"].get<std::string>();
+                
+                if (tipo == "comida"){
+                        if(mensagem == "liberar"){
+                            printf("Recebendo evento comida\r\n");
+                            int i;
+                            for (i=0; i < 200; ++i) {
+                                servo = 1; // Toggle the LED state
+                                wait_ms(1); // 200 ms
+                                servo = 0; // LED is OFF
+                                wait_ms(10); // 1 sec
+                            }
+                            MbedJSONValue json;
+                            std::string str;
+                            json["tipo"]        = "resp_comida";
+                            json["mensagem"]    = "Comida liberada com sucesso";
+                            json["destino"]     = "ws/cliente";
+                            str                 = json.serialize();
+                            char *jsonChar = new char[str.length() + 1];
+                            strcpy(jsonChar, str.c_str());
+                            ws.send(jsonChar);
+                            delete [] jsonChar;
+                        } 
+               }
+               else if(tipo == "reservatorio"){
+                        if(mensagem == "consultar"){
+                            int total = 20; //Distancia total em CM do reservatorio vazio
+                            int percentual = 0;
+                            usensor.start();
+                            wait_ms(500); 
+                            dist=usensor.get_dist_cm();
+                            printf("Distancia em CM:%ld\r\n",dist);
+                            if(dist != 0 && dist != NULL){
+                                percentual = (int)((dist*100)/total);
+                                percentual = 100-percentual;
+                            }
+                            MbedJSONValue json;
+                            std::string str;
+                            json["tipo"]        = "resp_reservatorio";
+                            json["mensagem"]    = percentual;
+                            json["destino"]     = "ws/cliente";
+                            str                 = json.serialize();
+                            char *jsonChar = new char[str.length() + 1];
+                            strcpy(jsonChar, str.c_str());
+                            ws.send(jsonChar);
+                            delete [] jsonChar;
+                        }
+               }
+               else if(tipo == "sensor_movimento"){
+                        if(motion_detected) {
+                            MbedJSONValue json;
+                            std::string str;
+                            json["tipo"]        = "resp_sensor_movimento";
+                            json["mensagem"]    = "Movimento detectado, seu animal quer comida.";
+                            json["destino"]     = "ws/cliente";
+                            str                 = json.serialize();
+                            char *jsonChar = new char[str.length() + 1];
+                            strcpy(jsonChar, str.c_str());
+                            ws.send(jsonChar);
+                            delete [] jsonChar;
+                        }
+               }
+               else if(tipo == "toca_audio") {
+                        int i;
+                        //### Toca 3 vezes o beep                        
+                        for (i=0; i < 3; ++i) {
+                            buzzer = 1;
+                            wait(.5);
+                            buzzer = 0;
+                            wait(.5);
+                            printf("Conectado com sucesso\r\n");
+                        }
+                        MbedJSONValue json;
+                        std::string str;
+                        json["tipo"]        = "resp_toca_audio";
+                        json["mensagem"]    = "Audio emitido com sucesso";
+                        json["destino"]     = "ws/cliente";
+                        str                 = json.serialize();
+                        char *jsonChar = new char[str.length() + 1];
+                        strcpy(jsonChar, str.c_str());
+                        ws.send(jsonChar);
+                        delete [] jsonChar;
+               }
+               else if(tipo == "temp_humidade"){
+                        int tentativasLeitura = 10;
+                        for(int i=0;i < tentativasLeitura; ++i){
+                            err = sensor.readData();
+                            if(err == 0) {
+                                break;
+                            }
+                            wait(0.5);
+                        }
+                        if (err == 0) {
+                            MbedJSONValue json;
+                            std::string str;
+                            json["tipo"]        = "resp_temp_humidade";
+                            json["mensagem"]    = "Dados de temperatura e humidade capitado com sucesso";
+                            json["temperatura"] = (int)(sensor.ReadTemperature(CELCIUS) * 1 + 0.5);
+                            json["humidade"]    = (int)sensor.ReadHumidity();
+                            json["destino"]     = "ws/cliente";
+                            str                 = json.serialize();
+                            char *jsonChar = new char[str.length() + 1];
+                            strcpy(jsonChar, str.c_str());
+                            ws.send(jsonChar);
+                            delete [] jsonChar;
+                        } else {
+                            printf("Erro ao ler a temperatura e humidade\r\n");
+                        }
+               } else {
+                    //### Mensagem nao reconhecida
+               }
+            }
+            /*
+             *   Mensagens serem enviadas como notificacao e 
+             */
+            //### Verifica o nivel do reservatorio para ver se precisa avisar o usuario
+            int total = 18; //Distancia total em CM do reservatorio vazio
+            int percentual = 0;
+            usensor.start();
+            wait_ms(500); 
+            dist=usensor.get_dist_cm();
+            printf("Distancia em CM:%ld\r\n",dist);
+            if(dist != 0 && dist != NULL){
+                percentual = (int)((dist*100)/total);
+                percentual = 100-percentual;
+            }
+            if(percentual > 5 && percentual < 10){
+                printf("Nivel do reservatorio esta baixo, avisar usuario\r\n");
+                MbedJSONValue json;
+                std::string str;
+                json["tipo"]        = "alerta";
+                json["mensagem"]    = "Nivel do reservatorio esta baixo, coloque mais racao no reservatorio";
+                json["destino"]     = "ws/cliente";
+                str                 = json.serialize();
+                char *jsonChar = new char[str.length() + 1];
+                strcpy(jsonChar, str.c_str());
+                ws.send(jsonChar);
+                delete [] jsonChar;
+            }
+            //### Verifica se teve movimento para avisar o usuario
+            if(motion_detected) {
+                printf("Movimento detectado\r\n");
+                MbedJSONValue json;
+                std::string str;
+                json["tipo"]        = "alerta";
+                json["mensagem"]    = "Movimento detectado, seu animal quer comida.";
+                json["destino"]     = "ws/cliente";
+                str                 = json.serialize();
+                char *jsonChar = new char[str.length() + 1];
+                strcpy(jsonChar, str.c_str());
+                ws.send(jsonChar);
+                delete [] jsonChar;
+                motion_detected = 0;
+            }
+            //### Verifica alera para alta temperatura e ou baixa humidade
+            err = sensor.readData();
+            if (err == 0) {
+                int temperatura = (int)(sensor.ReadTemperature(CELCIUS) * 1 + 0.5);
+                int humidade    = (int)sensor.ReadHumidity();
+                if(temperatura > 30 || humidade < 30){
+                    MbedJSONValue json;
+                    std::string str;
+                    json["tipo"]            = "alerta";
+                    if(temperatura > 30){
+                        json["mensagem"]    = "Atencao, alta temperatura nao deixe de dar agua para o seu animal.";
+                    } else {
+                        json["mensagem"]    = "Atencao, baixa humidade, nao deixe de dar agua para o seu animal.";
+                    }
+                    json["temperatura"] = (int)(sensor.ReadTemperature(CELCIUS) * 1 + 0.5);
+                    json["humidade"]    = (int)sensor.ReadHumidity();
+                    json["destino"]     = "ws/cliente";
+                    str                 = json.serialize();
+                    char *jsonChar = new char[str.length() + 1];
+                    strcpy(jsonChar, str.c_str());
+                    printf(str.c_str());
+                    ws.send(jsonChar);
+                    delete [] jsonChar;
+                }
+            } 
+            //### Verifica termperatura / humidade
+            wait(0.5);
+        }
+    } else {
+        //Ligado led vermelho
+        printf("Nao foi possivel conectar com servidor\r\n");
+        //### Desliga o LED verde
+        ledON  = 0;
+        //### Liga o LED vermelho
+        ledOFF = 1;
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 357c3cc612ca mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#21b438192b0f
diff -r 000000000000 -r 357c3cc612ca mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Aug 29 01:01:13 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/8ed44a420e5c
\ No newline at end of file