Elektronik / Embedded / Mobil Cihazlar
Arduino + Ethernet Shield + Rasperry Pi + Node Red Bağlantısı Hakkında
Arduino + Ethernet Shield + Rasperry Pi + Node Red Bağlantısı Hakkında
-
Merhaba Hocalar.
Bir süredir üzerinde çalıştığım bir proje var büyük bir kısmını tamamladım. Türkçe kaynak bulamadım internette sonuç olarak sormak istediğim şudur anlayan müritler varsa.
# Arduino MEGA ya Ethernet Shield bağladım içinde PubSubClient kütüphanesi mevcut.
# Rasperry Pi içinde bulunan Node Red ile MQTT protokolunu kullanarak Arduino pinlerini kontrol etmek istiyorum.
Net'te bulduğum bir kod ile ARDUINO'ya bağlı olan motorları yada işte çalıştıracak ne varsa bunları çalıştırabiliyorum.
Ama ARDUINO'ya bağlı Sensörlerden gelen bilgileri Node Red ile Alamıyorum Yada ARDUINO da yazmam gereken kodları bulamıyorum diyeyim.
Sonuç olarak Rasperry Pi içinde Bulunan Node Red ile MQTT protokolünü kullanarak aynı ağa bağlı Ethernet Shield takılı olan Arduinonun Pinlerine bağlı sensörlerden gelen verileri okumak istiyorum
Şu an aynı mantık ile Arduino Üzerinden Switch'ler çalıştırabiliyorum kodunu aşağıya ekleyeyim.
#include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> // Our first sensor, a cheap DHT11 temperature and humidty sensor #include <DHT.h> #define DHTPIN 2 #define DHTTYPE DHT11 //21 or 22 also an option DHT dht(DHTPIN, DHTTYPE); unsigned long readTime; // Update these with values suitable for your network. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; IPAddress ip(192, 168, 5, 122); IPAddress server(192, 168, 5, 174); char message_buff[100]; // this buffers our incoming messages so we can do something on certain commands EthernetClient ethClient; PubSubClient client(ethClient); //MOTOR PIN AYARLAMA const int karistirici1 = 22; const int karistirici2 = 23; const int karistirici3 = 24; const int karistirici4 = 25; const int karistirici5 = 26; void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); String messageTemp; for (int i = 0; i < length; i++) { Serial.print((char)message[i]); messageTemp += (char)message[i]; } Serial.println(); // Feel free to add more if statements to control more Pins with MQTT // If a message is received on the topic home/livingroom/arduino/ledPin6 // It's a value between 0 and 255 to adjust the LED brightness //if(String(topic)=="home/livingroom/arduino/ledPin6"){ //Serial.print("Changing Digital Pin 6 Brithness to "); //Serial.print(messageTemp); //analogWrite(ledPin6, messageTemp.toInt()); //} // If a message is received on the topic home/livingroom/arduino/ledPin7, //you check if the message is either 1 or 0. Turns the Arduino Digital Pin according to the message //KARIŞTIRICI MOTOR-1 if(String(topic)=="dozaj/arduino/karistirici1"){ Serial.print("Karistirici Motor 1 "); if(messageTemp == "1"){ digitalWrite(karistirici1, HIGH); Serial.print("On"); } else if(messageTemp == "0"){ digitalWrite(karistirici1, LOW); Serial.print("Off"); } } //KARIŞTIRICI MOTOR-2 if(String(topic)=="dozaj/arduino/karistirici2"){ Serial.print("Karistirici Motor 2 "); if(messageTemp == "1"){ digitalWrite(karistirici2, HIGH); Serial.print("On"); } else if(messageTemp == "0"){ digitalWrite(karistirici2, LOW); Serial.print("Off"); } } //KARIŞTIRICI MOTOR-3 if(String(topic)=="dozaj/arduino/karistirici3"){ Serial.print("Karistirici Motor 3 "); if(messageTemp == "1"){ digitalWrite(karistirici3, HIGH); Serial.print("On"); } else if(messageTemp == "0"){ digitalWrite(karistirici3, LOW); Serial.print("Off"); } } //KARIŞTIRICI MOTOR-4 if(String(topic)=="dozaj/arduino/karistirici4"){ Serial.print("Karistirici Motor 4 "); if(messageTemp == "1"){ digitalWrite(karistirici4, HIGH); Serial.print("On"); } else if(messageTemp == "0"){ digitalWrite(karistirici4, LOW); Serial.print("Off"); } } //KARIŞTIRICI MOTOR-5 if(String(topic)=="dozaj/arduino/karistirici5"){ Serial.print("Karistirici Motor 5 "); if(messageTemp == "1"){ digitalWrite(karistirici5, HIGH); Serial.print("On"); } else if(messageTemp == "0"){ digitalWrite(karistirici5, LOW); Serial.print("Off"); } } Serial.println(); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("arduinoClient", "mqttuser", "sifrem")) { Serial.println("connected"); // Subscribe or resubscribe to a topic // You can subscribe to more topics (to control more LEDs in this example) client.subscribe("dozaj/arduino/karistirici1"); client.subscribe("dozaj/arduino/karistirici2"); client.subscribe("dozaj/arduino/karistirici3"); client.subscribe("dozaj/arduino/karistirici4"); client.subscribe("dozaj/arduino/karistirici5"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { pinMode(karistirici1, OUTPUT); pinMode(karistirici2, OUTPUT); pinMode(karistirici3, OUTPUT); pinMode(karistirici4, OUTPUT); pinMode(karistirici5, OUTPUT); Serial.begin(57600); client.setServer("192.168.5.174", 1883); // can be replaced by: client.setServer(server, 1883); client.setCallback(callback); Ethernet.begin(mac); dht.begin(); // Allow the hardware to sort itself out delay(1500); Serial.println(Ethernet.localIP()); readTime = 0; } void loop() { if (!client.connected()) { reconnect(); } client.loop(); //check if 5 seconds has elapsed since the last time we read the sensors. if(millis() > readTime+6000){ sensorRead(); } } void sensorRead(){ readTime = millis(); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); }
-
Hocam Arduino tarafını hallettin Node Red tarafından MQTT bağlantısını mı soruyorsun tam anlamadım
-
HolyOne bunu yazdı
Hocam Arduino tarafını hallettin Node Red tarafından MQTT bağlantısını mı soruyorsun tam anlamadım
Hayır yani şöyle ki ;
//KARIŞTIRICI MOTOR-1 if(String(topic)=="dozaj/arduino/karistirici1"){ Serial.print("Karistirici Motor 1 "); if(messageTemp == "1"){ digitalWrite(karistirici1, HIGH); Serial.print("On"); } else if(messageTemp == "0"){ digitalWrite(karistirici1, LOW); Serial.print("Off"); }
Üstteki Kodların karşılığına Node Red ile
"dozaj/arduino/karistirici1" "On" "Off"
yaparak Motoru Durdurup Çalıştırabiliyorum. MQTT bağlantısında'da sorun yok
Ama Arduino üzerindeki mesela 2 numaralı pindeki DHT sensörden gelen verileri burada gönderen kod neresi onu anlayamadım onun içinde Node Red kısmından Okuyamıyorum
Edit: Kodlar TBT tagları Yüzünden Karışmış :)
Şöyle olacaktı
//KARIŞTIRICI MOTOR-1
risk53 tarafından 09/Kas/20 18:38 tarihinde düzenlenmiştir
if(String(topic)=="dozaj/arduino/karistirici1"){
Serial.print("Karistirici Motor 1 ");
if(messageTemp == "1"){
digitalWrite(karistirici1, HIGH);
Serial.print("On");
}
else if(messageTemp == "0"){
digitalWrite(karistirici1, LOW);
Serial.print("Off");
}