欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

delphi7 使用原生控件clientsocket 实现 阿里云三元组 连接阿里云mqtt

程序员文章站 2022-03-07 18:23:31
delphi7 使用原生控件clientsocket 实现 阿里云三元组 连接阿里云mqttprocedure TFmqtt.Button1Click(Sender: TObject);varipname:string;clientId:string;t:string;signmethod:string;passbegin:string;back:PAnsiChar;code:integer;beginProductKey:=trim(self.Edit1.Text);DeviceN...

delphi7 使用原生控件clientsocket 实现 阿里云三元组 连接阿里云mqtt

procedure TFmqtt.Button1Click(Sender: TObject);
var
ipname:string;
clientId:string;
t:string;
signmethod:string;
passbegin:string;
back:PAnsiChar;
code:integer;
begin
ProductKey:=trim(self.Edit1.Text);
DeviceName:=trim(self.Edit2.Text);
DeviceSecret:=trim(self.Edit3.Text);

ipname:=ProductKey+’.iot-as-mqtt.cn-shanghai.aliyuncs.com’;
serverip:=GetIPFromName(ipname);
self.Memo1.Lines.Add(‘serverip=’+serverip);
clientId:=self.IdIPWatch1.LocalIP;
t:=inttostr((DateTimeToUnix(now)-86060)*1000);
if self.RadioGroup1.ItemIndex=0 then
begin
signmethod:=‘hmacmd5’;
end else
begin
signmethod:=‘hmacsha1’;
end;
mqttUserName:= DeviceName + ‘&’ + ProductKey;
passbegin:=‘clientId’+clientId+‘deviceName’+deviceName+‘productKey’+productKey+‘timestamp’+t;
self.Memo1.Lines.Add(‘passbegin=’+passbegin);

if self.RadioGroup1.ItemIndex=0 then
begin
code:=hmacmd5(PAnsiChar(AnsiString(DeviceSecret)),PAnsiChar(AnsiString(passbegin)),back);
if code=0 then
begin
mqttPassword:=back;
self.Memo1.Lines.Add(‘mqttPassword=’+mqttPassword);
end;
end else
begin
code:=hmacsha1(PAnsiChar(AnsiString(DeviceSecret)),PAnsiChar(AnsiString(passbegin)),back);
if code=0 then
begin
mqttPassword:=back;
self.Memo1.Lines.Add(‘mqttPassword=’+mqttPassword);
end;
end;
mqttClientId := clientId + ‘|securemode=3,signmethod=’ + signmethod + ‘,timestamp=’ + t + ‘|’;

if self.RadioButton2.Checked then
begin
PubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/update’;
SubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/get’;
end else
begin
PubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/user/update’;
SubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/user/get’;
end;

rrpcTopic:=’/sys/’+ProductKey+’/’+DeviceName+’/rrpc/request/+’;
rrpcrecTopic:=’/sys/’+ProductKey+’/’+DeviceName+’/rrpc/request/’;
rrpcsendTopic:=’/sys/’+ProductKey+’/’+DeviceName+’/rrpc/response/’;

ClientSocket1.Active:=False;
ClientSocket1.Address:=serverip;
ClientSocket1.Port:=1883;
ClientSocket1.Active:=True; //526887369

end;

本文地址:https://blog.csdn.net/blue_apple_blue/article/details/108165144