多個 slave
以下示例顯示了主伺服器如何從多個從伺服器接收資料。在此示例中,從站傳送兩個短號碼。第一個是溫度,第二個是溼度。請注意溫度是浮點數(24.3)。為了只使用兩個位元組而不是四個(浮點數是四個位元組),我將溫度乘以 10,並將其儲存為短。所以這是主程式碼:
#include <Wire.h>
#define BUFFER_SIZE 4
#define MAX_NUMBER_OF_SLAVES 24
#define FIRST_SLAVE_ADDRESS 1
#define READ_CYCLE_DELAY 1000
byte buffer[BUFFER_SIZE];
void setup()
{
Serial.begin(9600);
Serial.println("MASTER READER");
Serial.println("*************");
Wire.begin(); // Activate I2C link
}
void loop()
{
for (int slaveAddress = FIRST_SLAVE_ADDRESS;
slaveAddress <= MAX_NUMBER_OF_SLAVES;
slaveAddress++)
{
Wire.requestFrom(slaveAddress, BUFFER_SIZE); // request data from the slave
if(Wire.available() == BUFFER_SIZE)
{ // if the available data size is same as I'm expecting
// Reads the buffer the slave sent
for (int i = 0; i < BUFFER_SIZE; i++)
{
buffer[i] = Wire.read(); // gets the data
}
// Parse the buffer
// In order to convert the incoming bytes info short, I use union
union short_tag {
byte b[2];
short val;
} short_cast;
// Parse the temperature
short_cast.b[0] = buffer[0];
short_cast.b[1] = buffer[1];
float temperature = ((float)(short_cast.val)) / 10;
// Parse the moisture
short_cast.b[0] = buffer[2];
short_cast.b[1] = buffer[3];
short moisture = short_cast.val;
// Prints the income data
Serial.print("Slave address ");
Serial.print(slaveAddress);
Serial.print(": Temprature = ");
Serial.print(temprature);
Serial.print("; Moisture = ");
Serial.println(moisture);
}
}
Serial.println("*************************");
delay(READ_CYCLE_DELAY);
}
}
現在 slave 程式碼:
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//=====================
// This is the hard-coded address. Change it from one device to another
#define SLAVE_ADDRESS 1
//=====================
// I2C Variables
#define BUFFER_SIZE 2
#define READ_CYCLE_DELAY 1000
short data[BUFFER_SIZE];
// Temprature Variables
OneWire oneWire(8);
DallasTemperature temperatureSensors(&oneWire);
float m_temperature;
// Moisture Variables
short m_moisture;
// General Variables
int m_timestamp;
void setup()
{
Serial.begin(9600);
Serial.println("SLAVE SENDER");
Serial.print("Node address: ");
Serial.println(SLAVE_ADDRESS);
Serial.print("Buffer size: ");
Serial.println(BUFFER_SIZE * sizeof(short));
Serial.println("***********************");
m_timestamp = millis();
Wire.begin(NODE_ADDRESS); // Activate I2C network
Wire.onRequest(requestEvent); // Set the request event handler
temperatureSensors.begin();
}
void loop()
{
if(millis() - m_timestamp < READ_CYCLE_DELAY) return;
// Reads the temperature
temperatureSensors.requestTemperatures();
m_temperature = temperatureSensors.getTempCByIndex(0);
// Reads the moisture
m_moisture = analogRead(A0);
}
void requestEvent()
{
data[0] = m_temperature * 10; // In order to use short, I multiple by 10
data[1] = m_moisture;
Wire.write((byte*)data, BUFFER_SIZE * sizeof(short));
}