#include #include #include // Data wire is plugged into pin 10 on the Arduino #define ONE_WIRE_BUS 10 #define TEMPERATURE_PRECISION 12 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); DeviceAddress sensor0,sensor1; int deviceCount =0; char digitCharAAAA[10]; char digitCharBBBB[10]; float tempA=27.39; float tempB=29.23; float tempZ=20.01; String testAA; String nothing; // This is important - the string precursor. String testAAAA; // File names need to be of sufficient complexity! String testBBBB; int stringLengthAA; int numberId =101; int dataPrecursor = 198; // A randomish number below 254. Be careful it does not conflict with numberIds. int n = 0; int digitAAAA; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { pinMode(13,OUTPUT); digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); Wire.begin(); Serial.begin(9600); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sensors.begin(); // locate devices on the bus // Serial.print("Locating devices..."); // Serial.print("Found "); // deviceCount = sensors.getDeviceCount(), DEC; // Serial.print(deviceCount); // Serial.println(" devices."); if (!sensors.getAddress(sensor0, 0)) Serial.println("Unable to find address for Device 0"); if (!sensors.getAddress(sensor1, 1)) Serial.println("Unable to find address for Device 1"); sensors.setResolution(sensor0, TEMPERATURE_PRECISION); sensors.setResolution(sensor1, TEMPERATURE_PRECISION); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Serial.println(""); sensors.requestTemperatures(); Serial.print("tempA: "); tempA =sensors.getTempCByIndex(0); // Change the index value accordingly. Serial.println(tempA); // sensors.requestTemperatures(); Serial.print("tempB: "); tempB =sensors.getTempCByIndex(1); // Change the index value accordingly. Serial.println(tempB); Serial.println(""); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// digitalWrite(13, HIGH); delay(5000); digitalWrite(13, LOW); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Serial.println("Now we are doing a transmission ....... "); Serial.println(""); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Wire.beginTransmission(9); // transmit to device #9 // Make sure the order is the same in master and slave. delay(20); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // We're going to send two chunks of data, tempA and tempB. Each of them is less than 1,000 and has two digits after the //decimal place. tempZ=tempA; numberId= 101; sendData(); tempZ=tempB; numberId= 109; sendData(); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Wire.endTransmission(); Serial.println(""); Serial.println("Transmission ended ....... "); Serial.println(""); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// delay(120000); } void sendData() { // We're going to turn testAAAA and testBBBB into shorter numbers to transmit: // assume the number is less than 1,000 and we want to preserve the first two decimal places testAAAA = nothing + tempZ*100; stringLengthAA = testAAAA.length(); testAAAA.remove((stringLengthAA-3),3); // Remove decimal place etc. Serial.print("testAAAA: ");Serial.println(testAAAA); Wire.write(dataPrecursor); // Must send this junk data precursor first! Wire.write(numberId); Serial.print("numberId is: ");Serial.println(numberId); Wire.write(stringLengthAA-3); n=stringLengthAA-4; while (n>-1) // Send the data back to front. { digitCharAAAA[n-1] = testAAAA.charAt(n); n--; digitAAAA = (digitCharAAAA[n]+nothing).toInt(); Serial.print("Digit to send: "); Serial.println(digitAAAA); delay(20); Wire.write(int (digitAAAA)); // Values must not be greater than 255 (1 byte)? } }