Shifting temperature readings from Arduino Uno over bluetooth

Hey guys...

need some help figuring out an issue with receiving 6 temp readings using DS18B20 sensors connected via an UNO and send over bluetooth to an MIT inventor created app.

I do get all readings to display but they shift a lot between the designated locations.
I have to play with clock timer in the app and delay in the arduino sketch to make it somewhat acceptable but still shifts but you really have to look for them. Downside is that the refreshrate takes to long for my liking.

any help would really be appreciated

Arduino sketch;

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

#define ONE_WIRE_BUS A0

SoftwareSerial mySerial(11, 12);
OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

uint8_t sensor1[8] = { 0x28, 0x67, 0xF4, 0x0D, 0x00, 0x00, 0x00, 0x7C };
uint8_t sensor2[8] = { 0x28, 0x1E, 0x4D, 0x0F, 0x00, 0x00, 0x00, 0xC9 };
uint8_t sensor3[8] = { 0x28, 0xFF, 0xB8, 0x0F, 0x00, 0x00, 0x00, 0xA3 };
uint8_t sensor4[8] = { 0x28, 0xFA, 0x46, 0x0B, 0x00, 0x00, 0x00, 0x42 };
uint8_t sensor5[8] = { 0x28, 0x09, 0xD9, 0x0A, 0x00, 0x00, 0x00, 0x3E };
uint8_t sensor6[8] = { 0x28, 0x98, 0x15, 0x49, 0x0C, 0x00, 0x00, 0x68 };

const byte auxPin [] {3, 4, 5, 6, 7, 8, 9, 10};

void setup(void)
{
for (auto& pin : auxPin) {
pinMode(pin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
mySerial.begin(9600);
Serial.begin(9600);
sensors.begin();
}

void loop(void)

{
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
}

if (mySerial.available()) {
int i = mySerial.read();

if (i >= '1' && i <= '8') {
  int index = i - '1';
  
  Serial.println ((char)i);
  digitalWrite(auxPin [index], HIGH);
  delay(500);
  digitalWrite(auxPin [index], LOW);
  delay(100);
}

}

sensors.requestTemperatures();
printTemperature(sensor1);
Serial.print(",");
printTemperature(sensor2);
Serial.print(",");
printTemperature(sensor3);
Serial.print(",");
printTemperature(sensor4);
Serial.print(",");
printTemperature(sensor5);
Serial.print(",");
printTemperature(sensor6);
Serial.println();
delay(500);

}

void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC, 1);

}

I don't see any AI2 blocks or exported .aia file in your post, so all I can offer is standard Delimiter advice:

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Already had Delimiter set to 10 and used println() at the end of each message.

here is the aia to check what on earth i did wrong..
D5200.aia (4.4 MB)

could you clarify this a bit more for me because i think my fault is in here;
"If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values."

thanks again

Here are your existing Clock1 Timer blocks:

Here are my corrected Clock1 Timer blocks:

What I fixed:

  • renamed global LIST to global readingList, for readability and to distinguish it from other lists (promises, mistakes, things I need to fix)
  • Asked for -1 bytes, to get everything up to the Delimiter (newLine),
  • Tested every index 1-6 for existence before deciding to copy its reading into label(s).
  • removed the blocks that clear those 2 global variables at the end of the Timer. You need those for Do It debugging when you suspect missing data.

I did not speed up your Clock, which looks slow. Speed it up by a factor of 10.
Clock1
You want to read faster than the incoming data arrives, or it will pile up and clog.

Your Delimiter looks good, but I see no need for High Byte First unless you are trying to read Integers, which don't mix well with text.
BT

P.S. These blocks can be dragged directly into your Blocks Editor workspace.

See A way to group projects by theme - #10 by ABG
for a demo.

Hey... very sorry to just see you replied..
i will test your blocks today the moment i get home.
Luckily i can edit my app from work

I will let you know the outcome
again... thank you

working flawlessly!

i can not thank you enough

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.