How to display temperature value on Blynk (Arduino + TMP36)

Hello blynkers.

I’m making a simple project with Blynk but I’m having some problem and it would be great to have a hand from you. I’m using the TMP36 sensor from the arduino starter kit, the widget “Value Display” on Blynk and this code:

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

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

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  // Default baud rate is 9600. You could specify it like this:
  //Blynk.begin(auth, 57600);
}

void loop()
{ 
  Blynk.run();
}

although I’m not sure if this is enough. The problem is that the value it gives me it’s around “150” which is obviously not my temperature in the room (around 20°C). It seems to me that there’s no conversion from volts to celsius degree. What do you think? Do you know why I’m having this issue?

I’ve found this code for the conversion from one of my old projects:

  int sensorValue = analogRead(A0);
  float volt = (sensorValue/1024.0) * 5;
  int tempC = (volt - 0.5) * 100;

should I add it into the code?

Thanks in advance for you help!

Yeah, I guess you should. You can use virtual pin to send value to Blynk:

  int sensorValue = analogRead(A0);
  float volt = (sensorValue/1024.0) * 5;
  int tempC = (volt - 0.5) * 100;
  Blynk.virtualWrite(0, tempC);

Thanks for your reply, although I don’t know why Blynk now is not connecting to the board.
I’ve added your code in the void loop, replacing blynk.run() but now the app says that arduino is offline.

Do you know why? Is this the Blynk way to tell me that the code is wrong?

You don’t need to remove Blynk.run();
Just leave it in top of loop function

@orion

Hi, please have a look in this example. It does exactly what you need. Also have in mind that doing

Blynk.virtualWrite(0, tempC);

in main loop is not recommended as it may create too many requests to server. Above example shows correct usage example.

1 Like

@Dmitriy Thanks for your reply. I looked at that example but I can’t fully understand it. I linked the tmp36 sensor to the digital pin 5 and loaded your example (after installing SimpleTimer library and changed my token) but the app keep saying “arduino uno is offline” and socat keep quitting and “reconnecting in 3s…” due to “inactivity timeout triggered”.

Does this mean the code is wrong? Shouldn’t the code include a conversion for celsius degree?

I’m struggling to make it work. Please help!

@asci Still not working. The app says “arduino uno is offline” and socat keeps reconnecting due to “inactivity timout triggered”. Do you know if that’s the way of the app to tell me that the code is wrong?

This is the code I’m using:

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

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

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  // Default baud rate is 9600. You could specify it like this:
  //Blynk.begin(auth, 57600);
}

void loop()
{ 
  Blynk.run();
  int sensorValue = analogRead(A0);
  float volt = (sensorValue/1024.0) * 5;
  int tempC = (volt - 0.5) * 100;
  Blynk.virtualWrite(0, tempC);
  
}

@orion

In case socat is reconnecting than you have problem connecting arduino to server, so message you retrieve in app. is correct. Please show me command line when running socat and its output.

Try to use this command to start server
./blynk-ser.sh -c /dev/<your_device> -b 9600 -s cloud.blynk.cc -p 8442
and then you will see something like this:
Select serial port [ <your_device> ]:
You need to copy and paste the name of your device, then press enter. It helped me, maybe it’ll help you

@asci Thanks but it didn’t work :pensive:

@Dmitriy This is command I’m using:

bash /Users/Orion/Desktop/blynk-script.sh 

I modified to original blynk-ser.sh to use the arduino usb port, so it has:

COMM_PORT_OSX=/dev/cu.usbmodem621

and this is the output:

Orion:~ Orion$ bash /Users/Orion/Desktop/blynk-script.sh
[ Press Ctrl+C to exit ]
Resetting device /dev/cu.usbmodem621...
Warning: certs/server.crt not found. Skipping server verification (connection may be insecure)!
Connecting: GOPEN:/dev/cu.usbmodem621,raw,echo=0,clocal=1,cs8,nonblock=1,ixoff=0,ixon=0,ispeed=9600,ospeed=9600,crtscts=0 <-> openssl-connect:cloud.blynk.cc:8441,verify=0,nodelay
2015/06/02 12:45:37 socat[3394] N opening character device "/dev/cu.usbmodem621" for reading and writing
2015/06/02 12:45:37 socat[3394] N opening connection to LEN=16 AF=2 45.55.195.102:8441
2015/06/02 12:45:37 socat[3394] N successfully connected from local address LEN=16 AF=2 192.168.1.73:49460
2015/06/02 12:45:37 socat[3394] N no check of certificate
2015/06/02 12:45:37 socat[3394] N SSL connection using ECDHE-RSA-AES128-GCM-SHA256
2015/06/02 12:45:37 socat[3394] N SSL connection compression "none"
2015/06/02 12:45:37 socat[3394] N SSL connection expansion "none"
2015/06/02 12:45:37 socat[3394] N starting data transfer loop with FDs [3,3] and [4,4]
2015/06/02 12:45:49 socat[3394] N socket 2 (fd 4) is at EOF
2015/06/02 12:45:50 socat[3394] N inactivity timeout triggered
2015/06/02 12:45:50 socat[3394] N exiting with status 0
Reconnecting in 3s...
Connecting: GOPEN:/dev/cu.usbmodem621,raw,echo=0,clocal=1,cs8,nonblock=1,ixoff=0,ixon=0,ispeed=9600,ospeed=9600,crtscts=0 <-> openssl-connect:cloud.blynk.cc:8441,verify=0,nodelay
2015/06/02 12:45:53 socat[3400] N opening character device "/dev/cu.usbmodem621" for reading and writing
2015/06/02 12:45:53 socat[3400] N opening connection to LEN=16 AF=2 45.55.195.102:8441
2015/06/02 12:45:53 socat[3400] N successfully connected from local address LEN=16 AF=2 192.168.1.73:49461
2015/06/02 12:45:53 socat[3400] N no check of certificate
2015/06/02 12:45:53 socat[3400] N SSL connection using ECDHE-RSA-AES128-GCM-SHA256
2015/06/02 12:45:53 socat[3400] N SSL connection compression "none"
2015/06/02 12:45:53 socat[3400] N SSL connection expansion "none"
2015/06/02 12:45:53 socat[3400] N starting data transfer loop with FDs [3,3] and [4,4]
^C2015/06/02 12:45:53 socat[3400] N exiting on signal 2
Exited!

2015/06/02 12:45:50 socat[3394] N inactivity timeout triggered

Socat specific error. Says that you have no data exchange between server and arduino. It is hard to say why…
Please check :

  1. /dev/cu.usbmodem621 is exactly your arduino
  2. Is that command works for you “telnet cloud.blynk.cc 8441”?
  3. Try to do not modify script, it will propose you to provide usb port

@vshymanskyy, do you have some ideas what it could be?

@Dmitriy

  1. yes, that’s the exact port for my arduino as showed in the arduino software.
  2. yes, that command is working and I can connect to the server.
  3. I’ve deleted and reinstalled the blynk library. When I run the command, it asks me to insert my port, but it still not working. Also, it keeps saying that “certs/server.crt” is not found, even tho I checked and the folder certs is there. I’ve also tried to move the blynk-ser.sh into the certs folder, but nothing.

What else can I do?

Also, just to be sure, I’m incurring in this problem because of the USB connection, right?
Should it be easier for me to buy the wifi shield in order to make this work?

Thanks so much for your help!

@orion

Also, it keeps saying that “certs/server.crt” is not found

It’s ok, in order to avoid this warning you need to run script from folder where it is placed (so run from folder “scripts”), but anyway it should work without certs provided.

Also, just to be sure, I’m incurring in this problem because of the USB connection, right?
Should it be easier for me to buy the wifi shield in order to make this work?

We don’t know where problem is. It should work with USB =).

So let’s try next :

  1. Comment " Blynk.virtualWrite(0, tempC)" just to be sure problem is not in this command;
  2. Add debug mode for your arduino sketch;
  3. Upload sketch to arduino, wait until it will start;
  4. Run blynk-ser.sh script;
  5. Show us output of debug;

Also what arduino board do you have?

@Dmitriy This is the new code I’m running:

#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

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

void setup()
{
  Serial.begin(9600);
  SwSerial.begin(9600);
  Blynk.begin(auth);
  // Default baud rate is 9600. You could specify it like this:
  //Blynk.begin(auth, 57600);
}

void loop()
{ 
  Blynk.run();
  // int sensorValue = analogRead(A0);
  // float volt = (sensorValue/1024.0) * 5;
  // int tempC = (volt - 0.5) * 100;
  // Blynk.virtualWrite(0, tempC);
} 

But in the serial monitor it only shows me my token. While in terminal, socat says “E open(”/dev/cu.usbmodem621", 016, 0666): Resource busy" and it tries to reconnect.

My board is the Arduino UNO.

@Dmitriy I’ve tried again and now is randomly working but the temp is still wrong. So I’m guessing that the problem is in the code maybe, what do you think?

By the way, if I run the script from its folder, it keeps saying no cert found.

I made this, this will work.
Virtual PIN 5

Connect Temp Sensor to A0 and a Value Display (app) on Virtual Pin 5

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  Blynk using a LED widget on your phone!

  App project setup:
    LED widget on V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

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

#define W5100_CS  10
#define SDCARD_CS 4

WidgetLED led1(V1);

BlynkTimer timer;

// V1 LED Widget is blinking
void blinkLedWidget()
{
  if (led1.getValue()) {
    led1.off();
    Serial.println("LED on V1: off");
  } else {
    led1.on();
    Serial.println("LED on V1: on");
  }
}

BLYNK_READ(V5) // Widget in the app READs Virtal Pin V5 with the certain frequency
{
  
  Blynk.virtualWrite(5,tempC);
}
void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8442);

  timer.setInterval(1000L, blinkLedWidget);
}
int counter=0;
void loop()
{
  Blynk.run();
  timer.run();
  int sensorValue = analogRead(A0);
  float volt = (sensorValue/1024.0) * 5;
  tempC = (volt - 0.5) * 100;
  

}

Very Old Topic - Please create new topic for any further discussions.