add second sensor to Modbus

i am logging temperature data to a coffee roasting program on my computer. i want to add a second temperature sensor to the arduino, how can i add a second slave to my sketch? i was going to use the same Max6675 thermocouple amplifier in the same configuration using pins 8-12.

#include <max6675.h>
#include <ModbusRtu.h>

// data array for modbus network sharing
uint16_t au16data[16] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1 };

/**

  • Modbus object declaration
  • u8id : node id = 0 for master, = 1..247 for slave
  • u8serno : serial port (use 0 for Serial)
  • u8txenpin : 0 for RS-232 and USB-FTDI
  • or any pin number > 1 for RS-485
    */
    Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI

int thermoDO = 2;
int thermoCS = 3;
int thermoCLK = 4;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 5;
int gndPin = 6;

int led = 9;

void setup() {
slave.begin( 19200);// 19200 baud, 8-bits, none, 1-bit stop
// use Arduino pins
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
pinMode(led, OUTPUT);
delay(500);

}

void loop() {
// basic readout test, just print the current temp

//Serial.print("C = ");

au16data[2] = (uint16_t) (thermocouple.readCelsius()*100);

slave.poll( au16data, 16 );

for(int i=1; i<=99; i++) {
if(i<=au16data[4])
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);

delay(5);
}

}

1 Like

So, pick a different number for the second slave...

Modbus slave(2,0,0); // this is slave @2

how can i add a second slave to my sketch?

Do you want to add a second sensor to the same Arduino or do you want to add a second Arduino to your Modbus network? In the later case MorganS' answer would solve your problem. In the former case you add a second thermocouple object:

MAX6675 thermocouple2(7,8,9);

and write the read value into the Modbus value array:

 au16data[3] = (uint16_t) (thermocouple2.readCelsius()*100);