Skip to content

Commit

Permalink
Major update, 2.0
Browse files Browse the repository at this point in the history
MQTT for messaging, control panel in React.js
  • Loading branch information
arturi committed Sep 11, 2015
1 parent 6d86982 commit 8c666b7
Show file tree
Hide file tree
Showing 40 changed files with 2,113 additions and 931 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
config.json
218 changes: 0 additions & 218 deletions Arduino/KotiServer.ino

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Koti Home uses Node.js on a server (I use a [DigitalOcean VPS](https://www.digit
2. Clone this repository, rename ```example_config.json``` to ```config.json``` and change all the credentials, like email and passwords. Also set your local IP address and the remote URL at ```Arduino/KotiServer.ino```.
3. Upload the ```Arduino/KotiServer.ino``` to Arduino board and set up the ```koti.js``` app on your server. I recommend [PM2](https://github.com/Unitech/pm2).
4. Buy a huge chair, a desk and a Darth Vader mask. Make a Margarita and control ~~the planet~~ your house.
Disclaimer: chair, desk, mask and Margarita are sold separately.
Disclaimer: chair, desk, mask and Margarita are sold separately.
130 changes: 130 additions & 0 deletions arduino/koti.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// analog pins
int tempPin = A5;
int luxPin = A4;

// digital pins
int lightPin = 8;
int motionPin = 5;

// variables
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false;
boolean lightIsON = false;
long lastMovement;

// setup
void setup() {
pinMode(tempPin, INPUT);
pinMode(luxPin, INPUT);
pinMode(lightPin, OUTPUT);
pinMode(motionPin, INPUT);

// inputString.reserve(200);
Serial.begin(9600);
}

// main loop
void loop() {

Serial.println("");
Serial.print("{");
Serial.print("\"temp\": ");
Serial.print(getTemp());
Serial.print(",");

Serial.print("\"lux\": ");
Serial.print(getLux());
Serial.print(",");

Serial.print("\"motion\": ");
Serial.print(getMotion());
Serial.print(",");

Serial.print("\"lightIsOn\": ");
Serial.print(lightIsON);

Serial.print("}");

//Serial.println("");

if (stringComplete) {
action(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}

delay(1000);

}

// take action based on the command from Serialport
void action(String str) {
// Serial.println(str);

if (str == "lightSwitch\n") {
lightSwitch();
} else if (str == "lightOFF\n") {

}

}

// get and return current temperature
int getTemp() {
float temp;
int analogTemp;
analogTemp = analogRead(tempPin);

// Math magic to convert resistance and voltage into temperature,
// and then convert to Celsius
temp = log(((10240000/analogTemp) - 10000));
temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp ))* temp );
temp = temp - 273.15;
return temp;
}

// get and return current light
int getLux() {
return analogRead(luxPin);
}

// turn light on/off
boolean lightSwitch() {
if (!lightIsON) {
digitalWrite(lightPin, HIGH);
lightIsON = true;
} else {
digitalWrite(lightPin, LOW);
lightIsON = false;
}
return lightIsON;
}

// get and return motion
boolean getMotion() {
long now = millis() / 1000;

if (digitalRead(motionPin) == HIGH && (now - lastMovement) > 6) {
lastMovement = now;
return true;
} else {
return false;
}
}

// handle the Serialport communication
void serialEvent() {
while (Serial.available()) {

// get the new byte, then add it to the inputString:
char inChar = (char)Serial.read();
inputString += inChar;

// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
16 changes: 0 additions & 16 deletions example_config.json

This file was deleted.

Loading

0 comments on commit 8c666b7

Please sign in to comment.