Hallo,
um meine Heimautomatisierung mit mehr Sensorwerten zu füttern möchte ich über mein Lan einen Arduino anbinden. Dazu habe ich einen Arduino Nano und einen Ethernet shield mit W5100 Chip verwendet. Als Protokoll benutze ich MQTT (library).
Momentan hab ich per 1-Wire drei DS18B20 Sensoren und mit dieser library zwei DHT22 Sensoren eingebunden. Ein bmp180 hat mit dieser library auch noch seinen Platz bekommen.
Diesen Code habe ich mir erstellt der auch soweit funktioniert.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <dht.h>
#include <SFE_BMP180.h>
#include <Wire.h>
//------------1Wire---------------------
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//-------------DHT-----------------------
dht DHT;
byte DHT22_PIN;
//------------- BMP180 --------------
SFE_BMP180 pressure;
#define ALTITUDE 221.0 // Altitude in meter
//----------------Netzwerk---------------
byte mac[] = { 0xDE, 0xED, 0xBA, 0x1A, 0x1A, 0x1A };
IPAddress ip(192, 168, 0, 214);
IPAddress server(192, 168, 0, 20);
EthernetClient ethClient;
PubSubClient client(ethClient);
char message_buff[100];
long lastReconnectAttempt = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long interval = 120000;
void setup()
{
Serial.begin(115200);
// Startup BMP180
pressure.begin();
//------------------------------
// Start up the Dallas library
sensors.begin();
//-----------------------------
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
DHT_shout();
ds18b20_reading();
bmp180();
previousMillis = currentMillis;
}
}
void DHT_reading()
{
// READ DATA
Serial.println();
int chk = DHT.read22(DHT22_PIN);
}
void DHT_shout()
{
DHT22_PIN = 5;
DHT_reading();
// DISPLAY DATA
Serial.println("DHT Sensors");
Serial.print("Sensor 0: h:");
Serial.print(DHT.humidity, 1);
Serial.print(" t:");
Serial.println(DHT.temperature, 1);
stringconv (DHT.humidity);
client.publish("dht_hum_0", message_buff);
stringconv (DHT.temperature);
client.publish("dht_temp_0", message_buff);
delay (5);
DHT22_PIN = 6;
DHT_reading();
// DISPLAY DATA
Serial.print("Sensor 1: h:");
Serial.print(DHT.humidity, 1);
Serial.print(" t:");
Serial.println(DHT.temperature, 1);
stringconv (DHT.humidity);
client.publish("dht_hum_1", message_buff);
stringconv (DHT.temperature);
client.publish("dht_temp_1", message_buff);
Serial.println();
DHT22_PIN = 5;
}
void ds18b20_reading()
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting DS18B20 temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
ds18b20_shout();
}
void ds18b20_shout()
{
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
stringconv (sensors.getTempCByIndex(0));
client.publish("temp_index_0", message_buff);
Serial.print("Temperature for the device 2 (index 1) is: ");
Serial.println(sensors.getTempCByIndex(1));
stringconv (sensors.getTempCByIndex(1));
client.publish("temp_index_1", message_buff);
Serial.print("Temperature for the device 3 (index 2) is: ");
Serial.println(sensors.getTempCByIndex(2));
stringconv (sensors.getTempCByIndex(2));
client.publish("temp_index_2", message_buff);
/*
Serial.print("Temperature for the device 4 (index 3) is: ");
Serial.println(sensors.getTempCByIndex(3));
stringconv (sensors.getTempCByIndex(3));
client.publish("temp_index_3", message_buff);
*/
}
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]);
}
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
lastReconnectAttempt = 0;
// ... and resubscribe
client.subscribe("inTopic");
}
}
}
}
void stringconv (float stc)
{
String pubString = String (stc);
pubString.toCharArray(message_buff, pubString.length() + 1);
Serial.print("stringumwandlung: ");
Serial.println(pubString);
}
weiter im nächsten post..