Help with Blynk code for temperature read

I am using an Arduino 101 with a Max31850 K thermocouple. This code reads and writes to my serial port and all works well accurate temperatures. It also connect to the BLYNK app but I cannot get the real temp number on the value display S widget. It writes the word “HIGH” not what I see on the serial.

I am connected to pin 2 on the Arduino 101 and using D2 on the widget. Can anyone help?
Here is the code:

//#define BLYNK_USE_DIRECT_CONNECT

#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <BlynkSimpleCurieBLE.h>
#include <CurieBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “YourAuthToken”;

BLEPeripheral blePeripheral;
#define ONE_WIRE_BUS 2
/-(Connect to Pin 2 )-/

/-----( Declare objects )-----/
/* Set up a oneWire instance to communicate with any OneWire device*/ OneWire ourWire(ONE_WIRE_BUS);

/* Tell Dallas Temperature Library to use oneWire Library / DallasTemperature sensors(&ourWire); void setup() {
Serial.begin(9600);
delay(1000);
/
-( Start up the DallasTemperature library )-*/

blePeripheral.setLocalName(“Blynk”);
blePeripheral.setDeviceName(“Blynk”);
blePeripheral.setAppearance(384);

Blynk.begin(blePeripheral, auth);

blePeripheral.begin();
sensors.begin();
Serial.println(“Waiting for connections…”); }

void loop() {

Serial.println();
Serial.print(“Requesting temperature…”); sensors.requestTemperatures(); // Send the command to get temperatures Serial.println(“DONE”);

Serial.print(“Device 1 (index 0) = “);
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” Degrees C”);
Serial.print(“Device 1 (index 0) = “);
Serial.print(sensors.getTempFByIndex(0));
Serial.println(” Degrees F”);
delay(5000);

Blynk.run();
blePeripheral.poll();

}/* --(end main loop )-- */

hello!

before answering questions, please search and read how to format your code properly when posting.

also, i would reccomend to you (if using arduino ide) to try right click > auto format… it can do magic things… :wink:

1 Like

Format your code with the following exmaple…

```cpp
CODE
```

Replace CODE with your entire sketch after auto-formatting it in the IDE (CTRL+T)

Is this good?

//#define BLYNK_USE_DIRECT_CONNECT

#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <BlynkSimpleCurieBLE.h>
#include <CurieBLE.h>
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";


BLEPeripheral  blePeripheral;

/*-(Connect to Pin 2 )-*/

/*-----( Declare objects )-----*/
/* Set up a oneWire instance to communicate with any OneWire device*/
OneWire ourWire(ONE_WIRE_BUS);

/* Tell Dallas Temperature Library to use oneWire Library */
DallasTemperature sensors(&ourWire);

void setup() {
  Serial.begin(9600);
  delay(1000);
  /*-( Start up the DallasTemperature library )-*/

  blePeripheral.setLocalName("Blynk");
  blePeripheral.setDeviceName("Blynk");
  blePeripheral.setAppearance(384);

  Blynk.begin(blePeripheral, auth);

  blePeripheral.begin();
  sensors.begin();
  Serial.println("Waiting for connections...");
}



