made temp function and made a begin on other funcs

This commit is contained in:
Ryan Bakkes 2021-04-15 01:55:25 +02:00
parent 49e38e2fc2
commit 8ba23873b8
8 changed files with 272 additions and 41 deletions

View File

@ -12,7 +12,11 @@
platform = atmelavr platform = atmelavr
board = megaatmega2560 board = megaatmega2560
framework = arduino framework = arduino
monitor_speed = 115200
lib_deps = lib_deps =
https://github.com/adafruit/Adafruit_Sensor.git https://git.aterve.com/Frozenverse/SimpleSerialProtocol.git
https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library 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

View File

@ -0,0 +1,39 @@
#pragma once
#include <Arduino.h>
#include <SimpleSerialProtocol.h>
#include <Temperature.h>
#include <RFTransmit.h>
#include <DistanceSensor.h>
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);
}
}

View File

@ -0,0 +1,38 @@
#pragma once
#include <NewPing.h>
#include <Screen.h>
// 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");
}
}
}

View File

@ -0,0 +1,71 @@
#pragma once
#include <Arduino.h>
#include <NewRemoteTransmitter.h>
#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);
}
}
}

View File

@ -1,14 +1,16 @@
#pragma once
#include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2); LiquidCrystal_I2C lcd(0x3f, 16, 2);
namespace screen{ namespace Screen{
void init(); void init();
void display(char* title, char* text); void display(char* title, char* text);
} }
void screen::init(){ void Screen::init(){
lcd.begin(); lcd.begin();
// Print a message to the LCD. // Print a message to the LCD.
lcd.backlight(); lcd.backlight();
@ -18,7 +20,7 @@ void screen::init(){
lcd.print("Activated!"); lcd.print("Activated!");
} }
void screen::display(char* title, char* text){ void Screen::display(char* title, char* text){
int titleSize = strlen(title); int titleSize = strlen(title);
int textSize = strlen(text); int textSize = strlen(text);
lcd.clear(); lcd.clear();

View File

@ -0,0 +1,98 @@
#pragma once
#include <DHT.h>
#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);
// }

View File

@ -1,13 +1,21 @@
#include <Arduino.h> #include <Arduino.h>
#include <temperature.h>
#include <screen.h> #include <Connection.h>
#include <Temperature.h>
#include <Screen.h>
// // #include <DistanceSensor.h>
// #include <RFTransmit.h>
void setup() { void setup() {
Serial.begin(9600); Connection::init();
temperature::init(); Temperature::init();
screen::init(); Screen::init();
RFTransmit::send(1, "switchgroup", "true");
} }
void loop() { void loop()
{
// delay(500);
Connection::handle();
} }

View File

@ -1,29 +0,0 @@
#include <DHT.h>
#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;
}