我们有一个带有bpm180传感器的树莓pi3,我们像这样初始化连接
TpmDevice myDevice = new TpmDevice(0); // Use logical device 0 on the TPM
string hubUri = myDevice.GetHostName();
string deviceId = myDevice.GetDeviceId();
string sasToken = myDevice.GetSASToken();
deviceClient = DeviceClient.Create(
hubUri,
Microsoft.Azure.Devices.Client.AuthenticationMethodFactory.
CreateAuthenticationWithToken(deviceId, sasToken), Microsoft.Azure.Devices.Client.TransportType.Amqp);
然后我们发送这样的消息:
public async void Bpm180Tick(object state)
{
string temperatureText;
try
{
var sensorData = await _bmp180.GetSensorDataAsync(Bmp180AccuracyMode.UltraHighResolution);
temperatureText = sensorData.Temperature.ToString("");
temperatureText += "°C";
var temperatureDataPoint = new
{
deviceId = Global.GlobalTemperature,
valor = sensorData.Temperature,
fecha = DateTime.Now
};
//// actualizaciones de la interfaz de usuario... deben ser invocados en el subproceso de interfaz de usuario
var messageString = JsonConvert.SerializeObject(temperatureDataPoint);
var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString));
//message.Properties["Ambiente"] = ambiente;
//autoResetEvent.WaitOne();
await deviceClient.SendEventAsync(message);
//autoResetEvent.Set();
var task = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Txt_Temperaura.Text = temperatureText;
});
}
catch (Exception ex)
{
var a = ex.StackTrace;
temperatureText = "Sensor Error: " + ex.Message;
}
}
然而,我们正在获取和对象引用,但错误似乎是在库(sdk),而不是在我们的代码
堆栈跟踪是thisÑ
at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.<SendAmqpMessageAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.<SendEventAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<>c__DisplayClass13_0.<<ExecuteWithErrorHandlingAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<ExecuteWithErrorHandlingAsync>d__14`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.GateKeeperDelegatingHandler.<SendEventAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at ActiveSense.Tempsense.Sensor.MainPage.<Bpm180Tick>d__23.MoveNext()
我想知道是否有可能,温度文本不再存在的时间异步代码正在运行。我将首先尝试使其成为静态类变量,看看会发生什么。
很可能SAS令牌过期,但AMQP库没有给您有用的错误。要确认这一点,请使用带有SharedAccessKey的连接字符串:
DeviceClient.CreateFromConnectionString("...;SharedAccessKey=xyz;...", TransportType.Amqp);
如果有效,您需要考虑更新您的SAS令牌(不要在产品代码中留下SharedAccessKey !)。如果出现相同的错误,则问题出在代码的其他地方。
SAS令牌可能过期。
另一方面,由于azure - iot - sdk是开源的,您可以直接引用sdk源代码来检查错误。
- 删除你的项目nuget引用"Microsoft.Azure.Devices.Client"
- 将azure-iot-sdk存储库与您的项目并行克隆
- 在项目中引用Microsoft.Azure.Devices.Client.WinRT
您应该能够调试AmqpTransportHandler。SendAmqpMessageAsync的源代码,看看问题到底是什么。
我自己没有运气在Windows IoT Core 14393的RPi3上重现您的问题。
发生这种情况是因为TPM库中有一个错误,我提交了一个问题并修复了拉取请求。
这是问题的链接:https://github.com/ms-iot/security/issues/11
SAS令牌立即过期。它能工作一段时间的唯一原因是物联网集线器允许一些时钟偏差。我还在等待NuGet上的Microsoft.Devices.Tpm的新版本