Raspberry Pi or Arduino custom sensors?

Gotta say I love OpenHab! I have been slowly converting our house to be as smart as possible and I love not be trapped by a specific product or protocol. Recently I have started exploring building custom sensors as I have not been very satisfied with some of the different multi-sensors I have tried. My experiment was buying some different Arduino’s and different types of sensors. I did some testing with mqtt and souliss and got a basic temp/hum sensor up and working through the ethernet shield. Next step was to go wireless and battery powered for some of the exterior sensors I wanted to incorporate. I quickly ran into issues here as most of the WiFi modules/chips either need some sort of voltage modification or were ridiculously expensive so my project came to a halt. As the parts I needed quickly started adding up I came to the realization the price point was starting to become very unsatisfying. Last week I found an old Pi in my computer bin and thought hey I already have a cheap $10 wifi dongle I bet I could use the sensors right off its GPIO pins. This actually worked great so I’m re-inspired to start playing again. Now with the Pi mini and the Pi 3 with on board wifi/Bluetooth available. My question is for anybody out there building your own sensors is what do you use and found to be cost efficient, reliable, and not overly complex to build (I can do some simple soldering but prefer to keep that to a minimum)?

1 Like

Good morning, have a look at mysensors.org

… or take a look at ESP8266 wifi modules and use it with mosquitto broker and mqtt binding in oh.

Second using these as the ESP8266 modules have built-in wifi and work great. If you do try these, don’t use the latest IDE from Arduino for programming, your sketches won’t compile properly, newest one to support these correctly is 1.6.5.

http://www.ebay.com/itm/NodeMcu-Lua-ESP8266-ESP-12E-CH340G-WIFI-Serial-Development-Board-for-Arduino-/291657458911?hash=item43e823a4df:g:I8AAAOSw5dNWk2cl

For the MQTT broker I’m using a standalone raspberry pi, works great!

If you plan on making several sensors, I’d advise MySensors.org as well.
If it is only for one sensor, implementing a gateway just for that one sensor would be a bit much though…

What’s great with mysensors is that it’s a mesh network, not a star network. So your sensor outdoor could talk with a sensor on a window, which would relay the message to a temperature sensor, which would forward to your central system. No need to have direct connectivity from central to all sensors.

1 Like

Thanks for all the replies! I actually have a couple ESP8266’s and Arduino Uno’s. I already have mqtt broker up and running on my RasPi too. The only thing that has stumped me with the ESP8266 is from what I have read the need to change the tx/rx pins down to 3.3V. Which to me is kinda strange to me considering the Arduino has the 3.3V power from your experience do I need to do something to adjust the voltage on the communication pins?

@naturalblaze - I’ve made a couple of the ones shown here, the DHT22 sensors were shown farther up in that same thread and come on a board with a capacitor and resistor. It is connected to 3V3, GND and D4 when looking at this diagram.

And the board itself is powered by a 5V wall plug.

@anthony_maragou - to clarify your comment about direct connectivity, are you saying you favor those because you don’t need to have wifi in a particular area which is required by the ESP8266 wifi style sensor? Thanks

Personnaly, for connectivity of my sensors, I chose 2,4GHz because of the very low cost of these modules (less than 1€). This is not wifi, just direct RF between the sensors and my gateway.

The mesh nature of the network is a functionality I don’t use now, but I would if I had a house. It enables you to extend coverage very far without adding dedicated repeater.

Let’s take an extreme exemple: Your house is 2km from the main road and you want to control lights every 100m on your path to the road. Each light can act as a repeater for the next one, so the last one, 2km from your house, would be able to talk to the base because its messages are forwarded by the other lights on the way.

Come take a look at our DIY Home Automation with OpenHAB & RFM69 radios

We mostly use Arduino compatibles for the sensor / control nodes,

but OpenHAB often runs on a Raspberry Pi

Thanks for the follow-up. That type of mesh network is how I cover my house/property with wifi using http://www.open-mesh.com/, so I have wifi everywhere.

I’d like to check that out. Can you share the sketch you have to upload to that?

Here it is!

/*
  Outbound:
  Updates shop temperature and humidity every 10 minutes.
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

#define ESP8266_LED 16
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

int chk;
int temp;
int humd;
char buffer[12];

const char* ssid = "YOURSSID";
const char* password = "YOURPASSWORD";
const char* mqtt_server = "xxx.xxx.xxx.xxx";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  //initialize onboard led and turn off
  pinMode(ESP8266_LED, OUTPUT);
  digitalWrite(ESP8266_LED, HIGH);

  Serial.begin(115200);

  //connect to wifi
  setup_wifi();
  
  //connect  to mqtt broker
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  dht.begin();
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(ESP8266_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(ESP8266_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
       //client name must be unique
    if (client.connect("ESP8266ShopClient")) {
      Serial.println("connected");
      client.subscribe("/openhab/#");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();

  //every 10 minutes
  if (now - lastMsg > 600000) {
    lastMsg = now;
    ++value;
    
    //heartbeat
    digitalWrite(ESP8266_LED, LOW);
    delay(100);
    digitalWrite(ESP8266_LED, HIGH);
    
    temp = (dht.readTemperature() * 9.0)/5.0 + 32.0;
    dtostrf(temp, 3, 0, buffer);
    snprintf (msg, 75, buffer, value);
    Serial.print("Current Temp: ");
    Serial.print(msg);
    Serial.println("");
    client.publish("/openhab/shoptemp", msg);
    
    humd = dht.readHumidity();
    dtostrf(humd, 3, 0, buffer);
    snprintf (msg, 75, buffer, value);
    Serial.print("Current Humd: ");
    Serial.print(humd);
    Serial.println("");
    client.publish("/openhab/shophumidity", msg);
  }
}
1 Like

Thanks for the code. I have a question. I noticed in your sketch you have the ESP8266WiFi.h library referenced. I can’t seem to get that library installed do you know where I could find it? I was able to get the ESP8266 working just using AT commands and with a sample sketch from the ESP8266wifi.h library but it doesn’t seem to have the same functions. Any help with that library would be awesome as I think that the last thing stopping me from getting this working.

If I remember correctly it was from the Arduino IDE. Sketch --> Include Library --> Manage Libraries.

Also, if you’re using the NodeMcu like above you want to use an older version of the IDE. You can still download v1.6.5. Versions after that don’t compile correctly with the NodeMcu.

Is that still a requirement?
I run a nodemcu v1 board with Arduino IDE 1.6.8 and was able to upload a sketch to my ESP8266 without any problems …

Looks like 1.6.8 is new, if it compiled the above script then they have fixed the problem. Although it’s not mentioned in the release notes - https://www.arduino.cc/en/Main/ReleaseNotes.

1.6.8 was released on 3/9 so I would have been using 1.6.7 in the end of February when I first bought these little boards.

You are right.
But what I’ve found a minute ago:

So they removed the recommendation to stick with 1.6.5 seven days ago.

I just downloaded 1.6.8 and it compiled the above just fine. Glad to know they’ve corrected the problem!

Thanks for the update

This might also be interessting for you.

1 Like

Thanks for you help. I ordered a NodeMCU have it working with a DHT11 temp/hum sensor for now. Stuck it in a old Motion sensor case and modified a usb cable and hooked to a 4 AA battery pack I had laying around so I could stick it outside on our covered deck. I added a deep sleep to put the NodeMCU to sleep for 2 minutes then wake up and send me the temp/hum. Waiting to see how long I get out of the battery pack as this is my first go at getting the wireless working and on batteries. I plan to eventually add some more sensors and add some around the outside of the house.