From 8ba23873b8e60c8ad2627bd433ec7e9240a6b987 Mon Sep 17 00:00:00 2001 From: spekulaas Date: Thu, 15 Apr 2021 01:55:25 +0200 Subject: [PATCH] made temp function and made a begin on other funcs --- .../CrabbyHome-master/platformio.ini | 8 +- .../CrabbyHome-master/src/Connection.h | 39 ++++++++ .../CrabbyHome-master/src/DistanceSensor.h | 38 +++++++ .../CrabbyHome-master/src/RFTransmit.h | 71 ++++++++++++++ .../src/{screen.h => Screen.h} | 8 +- .../CrabbyHome-master/src/Temperature.h | 98 +++++++++++++++++++ .../CrabbyHome-master/src/main.cpp | 22 +++-- .../CrabbyHome-master/src/temperature.h | 29 ------ 8 files changed, 272 insertions(+), 41 deletions(-) create mode 100644 CrabbyHome-comp/CrabbyHome-master/src/Connection.h create mode 100644 CrabbyHome-comp/CrabbyHome-master/src/DistanceSensor.h create mode 100644 CrabbyHome-comp/CrabbyHome-master/src/RFTransmit.h rename CrabbyHome-comp/CrabbyHome-master/src/{screen.h => Screen.h} (85%) create mode 100644 CrabbyHome-comp/CrabbyHome-master/src/Temperature.h delete mode 100644 CrabbyHome-comp/CrabbyHome-master/src/temperature.h diff --git a/CrabbyHome-comp/CrabbyHome-master/platformio.ini b/CrabbyHome-comp/CrabbyHome-master/platformio.ini index 524f15e..e64ff61 100644 --- a/CrabbyHome-comp/CrabbyHome-master/platformio.ini +++ b/CrabbyHome-comp/CrabbyHome-master/platformio.ini @@ -12,7 +12,11 @@ platform = atmelavr board = megaatmega2560 framework = arduino +monitor_speed = 115200 lib_deps = - https://github.com/adafruit/Adafruit_Sensor.git - https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library \ No newline at end of file + https://git.aterve.com/Frozenverse/SimpleSerialProtocol.git + https://github.com/adafruit/Adafruit_Sensor.git + https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library + https://github.com/1technophile/NewRemoteSwitch.git + https://bitbucket.org/teckel12/arduino-new-ping.git \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-master/src/Connection.h b/CrabbyHome-comp/CrabbyHome-master/src/Connection.h new file mode 100644 index 0000000..8e74897 --- /dev/null +++ b/CrabbyHome-comp/CrabbyHome-master/src/Connection.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include +#include +namespace Connection +{ + SimpleSerialProtocol debugger; + SimpleSerialProtocol networker; + + void init(); + void handle(); + void onNetworkerPacket(Packet packet); +}; + +void Connection::init() { + debugger.init(0, 115200); + debugger.sendPacket("debugger", "serial_status", "ready"); + + networker.init(2, 115200); + networker.sendPacket("networker", "serial_status", "ready"); +} + +void Connection::handle() { + networker.receivePackets(onNetworkerPacket); + + Temperature::handle(&networker); + // DistanceSensor::handle(&networker); +} + +void Connection::onNetworkerPacket(Packet packet) { + debugger.sendPacket(packet.id, packet.command, packet.data); + + // Handle Lights + if (strcmp(packet.id, "main_light") == 0) { + RFTransmit::handlePacket(&networker, packet.id, packet.command, packet.data); + } +} \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-master/src/DistanceSensor.h b/CrabbyHome-comp/CrabbyHome-master/src/DistanceSensor.h new file mode 100644 index 0000000..6ae4ec1 --- /dev/null +++ b/CrabbyHome-comp/CrabbyHome-master/src/DistanceSensor.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +// define pins +#define TRIGGER_PIN 12 +#define ECHO_PIN 11 + +// define max distance so it doesnt go infinite +#define MAX_DISTANCE 400 +NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); + +namespace DistanceSensor{ + unsigned long previousMillis = 0; + unsigned long timerInterval = 500; + + float stdDistance = 180; + + void handle(SimpleSerialProtocol *endpoint); +} + +void DistanceSensor::handle(SimpleSerialProtocol *endpoint){ + if ((millis() - previousMillis) > timerInterval){ + previousMillis = millis(); + + // Send ping, get distance in cm + float distance = sonar.ping_cm(); + + // Send results to Serial Monitor + if(distance < 30 - 20){ + endpoint->sendPacket("access", "update_state", "bool;motion;true"); + }else{ + endpoint->sendPacket("access", "update_state", "bool;motion;false"); + } + + } +} \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-master/src/RFTransmit.h b/CrabbyHome-comp/CrabbyHome-master/src/RFTransmit.h new file mode 100644 index 0000000..54057fa --- /dev/null +++ b/CrabbyHome-comp/CrabbyHome-master/src/RFTransmit.h @@ -0,0 +1,71 @@ +#pragma once +#include +#include + +#define RF_SEND_PIN 11 + +namespace RFTransmit{ + char rf_switch_on[10] = "true"; + char rf_switch_off[10] = "false"; + char rf_type_switch[10] = "switch"; + char rf_type_switchgroup[20] = "switchgroup"; + + void handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data); + void send(int unitcode, char* commandType, char* commandValue); +} + +void RFTransmit::handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data){ + //ambiance light + if(strcmp(id, "ambiance_light") == 0){ + if (strcmp(action, "on") == 0){ + RFTransmit::send(0, rf_type_switch, rf_switch_on); + endpoint->sendPacket("access", "update_state", "bool;ambiance_light;true"); + } + + if (strcmp(action, "off") == 0){ + RFTransmit::send(0, rf_type_switch, rf_switch_off); + endpoint->sendPacket("access", "update_state", "bool;ambiance_light;false"); + } + } + + //Main group lights + if(strcmp(id, "main_group") == 0){ + if (strcmp(action, "on") == 0){ + RFTransmit::send(0, rf_type_switchgroup, rf_switch_on); + endpoint->sendPacket("access", "update_state", "bool;ambiance_light;true"); + } + + if (strcmp(action, "off") == 0){ + RFTransmit::send(0, rf_type_switchgroup, rf_switch_off); + endpoint->sendPacket("access", "update_state", "bool;ambiance_light;false"); + } + } +} + +void RFTransmit::send(int unitcode, char* commandType, char* commandValue) { + NewRemoteTransmitter mainTransmitter(long(27491570), RF_SEND_PIN, 268); + // for switch remotes + if(strcmp(commandType, rf_type_switch) == 0) { + + // ambiance light + if(strcmp(commandValue, rf_switch_on) == 0) { + mainTransmitter.sendUnit(unitcode, true); + } + + if(strcmp(commandValue, rf_switch_off) == 0) { + mainTransmitter.sendUnit(unitcode, false); + } + } + if(strcmp(commandType, rf_type_switchgroup) == 0) { + + // main group + if(strcmp(commandValue, rf_switch_on) == 0) { + mainTransmitter.sendGroup(true); + } + + if(strcmp(commandValue, rf_switch_off) == 0) { + mainTransmitter.sendGroup(false); + } + } + +} diff --git a/CrabbyHome-comp/CrabbyHome-master/src/screen.h b/CrabbyHome-comp/CrabbyHome-master/src/Screen.h similarity index 85% rename from CrabbyHome-comp/CrabbyHome-master/src/screen.h rename to CrabbyHome-comp/CrabbyHome-master/src/Screen.h index 8d46f12..cb847e0 100644 --- a/CrabbyHome-comp/CrabbyHome-master/src/screen.h +++ b/CrabbyHome-comp/CrabbyHome-master/src/Screen.h @@ -1,14 +1,16 @@ +#pragma once +#include #include #include LiquidCrystal_I2C lcd(0x3f, 16, 2); -namespace screen{ +namespace Screen{ void init(); void display(char* title, char* text); } -void screen::init(){ +void Screen::init(){ lcd.begin(); // Print a message to the LCD. lcd.backlight(); @@ -18,7 +20,7 @@ void screen::init(){ lcd.print("Activated!"); } -void screen::display(char* title, char* text){ +void Screen::display(char* title, char* text){ int titleSize = strlen(title); int textSize = strlen(text); lcd.clear(); diff --git a/CrabbyHome-comp/CrabbyHome-master/src/Temperature.h b/CrabbyHome-comp/CrabbyHome-master/src/Temperature.h new file mode 100644 index 0000000..a2c0302 --- /dev/null +++ b/CrabbyHome-comp/CrabbyHome-master/src/Temperature.h @@ -0,0 +1,98 @@ +#pragma once + +#include + +#define DHT_SENSOR_PIN 7 // Define DHT Connected Data Pin +#define DHT_TYPE DHT11 // Define DHT Sensor Type +DHT dht_sensor(DHT_SENSOR_PIN, DHT_TYPE); // Set info for dht + +// create namespace +namespace Temperature{ + unsigned long previousMillis = 0; + unsigned long timerInterval = 3000; + + float temp = 0; + float hum = 0; + + void init(); + + void handle(SimpleSerialProtocol *endpoint); + // void handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data); + // void sendPacket(SimpleSerialProtocol *endpoint); +} + +void Temperature::init(){ + dht_sensor.begin(); + Serial.println("temp sensor init"); +} + +void Temperature::handle(SimpleSerialProtocol *endpoint){ + if ((millis() - previousMillis) > timerInterval){ + previousMillis = millis(); + // Temperature::sendPacket(endpoint); + + temp = dht_sensor.readTemperature(); + hum = dht_sensor.readHumidity(); + + // Get temperature and convert float to char[] + char currentTemp[10]; + dtostrf(temp, 9, 1, currentTemp); + + + // Get humidity and convert float to char[] + char currentHum[10]; + dtostrf(hum, 9, 1, currentHum); + + // Send packet temp + char tempDataBuffer[64]; + sprintf(tempDataBuffer, "%s;%s;%s", "float", "main_temp", currentTemp); + endpoint->sendPacket("access", "update_state", tempDataBuffer); + + // Send packet temp + char humDataBuffer[64]; + sprintf(humDataBuffer, "%s;%s;%s", "float", "main_hum", currentHum); + endpoint->sendPacket("access", "update_state", humDataBuffer); + + + + } +} + +// void Temperature::handlePacket(SimpleSerialProtocol *endpoint, char* id, char* action, char* data){ +// if (strcmp(action, "get_temperature_data") == 0) { +// // Get temperature and convert float to char[] +// char currentTemp[10]; +// dtostrf(temp, 9, 1, currentTemp); + +// // Get humidity and convert float to char[] +// char currentHum[10]; +// dtostrf(hum, 9, 1, currentHum); + +// // Send packet +// char dataBuffer[40]; +// sprintf(dataBuffer, "%s,%s", currentTemp, currentHum); + +// endpoint->sendPacket(id, "climate_data", dataBuffer); +// } +// } + +// void sendPacket(SimpleSerialProtocol *endpoint){ +// // Get temperature and convert float to char[] +// char currentTemp[10]; +// dtostrf(readTemp(), 9, 1, currentTemp); + + +// // Get humidity and convert float to char[] +// char currentHum[10]; +// dtostrf(readHum(), 9, 1, currentHum); + +// // Send packet temp +// char tempDataBuffer[40]; +// sprintf(tempDataBuffer, "%s,%s,%s", "float", "main_temp", currentTemp); +// endpoint->sendPacket("access", "update_state", tempDataBuffer); + +// // Send packet temp +// char humDataBuffer[40]; +// sprintf(humDataBuffer, "%s,%s,%s", "float", "main_hum", currentHum); +// endpoint->sendPacket("access", "update_state", humDataBuffer); +// } \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-master/src/main.cpp b/CrabbyHome-comp/CrabbyHome-master/src/main.cpp index 14b5a71..4654fbe 100644 --- a/CrabbyHome-comp/CrabbyHome-master/src/main.cpp +++ b/CrabbyHome-comp/CrabbyHome-master/src/main.cpp @@ -1,13 +1,21 @@ #include -#include -#include + +#include +#include +#include +// // #include +// #include void setup() { - Serial.begin(9600); - temperature::init(); - screen::init(); + Connection::init(); + Temperature::init(); + Screen::init(); + RFTransmit::send(1, "switchgroup", "true"); } -void loop() { - +void loop() +{ + + // delay(500); + Connection::handle(); } \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-master/src/temperature.h b/CrabbyHome-comp/CrabbyHome-master/src/temperature.h deleted file mode 100644 index 5c01af2..0000000 --- a/CrabbyHome-comp/CrabbyHome-master/src/temperature.h +++ /dev/null @@ -1,29 +0,0 @@ -#include - -#define DHT_SENSOR_PIN 7 // Define DHT Connected Data Pin -#define DHT_TYPE DHT11 // Define DHT Sensor Type -DHT dht_sensor(DHT_SENSOR_PIN, DHT_TYPE); // Set info for dht - -// create namespace -namespace temperature{ - void init(); - float readTemp(); - float readHum(); -} - -void temperature::init(){ - dht_sensor.begin(); - Serial.println("temp sensor init"); -} - -float temperature::readTemp(){ - float temp = dht_sensor.readTemperature(); // read temp - - return temp; -} - -float temperature::readHum(){ - float hum = dht_sensor.readHumidity(); // read temp - - return hum; -} \ No newline at end of file