Beginner trying to understand MQTT and make my own sensor

So i just bought a Raspberry Pi 2 and so far i have gotten to the point in this tutorial where i have set up wireless access to my Pi. I stopped following the tutorial here because it seems like it wants me to set up a script that listens for RF24 signals nonstop and picks them up and publishes them. The problem is that the given script runs constantly and only receives on the Pi side and sends on the arduino side. Whereas i need to do both on both sides. And if its constantly running, will my Pi be blocked from doing other things like sending/receiver to/from Z-wave devices when i eventually add those into the mix?

I have a set of WS2812B LED strips (RGB individually addressable) which i can easily control using arduino using the FastLED library. This is my goal:

Connect an nRF24L0+ to my arduino and my Raspberry Pi

Arduino Side:
Receive instructions from OpenHab running on Pi to do the following:

  • Turn LEDs on/off
  • Change color using color picker
  • Start a scripted pattern thats coded onto the arduino.

Send the following data back to the Raspberry Pi:

  • Current on/off status
  • current color or pattern running

OpenHab (Pi) Side:
Send following data to arduino:

  • Tell it to turn on/off
  • Tell if which color to change to
  • Tell it which pattern to start running

Receive from arduino:

  • Current on/off status
  • color or pattern currently running

So as you can see both the Pi and Arduino need to be able to send and receive a variety of messages.

This shouldn’t be too hard to code on the arduino side but i am not sure how i should go about receiving the data and how often i should send it. Processing the data to control the lights should be easy.
How do i ensure that the message i am receiving is from OpenHab instead of another potential RF transceiver?
What library should i use for sending MQTT messages over the nRF24L0+ on my Arduino to OpenHab?

As for the OpenHab side, i have a lot of questions:
Which hardware should i use for picking up these signals from the arduino?
How do make it possible to send/receive only when there is data to be sent or received?
Is MQTT the best way to set this up?
What library should i use for sending MQTT messages over the nRF24L0+ on my Pi to arduino?

EDIT: It seems some people use an entire arduino to pick up the RF signals from the other arduinos, then this arduino sends the MQTT data over to OpenHab (either over Serial USB or Ethernet). Is this the best solution? Can’t the Pi access the nRF24L0+ directly? or is it a better idea to have this transceiver arduino as a gateway to the Pi?

I’m a bit of a newb myself with rf24 but in my research prior to changing gears with my first sensor build, the rf24 supports only 5 (iirc) connections. That means theoretically you could set your pi to talk to up to 5 sensors at once… kinda limiting. There are newer libraries that create a forked network allowing each node to talk to 5 other nods out to some odd thousand nodes. They were a bit confusing and required keeping track of nodes which could be really painful.

The price of an arduino with ethernet shield is low (my knockoff mega and Ethernet shield were like $18 shipped from amazon). I opted to start there. I can then add a receiver to it and add other sensors if I so choose.

In my case, I have an arduino mega controlling several temp sensors, relays, motion and other things that is central to a bunch of needs in my basement. It has an ethernet shield and talks to and from OH via mqtt VERY reliably. I do have a “project” in the back of my head where I play with my rf24’s to add additional arduinos to this base mega but in my earlier findings, I was turned off by the confusing node forking.

Additionally, I started looking more down the path of the ESP8266. There a a bunch of all in one solutions that have the esp8266 chip with gpio pins and can accept adruino ide. I messed with a standalone one a bit and had it doing things via serial but would probably buy a Huzzah to make things easier.

@Moxified
Thanks for the response! 5 devices does seem kind of limiting. I have an esp8226 on hand. Is there a way i can set that up to make my arduino talk directly to OpenHab via my local network?

It’s complicated but yes. The esp8266 is VERY powerful and as such can get very complicated.

By default, most use (opinion alert?) the AT command set. Basically you send AT commands via serial and listen for responses. You set your arduino code to talk to and handle serial from the esp8266. The biggest issue is you must talk to the esp over serial. That means you have to create a software serial on most arduino other than mega. Once you get them talking over the software serial port it’s not too bad but in my case, I relized I needed a mega worth of io pins (as software serial sucked up yet another 2) to do my project and moved on.

As I mentioned, I plan to circle back but life has been very hectic lately and I haven’t had time for fun projects.

To actually ANSWER your question, I still would recommend MQTT but you could use http bindings as well. I think MQTT makes more sense and gives you more options. You can go two routes, either program an esp8266 or variation to talk directly to OH via mqtt or connect the esp8266 to an arduino and have the arduino do the thinking and use the esp as a “serial modem” if you will. I think it depends on how many devices you need to connect to the esp. The standard 8 pin esp is limited but the breakouts like huzzah present more io pins.

I would google around a bit for MQTT esp8266 projects. Like: https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/

There is one on the forum that doesn’t use MQTT: DIY: Cheap wifi-based temperature/humidity sensor based on ESP8266 & DHT22

This blog series is fantastic for learning mqtt and arduino among other things: https://openhardwarecoza.wordpress.com/?s=mqtt

thanks for the info! im gonna do some tinkering and hopefully learn a bit!

If anyone has any more to add, i appreciate all input!

I just spent some time with esp8266 and mqtt as well and I’m really happy with how things turned out.
The raw code for my stuff you can find here https://github.com/tarioch/irrigation and if there’s interest I can add some more info. Basically what I did is get an esp8266 and hook it up with a relay board. Now I’m able to switch it through openhab. Also if I unplug the esp and replug it, it will update to the correct state. In addition openhab shows the “switched” state. If for some reason the esp is not processing the command it should not get displayed as switched in openhab.
The last thing is that I have an item in openhab to monitor the state of the esp, so I created a rule to notify me if that one goes offline.

1 Like

Good Day

I am using arduino mini pro + ESP-05 breakout module to try to get mqtt working wireless.

Herewith my code for Ethernet, but need same coding for ESP8266-05 + Arduino:


#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

#define DHTTYPE DHT22
#define DHTPIN 2

String datain;

byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
char datain_buff[8];

IPAddress ip(192, 168, 20, 164);
IPAddress server(192, 168, 20, 15);

EthernetClient ethClient;
DHT dht(DHTPIN, DHT22);
PubSubClient client(ethClient);

void reconnect() {
while (!client.connected()) {
if (client.connect(“EnviroClient”)) {
} else {
delay(5000);
}
}
}

void setup()
{
client.setServer(server, 1883);
Ethernet.begin(mac, ip);
delay(1500);
}

void loop()
{
if (!client.connected()) {
reconnect();
}
datain = String(dht.readTemperature());
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish(“enviro/192.168.20.164/temperatureC”, datain_buff);
datain = String(Fahrenheit(dht.readTemperature()));
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish(“enviro/192.168.20.164/temperatureF”, datain_buff);
datain = String(Kelvin(dht.readTemperature()));
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish(“enviro/192.168.20.164/temperatureK”, datain_buff);
datain = String(dht.readHumidity());
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish(“enviro/192.168.20.164/humidity”, datain_buff);
datain = String(dewPoint(dht.readTemperature(), dht.readHumidity()));
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish(“enviro/192.168.20.164/dewpoint”, datain_buff);
client.loop();

delay(30000);
}

double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

double Kelvin(double celsius)
{
return celsius + 273.15;
}

double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);
return (241.88 * T) / (17.558-T);
}


Hope Someone could help me with ESP-05 alternative coding.

Thank You

Albertus Geyser