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

Committer:
cloudhome
Date:
Sat Aug 29 01:01:13 2015 +0000
Revision:
0:357c3cc612ca
Concurso Casa Conectada - PetIotFreescale; Por: Jo?o Henrique B. Gomes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cloudhome 0:357c3cc612ca 1 #include "mbed.h"
cloudhome 0:357c3cc612ca 2 #include "DHT.h"
cloudhome 0:357c3cc612ca 3 #include "hcsr04.h"
cloudhome 0:357c3cc612ca 4 #include "EthernetInterface.h"
cloudhome 0:357c3cc612ca 5 #include "Websocket.h"
cloudhome 0:357c3cc612ca 6 #include "MbedJSONValue.h"
cloudhome 0:357c3cc612ca 7 #include <string>
cloudhome 0:357c3cc612ca 8
cloudhome 0:357c3cc612ca 9 //### Declaracao do sensor de movimento
cloudhome 0:357c3cc612ca 10 InterruptIn motion(PTC2);
cloudhome 0:357c3cc612ca 11 int motion_detected = 0;
cloudhome 0:357c3cc612ca 12 void irq_handler(void)
cloudhome 0:357c3cc612ca 13 {
cloudhome 0:357c3cc612ca 14 motion_detected = 1;
cloudhome 0:357c3cc612ca 15 }
cloudhome 0:357c3cc612ca 16
cloudhome 0:357c3cc612ca 17 //### Declaracao do sensor de distancia
cloudhome 0:357c3cc612ca 18 HCSR04 usensor(PTA0, PTC3); //usensor(TRIGGER, ECHO)
cloudhome 0:357c3cc612ca 19 unsigned int dist;
cloudhome 0:357c3cc612ca 20
cloudhome 0:357c3cc612ca 21 //### Inicio do programa
cloudhome 0:357c3cc612ca 22 int main() {
cloudhome 0:357c3cc612ca 23 //### Inicio da interface de rede
cloudhome 0:357c3cc612ca 24 EthernetInterface eth;
cloudhome 0:357c3cc612ca 25 //### Declaracao do LED para indicar conexao de WS
cloudhome 0:357c3cc612ca 26 DigitalOut ledON(PTA2);
cloudhome 0:357c3cc612ca 27 //### Declaracao do LED para indicar erro de conexao de WS
cloudhome 0:357c3cc612ca 28 DigitalOut ledOFF(PTB23);
cloudhome 0:357c3cc612ca 29 //### Declaracao do SERVO que ira liberar alimento
cloudhome 0:357c3cc612ca 30 DigitalOut servo(PTC10);
cloudhome 0:357c3cc612ca 31 //### Declaracao de sensor de temperatura
cloudhome 0:357c3cc612ca 32 DHT sensor(PTA1,SEN11301P);
cloudhome 0:357c3cc612ca 33 //### Inicializacao do sensor de movimento
cloudhome 0:357c3cc612ca 34 motion.rise(&irq_handler);
cloudhome 0:357c3cc612ca 35 //### Declaracao do buzzer
cloudhome 0:357c3cc612ca 36 DigitalOut buzzer(PTC16);
cloudhome 0:357c3cc612ca 37
cloudhome 0:357c3cc612ca 38 //### Variavel para armazener sinal de erro
cloudhome 0:357c3cc612ca 39 int err;
cloudhome 0:357c3cc612ca 40
cloudhome 0:357c3cc612ca 41 printf("Inciando programa Pet-iot-ws\r\n");
cloudhome 0:357c3cc612ca 42 //### Iniciar inteface de rede
cloudhome 0:357c3cc612ca 43 eth.init(); //Use DHCP
cloudhome 0:357c3cc612ca 44 eth.connect();
cloudhome 0:357c3cc612ca 45 printf("IP adquirido %s\n\r", eth.getIPAddress());
cloudhome 0:357c3cc612ca 46
cloudhome 0:357c3cc612ca 47 //### Tenta se conectar ao socket server
cloudhome 0:357c3cc612ca 48 printf("Inciando conexao com o servidor Websocket\r\n");
cloudhome 0:357c3cc612ca 49 Websocket ws("ws://192.168.100.115:3000/ws/freescale");
cloudhome 0:357c3cc612ca 50
cloudhome 0:357c3cc612ca 51 if(ws.connect()){
cloudhome 0:357c3cc612ca 52 //### Variavel para armazenar as mensagems JSON
cloudhome 0:357c3cc612ca 53 MbedJSONValue mensagemJSON;
cloudhome 0:357c3cc612ca 54 //### Variavel para os parses do JSON
cloudhome 0:357c3cc612ca 55 std::string tipo;
cloudhome 0:357c3cc612ca 56 std::string mensagem;
cloudhome 0:357c3cc612ca 57
cloudhome 0:357c3cc612ca 58 printf("Conectado com sucesso\r\n");
cloudhome 0:357c3cc612ca 59
cloudhome 0:357c3cc612ca 60 //### Liga o LED verde
cloudhome 0:357c3cc612ca 61 ledON = 1;
cloudhome 0:357c3cc612ca 62 //### Desliga o LED vermelho
cloudhome 0:357c3cc612ca 63 ledOFF = 0;
cloudhome 0:357c3cc612ca 64
cloudhome 0:357c3cc612ca 65 char recv[256];
cloudhome 0:357c3cc612ca 66 int res = ws.send("{\"tipo\": \"conexao\", \"mensagem\": \"Freescale Pet IoT - Conectado\", \"destino\": \"ws/cliente\" }");
cloudhome 0:357c3cc612ca 67 while (1) {
cloudhome 0:357c3cc612ca 68 if (ws.read(recv)) {
cloudhome 0:357c3cc612ca 69 printf("Dados recebidos: %s\r\n", recv);
cloudhome 0:357c3cc612ca 70 parse(mensagemJSON, recv);
cloudhome 0:357c3cc612ca 71 tipo = mensagemJSON["tipo"].get<std::string>();
cloudhome 0:357c3cc612ca 72 mensagem = mensagemJSON["mensagem"].get<std::string>();
cloudhome 0:357c3cc612ca 73
cloudhome 0:357c3cc612ca 74 if (tipo == "comida"){
cloudhome 0:357c3cc612ca 75 if(mensagem == "liberar"){
cloudhome 0:357c3cc612ca 76 printf("Recebendo evento comida\r\n");
cloudhome 0:357c3cc612ca 77 int i;
cloudhome 0:357c3cc612ca 78 for (i=0; i < 200; ++i) {
cloudhome 0:357c3cc612ca 79 servo = 1; // Toggle the LED state
cloudhome 0:357c3cc612ca 80 wait_ms(1); // 200 ms
cloudhome 0:357c3cc612ca 81 servo = 0; // LED is OFF
cloudhome 0:357c3cc612ca 82 wait_ms(10); // 1 sec
cloudhome 0:357c3cc612ca 83 }
cloudhome 0:357c3cc612ca 84 MbedJSONValue json;
cloudhome 0:357c3cc612ca 85 std::string str;
cloudhome 0:357c3cc612ca 86 json["tipo"] = "resp_comida";
cloudhome 0:357c3cc612ca 87 json["mensagem"] = "Comida liberada com sucesso";
cloudhome 0:357c3cc612ca 88 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 89 str = json.serialize();
cloudhome 0:357c3cc612ca 90 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 91 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 92 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 93 delete [] jsonChar;
cloudhome 0:357c3cc612ca 94 }
cloudhome 0:357c3cc612ca 95 }
cloudhome 0:357c3cc612ca 96 else if(tipo == "reservatorio"){
cloudhome 0:357c3cc612ca 97 if(mensagem == "consultar"){
cloudhome 0:357c3cc612ca 98 int total = 20; //Distancia total em CM do reservatorio vazio
cloudhome 0:357c3cc612ca 99 int percentual = 0;
cloudhome 0:357c3cc612ca 100 usensor.start();
cloudhome 0:357c3cc612ca 101 wait_ms(500);
cloudhome 0:357c3cc612ca 102 dist=usensor.get_dist_cm();
cloudhome 0:357c3cc612ca 103 printf("Distancia em CM:%ld\r\n",dist);
cloudhome 0:357c3cc612ca 104 if(dist != 0 && dist != NULL){
cloudhome 0:357c3cc612ca 105 percentual = (int)((dist*100)/total);
cloudhome 0:357c3cc612ca 106 percentual = 100-percentual;
cloudhome 0:357c3cc612ca 107 }
cloudhome 0:357c3cc612ca 108 MbedJSONValue json;
cloudhome 0:357c3cc612ca 109 std::string str;
cloudhome 0:357c3cc612ca 110 json["tipo"] = "resp_reservatorio";
cloudhome 0:357c3cc612ca 111 json["mensagem"] = percentual;
cloudhome 0:357c3cc612ca 112 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 113 str = json.serialize();
cloudhome 0:357c3cc612ca 114 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 115 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 116 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 117 delete [] jsonChar;
cloudhome 0:357c3cc612ca 118 }
cloudhome 0:357c3cc612ca 119 }
cloudhome 0:357c3cc612ca 120 else if(tipo == "sensor_movimento"){
cloudhome 0:357c3cc612ca 121 if(motion_detected) {
cloudhome 0:357c3cc612ca 122 MbedJSONValue json;
cloudhome 0:357c3cc612ca 123 std::string str;
cloudhome 0:357c3cc612ca 124 json["tipo"] = "resp_sensor_movimento";
cloudhome 0:357c3cc612ca 125 json["mensagem"] = "Movimento detectado, seu animal quer comida.";
cloudhome 0:357c3cc612ca 126 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 127 str = json.serialize();
cloudhome 0:357c3cc612ca 128 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 129 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 130 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 131 delete [] jsonChar;
cloudhome 0:357c3cc612ca 132 }
cloudhome 0:357c3cc612ca 133 }
cloudhome 0:357c3cc612ca 134 else if(tipo == "toca_audio") {
cloudhome 0:357c3cc612ca 135 int i;
cloudhome 0:357c3cc612ca 136 //### Toca 3 vezes o beep
cloudhome 0:357c3cc612ca 137 for (i=0; i < 3; ++i) {
cloudhome 0:357c3cc612ca 138 buzzer = 1;
cloudhome 0:357c3cc612ca 139 wait(.5);
cloudhome 0:357c3cc612ca 140 buzzer = 0;
cloudhome 0:357c3cc612ca 141 wait(.5);
cloudhome 0:357c3cc612ca 142 printf("Conectado com sucesso\r\n");
cloudhome 0:357c3cc612ca 143 }
cloudhome 0:357c3cc612ca 144 MbedJSONValue json;
cloudhome 0:357c3cc612ca 145 std::string str;
cloudhome 0:357c3cc612ca 146 json["tipo"] = "resp_toca_audio";
cloudhome 0:357c3cc612ca 147 json["mensagem"] = "Audio emitido com sucesso";
cloudhome 0:357c3cc612ca 148 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 149 str = json.serialize();
cloudhome 0:357c3cc612ca 150 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 151 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 152 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 153 delete [] jsonChar;
cloudhome 0:357c3cc612ca 154 }
cloudhome 0:357c3cc612ca 155 else if(tipo == "temp_humidade"){
cloudhome 0:357c3cc612ca 156 int tentativasLeitura = 10;
cloudhome 0:357c3cc612ca 157 for(int i=0;i < tentativasLeitura; ++i){
cloudhome 0:357c3cc612ca 158 err = sensor.readData();
cloudhome 0:357c3cc612ca 159 if(err == 0) {
cloudhome 0:357c3cc612ca 160 break;
cloudhome 0:357c3cc612ca 161 }
cloudhome 0:357c3cc612ca 162 wait(0.5);
cloudhome 0:357c3cc612ca 163 }
cloudhome 0:357c3cc612ca 164 if (err == 0) {
cloudhome 0:357c3cc612ca 165 MbedJSONValue json;
cloudhome 0:357c3cc612ca 166 std::string str;
cloudhome 0:357c3cc612ca 167 json["tipo"] = "resp_temp_humidade";
cloudhome 0:357c3cc612ca 168 json["mensagem"] = "Dados de temperatura e humidade capitado com sucesso";
cloudhome 0:357c3cc612ca 169 json["temperatura"] = (int)(sensor.ReadTemperature(CELCIUS) * 1 + 0.5);
cloudhome 0:357c3cc612ca 170 json["humidade"] = (int)sensor.ReadHumidity();
cloudhome 0:357c3cc612ca 171 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 172 str = json.serialize();
cloudhome 0:357c3cc612ca 173 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 174 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 175 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 176 delete [] jsonChar;
cloudhome 0:357c3cc612ca 177 } else {
cloudhome 0:357c3cc612ca 178 printf("Erro ao ler a temperatura e humidade\r\n");
cloudhome 0:357c3cc612ca 179 }
cloudhome 0:357c3cc612ca 180 } else {
cloudhome 0:357c3cc612ca 181 //### Mensagem nao reconhecida
cloudhome 0:357c3cc612ca 182 }
cloudhome 0:357c3cc612ca 183 }
cloudhome 0:357c3cc612ca 184 /*
cloudhome 0:357c3cc612ca 185 * Mensagens serem enviadas como notificacao e
cloudhome 0:357c3cc612ca 186 */
cloudhome 0:357c3cc612ca 187 //### Verifica o nivel do reservatorio para ver se precisa avisar o usuario
cloudhome 0:357c3cc612ca 188 int total = 18; //Distancia total em CM do reservatorio vazio
cloudhome 0:357c3cc612ca 189 int percentual = 0;
cloudhome 0:357c3cc612ca 190 usensor.start();
cloudhome 0:357c3cc612ca 191 wait_ms(500);
cloudhome 0:357c3cc612ca 192 dist=usensor.get_dist_cm();
cloudhome 0:357c3cc612ca 193 printf("Distancia em CM:%ld\r\n",dist);
cloudhome 0:357c3cc612ca 194 if(dist != 0 && dist != NULL){
cloudhome 0:357c3cc612ca 195 percentual = (int)((dist*100)/total);
cloudhome 0:357c3cc612ca 196 percentual = 100-percentual;
cloudhome 0:357c3cc612ca 197 }
cloudhome 0:357c3cc612ca 198 if(percentual > 5 && percentual < 10){
cloudhome 0:357c3cc612ca 199 printf("Nivel do reservatorio esta baixo, avisar usuario\r\n");
cloudhome 0:357c3cc612ca 200 MbedJSONValue json;
cloudhome 0:357c3cc612ca 201 std::string str;
cloudhome 0:357c3cc612ca 202 json["tipo"] = "alerta";
cloudhome 0:357c3cc612ca 203 json["mensagem"] = "Nivel do reservatorio esta baixo, coloque mais racao no reservatorio";
cloudhome 0:357c3cc612ca 204 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 205 str = json.serialize();
cloudhome 0:357c3cc612ca 206 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 207 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 208 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 209 delete [] jsonChar;
cloudhome 0:357c3cc612ca 210 }
cloudhome 0:357c3cc612ca 211 //### Verifica se teve movimento para avisar o usuario
cloudhome 0:357c3cc612ca 212 if(motion_detected) {
cloudhome 0:357c3cc612ca 213 printf("Movimento detectado\r\n");
cloudhome 0:357c3cc612ca 214 MbedJSONValue json;
cloudhome 0:357c3cc612ca 215 std::string str;
cloudhome 0:357c3cc612ca 216 json["tipo"] = "alerta";
cloudhome 0:357c3cc612ca 217 json["mensagem"] = "Movimento detectado, seu animal quer comida.";
cloudhome 0:357c3cc612ca 218 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 219 str = json.serialize();
cloudhome 0:357c3cc612ca 220 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 221 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 222 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 223 delete [] jsonChar;
cloudhome 0:357c3cc612ca 224 motion_detected = 0;
cloudhome 0:357c3cc612ca 225 }
cloudhome 0:357c3cc612ca 226 //### Verifica alera para alta temperatura e ou baixa humidade
cloudhome 0:357c3cc612ca 227 err = sensor.readData();
cloudhome 0:357c3cc612ca 228 if (err == 0) {
cloudhome 0:357c3cc612ca 229 int temperatura = (int)(sensor.ReadTemperature(CELCIUS) * 1 + 0.5);
cloudhome 0:357c3cc612ca 230 int humidade = (int)sensor.ReadHumidity();
cloudhome 0:357c3cc612ca 231 if(temperatura > 30 || humidade < 30){
cloudhome 0:357c3cc612ca 232 MbedJSONValue json;
cloudhome 0:357c3cc612ca 233 std::string str;
cloudhome 0:357c3cc612ca 234 json["tipo"] = "alerta";
cloudhome 0:357c3cc612ca 235 if(temperatura > 30){
cloudhome 0:357c3cc612ca 236 json["mensagem"] = "Atencao, alta temperatura nao deixe de dar agua para o seu animal.";
cloudhome 0:357c3cc612ca 237 } else {
cloudhome 0:357c3cc612ca 238 json["mensagem"] = "Atencao, baixa humidade, nao deixe de dar agua para o seu animal.";
cloudhome 0:357c3cc612ca 239 }
cloudhome 0:357c3cc612ca 240 json["temperatura"] = (int)(sensor.ReadTemperature(CELCIUS) * 1 + 0.5);
cloudhome 0:357c3cc612ca 241 json["humidade"] = (int)sensor.ReadHumidity();
cloudhome 0:357c3cc612ca 242 json["destino"] = "ws/cliente";
cloudhome 0:357c3cc612ca 243 str = json.serialize();
cloudhome 0:357c3cc612ca 244 char *jsonChar = new char[str.length() + 1];
cloudhome 0:357c3cc612ca 245 strcpy(jsonChar, str.c_str());
cloudhome 0:357c3cc612ca 246 printf(str.c_str());
cloudhome 0:357c3cc612ca 247 ws.send(jsonChar);
cloudhome 0:357c3cc612ca 248 delete [] jsonChar;
cloudhome 0:357c3cc612ca 249 }
cloudhome 0:357c3cc612ca 250 }
cloudhome 0:357c3cc612ca 251 //### Verifica termperatura / humidade
cloudhome 0:357c3cc612ca 252 wait(0.5);
cloudhome 0:357c3cc612ca 253 }
cloudhome 0:357c3cc612ca 254 } else {
cloudhome 0:357c3cc612ca 255 //Ligado led vermelho
cloudhome 0:357c3cc612ca 256 printf("Nao foi possivel conectar com servidor\r\n");
cloudhome 0:357c3cc612ca 257 //### Desliga o LED verde
cloudhome 0:357c3cc612ca 258 ledON = 0;
cloudhome 0:357c3cc612ca 259 //### Liga o LED vermelho
cloudhome 0:357c3cc612ca 260 ledOFF = 1;
cloudhome 0:357c3cc612ca 261 }
cloudhome 0:357c3cc612ca 262 }