First, I'm using a Mac 10.6.8 with Indigo 6 and the MySensors plugin (newest Arduino software, two Diecimillias with 328 chips). I have two arduinos that communicate with each other, and I have caps on the radios for both so they are reliably communicating.
One of my arduinos has multiple devices attached from a previous project and I'm trying to code them over to MySensors. Specifically, there is a Temp/Humidity sensor, a PIR motion sensor, an RGB LED to indicate what the arduino is doing, and 4 relays. I can get each device/sensor to behave properly on its own, but when I try using them together I get strange behavior, such as:
-
A build-up of messages or something in a buffer somewhere, so that it takes the Indigo plugin a couple minutes to get through the messages (it processes about one every 10 seconds, and I'm sending 1 to 3 messages every ten seconds. In the code below, after all the devices are registered, the temperature and humidity update once and then the only updates I get are from the PIR sensor.
-
Extra relays - the plugin mistakes two of the sensors for relays, despite manually registering only 4. This is not a problem in the code below but has happened with previous attempts, I THINK I fixed it when I turned the gw.begin node to false but I could be wrong.
-
Erratic relay behavior/unreliability. The wrong relay or state is sometimes triggered. With the code below, the relays are not even responding.
Below is my code, I'm hoping someone can give me insight on why I may be getting such behavior.
#include <SPI.h>
#include <MySensor.h>
#include <DHT.h>
//Temp
#define CHILD_ID_HUM 10
#define CHILD_ID_TEMP 20
#define HUMIDITY_SENSOR_DIGITAL_PIN 7
//Motion
#define DIGITAL_INPUT_SENSOR A0
#define CHILD_ID_PIR 30
float lastMo;
// Relay
#define RELAY_1 3
#define RELAY_2 4
#define RELAY_3 5
#define RELAY_4 6
#define RELAY_ON 0
#define RELAY_OFF 1
// LED
int red = A5;
int blue = A4;
int green = A3;
//
MySensor gw;
DHT dht;
float lastTemp;
float lastHum;
//Timer
long previousMillis1 = 0;
long interval1 = 60000;
long previousMillis2 = 0;
long interval2 = 5000;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
void setup()
{
//Still unsure on conditions for this:
gw.begin(NULL, AUTO, false);
//start temp sensor
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
// Send the Sketch Version Information to the Gateway
gw.sendSketchInfo("Arduino 2", "1.0");
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_PIR, S_MOTION);
//LED
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
// Register Relays
gw.present(RELAY_1, S_LIGHT);
pinMode(RELAY_1, OUTPUT);
digitalWrite(RELAY_1,RELAY_OFF);
gw.present(RELAY_2, S_LIGHT);
pinMode(RELAY_2, OUTPUT);
digitalWrite(RELAY_2,RELAY_OFF);
gw.present(RELAY_3, S_LIGHT);
pinMode(RELAY_3, OUTPUT);
digitalWrite(RELAY_3,RELAY_OFF);
gw.present(RELAY_4, S_LIGHT);
pinMode(RELAY_4, OUTPUT);
digitalWrite(RELAY_4,RELAY_OFF);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 > interval1) {
sendTemp();
delay(500);
sendHumid();
previousMillis1 = currentMillis;
}
if (currentMillis - previousMillis2 > interval2) {
float state = digitalRead(DIGITAL_INPUT_SENSOR);
if (state != lastMo) {
gw.send(msgPIR.set(state?"1":"0"));
lastMo = state;
}
previousMillis2 = currentMillis;
}
gw.process();
}
void sendTemp(){
float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
} else if (temperature != lastTemp) {
lastTemp = temperature;
temperature = dht.toFahrenheit(temperature);
gw.send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
}
void sendHumid(){
float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else if (humidity != lastHum) {
lastHum = humidity;
gw.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_LIGHT) {
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
gw.saveState(message.sensor, message.getBool());
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}