简体   繁体   中英

Raspberry Pi, Windows IoT, C#

I am currently experimenting with my Raspberry Pi with Grove sensors and Windows IoT. I am trying to transfer information from the Raspberry Pi to Azure and I receive an error (In line 85).

The code looks this way:

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);
        }
    }
}

And errors I am getting are following (Translated from german language):

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).

Kind regards,

Alex

The error means that the "JSON" class definition can't be found. As @Eric Magers pointed out, you can find from the code source you referenced where is the JSON class defined.

Or you can also define your own "JSON" class, for example, like this:

internal class JSON
{
    public JSON()
    {
    }

    public string deviceId { get; internal set; }
    public long distance { get; internal set; }
    public DateTime time { get; internal set; }
}

Another method without defining "JSON" class like this:

        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);

For sending messages to Azure IoT Hub using HTTP protocol in C# you can reference this official sample . Note: the official sample is in Console Application and you are using a background application on Windows IoT Core. Application types are different but how to use Azure IoT Hub SDK is the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM