17-05-2023, 10:37
(Ten post był ostatnio modyfikowany: 17-05-2023, 10:45 przez tabazka.)
Oto moje skatche...
Dla MASTER:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <OneWire.h>
#include <DallasTemperature.h>
//=====================================================================================================================================
#include "Arduino.h"
// Library to allow a Serial port on arbitrary pins
#include <SoftwareSerial.h>
// These are the pins we'll be talking to the RS485 device on
#define RS485rx 2 // RS485 Receive pin
#define RS485Tx 3 // RS485 Transmit pin
#define RS485Transmit HIGH
#define RS485Receive LOW
#define baudRate 115200
SoftwareSerial RS485(RS485rx, RS485Tx);
int buttonState = 0;
//=====================================================================================================================================
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Addresses of 3 DS18B20s
uint8_t sensor1[8] = { 0xX, 0xX, 0xX, 0xX, 0xX, 0xX, 0xX, 0xX };
uint8_t sensor2[8] = { 0xX, 0xX, 0xX, 0xX, 0xX, 0xX, 0xX, 0xX };
uint8_t sensor3[8] = { 0xX, 0xX, 0xX, 0xX, 0xX, 0xX, 0xX, 0xX };
float tempC1 = 0;
float tempC2 = 0;
float tempC3 = 0;
float times =10000;
void setup()
{
lcd.init();
lcd.begin(20, 4);
lcd.backlight();
Wire.begin();
sensors.begin();
lcd.setCursor(4, 0);
lcd.print("TEMPERATURA");
Serial.begin(9600);
sensors.setResolution(sensor1, 12);
sensors.setResolution(sensor2, 12);
sensors.setResolution(sensor3, 12);
pinMode(5, OUTPUT);
pinMode(A0, INPUT);
sensors.requestTemperatures();
float tempC1 = sensors.getTempC(sensor1);
float tempC2 = sensors.getTempC(sensor2);
float tempC3 = sensors.getTempC(sensor3);
lcd.setCursor(1, 1);
lcd.print("Wewnatrz: ");
lcd.setCursor(11, 1);
lcd.print(tempC1);
lcd.setCursor(1, 2);
lcd.print("Zewnatrz: ");
lcd.setCursor(11, 2);
lcd.print(tempC2);
lcd.setCursor(1, 3);
lcd.print("Sensor 3: ");
lcd.setCursor(11, 3);
lcd.print(tempC3);
//=====================================================================================================================================
RS485.begin(baudRate);
//=====================================================================================================================================
}
void loop()
{
buttonState = analogRead(A0);
if (buttonState >= 1000)
{
digitalWrite(5, HIGH);
}
if (buttonState <= 1000)
{
digitalWrite(5, LOW);
}
static float count = 0;
count++; if (count==times)
{
sensors.requestTemperatures();
float tempC1 = sensors.getTempC(sensor1);
float tempC2 = sensors.getTempC(sensor2);
float tempC3 = sensors.getTempC(sensor3);
float rxtempC1;
float rxtempC2;
float rxtempC3;
lcd.setCursor(11, 1);
lcd.print(tempC1);
lcd.setCursor(11, 2);
lcd.print(tempC2);
lcd.setCursor(11, 3);
lcd.print(tempC3);
//=====================================================================================================================================
Serial.print("Sending:");Serial.println(sensors.getTempC(sensor3));
RS485.write(sensors.getTempC(sensor3));
delay(1);
if (RS485.available())
{
rxtempC3 = RS485.read();
// Display it on the Serial Monitor as a char (not an integer value)
Serial.print(" Got back:"); Serial.println(rxtempC3);
}
count =0;
}
//=====================================================================================================================================
}
Oraz SLAVE:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
int tempC1= 0;
int tempC2= 0;
int tempC3= 0;
//=====================================================================================================================================
#include "Arduino.h"
// Library to allow a Serial port on arbitrary pins
#include <SoftwareSerial.h>
// These are the pins we'll be talking to the RS485 device on
#define RS485rx 2 // RS485 Receive pin
#define RS485Tx 3 // RS485 Transmit pin
#define RS485Transmit HIGH
#define RS485Receive LOW
#define baudRate 115200
// Define the RS485 object
SoftwareSerial RS485(RS485rx, RS485Tx);
float rxtempC1;
float rxtempC2;
float rxtempC3;
//=====================================================================================================================================
void setup()
{
lcd.init();
lcd.begin(20, 2);
lcd.backlight();
Wire.begin();
Serial.begin(9600);
//=====================================================================================================================================
RS485.begin(baudRate);
//=====================================================================================================================================
}
void loop()
{
//=====================================================================================================================================
// Is there something on the serial pin?
if (RS485.available())
{
float rxtempC3 = RS485.read();
Serial.print("Received:");Serial.println(rxtempC3);
lcd.setCursor(1, 0);
lcd.print(rxtempC3);
lcd.print(" ");
delay(10);
// Send back a modified value
RS485.write(rxtempC3);
}
//=====================================================================================================================================
}
Monitor portu dla MASTER:
Sending:19.69
Got back:19.00
Sending:19.69
Got back:19.00
Sending:19.69
Got back:19.00
Monitur portu dla SLAVE:
Received:19.00
Received:19.00
Received:19.00
Received:19.00
Received:19.00