Skip to content

Instantly share code, notes, and snippets.

@antonyalkmim
Last active June 4, 2017 23:55
Show Gist options
  • Save antonyalkmim/e8e41bfaa65cc21cfccc84dc9d56ddd2 to your computer and use it in GitHub Desktop.
Save antonyalkmim/e8e41bfaa65cc21cfccc84dc9d56ddd2 to your computer and use it in GitHub Desktop.
Coração da PicassoHouse - Comandos para controle dos componentes eletrônicos
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Ethernet.h>
#include "RestClient.h"
#include <PubSubClient.h>
#include <aREST.h>
#include <Servo.h>
//Portas correspondentes aos sensores de movimento
//#define S_MOV_1 51
//#define S_MOV_2 52
//#define S_MOV_3 53
//#define S_MOV_4 54
//Portas correspondentes ao controlador dos reles
#define IN_1 47 //room_id 1
#define IN_2 46 //room_id 2
#define IN_3 49 //room_id 3
#define IN_4 48 //room_id 4
//Portas correspondentes aos sensores de movimento
#define S_RAIN_ANALOG A15
boolean hasDetectedRain = false;
//Portas correspondentes ao buzzer
#define BUZZER_P 45
//Portas correspondentes aos servos motores
#define SERVO_GARAGE 42
#define SERVO_WINDOW 43
Servo garage;
Servo window;
//Mapeamento das teclas
char keyMap[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// quais pinos conectamos as linhas e colunas do teclado.
byte rowPins[4] = { 24, 25, 26, 27 };
byte colPins[4] = { 28, 29, 30, 31 };
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, 4, 4);
String keyboardInput = "";
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Server
RestClient apiService = RestClient("192.168.1.10", 3000);
EthernetServer server(80);
String response;
//Clients
EthernetClient ethClient;
PubSubClient client(ethClient);
aREST rest = aREST(client); // Create aREST instance
char* device_id = "123456";
// ------------------------------------------------
// Setup
// ------------------------------------------------
void setup() {
Serial.begin(115200);
//Inicializa a interface de rede
setupClient();
setupServer();
//out
setupLcd();
setupBuzzer();
setupServos();
//in
// setupMoveSensors();
setupRainSensor();
setupRelay();
}
// ------------------------------------------------
// LOOP
// ------------------------------------------------
void loop() {
//aREST
EthernetClient client = server.available();
rest.handle(client);
//Keyboard
lerTeclado();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Picasso House");
lcd.setCursor(0,1);
lcd.print("Senha: ");
lcd.print(keyboardInput);
//detectar se pressionou a tecla para enviar
if(keyboardInput.indexOf('#') > 0) {
String url = "/api/house/hasReceivedAuthCode?device_id=123456&auth_code=" + keyboardInput;
apiService.get(url.c_str(), &response);
keyboardInput = "";
}
//Move sensors
// readMoveSonsors();
// rain sensors
readRainSensor();
delay(100);
}
// ------------------------------------------------
// Setups
// ------------------------------------------------
void setupClient() {
apiService.dhcp();
server.begin();
Serial.println(Ethernet.localIP());
}
void setupServer() {
// Set callback
client.setCallback(callback);
// Give name & ID to the device
rest.set_id(device_id);
rest.set_name("ethernet");
//API Interface
rest.function("turn_ligth_on", turn_ligth_on);
rest.function("set_buzzer_on", set_buzzer_on);
rest.function("open_garage", open_garage);
rest.function("open_window", open_window);
}
void setupLcd() {
lcd.begin(16, 2);
}
void setupBuzzer() {
pinMode(BUZZER_P, OUTPUT);
}
void setupMoveSensors() {
//TODO: setup todos os quatro sensores de movimento
// pinMode(S_MOV_1, INPUT);
}
void setupRainSensor() {
pinMode(S_RAIN_ANALOG, INPUT); //analog sensor
}
void setupRelay() {
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
}
void setupServos() {
garage.attach(SERVO_GARAGE);
// window.attach(SERVO_WINDOW);
garage.write(0);
// window.write(0);
}
// ------------------------------------------------
// Components Listeners
// ------------------------------------------------
void lerTeclado() {
char key = keypad.getKey();
// Se foi pressionada uma tecla, mostramos qual foi.
if (key != NO_KEY) {
keyboardInput += key;
}
}
void readMoveSonsors() {
//TODO: ler todos os sensores de movimentos e informar a API
//apiService.get("/hasDetectedPresence?device_id=123456&room_id={room_id}", &response);
// int acionamento = digitalRead(S_MOV_1);
//
// if(acionamento == HIGH) {
// digitalWrite(13, HIGH);
// Serial.println("Ligou");
// } else {
// digitalWrite(13, LOW);
// Serial.println("Desligou");
// }
}
void readRainSensor() {
int analogico = analogRead(S_RAIN_ANALOG);
if(analogico > 900) {
hasDetectedRain = false;
}
if (!hasDetectedRain && analogico > 0 && analogico < 400) { // intensidade alta
hasDetectedRain = true;
String url = "/api/house/hasDetectedRain?device_id=123456";
apiService.get(url.c_str(), &response);
}
}
// ------------------------------------------------
// API Interface
// ------------------------------------------------
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
rest.handle_callback(client, topic, payload, length);
}
// GET /turn_ligth_on?param=_{room_id}&{is_on}
void turn_ligth_on(String message) {
int room_id, is_on;
int IN_X;
int i = message.indexOf('&');
room_id = message.substring(0,i).toInt();
is_on = message.substring(i+1).toInt();
//descobrir a porta referente ao rele
switch(room_id) {
case 1: IN_X = IN_1; break;
case 2: IN_X = IN_2; break;
case 3: IN_X = IN_3; break;
case 4: IN_X = IN_4; break;
}
//ligar ou desligar a luz
digitalWrite(IN_X, is_on ? LOW : HIGH);
}
// GET /set_buzzer_on?param=_{is_on}
void set_buzzer_on(String message) {
int is_on = message.toInt();
digitalWrite(BUZZER_P, is_on ? HIGH : LOW);
}
// GET /open_garage?param=_{is_on}
void open_garage(String message) {
int is_open = message.toInt();
garage.write(is_open ? 90 : 5);
}
// GET /open_window?param=_{room_id}&{is_open}
void open_window(String message) {
int room_id, is_open;
int i = message.indexOf('&');
room_id = message.substring(0,i).toInt();
is_open = message.substring(i+1).toInt();
window.write(is_open ? 90 : 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment