Hi, my first post, and i thought i would contribute what i have done so far.
Keep in mind this is all new to me. I have little experience with coding so it will be messy.
I took @Overflow’s RF temp sensor sketch from here… https://forum.pimatic.org/topic/646/rf-temperature-sensor-with-arduino-and-dht22 and added a PIR sensor.
I forget the source of the PIR stuff so cant give credit… but the sketch below works. It transmits the temp + humidity on the weather 1 protocol, and sends the pir data on several other protocols. This makes it easy to integrate into pimatic.
The device section in config.json should look something like this
{
"id": "homeduino-pir",
"name": "PIR",
"class": "HomeduinoRFPir",
"protocols": [
{
"name": "pir1",
"options": {
"unit": 25,
"id": 26
}
}
]
and
{
"id": "dhttemp",
"name": "Temp + Hum sensor",
"class": "HomeduinoRFTemperature",
"protocols": [
{
"name": "weather1",
"options": {
"id": 1,
"channel": 1
}
}
]
},
And this is the sketch for the arduino…
#include <RemoteTransmitter.h> //Library for transmitting PIR data
int statePin = LOW; //Transmitter pin state
KaKuTransmitter kaKuTransmitter(7); //Set the number your RF Tx is connected to
int ledPin = 13; //Led pin
//include the library to work with the DHT
#include "DHT.h"
//The values below have been identified by the documentation on weather1
#define SIGNAL_BREAK 456 //
#define SIGNAL_SHORT 1990 //
#define SIGNAL_LONG 3940 // DHT Stuff, RF protocol
#define SIGNAL_STOP 9236 //
#define REPEATS 7 //
#define RFPIN 7 //Pin that the RF transmitter is connected to
#define RFPWRPIN 6 //Pin that the VCC of the transmitter is connected to
#define DHTPIN 4 //Pin that the DHT data is connected to
#define DHTPWRPIN 5 //Pin that the DHT VCC is connected to
#define DHTTYPE DHT11 //DHT11 or DHT21 (AM2301) or DHT22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
int MODULE_ID = 1; //Used to identify the module in pimatic (1-255)
int CHANNEL = 1; //Multiple channels can be used (can be 1 - 4)
int TEMP_UNIT = 'C'; //may be set to F
/**
* This is a template of the binary message that will be send encoded
* by pulses of the RF transmitter
* Here is what the bits mean
* 0101 | 11010000 | 00 | 00 | 000100001001 | 00111101
* ? ID BT CH Temp. Humid.
*
* The temperature in this example is 26.5° (but sended at 265) and will
* be decoded in Pimatic again to the correct value.
* The value of the relative humidity is 65%
*/
byte message[] = {
0,1,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,1,1,1,1,1
};
volatile byte motion;
void setup() {
pinMode(RFPIN, OUTPUT);
pinMode(RFPWRPIN, OUTPUT);
pinMode(DHTPWRPIN, OUTPUT);
pinMode(13, OUTPUT); //LED Pin
setMessage(MODULE_ID, 4, 11);
setMessage(CHANNEL-1, 14, 15);
dht.begin();
attachInterrupt(0, motion_detect, RISING);//Initialize the interrupt pin for PIR Sensore (Arduino digital pin 2)
Serial.begin(9600);
}
/*
* @loop()
* turn on the hardware, send the message (other functions get called)
* to retrieve the data, so look below, then switch the hardware off
* and wait for the next cycle
*/
void loop() {
setHardware(1);
delay(500);
Serial.println("hardware on..");
sendMessage();
Serial.println("Message sent");
setHardware(0);
Serial.println("Hardware off");
delay(60000UL); //adjusted due to necessary wait from DHT
}
void motion_detect()//This function is called whenever presence is detected by the arduino
{
Serial.println("Motion Detected!");
digitalWrite(RFPWRPIN, HIGH);
digitalWrite(ledPin, HIGH);
kaKuTransmitter.sendSignal('M',25,2,true);//Sends KAKU singal
Serial.println("Alert Sent!");
delay(1000);
digitalWrite(RFPWRPIN, LOW);
digitalWrite(ledPin, LOW);
Serial.println("Hardware off");
}
/*
* @setHardware(byte on_off)
* turn the attached devices either on or off, whatever
* you need right now
*/
void setHardware(byte on_off){
if(on_off == 1){
digitalWrite(DHTPWRPIN, HIGH);
digitalWrite(RFPWRPIN, HIGH);
} else {
digitalWrite(DHTPWRPIN, LOW);
digitalWrite(RFPWRPIN, LOW);
}
}
/*
* @sendBit(byte b)
* takes a given byte which is either 1 or 0 and then sends it
* over the air using the breaks necessary for pimatic to recognize
* the profile of the a weather1 station
*/
void sendBit(byte b) {
if (b == 0) {
digitalWrite(RFPIN, HIGH);
delayMicroseconds(SIGNAL_BREAK);
digitalWrite(RFPIN, LOW);
delayMicroseconds(SIGNAL_SHORT);
}
else {
digitalWrite(RFPIN, HIGH);
delayMicroseconds(SIGNAL_BREAK);
digitalWrite(RFPIN, LOW);
delayMicroseconds(SIGNAL_LONG);
}
}
/*
* @sendTerminator()
* This method sends Arnold Schwarzenegger back in time. Just kidding,
* actually it does deliver the long break to show the end of the
* message[]
*/
void sendTerminator(){
digitalWrite(RFPIN, HIGH);
delayMicroseconds(SIGNAL_BREAK);
digitalWrite(RFPIN, LOW);
delayMicroseconds(SIGNAL_STOP);
digitalWrite(RFPIN, HIGH);
digitalWrite(RFPIN, LOW);
}
/*
* @sendMessage()
* execute the getDHTvalues() method to get some input in the
* message[] and then send the message X times using the sendBit() method
* followed by the terminator pulse
*/
void sendMessage() {
getDHTvalues();
for (byte i = 0; i < REPEATS; i++) {
digitalWrite(13, HIGH); //LED PIN
for (byte i = 0; i < sizeof(message); i++){
sendBit(message[i]);
}
digitalWrite(13, LOW);
sendTerminator();
}
}
/*
* @setMessage(int val, int startPos, int endPos)
* convert an integer to binary representation and
* write the output to the locations specified in the
* message[]
*/
void setMessage(int val, int startPos, int endPos){
int result[12];
int theValue = val;
for (byte i = 0, j = 11; i < 12; ++i, j--) {
result[j] = theValue & (1 << i) ? 1 : 0;
}
for (byte i = endPos, j=11; i > startPos-1; i--,j--){
message[i] = result[j];
}
}
/*
* @getDHTvalues()
* wait for two seconds to read the values of the DHT and
* replace their counterparts in the message[]
*/
void getDHTvalues(){
delay(2000); //DHT needs 2 seconds to get started
int h = (int) dht.readHumidity();
int t;
if(TEMP_UNIT == 'C'){
t = (int) (dht.readTemperature() * 10);
} else {
t = (int) (dht.readTemperature(true) * 10);
}
//if the values could not be red
if (isnan(h) || isnan(t)) {
Serial.println("DHT could not be read!!");
setMessage(0, 16, 27); //Temperature
setMessage(0, 28, 35); //Humidity
} else {
setMessage(t, 16, 27); //Temperature
setMessage(h, 28, 35); //Humidity
}
}
I hope someone has use for this. Sorry again about how messy it is!