void loop() {

  Serial.println();
  Serial.print("Requesting temperature...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Device 1 (index 0) = ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.println(" Degrees C");
  Serial.print("Device 1 (index 0) = ");
  Serial.print(sensors.getTempFByIndex(0));
  Serial.println(" Degrees F");


  delay(5000);


  Blynk.run();
  blePeripheral.poll();
}
/* --(end main loop )-- */
1 Like

A couple of Blynk guidelines is:

  • the loop should contain only
void loop(){
  Blynk.run();
  timer.run();
}

Study the PushData example to learn about timers.

  • Delay, especially 5 seconds, will cause a disconnect due to heartbeat timeout.

So remove all delay()s from your code and replace with timers (SimpleTimer which is included with Blynk Library).

Same concept, different devices. Still, this is what I use to transmit temp and humidity readings from my Mega over to Blynk.

    void climateCheck()
    {
      h = dht.readHumidity();
      f = dht.readTemperature(true);

      Blynk.virtualWrite(V0, f);    //  Set Virtual Pin 0 frequency to PUSH in Blynk app (Value Display)
      Blynk.virtualWrite(V1, h);      //  Set Virtual Pin 1 frequency to PUSH in Blynk app (Value Display)
      //Serial.print(f);
      //Serial.print(h);
    }

As Jamin said, get rid of delay all together and use timers. They are simple to use. Just put put related code into a function, and set that function to be ran at whatever frequency you want. I set mine to 3000 so I get a reading every 3 seconds. This is how timer.run() in the loop works. It’s just another loop, or series of loops if you have multiple functions.

yes, it looks much cleaner, isn’t it? but you should have to do this on your first post, at the top of this page :slight_smile: use the edit button.

regarding your question, i think the problem is (among what @Jamin and the others say) , that you are reading the state of the digital pin in the widget (you are reading d2). this is why it reads “high”. (the “value” of a digital pin is either 1 or 0. actually the temperature value is obtained from reading a digital signal, a lots of 0 and 1)

to display the actual temperature value, you should pass the sensor reading to a virtual pin, and use that in the widget. look into virtual pins in the documentation.

Thank you all for your input. I am new to this so I am having trouble using virtual pins and the example because I get various before token errors depending on where I interject the virtual code.
When you say pass on the sensor to virtual pin, can you show me exactly where it goes in the code?

i will try to explain:

first, about simple timer:
simple timer is used for invoking periodically some functions. this is used instead of the delay(); if you’re using delay to time your events, you’re not halting just the function you wish to time, but the whole code on the mcu. this is not a good practice, especially for boards with internet / bluetooth etc connections.

PLEASE READ THIS (actually, one should read all the docs, to have a basic image how to use blynk): http://docs.blynk.cc/#troubleshooting-delay

to use simple timer:

  1. you have to initialize first, outside of any functions:
    SimpleTimer timer;

  2. then, you have to create a function what you will call periodically through simple timer:

    void myTimerEvent()
    {
    // You can send any value at any time.
    // Please don’t send more that 10 values per second.
    Blynk.virtualWrite(V5, millis() / 1000);
    }

observe, that here we are sending a value (the uptime in seconds) to a virtual pin: V5

  1. next, you have to setup how often you wish to run this function. this is made inside the void setup() function:

    // Setup a function to be called every second
    timer.setInterval(1000L, myTimerEvent);

  2. you have to put timer.run(); into the void loop()

  3. and last, just create a display widget in the app, set virtual pin V5, and set the frequency to push

it should work now. if you wish to display the temperature in display widget instead of uptime, you simply replace in code:

Blynk.virtualWrite(V5, millis() / 1000);
with:
Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));

1 Like

SUCCESS…Thank you very much from all who supported me. I got rid of the delay and incorporated the virtual 5 pin with push. I now get an accurate temperature.
I needed a proof before building the real project. It will read 6 type K thermocouples that are attached to the cylinder heads on my airplane engine. I want to monitor those on my iPad as I fly.
I already have the plane wired ready to go.
I may be back at some point. Again, I appreciate your expertise and patience.

1 Like

glad you made it working!
just take it easy with the airplane + blynk combo, all this blynk stuff is surely not designed to use where one’s life depends on it!

and do not forget, that the wifi or ble is not 100% stable and blynk actually needs internet on the app side to work even with ble!

1 Like

Thanks for the concern but this would be for information only. However, I wanted to learn how to use all the tools as I am righting an apple app to replace the BLYNK on my iPad. It does not need WiFi

Are there any apps like BLYNK already made that do not use wifi to operate?

Thanks

You are asking that here… heretic :stuck_out_tongue_winking_eye: (just kidding).

What you are describing is more a direct hardware to physical display option. But I suppose Bluetooth and/or USB On-The-Go connectivity might be other options (not sure about OTG ever being supported with Blynk?)

Did not think of that. I think the BLYNK app is awesome, in the end I need to do something else or Ask BLYNK the question. Thank you for the information.

if you just need to display some temperature values on your phone, without internet / wifi, you should try:

http://appinventor.mit.edu/explore/index-2.html

https://www.google.ro/search?q=hc-05&oq=hc-05&aqs=chrome..69i57j69i65l2j0l3.6386j0j7&sourceid=chrome&ie=UTF-8#q=arduino+bluetooth+instructables&*

some time ago i’ve used this to create a very simple app, using arduino uno + bluetooth hc-5 module, and switch a led on/off on arduino with my phone, and displaying some values on phone.

Thank you