initial push
This commit is contained in:
7
echo-main-controller/.vscode/arduino.json
vendored
Normal file
7
echo-main-controller/.vscode/arduino.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"board": "arduino:avr:mega",
|
||||
"configuration": "cpu=atmega2560",
|
||||
"port": "/dev/ttyACM0",
|
||||
"sketch": "echo-main-controller.ino",
|
||||
"programmer": "AVR ISP"
|
||||
}
|
28
echo-main-controller/.vscode/c_cpp_properties.json
vendored
Normal file
28
echo-main-controller/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"/home/nick/Arduino/libraries/IRremote",
|
||||
"/home/nick/Arduino/libraries/U8g2/src",
|
||||
"/home/nick/Arduino/libraries/DHT_sensor_library",
|
||||
"/home/nick/Arduino/libraries/NewRemoteSwitch",
|
||||
"/home/nick/Software/arduino-1.8.10/tools/**",
|
||||
"/home/nick/Software/arduino-1.8.10/hardware/arduino/avr/**",
|
||||
"/home/nick/Software/arduino-1.8.10/libraries/**",
|
||||
"/home/nick/Software/arduino-1.8.10/hardware/tools/avr/avr/**"
|
||||
],
|
||||
"forcedInclude": [
|
||||
"/home/nick/Software/arduino-1.8.10/hardware/arduino/avr/cores/arduino/Arduino.h"
|
||||
],
|
||||
"intelliSenseMode": "gcc-x64",
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++17",
|
||||
"defines": [
|
||||
"USBCON"
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
3
echo-main-controller/.vscode/settings.json
vendored
Normal file
3
echo-main-controller/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "Disabled"
|
||||
}
|
35
echo-main-controller/echo-main-ac.ino
Normal file
35
echo-main-controller/echo-main-ac.ino
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
void airConditionerCommand(char* deviceUUID, char* command, char* data) {
|
||||
|
||||
// Main Air Conditioner
|
||||
if (strcmp(deviceUUID, "70c4-40d2") == 0) {
|
||||
|
||||
if (strcmp(command, "power") == 0) {
|
||||
unsigned int rawData[67] = {8950,4500, 600,550, 600,550, 600,1700, 550,600, 550,600, 550,600, 550,600, 550,600, 550,1700, 600,1650, 600,550, 600,1650, 600,1700, 550,1700, 550,1700, 550,1700, 550,600, 550,600, 600,1650, 600,550, 600,550, 600,600, 550,600, 550,600, 550,1700, 550,1700, 550,600, 550,1700, 600,1650, 600,1650, 600,1700, 550,1700, 550}; // NEC 20DF20DF
|
||||
irTransmitter.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), irTransmitterFreq);
|
||||
Serial.println("whoop");
|
||||
}
|
||||
|
||||
if (strcmp(command, "fanspeed") == 0) {
|
||||
unsigned int rawData[67] = {9000,4500, 550,600, 550,600, 600,1650, 600,550, 600,550, 600,550, 600,600, 550,600, 550,1700, 550,1700, 550,600, 550,1700, 550,1700, 600,1650, 600,1650, 600,1700, 550,600, 550,1700, 550,1700, 550,600, 550,600, 550,600, 600,550, 600,550, 600,1650, 600,600, 550,600, 550,1700, 550,1700, 550,1700, 550,1700, 600,1650, 600}; // NEC 20DF609F
|
||||
irTransmitter.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), irTransmitterFreq);
|
||||
}
|
||||
|
||||
if (strcmp(command, "up") == 0) {
|
||||
unsigned int rawData[67] = {9000,4500, 550,600, 550,600, 550,1700, 550,600, 550,600, 550,600, 600,550, 600,550, 600,1650, 600,1700, 550,600, 550,1700, 550,1700, 550,1700, 550,1700, 600,1650, 600,1650, 600,600, 550,1700, 550,600, 550,600, 550,600, 550,600, 550,600, 550,600, 600,1650, 600,550, 600,1650, 600,1700, 550,1700, 550,1700, 550,1700, 550}; // NEC 20DFA05F
|
||||
irTransmitter.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), irTransmitterFreq);
|
||||
}
|
||||
|
||||
if (strcmp(command, "down") == 0) {
|
||||
unsigned int rawData[67] = {8950,4500, 600,550, 600,550, 600,1650, 600,600, 550,600, 550,600, 550,600, 550,600, 550,1700, 550,1700, 600,550, 600,1650, 600,1650, 600,1700, 550,1700, 550,1700, 550,1700, 550,600, 550,600, 600,550, 600,550, 600,550, 600,550, 600,600, 550,600, 550,1700, 550,1700, 550,1700, 550,1700, 600,1650, 600,1650, 600,1650, 600}; // NEC 20DF807F
|
||||
irTransmitter.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), irTransmitterFreq);
|
||||
}
|
||||
|
||||
if (strcmp(command, "mode") == 0) {
|
||||
unsigned int rawData[67] = {8950,4500, 600,550, 600,550, 600,1700, 550,600, 550,600, 550,600, 550,600, 550,600, 550,1700, 550,1700, 600,550, 600,1650, 600,1650, 600,1700, 550,1700, 550,1700, 550,1700, 550,600, 600,1650, 600,1650, 600,550, 600,600, 550,600, 550,600, 550,600, 550,1700, 550,600, 550,600, 550,1700, 600,1650, 600,1650, 600,1700, 550}; // NEC 20DFB04F
|
||||
irTransmitter.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), irTransmitterFreq);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
17
echo-main-controller/echo-main-blinds.ino
Normal file
17
echo-main-controller/echo-main-blinds.ino
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Sun Blinds
|
||||
//
|
||||
|
||||
void sunBlindsCommand(char* deviceUUID, char* command, char* data) {
|
||||
|
||||
// Main Sun Blinds
|
||||
if (strcmp(deviceUUID, "b039-471a") == 0) {
|
||||
if (strcmp(command, "up") == 0) {
|
||||
sendRFNCommand(0, rf_type_switch, rf_switch_on);
|
||||
}
|
||||
|
||||
if (strcmp(command, "down") == 0) {
|
||||
sendRFNCommand(0, rf_type_switch, rf_switch_off);
|
||||
}
|
||||
}
|
||||
}
|
42
echo-main-controller/echo-main-climate.ino
Normal file
42
echo-main-controller/echo-main-climate.ino
Normal file
@@ -0,0 +1,42 @@
|
||||
// DHT Climate Sensor
|
||||
//
|
||||
#include <DHT.h>
|
||||
#define DHTPIN 6 // Define DHT Connected Data Pin
|
||||
#define DHTTYPE DHT22 // Define DHT Sensor Type
|
||||
DHT dht_main(DHTPIN, DHTTYPE); // Initialize dht Object
|
||||
|
||||
//
|
||||
// Climate Sensors
|
||||
//
|
||||
|
||||
void initClimateSensors() {
|
||||
// Init Main Climate Sensor
|
||||
dht_main.begin();
|
||||
}
|
||||
|
||||
float getClimateTemperature(char* uuid) {
|
||||
if(strcmp(uuid, "aa50-48fd") == 0){
|
||||
return dht_main.readTemperature();
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float getClimateHumidity(char* uuid) {
|
||||
if(strcmp(uuid, "aa50-48fd") == 0){
|
||||
return dht_main.readHumidity();
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
void climateSensorCommand(char* deviceUUID, char* command, char* data) {
|
||||
if (strcmp(command, "gather_climate_data") == 0) {
|
||||
char currentTemp[10];
|
||||
dtostrf(getClimateTemperature(deviceUUID), 9, 1, currentTemp);
|
||||
char currentHumidity[10];
|
||||
dtostrf(getClimateHumidity(deviceUUID), 9, 1, currentHumidity);
|
||||
char returnCommand[20] = "climate_data";
|
||||
char dataBuffer[40];
|
||||
sprintf(dataBuffer, "%s,%s", currentTemp, currentHumidity);
|
||||
sendSerialCommand(deviceUUID, returnCommand, dataBuffer);
|
||||
}
|
||||
}
|
240
echo-main-controller/echo-main-controller.ino
Normal file
240
echo-main-controller/echo-main-controller.ino
Normal file
@@ -0,0 +1,240 @@
|
||||
#include <NewRemoteReceiver.h>
|
||||
#include <NewRemoteTransmitter.h>
|
||||
#include <IRremote.h>
|
||||
|
||||
// Serial Connection Variables
|
||||
//
|
||||
const byte serialBufferSize = 100;
|
||||
char serialBuffer[serialBufferSize];
|
||||
const char startMarker = '<';
|
||||
const char endMarker = '>';
|
||||
byte serialBytesReceived = 0;
|
||||
boolean serialReadInProgress = false;
|
||||
boolean newSerialData = false;
|
||||
char serialData[serialBufferSize] = {0};
|
||||
|
||||
char commandLimiter[2] = ":";
|
||||
|
||||
// IR Comms
|
||||
//
|
||||
#define IR_SEND_PIN 9 // NOT CHANGEABLE
|
||||
IRsend irTransmitter;
|
||||
int irTransmitterFreq = 38; // 38KHz NEC Protocol
|
||||
|
||||
// RF Comms
|
||||
//
|
||||
#define RF_SEND_PIN 7
|
||||
char rf_switch_on[10] = "true";
|
||||
char rf_switch_off[10] = "false";
|
||||
char rf_type_switch[10] = "switch";
|
||||
|
||||
// Main Program
|
||||
//
|
||||
|
||||
void setup() {
|
||||
// Init Serial Connection
|
||||
Serial.begin(115200);
|
||||
|
||||
// Init Serial Connection LED Controller
|
||||
Serial1.begin(115200);
|
||||
|
||||
// Init RF Transmitters / Receivers
|
||||
|
||||
// Init Climate Sensors
|
||||
initClimateSensors();
|
||||
|
||||
// Init Movement Sensor
|
||||
initMovementSensor();
|
||||
|
||||
Serial.println("<0:0:0>");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Handle Serial Comms
|
||||
handleSerial();
|
||||
|
||||
// Handle Motion Sensor
|
||||
handleMotionSensor();
|
||||
}
|
||||
|
||||
// Serial Handling
|
||||
//
|
||||
void handleSerial() {
|
||||
if(Serial.available() > 0) {
|
||||
char x = Serial.read();
|
||||
|
||||
// the order of these IF clauses is significant
|
||||
if (x == endMarker) {
|
||||
serialReadInProgress = false;
|
||||
newSerialData = true;
|
||||
serialBuffer[serialBytesReceived] = 0;
|
||||
parseData();
|
||||
}
|
||||
|
||||
if(serialReadInProgress) {
|
||||
serialBuffer[serialBytesReceived] = x;
|
||||
serialBytesReceived ++;
|
||||
if (serialBytesReceived == serialBufferSize) {
|
||||
serialBytesReceived = serialBufferSize - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (x == startMarker) {
|
||||
serialBytesReceived = 0;
|
||||
serialReadInProgress = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(Serial1.available() > 0) {
|
||||
char x = Serial1.read();
|
||||
|
||||
// the order of these IF clauses is significant
|
||||
if (x == endMarker) {
|
||||
serialReadInProgress = false;
|
||||
newSerialData = true;
|
||||
serialBuffer[serialBytesReceived] = 0;
|
||||
parseSerial1Data();
|
||||
}
|
||||
|
||||
if(serialReadInProgress) {
|
||||
serialBuffer[serialBytesReceived] = x;
|
||||
serialBytesReceived ++;
|
||||
if (serialBytesReceived == serialBufferSize) {
|
||||
serialBytesReceived = serialBufferSize - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (x == startMarker) {
|
||||
serialBytesReceived = 0;
|
||||
serialReadInProgress = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void parseData() {
|
||||
char * strtokIndex;
|
||||
char deviceUUID[10];
|
||||
char command[20];
|
||||
char data[69];
|
||||
|
||||
// Get Device UUID
|
||||
strtokIndex = strtok(serialBuffer, ":");
|
||||
strcpy(deviceUUID, strtokIndex);
|
||||
|
||||
// Get Command
|
||||
strtokIndex = strtok(NULL, ":");
|
||||
strcpy(command, strtokIndex);
|
||||
|
||||
// Get Data
|
||||
strtokIndex = strtok(NULL, ":");
|
||||
strcpy(data, strtokIndex);
|
||||
|
||||
handleCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
void parseSerial1Data() {
|
||||
char * strtokIndex;
|
||||
char deviceUUID[10];
|
||||
char command[20];
|
||||
char data[69];
|
||||
|
||||
// Get Device UUID
|
||||
strtokIndex = strtok(serialBuffer, ":");
|
||||
strcpy(deviceUUID, strtokIndex);
|
||||
|
||||
strtokIndex = strtok(NULL, ":");
|
||||
strcpy(command, strtokIndex);
|
||||
|
||||
strtokIndex = strtok(NULL, ":");
|
||||
strcpy(data, strtokIndex);
|
||||
|
||||
sendSerialCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
void sendSerialCommand(char* deviceUUID, char* command, char* data) {
|
||||
char serialCommandBuffer[serialBufferSize];
|
||||
char _startMarker[2] = "<";
|
||||
char _endMarker[2] = ">";
|
||||
sprintf(serialCommandBuffer, "%s%s:%s:%s%s", _startMarker, deviceUUID, command, data, _endMarker);
|
||||
Serial.println(serialCommandBuffer);
|
||||
}
|
||||
|
||||
|
||||
void sendSerial1Command(char* deviceUUID, char* command, char* data) {
|
||||
char serialCommandBuffer[serialBufferSize];
|
||||
char _startMarker[2] = "<";
|
||||
char _endMarker[2] = ">";
|
||||
sprintf(serialCommandBuffer, "%s%s:%s:%s%s", _startMarker, deviceUUID, command, data, _endMarker);
|
||||
Serial1.println(serialCommandBuffer);
|
||||
}
|
||||
|
||||
|
||||
void handleCommand(char* deviceUUID, char* command, char* data) {
|
||||
//
|
||||
// Climate Sensors
|
||||
//
|
||||
if (strcmp(deviceUUID, "aa50-48fd") == 0) {
|
||||
climateSensorCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
//
|
||||
// Light Sensors
|
||||
//
|
||||
if (strcmp(deviceUUID, "be0c-4bc7") == 0) {
|
||||
lightSensorCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Lights
|
||||
//
|
||||
if (strcmp(deviceUUID, "9472-4f60") == 0) {
|
||||
lightSwitchCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Sun Blinds
|
||||
//
|
||||
|
||||
if (strcmp(deviceUUID, "b039-471a") == 0) {
|
||||
sunBlindsCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Air Conditioner
|
||||
//
|
||||
|
||||
if (strcmp(deviceUUID, "70c4-40d2") == 0) {
|
||||
airConditionerCommand(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle LED Strip
|
||||
//
|
||||
if (strcmp(deviceUUID, "34gr-54jf") == 0) {
|
||||
sendSerial1Command(deviceUUID, command, data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// RF Trancieving
|
||||
//
|
||||
|
||||
void sendRFNCommand(int unitCode, char* commandType, char* commandValue) {
|
||||
NewRemoteTransmitter mainTransmitter(long(34659090), 7, 255);
|
||||
|
||||
if(strcmp(commandType, "switch") == 0) {
|
||||
|
||||
// True means on
|
||||
if(strcmp(commandValue, "true") == 0) {
|
||||
mainTransmitter.sendUnit(unitCode, true);
|
||||
}
|
||||
|
||||
// False means off
|
||||
if(strcmp(commandValue, "false") == 0) {
|
||||
mainTransmitter.sendUnit(unitCode, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
52
echo-main-controller/echo-main-helpers.ino
Normal file
52
echo-main-controller/echo-main-helpers.ino
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Helper functions
|
||||
//
|
||||
|
||||
char* dtostrf(double number, signed char width, unsigned char prec, char *s) {
|
||||
|
||||
if(isnan(number)) {
|
||||
strcpy(s, "nan");
|
||||
return s;
|
||||
}
|
||||
if(isinf(number)) {
|
||||
strcpy(s, "inf");
|
||||
return s;
|
||||
}
|
||||
|
||||
if(number > 4294967040.0 || number < -4294967040.0) {
|
||||
strcpy(s, "ovf");
|
||||
return s;
|
||||
}
|
||||
char* out = s;
|
||||
// Handle negative numbers
|
||||
if(number < 0.0) {
|
||||
*out = '-';
|
||||
++out;
|
||||
number = -number;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
double rounding = 0.5;
|
||||
for(uint8_t i = 0; i < prec; ++i)
|
||||
rounding /= 10.0;
|
||||
|
||||
number += rounding;
|
||||
|
||||
// Extract the integer part of the number and print it
|
||||
unsigned long int_part = (unsigned long) number;
|
||||
double remainder = number - (double) int_part;
|
||||
out += sprintf(out, "%d", int_part);
|
||||
|
||||
// Print the decimal point, but only if there are digits beyond
|
||||
if(prec > 0) {
|
||||
*out = '.';
|
||||
++out;
|
||||
}
|
||||
|
||||
while(prec-- > 0) {
|
||||
remainder *= 10.0;
|
||||
}
|
||||
sprintf(out, "%d", (int) remainder);
|
||||
|
||||
return s;
|
||||
}
|
15
echo-main-controller/echo-main-light.ino
Normal file
15
echo-main-controller/echo-main-light.ino
Normal file
@@ -0,0 +1,15 @@
|
||||
int lightAmount = 0;
|
||||
|
||||
int getLightSensorAmount () {
|
||||
lightAmount = analogRead(A0);
|
||||
return lightAmount;
|
||||
}
|
||||
|
||||
void lightSensorCommand(char* deviceUUID, char* command, char* data) {
|
||||
if (strcmp(command, "gather_light_data") == 0) {
|
||||
char returnCommand[20] = "light_data";
|
||||
char dataBuffer[40];
|
||||
itoa(getLightSensorAmount(), dataBuffer, 10);
|
||||
sendSerialCommand(deviceUUID, returnCommand, dataBuffer);
|
||||
}
|
||||
}
|
19
echo-main-controller/echo-main-lights.ino
Normal file
19
echo-main-controller/echo-main-lights.ino
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Lights
|
||||
//
|
||||
|
||||
void lightSwitchCommand(char* deviceUUID, char* command, char* data) {
|
||||
|
||||
// Main Light
|
||||
if (strcmp(deviceUUID, "9472-4f60") == 0) {
|
||||
|
||||
if (strcmp(command, "on") == 0) {
|
||||
sendRFNCommand(1, rf_type_switch, rf_switch_on);
|
||||
}
|
||||
|
||||
if (strcmp(command, "off") == 0) {
|
||||
sendRFNCommand(1, rf_type_switch, rf_switch_off);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
34
echo-main-controller/echo-main-movement.ino
Normal file
34
echo-main-controller/echo-main-movement.ino
Normal file
@@ -0,0 +1,34 @@
|
||||
#define MovementSensorPin 5
|
||||
|
||||
int motionDetected = LOW; // Default no motion Detected
|
||||
int motionDetectedAmount = 0; // Amount of motion detected count
|
||||
int motionValue = 0;
|
||||
|
||||
void initMovementSensor() {
|
||||
pinMode(MovementSensorPin, INPUT);
|
||||
}
|
||||
|
||||
void handleMotionSensor() {
|
||||
char deviceUUID[10] = "a328-4229";
|
||||
motionValue = digitalRead(MovementSensorPin);
|
||||
|
||||
// Detected Movement
|
||||
if (motionValue == HIGH) {
|
||||
|
||||
if (motionDetected == LOW) {
|
||||
motionDetected = HIGH;
|
||||
char command[20] = "movement_data";
|
||||
char dataBuffer[40] = "detected_movement";
|
||||
sendSerialCommand(deviceUUID, command, dataBuffer);
|
||||
}
|
||||
} else {
|
||||
|
||||
if(motionDetected == HIGH) {
|
||||
motionDetected = LOW;
|
||||
char command[20] = "movement_data";
|
||||
char dataBuffer[40] = "no_movement";
|
||||
sendSerialCommand(deviceUUID, command, dataBuffer);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user