I have two Arduino boards and I want to make them communicate via I2C. The first board (slave) should read a temperature value from a DS18B20 sensor (OneWire) and send it to the second board (master) via I2C.
So, I tried to merge some code from the standard examples (Wire/master_reader, Wire/slave_sender and dallas/simple).
When I use the two sketches bellow, the serial monitor of the master board is printing the "hello " message, but when I enable the request temperature lines (slave code)
sensors.requestTemperatures();
sensors.getTempCByIndex(0);
the serial monitor shows nothing.
I wander if there is a conflict between OneWire and I2C, or if I've made some serious mistakes in the code.
Any suggestions would be appreciated.
Master
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
Wire.requestFrom(2, 6);
while(Wire.available()){
char c = Wire.read();
Serial.print(c);
}
delay(500);
}
Slave
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(){
sensors.begin();
Wire.begin(2);
Wire.onRequest(requestEvent);
}
void loop(){
}
void requestEvent(){
//sensors.requestTemperatures();
//sensors.getTempCByIndex(0);
Wire.write("hello "); // respond with message of 6 bytes
}