Example #1
0
        static void Main(string[] args)
        {
            try
            {
                /*
                 #region lora
                 * string SerialPortName = ConfigurationManager.AppSettings["Port"];
                 * UART = new SimpleSerial(SerialPortName, 57600);
                 * UART.ReadTimeout = 0;
                 *
                 * //UART.ReadBufferSize = 1024;
                 * //UART.WriteBufferSize = 1024;
                 * //UART.BaudRate = 38400;
                 * //UART.DataBits = 8;
                 * //UART.Parity = Parity.None;
                 * //UART.StopBits = StopBits.One;
                 *
                 * UART.DataReceived += UART_DataReceived;
                 * Console.WriteLine("57600");
                 * Console.WriteLine("RN2483 Test");
                 *
                 * var reset = Pi.Gpio[BcmPin.Gpio06]; //pin 6
                 * var reset2 =  Pi.Gpio[BcmPin.Gpio06]; //pin 3
                 #endregion
                 */
                Pi.Init <BootstrapWiringPi>();
                Console.WriteLine(">> Init mqtt");
                MqttService mqtt       = new MqttService();
                DS1307      jam        = new DS1307();
                TempSensor  tempSensor = new TempSensor();
                var         nowDate    = DateTime.Now;
                Console.WriteLine("Device Date :" + nowDate);
                jam.SetDateAsync(nowDate).GetAwaiter().GetResult();
                Console.WriteLine("TGL RTC:" + jam.GetDateAsync().GetAwaiter().GetResult().ToString());
                ADS1115_PY analog = new ADS1115_PY();
                Relay1       = Pi.Gpio[BcmPin.Gpio06]; //Pi.Gpio.Pin06;
                Relay2       = Pi.Gpio[BcmPin.Gpio13]; //Pi.Gpio.Pin13;
                Limit1       = Pi.Gpio[BcmPin.Gpio19]; //Pi.Gpio.Pin19;
                Limit2       = Pi.Gpio[BcmPin.Gpio26]; //Pi.Gpio.Pin26;
                Relay1Status = true;
                Relay2Status = true;

                WriteDigital(Relay1, Relay1Status);
                WriteDigital(Relay2, Relay2Status);
                mqtt.CommandReceived += (string Message) =>
                {
                    Task.Run(async() => { await DoAction(Message); });
                };
                var INTERVAL = int.Parse(ConfigurationManager.AppSettings["Interval"]);
                //analog.Start();

                /*
                 * analog.ChannelChanged += (object sender, ChannelReadingDone e) =>
                 * {
                 *  Console.WriteLine($">> channel {e.Channel} : {e.RawValue}");
                 * };*/
                /*
                 #region lora
                 * reset.Write(true);
                 * reset2.Write(true);
                 *
                 * Thread.Sleep(100);
                 * reset.Write(false);
                 * reset2.Write(false);
                 *
                 * Thread.Sleep(100);
                 * reset.Write(true);
                 * reset2.Write(true);
                 *
                 * Thread.Sleep(100);
                 *
                 * waitForResponse();
                 *
                 * sendCmd("sys factoryRESET");
                 * sendCmd("sys get hweui");
                 * sendCmd("mac get deveui");
                 *
                 * // For TTN
                 * sendCmd("mac set devaddr AAABBBEE");  // Set own address
                 * Thread.Sleep(1000);
                 * sendCmd("mac set appskey 2B7E151628AED2A6ABF7158809CF4F3D");
                 * Thread.Sleep(1000);
                 *
                 * sendCmd("mac set nwkskey 2B7E151628AED2A6ABF7158809CF4F3D");
                 * Thread.Sleep(1000);
                 *
                 * sendCmd("mac set adr off");
                 * Thread.Sleep(1000);
                 *
                 * sendCmd("mac set rx2 3 868400000");//869525000
                 * Thread.Sleep(1000);
                 *
                 * sendCmd("mac join abp");
                 * sendCmd("mac get status");
                 * sendCmd("mac get devaddr");
                 * Thread.Sleep(1000);
                 #endregion
                 */
                while (true)
                {
                    /*
                     * for (int i = 0; i < 4; i++)
                     * {
                     *  Console.WriteLine($"A{i} = {analog.read_adc(i)}");
                     * }*/
                    var sensor = new DeviceData()
                    {
                        LimitSwitch1 = ReadDigital(Limit1), LimitSwitch2 = ReadDigital(Limit2), Relay1 = Relay1Status, Relay2 = Relay2Status, TDS1 = analog.read_adc(0),
                        TDS2         = analog.read_adc(1), Temp = tempSensor.Read()
                    };
                    Console.WriteLine(">>------------------>>");
                    Console.WriteLine($"TDS 1: {sensor.TDS1}");
                    Console.WriteLine($"TDS 2: {sensor.TDS2}");
                    Console.WriteLine($"Relay 1: {sensor.Relay1}");
                    Console.WriteLine($"Relay 2: {sensor.Relay2}");
                    Console.WriteLine($"Limit 1: {sensor.LimitSwitch1}");
                    Console.WriteLine($"Limit 2: {sensor.LimitSwitch2}");
                    Console.WriteLine($"Temp: {sensor.Temp}");
                    mqtt.PublishMessage(JsonConvert.SerializeObject(sensor));

                    /*
                     #region lora
                     * var jsonStr = JsonConvert.SerializeObject(sensor);
                     * Debug.Print("kirim :" + jsonStr);
                     * //PrintToLcd("send count: " + counter);
                     * sendData(jsonStr);
                     * Thread.Sleep(INTERVAL);
                     * byte[] rx_data = new byte[20];
                     *
                     * if (UART.BytesToRead > 0)
                     * {
                     *  var count = UART.Read(rx_data, 0, rx_data.Length);
                     *  if (count > 0)
                     *  {
                     *      Debug.Print("count:" + count);
                     *      var hasil = new string(System.Text.Encoding.UTF8.GetChars(rx_data));
                     *      Debug.Print("read:" + hasil);
                     *
                     *      //mac_rx 2 AABBCC
                     *  }
                     * }
                     #endregion
                     */
                    Thread.Sleep(INTERVAL);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }