cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

How to connect TMP36 Analog Temperature Sensor with ThingWorx.

smishra-31
5-Regular Member

How to connect TMP36 Analog Temperature Sensor with ThingWorx.

Hi,

 

Today I was going through one of the QuickStart Guides: https://developer.thingworx.com/resources/guides/thingworx-raspberry-pi-quickstart/

Where ThingWorx team has used Adafruit DHT11Temperature Sensor to connect it to Raspberry Pi.

But in place of that I have ​TMP36 Analog Temperature Sensor​ and Arduino Uno. As Raspberry Pi cannot read/convert analog values into digital values, I have attached the sensor to Arduino and than connect it to Raspberry Pi.

 

In Arduino I have used this:

 

const int temperaturePin = 0;

 

void setup()

  Serial.begin(9600);

}

 

void loop()

{

  float voltage, degreesC;

 

  voltage = getVoltage(temperaturePin);

 

  degreesC = (voltage - 0.5) * 100.0;

 

  Serial.print("Temp=");

  Serial.println(degreesC);

  

  delay(1000);

}

 

float getVoltage(int pin)

  return (analogRead(pin) * 0.004882814);

}

 

Python Script that I have place in /microserver/ArduinoTempSensor/ArduinoTempSensor.py is:

 

 

#!/usr/bin/python

 

import serial

import time

 

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)

 

while True:

    if ser.inWaiting > 0:

        print(ser.readline())

 

and in PiTemplate.lua file, I have added these lines as mentioned:

 

1) properties.temp = { baseType="NUMBER", pushType="ALWAYS", value=0 }

2) local sensorCmd = io.popen("./ArduinoTempSensor/ArduinoTempSensor.py")

3)     -- set property temp
local sensor = sensorCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw sensor %s", sensor))
s = string.match(sensor,"Temp=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("scaled temp %.1f", s))
properties.temp.value = s

 

This far I have gone but results are not so good. The values got  updated but only once, after that it is not getting refreshed or new values.

 

PiThing.PNG

Errors that I am getting while running EMS is:

EMS Error.PNG

 

While running Lua Script:

Lua Script.PNG

 

Regards,

Shashank

1 ACCEPTED SOLUTION

Accepted Solutions
smishra-31
5-Regular Member
(To:supandey)

Hi @supandey,

 

Apologies for late reply.

 

I am using Python 2.4

 

I have completely changed my old python script. Instead of initiating a infinite loop in script ( while True: ), I am returning only the 5th Value ( retries=5 ). I have used regular expression also to check the correct format.

So it is working fine now.

#!/usr/bin/python

import sys
import serial
import time
import re

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)
r = re.compile("\d{2}.\d{2}")

def read_retry(retries=5):
    temperature = None
    for i in range(retries):
        temp = ser.readline()
        if i == retries-1:
            if r.match(temp) is not None:
                temperature = float(temp)
            else:
                temperature = None
    return temperature

SensorTemp = read_retry()

if SensorTemp is not None:
    print('Temp={0:0.2f}'.format(SensorTemp))
else:
    print('Failed to get reading. Try again!')
    sys.exit(1)

 

Thanks & Regards,

Shashank

View solution in original post

3 REPLIES 3
supandey
19-Tanzanite
(To:smishra-31)

Hi @smishra-31 could you have both EMS and Lua config files enabled with Trace logging

 

EMS logging:

 

"logger": {
"level": "TRACE"
},

 

LSR logging setting :

 

scripts.log_level = "INFO"

What's also interesting is that your py scripts are returning float as datatype for the values while the ThingWorx and lua are attempting to work with Number base type. Have you already tried debugging the python script for possible mismatch of datatypes?

supandey
19-Tanzanite
(To:smishra-31)

BTW, which python version are you working with?
smishra-31
5-Regular Member
(To:supandey)

Hi @supandey,

 

Apologies for late reply.

 

I am using Python 2.4

 

I have completely changed my old python script. Instead of initiating a infinite loop in script ( while True: ), I am returning only the 5th Value ( retries=5 ). I have used regular expression also to check the correct format.

So it is working fine now.

#!/usr/bin/python

import sys
import serial
import time
import re

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)
r = re.compile("\d{2}.\d{2}")

def read_retry(retries=5):
    temperature = None
    for i in range(retries):
        temp = ser.readline()
        if i == retries-1:
            if r.match(temp) is not None:
                temperature = float(temp)
            else:
                temperature = None
    return temperature

SensorTemp = read_retry()

if SensorTemp is not None:
    print('Temp={0:0.2f}'.format(SensorTemp))
else:
    print('Failed to get reading. Try again!')
    sys.exit(1)

 

Thanks & Regards,

Shashank

Top Tags