-1

我在 esp32 上使用 nanoframwork,调试时它工作正常,但是当我停止调试并断开 esp32 与计算机的连接并重新插入 USB 电缆时,它不像调试时那样工作,是代码问题?

using nanoFramework.Json;
using nanoFramework.M2Mqtt;
using nanoFramework.M2Mqtt.Messages;
using System;
using System.Device.Gpio;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Windows.Devices.Adc;

namespace NfStrawberry
{
    public class Program
    {
        public static GpioController gpioController;
        public static AdcController adcController;
        public static GpioPin relayO;//gpio17,control relay,Pinmode:OutputOpenDrain
        public static GpioPin relayI;//gpio18,monitor gpio 17,pinmode:input
        public static Sht1x sht1X;//gpio0->data,4->clock,humidity and temperature
        public static AdcChannel mositure;//gpio15
        public static MqttClient client;

        public static GpioPin led;  // led on board
        public static void Main()
        {
            gpioController = new GpioController();
            led = gpioController.OpenPin(2, PinMode.Output);
            adcController = AdcController.GetDefault();
            mositure = adcController.OpenChannel(0);
            relayO = gpioController.OpenPin(17, PinMode.OutputOpenDrain);
            relayI = gpioController.OpenPin(34, PinMode.Input);
            led.Write(PinValue.Low);
            sht1X = new Sht1x(0, 4, gpioController);

            relayO.Write(PinValue.Low);
            client = new MqttClient("mqttbroker");
            var clientId = Guid.NewGuid().ToString();
            client.Connect(clientId);
            client.Subscribe(new[] { "Mystrawberry/action/pump/" }, new[] { MqttQoSLevel.AtLeastOnce });
            client.MqttMsgPublishReceived += HandleIncomingMessage;
            client.ConnectionClosed += WhileMqttclientConnectionClosed;

            Thread publishCaller = new Thread(
                        new ThreadStart(PublishMethod));
            publishCaller.Start();



            Thread.Sleep(Timeout.Infinite);

            // Browse our samples repository: https://github.com/nanoframework/samples
            // Check our documentation online: https://docs.nanoframework.net/
            // Join our lively Discord community: https://discord.gg/gCyBu8T
        }

        private static void WhileMqttclientConnectionClosed(object sender, EventArgs e)
        {
            client.Connect(Guid.NewGuid().ToString());
        }

        private static void HandleIncomingMessage(object sender, MqttMsgPublishEventArgs e)
        {
            switch (Encoding.UTF8.GetString(e.Message, 0, e.Message.Length))
            {
                case "true": relayO.Write(PinValue.High); break;
                case "false": relayO.Write(PinValue.Low); break;
                default:
                    break;
            }
        }

        private static void PublishMethod()
        {
            while (true)
            {

                var str = GetSerializeObject();
                client.Publish("Mystrawberry/status/", Encoding.UTF8.GetBytes(str), MqttQoSLevel.AtLeastOnce, false);
                Debug.WriteLine($"publish message:{str}");
                Thread.Sleep(2000);

            }

        }

        private static string GetSerializeObject()
        {
            Mystrawberry mystrawberry = new Mystrawberry();
            int RawTemperature = sht1X.ReadRawTemperature();

            mystrawberry.TemperatureC = sht1X.CalculateTemperatureC(RawTemperature);
            //  //TemperatureF = sht1X.CalculateTemperatureF(RawTemperature);
            mystrawberry.Humidity = sht1X.ReadHumidity(mystrawberry.TemperatureC);
            mystrawberry.Mositure = GetPercentmoisture(mositure.ReadValue());
            mystrawberry.PumpIsOn = relayI.Read() == PinValue.High;
            return JsonConvert.SerializeObject(mystrawberry);
        }

        private static string GetPercentmoisture(int value, int max = 2750, int min = 1000)
        {
            //throw new NotImplementedException();
            return ((1 - ((float)(value - min) / (max - min))) * 100).ToString("F4");
        }
    }
}

有没有人遇到过同样的问题,是如何解决的?谢谢你的回答

4

0 回答 0