Raspberry Pi、Windows 物联网、C#
Raspberry Pi, Windows IoT, C#
我目前正在使用我的 Raspberry Pi Grove 传感器和 Windows IoT 进行试验。我正在尝试将信息从 Raspberry Pi 传输到 Azure,但收到错误消息(第 85 行)。
代码看起来是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;
using GrovePi;
using GrovePi.I2CDevices;
using GrovePi.Sensors;
using GrovePi.Common;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;
// The Background Application template is documented at http://go.microsoft.com/fwlink/?LinkID=533884&clcid=0x409
namespace GrooveTHS
{
public sealed class StartupTask : IBackgroundTask
{
IRgbLcdDisplay LCDDisplay;
public void Run(IBackgroundTaskInstance taskInstance)
{
IDHTTemperatureAndHumiditySensor sensor = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin7, DHTModel.Dht11); // pinD7
LCDDisplay = DeviceFactory.Build.RgbLcdDisplay(); // pinI2C-1
IRotaryAngleSensor potentiometer = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin2); // pinA2
ILed red = DeviceFactory.Build.Led(Pin.DigitalPin5); // pinD5
ILed green = DeviceFactory.Build.Led(Pin.DigitalPin6); // pinD6
IUltrasonicRangerSensor dsensor = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4); // pin D4
double angle = 0;
double tmax = 0;
while (true)
{
Task.Delay(500).Wait();
angle = potentiometer.SensorValue();
sensor.Measure();
string sensortemp = sensor.TemperatureInCelsius.ToString();
tmax = Math.Floor(angle / 10);
string sensorvalue = dsensor.MeasureInCentimeters().ToString();
if (sensor.TemperatureInCelsius > tmax)
{
System.Diagnostics.Debug.WriteLine("Raumtemperatur: " + sensortemp + "C " + "Tmax: " + tmax.ToString() + "C");
red.AnalogWrite(Convert.ToByte(240));
green.AnalogWrite(Convert.ToByte(0));
}
else
{
System.Diagnostics.Debug.WriteLine("Raumtemperatur: " + sensortemp + "C" + "Tmax: " + tmax.ToString() + "C");
red.AnalogWrite(Convert.ToByte(0));
green.AnalogWrite(Convert.ToByte(240));
}
if (dsensor.MeasureInCentimeters() < 150)
{
LCDDisplay.SetBacklightRgb(BitConverter.GetBytes(990)[0], BitConverter.GetBytes(990)[0], BitConverter.GetBytes(990)[0]);
LCDDisplay.SetText("Raumtemperatur: " + sensortemp + "C " + "Tmax: " + tmax.ToString() + "C");
}
else
{
LCDDisplay.SetText("");
LCDDisplay.SetBacklightRgb(BitConverter.GetBytes(0)[0], BitConverter.GetBytes(0)[0], BitConverter.GetBytes(0)[0]);
}
}
}
static async void SendDeviceToCloudMessagesAsync(long inputDistance)
{
string iotHubUri = "IotHubAuburn.azure-devices.net"; // ! put in value !
string deviceId = "jb"; // ! put in value !
string deviceKey = "sHGJlQbLLMeMExNaqtvh8/7N7MHWlBZ0ESj2ePahSwQ="; // ! put in value !
DateTime time = DateTime.UtcNow;
var deviceClient = DeviceClient.Create(iotHubUri, AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey), TransportType.Http1);
JSON jsonStr = new JSON();
jsonStr.distance = inputDistance;
jsonStr.time = time;
jsonStr.deviceId = deviceId;
var list = JsonConvert.SerializeObject(jsonStr);
System.Diagnostics.Debug.WriteLine(list);
var message = new Message(Encoding.UTF8.GetBytes(list));
await deviceClient.SendEventAsync(message);
}
}
}
我得到的错误如下(翻译自德语):
1) CS0246 The type or a namespacename "JSON" was not found (maybe a
using-derective or a reprimand assembly is missing).
2) CS0246 The type or a namespacename "JSON" was not found (maybe a
using-derective or a reprimand assembly is missing).
亲切的问候,
亚历克斯
错误的意思是找不到"JSON" class定义。
正如@Eric Magers 指出的那样,您可以从您引用的代码源中找到 JSON class 定义的位置。
或者您也可以定义自己的"JSON" class,例如,像这样:
internal class JSON
{
public JSON()
{
}
public string deviceId { get; internal set; }
public long distance { get; internal set; }
public DateTime time { get; internal set; }
}
另一种没有定义 "JSON" class 的方法是这样的:
string dataBuffer;
DateTime time = DateTime.UtcNow;
long inputDistance = 0;
String deviceId = "MyCSharpDevice";
dataBuffer = string.Format("{{\"deviceId\":\"{0}\",\"distance\":{1},\"time\":{2}}}", deviceId, inputDistance, time);
Message message = new Message(Encoding.UTF8.GetBytes(dataBuffer));
await deviceClient.SendEventAsync(message);
关于在 C# 中使用 HTTP 协议向 Azure IoT Hub 发送消息,您可以参考 this official sample。注意:官方示例在控制台应用程序中,您正在 Windows IoT Core 上使用后台应用程序。应用程序类型不同,但如何使用 Azure IoT Hub SDK 是相同的。
我目前正在使用我的 Raspberry Pi Grove 传感器和 Windows IoT 进行试验。我正在尝试将信息从 Raspberry Pi 传输到 Azure,但收到错误消息(第 85 行)。
代码看起来是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;
using GrovePi;
using GrovePi.I2CDevices;
using GrovePi.Sensors;
using GrovePi.Common;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;
// The Background Application template is documented at http://go.microsoft.com/fwlink/?LinkID=533884&clcid=0x409
namespace GrooveTHS
{
public sealed class StartupTask : IBackgroundTask
{
IRgbLcdDisplay LCDDisplay;
public void Run(IBackgroundTaskInstance taskInstance)
{
IDHTTemperatureAndHumiditySensor sensor = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin7, DHTModel.Dht11); // pinD7
LCDDisplay = DeviceFactory.Build.RgbLcdDisplay(); // pinI2C-1
IRotaryAngleSensor potentiometer = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin2); // pinA2
ILed red = DeviceFactory.Build.Led(Pin.DigitalPin5); // pinD5
ILed green = DeviceFactory.Build.Led(Pin.DigitalPin6); // pinD6
IUltrasonicRangerSensor dsensor = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4); // pin D4
double angle = 0;
double tmax = 0;
while (true)
{
Task.Delay(500).Wait();
angle = potentiometer.SensorValue();
sensor.Measure();
string sensortemp = sensor.TemperatureInCelsius.ToString();
tmax = Math.Floor(angle / 10);
string sensorvalue = dsensor.MeasureInCentimeters().ToString();
if (sensor.TemperatureInCelsius > tmax)
{
System.Diagnostics.Debug.WriteLine("Raumtemperatur: " + sensortemp + "C " + "Tmax: " + tmax.ToString() + "C");
red.AnalogWrite(Convert.ToByte(240));
green.AnalogWrite(Convert.ToByte(0));
}
else
{
System.Diagnostics.Debug.WriteLine("Raumtemperatur: " + sensortemp + "C" + "Tmax: " + tmax.ToString() + "C");
red.AnalogWrite(Convert.ToByte(0));
green.AnalogWrite(Convert.ToByte(240));
}
if (dsensor.MeasureInCentimeters() < 150)
{
LCDDisplay.SetBacklightRgb(BitConverter.GetBytes(990)[0], BitConverter.GetBytes(990)[0], BitConverter.GetBytes(990)[0]);
LCDDisplay.SetText("Raumtemperatur: " + sensortemp + "C " + "Tmax: " + tmax.ToString() + "C");
}
else
{
LCDDisplay.SetText("");
LCDDisplay.SetBacklightRgb(BitConverter.GetBytes(0)[0], BitConverter.GetBytes(0)[0], BitConverter.GetBytes(0)[0]);
}
}
}
static async void SendDeviceToCloudMessagesAsync(long inputDistance)
{
string iotHubUri = "IotHubAuburn.azure-devices.net"; // ! put in value !
string deviceId = "jb"; // ! put in value !
string deviceKey = "sHGJlQbLLMeMExNaqtvh8/7N7MHWlBZ0ESj2ePahSwQ="; // ! put in value !
DateTime time = DateTime.UtcNow;
var deviceClient = DeviceClient.Create(iotHubUri, AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey), TransportType.Http1);
JSON jsonStr = new JSON();
jsonStr.distance = inputDistance;
jsonStr.time = time;
jsonStr.deviceId = deviceId;
var list = JsonConvert.SerializeObject(jsonStr);
System.Diagnostics.Debug.WriteLine(list);
var message = new Message(Encoding.UTF8.GetBytes(list));
await deviceClient.SendEventAsync(message);
}
}
}
我得到的错误如下(翻译自德语):
1) CS0246 The type or a namespacename "JSON" was not found (maybe a using-derective or a reprimand assembly is missing).
2) CS0246 The type or a namespacename "JSON" was not found (maybe a using-derective or a reprimand assembly is missing).
亲切的问候,
亚历克斯
错误的意思是找不到"JSON" class定义。 正如@Eric Magers 指出的那样,您可以从您引用的代码源中找到 JSON class 定义的位置。
或者您也可以定义自己的"JSON" class,例如,像这样:
internal class JSON
{
public JSON()
{
}
public string deviceId { get; internal set; }
public long distance { get; internal set; }
public DateTime time { get; internal set; }
}
另一种没有定义 "JSON" class 的方法是这样的:
string dataBuffer;
DateTime time = DateTime.UtcNow;
long inputDistance = 0;
String deviceId = "MyCSharpDevice";
dataBuffer = string.Format("{{\"deviceId\":\"{0}\",\"distance\":{1},\"time\":{2}}}", deviceId, inputDistance, time);
Message message = new Message(Encoding.UTF8.GetBytes(dataBuffer));
await deviceClient.SendEventAsync(message);
关于在 C# 中使用 HTTP 协议向 Azure IoT Hub 发送消息,您可以参考 this official sample。注意:官方示例在控制台应用程序中,您正在 Windows IoT Core 上使用后台应用程序。应用程序类型不同,但如何使用 Azure IoT Hub SDK 是相同的。