Skip to content

Instantly share code, notes, and snippets.

@kartben
Last active August 29, 2015 13:56
Show Gist options
  • Save kartben/8905471 to your computer and use it in GitHub Desktop.
Save kartben/8905471 to your computer and use it in GitHub Desktop.
Greenhouse scripts
/*******************************************************************************
* Copyright (c) 2013, 2014 Benjamin Cabé and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Benjamin Cabé - initial API and implementation
*******************************************************************************/
#include <Servo.h>
Servo myservo;
#define CLOSED 142
#define OPENED 42
int previousServoPosition = CLOSED;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
unsigned long lastPrint ;
#define DELAY 1000
void setup() {
Serial.begin(9600);
myservo.attach(13);
myservo.write(previousServoPosition);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A4, INPUT);
lastPrint = millis();
}
void loop() {
if (millis() - lastPrint >= DELAY) {
lastPrint = millis();
Serial.print("{\"temperature\":");
Serial.print(readTemp());
Serial.println("}");
Serial.flush();
Serial.print("{\"luminosity\":");
Serial.print(readLum());
Serial.println("}");
Serial.flush();
Serial.print("{\"humidity\":");
Serial.print(readHum());
Serial.println("}");
Serial.flush();
}
if (stringComplete && inputString=="TOGGLE\n")
{
// Serial.println("!!!");
// Serial.println(inputString);
toggleServo();
inputString = "";
stringComplete = false;
}
}
float readTemp()
{
const int B=3975;
double TEMP;
int sensorValue = analogRead( A0 );
float Rsensor;
Rsensor=(float)(1023-sensorValue)*10000/sensorValue;
TEMP=1/(log(Rsensor/10000)/B+1/298.15)-273.15;
return (int) (TEMP * 100) / 100.0 ;
}
float readHum()
{
int sensorValue = analogRead(A1);
if (sensorValue < 400)
return 0;
else {
float hum = (sensorValue - 300) / 450. * 100;
return min(((int) (hum * 100)) / 100.0, 100) ;
}
}
float readLum()
{
int sensorValue = analogRead(A4);
// Serial.println(sensorValue);
double voltage = 5.0 * ((double)sensorValue / 1024.0);
double resistance = (10 * 5.0) / voltage - 10.0;
// Calculating the intensity of light in lux
float illuminance = 255.84 * pow(resistance, -10./9.);
return illuminance;
}
void toggleServo()
{
if(previousServoPosition == CLOSED)
{
while(previousServoPosition > OPENED)
{
myservo.write(previousServoPosition--);
delay (5);
}
}
else {
while(previousServoPosition < CLOSED)
{
myservo.write(previousServoPosition++);
delay (5);
}
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
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;
}
}
}
/*jslint node:true */
/*******************************************************************************
* Copyright (c) 2013, 2014 Benjamin Cabé and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Benjamin Cabé - initial API and implementation
*******************************************************************************/
/** Serial port configuration **/
var serialport = require("serialport");
var SerialPort = serialport.SerialPort; // localize object constructor
var sp = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
/** MQTT client configuration **/
var mqtt = require('mqtt');
var mqttClient = mqtt.createClient(1883, 'iot.eclipse.org');
mqttClient.subscribe('/fosdem/demo/command/#');
var parseSerialInput = function(data) {
console.log('--> data received: ' + data);
try {
var parsedData = JSON.parse(data);
for(var key in parsedData) {
mqttClient.publish('/fosdem/demo/data/' + key, '' + parsedData[key]);
}
} catch (e) {}
};
sp.on("open", function() {
console.log('open');
sp.on('data', parseSerialInput);
});
var toggleRoof = function(topic, message) {
console.log('mqtt message received: ' + message);
sp.write('TOGGLE\n');
};
mqttClient.on('message', toggleRoof);
{
"version": "0.0.1",
"name": "greenhouse",
"description": "MQTT Greenhouse",
"scripts": {
"start": "/usr/local/bin/node ./greenhouse.js",
"stop": "pkill -f greenhouse.js"
},
"dependencies": {
"mqtt": "0",
"serialport": "0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment