Hello, I am new with openhab and i have hard time to understand how to bind Arduino Uno with ethernet shield to rasperry pi 3 running openhab. I have tried to read information about MQTT, but i have found nothing that i can get to work. If somebody could help me how to set up simple temperature sensor feedback thro MQTT to openhab, it would be great.
Now i have tried to establish information change between DHT11 temperature/humidity sensor and openhab. DHT11 sensor is connected to arduino. I believe that i have installed Mosquitto correctly, i used this instruction link to install mosquitto to rasperry pi 3.
Here are the settings that i have used on openhab:
Items:
Number Temperature āTemperature [%.1f %]ā (sensors) <> {mqtt="<[broker:/arduino/ard1/TempC/state:state:default]"}
Sitemap:
sitemap default label=āopenHAB User Interfaceā {
Frame label =āOutside Temperatureā {
Text label=āTemperatureā icon=āchartā {
Switch item=Chart_period label=āTime scale of trendā mappings=[0=āHourā, 1=āDayā, 2=āWeekā,3=āMonthā]
Chart item=Temperature period=h refresh=300 visibility=[Chart_period==0, Chart_period==āUninitializedā] Chart item=Temperature period=D refresh=300 visibility=[Chart_period==1]
Chart item=Temperature period=W refresh=700 visibility=[Chart_period==2]
Chart item=Temperature period=M refresh=1000 visibility=[Chart_period==3]
}
}
}
Settings:
//################################ MQTT Transport ######################################
//
// Define your MQTT broker connections here for use in the MQTT Binding or MQTT
// Persistence bundles. Replace with a id you choose.
//
// URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqtt:broker.url=tcp://192.168.1.100:1883
rrd4j persistence:
Strategies {
default = ā0 * * * * ?ā
}
Items {
* : default
}
Code used in Arduino uno with ethernet shield (i know this code probably isnĀ“t the best example but i donĀØt know how to change it in order to get it work with openhab):
/*
MQTT IOT Example
- continuously obtains values from the Virtuabotix DHT11 temperature/humidity sensor
- formats the results as a JSON string for the IBM IOT example
- connects to an MQTT server (either local or at the IBM IOT Cloud)
- and publishes the JSON String to the topic named quickstart:MY_MAC_ADDRESS
*/
include <SPI.h>
include <Ethernet.h>
include <PubSubClient.h>
include <dht11.h>
// Update this to either the MAC address found on the sticker on your ethernet shield (newer shields)
// or a different random hexadecimal value (change at least the last four bytes)
byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0x00 };
char macstr[] = ādeedbafefeedā;
// Note this next value is only used if you intend to test against a local MQTT server
byte localserver[] = {192, 168, 1, 100 };
// Update this value to an appropriate open IP on your local network
byte ip[] = {192, 168, 1, 80 };
char servername[]=āquickstart.messaging.internetofthings.ibmcloud.comā;
String clientName = String(ād:quickstart:arduino:ā) + macstr;
String topicName = String(āiot-2/evt/status/fmt/jsonā);
dht11 DHT11;
float tempF = 0.0;
float tempC = 0.0;
float humidity = 0.0;
EthernetClient ethClient;
// Uncomment this next line and comment out the line after it to test against a local MQTT server
PubSubClient client(localserver, 1883, 0, ethClient);
//PubSubClient client(servername, 1883, 0, ethClient);
void setup()
{
// Start the ethernet client, open up serial port for debugging, and attach the DHT11 sensor
Ethernet.begin(mac, ip);
Serial.begin(9600);
DHT11.attach(3);
}
void loop()
{
char clientStr[34];
clientName.toCharArray(clientStr,34);
char topicStr[26];
topicName.toCharArray(topicStr,26);
getData();
if (!client.connected()) {
Serial.print("Trying to connect to: ");
Serial.println(clientStr);
client.connect(clientStr);
}
if (client.connected() ) {
String json = buildJson();
char jsonStr[200];
json.toCharArray(jsonStr,200);
boolean pubresult = client.publish(topicStr,jsonStr);
Serial.print("attempt to send ");
Serial.println(jsonStr);
Serial.print("to ");
Serial.println(topicStr);
if (pubresult)
Serial.println(āsuccessfully sentā);
else
Serial.println(āunsuccessfully sentā);
}
delay(5000);
}
String buildJson() {
String data = ā{ā;
data+="\n";
data+= āādā: {ā;
data+="\n";
data+="āmyNameā: āArduino DHT11ā,";
data+="\n";
data+="ātemperature (F)ā: ā;
data+=(int)tempF;
data+= ā,ā;
data+=ā\n";
data+="ātemperature Ā©ā: ā;
data+=(int)tempC;
data+= ā,ā;
data+=ā\n";
data+="āhumidityā: ā;
data+=(int)humidity;
data+=ā\n";
data+="}";
data+="\n";
data+="}";
return data;
}
void getData() {
int chk = DHT11.read();
switch (chk)
{
case 0:
Serial.println(āRead OKā);
humidity = (float)DHT11.humidity;
tempF = DHT11.fahrenheit();
tempC = DHT11.temperature;
break;
case -1:
Serial.println(āChecksum errorā);
break;
case -2:
Serial.println(āTime out errorā);
break;
default:
Serial.println(āUnknown errorā);
break;
}
}
Link to instructions
When running code above i can get readings thro arduino uno serial port and i can also see that information is unsuccessfully sent to MQTT.
Here is information from MQTT:
pi@raspberrypi:~ $ mosquitto -v
1462261200: mosquitto version 1.4.8 (build date Sun, 14 Feb 2016 15:06:55 +0000) starting
1462261200: Using default config.
1462261200: Opening ipv4 listen socket on port 1883.
1462261200: Opening ipv6 listen socket on port 1883.
1462261203: New connection from 192.168.1.80 on port 1883.
1462261203: New client connected from 192.168.1.80 as d:quickstart:arduino:deedbafefeed (c1, k15).
1462261203: Sending CONNACK to d:quickstart:arduino:deedbafefeed (0, 0)
1462261224: Client d:quickstart:arduino:deedbafefeed has exceeded timeout, disconnecting.
1462261224: Socket error on client d:quickstart:arduino:deedbafefeed, disconnecting.
1462261229: New connection from 192.168.1.80 on port 1883.
1462261229: New client connected from 192.168.1.80 as d:quickstart:arduino:deedbafefeed (c1, k15).
1462261229: Sending CONNACK to d:quickstart:arduino:deedbafefeed (0, 0)
If someone could tell me what changes i need to do, it would be greatly appreciated.