Reading temperature from Delphi

Moderator: Telldus

Post Reply
oler
Posts: 1
Joined: Fri Mar 17, 2023 9:45 am

Reading temperature from Delphi

Post by oler »

This is how I did it:

First you have to declare some constants and procedures:

unit TelldusCoreAPI;

interface

type IntegerPtr = ^integer;

//Sensor value types
//define TELLSTICK_TEMPERATURE 1
TELLSTICK_TEMPERATURE = 1;
//define TELLSTICK_HUMIDITY 2
TELLSTICK_HUMIDITY = 2;

// TELLSTICK_API int WINAPI tdSensor(char *protocol, int protocolLen, char *model, int modelLen, int *id, int *dataTypes);
function tdSensor(Protocol: pchar; ProtocolLen: integer; Model: pchar; ModelLen: integer; Id: integerPtr; DataTypes: integerPtr): integer; stdcall; external 'TelldusCore.dll';
// TELLSTICK_API int WINAPI tdSensorValue(const char *protocol, const char *model, int id, int dataType, char *value, int len, int *timestamp);
function tdSensorValue(Protocol: pchar; Model: pchar; Id: integer; DataType: integer; Value: pchar; Len: integer; TimeStamp: integerPtr): integer; stdcall; external 'TelldusCore.dll';

implementation

initialization
finalization

end.

Then create a project an put a TButton (Button10) on a Form together with a TMemo (M1).

uses TelldusCoreAPI;


procedure TForm1.Button10Click(Sender: TObject);
const DataLength = 20;
var Protocol,Model,Value: string;
SensorIdPtr,DataTypesPtr,TimeStampPtr: integerPtr;
begin
new(SensorIdPtr);
new(DataTypesPtr);
new(TimeStampPtr);
SetLength(Protocol,DataLength);
SetLength(Model,DataLength);
SetLength(Value,DataLength);
tdInit;
while(tdSensor(pchar(Protocol),DataLength, pchar(Model), DataLength, sensorIdPtr, dataTypesPtr) = TELLSTICK_SUCCESS) do
begin
M1.Lines.Add('Protocol='+Trim(Protocol)+',Model='+Trim(Model)+',SensorId='+IntToStr(SensorIdPtr^));
if (DataTypesPtr^ and TELLSTICK_TEMPERATURE) <> 0
then
begin
tdSensorValue(pchar(Protocol), pchar(Model), SensorIdPtr^, TELLSTICK_TEMPERATURE, pchar(Value), DataLength, TimeStampPtr);
M1.Lines.Add('Temperature='+Trim(Value)+'degrees');
end;
end;
end;

with this temperature sensor:

http://www.clasohlson.se/Product/Produc ... =164635172

It works!

/Ole
Post Reply