Exemplo n.º 1
0
        public float ReadTemperature()
        {
            try
            {
                oneWire.TouchReset();

                oneWire.WriteByte(SkipROM);
                oneWire.WriteByte(ConvertT);
                Thread.Sleep(1000);

                oneWire.TouchReset();
                oneWire.WriteByte(SkipROM);
                oneWire.WriteByte(ReadScratchpad);

                // Read just the temperature (2 bytes)
                var   tempLo      = oneWire.ReadByte();
                var   tempHi      = oneWire.ReadByte();
                float temperature = GetTemperature((byte)tempLo, (byte)tempHi);

                return(temperature);
            }
            catch (Exception exception)
            {
                //Logger.Instance.Write(exception.ToString());
                return(0f);
            }
        }
Exemplo n.º 2
0
        void Setup()
        {
            OneWire ow = new OneWire(myPin);
            ushort  temperature;

            // read every second
            while (true)
            {
                if (ow.TouchReset() > 0)
                {
                    ow.WriteByte(0xCC);     // Skip ROM, we only have one device
                    ow.WriteByte(0x44);     // Start temperature conversion

                    while (ow.ReadByte() == 0)
                    {
                        ;                          // wait while busy
                    }
                    ow.TouchReset();
                    ow.WriteByte(0xCC);                          // skip ROM
                    ow.WriteByte(0xBE);                          // Read Scratchpad

                    temperature  = (byte)ow.ReadByte();          // LSB
                    temperature |= (ushort)(ow.ReadByte() << 8); // MSB
                    TempValue    = temperature / 16;
                    Debug.Print("Temperature: " + TempValue);
                    Thread.Sleep(1000);
                }
                else
                {
                    Debug.Print("Device is not detected.");
                }

                Thread.Sleep(1000);
            }
        }
Exemplo n.º 3
0
 private void Write(params byte[] sendValues)
 {
     foreach (byte sendValue in sendValues)
     {
         _bus.WriteByte(sendValue);
     }
 }
Exemplo n.º 4
0
        public static string OneWire_Temperaturea()
        {
            // Change this your correct pin!
            Cpu.Pin myPin = (Cpu.Pin)FEZ_Pin.Digital.Di4;

            OneWire ow = new OneWire(myPin);
            ushort  temperature;

            {
                BMP085 pressureSensor = new BMP085(0x77, BMP085.DeviceMode.UltraLowPower);

                string temperature_Pressure;
                if (ow.Reset())
                {
                    ow.WriteByte(0xCC);     // Skip ROM, we only have one device
                    ow.WriteByte(0x44);     // Start temperature conversion

                    while (ow.ReadByte() == 0)
                    {
                        ;                          // wait while busy
                    }
                    ow.Reset();
                    ow.WriteByte(0xCC);                          // skip ROM
                    ow.WriteByte(0xBE);                          // Read Scratchpad

                    temperature  = ow.ReadByte();                // LSB
                    temperature |= (ushort)(ow.ReadByte() << 8); // MSB

                    temperature_Pressure = "Temperature: " + temperature / 16 + "\n" + "BMP085 Pascal: " + pressureSensor.Pascal +
                                           "   BMP085 InchesMercury: " + pressureSensor.InchesMercury.ToString("F2") + "   BMP085 Temp*C: " + pressureSensor.Celsius.ToString("F2");
                }
                else
                {
                    temperature_Pressure = "Temperature Device is not Detected";
                }

                ow.Dispose();
                Thread.Sleep(1000);
                return(temperature_Pressure);
            }
        }
Exemplo n.º 5
0
        private double ReadTemperture(byte[] owAddress)
        {
            var scratchpad = new byte[9];

            _ow.TouchReset();
            _ow.WriteByte(MatchRom);
            for (int i = 0; i <= 7; i++)
            {
                _ow.WriteByte(owAddress[i]);
            }
            _ow.WriteByte(StartTemperatureConversion);
            while (_ow.ReadByte() == 0)
            {
            }
            _ow.TouchReset();
            _ow.WriteByte(MatchRom);
            for (int i = 0; i <= 7; i++)
            {
                _ow.WriteByte(owAddress[i]);
            }
            _ow.WriteByte(ReadScratchPad);
            for (int i = 0; i <= 8; i++)
            {
                scratchpad[i] = (byte)_ow.ReadByte();
            }
            short tempLow      = scratchpad[0];
            short tempHigh     = scratchpad[1];
            long  temptemp     = (((long)tempHigh) << 8) | ((uint)tempLow);
            float temperatureC = temptemp * 0.0625f;

            return(temperatureC);
        }
        public void ReadTemperature()
        {
            long data = 0;

            if (DataPin.ReadByte() > 0 && ConvStarted)
            {
                //Read temperature1
                DataPin.Reset();
                DataPin.WriteByte(MatchROM);
                DataPin.Write(IDs[0], 0, 8);
                DataPin.WriteByte(ReadScratchPad);

                data         = DataPin.ReadByte();                // LSB
                data        |= (ushort)(DataPin.ReadByte() << 8); // MSB
                Temperature1 = data * 0.0625f;

                //Read temperature2
                DataPin.Reset();
                DataPin.WriteByte(MatchROM);
                DataPin.Write(IDs[1], 0, 8);
                DataPin.WriteByte(ReadScratchPad);

                data         = DataPin.ReadByte();                // LSB
                data        |= (ushort)(DataPin.ReadByte() << 8); // MSB
                Temperature2 = data * 0.0625f;


                ConvStarted = false;
            }

            DataPin.Reset();
            DataPin.WriteByte(MatchROM);
            DataPin.Write(IDs[0], 0, 8);
            DataPin.WriteByte(StartTemperatureConversion);

            DataPin.Reset();
            DataPin.WriteByte(MatchROM);
            DataPin.Write(IDs[1], 0, 8);
            DataPin.WriteByte(StartTemperatureConversion);

            ConvStarted = true;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Lettura del codice ROM di identificazione
        /// </summary>
        public string GetRomCode(bool checkCrc = true)
        {
            byte[] code = new byte[8];

            try
            {
                // verifico se sul bus è presente un dispositivo 1-Wire
                if (Ow.TouchReset() > 0)
                {
                    // invio comando lettura codice ROM
                    Ow.WriteByte(ReadROM);

                    // Ricezione del codice ROM composto da 8 byte
                    for (int i = 0; i < 8; i++)
                    {
                        code[i] = (byte)Ow.TouchByte(0xFF);
                    }

                    // Verifico se è un codice valido
                    if (checkCrc)
                    {
                        if (OnewireUtil.ComputeCrc(code, 0, 7) == code[7])
                        {
                            // Chiave valida
                            return(ByteArrayToHexString(code, 0, 8));
                        }
                        return("");
                    }
                    if (code[0] != 0)
                    {
                        return(ByteArrayToHexString(code, 0, 8));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return("");
        }
Exemplo n.º 8
0
        /// <summary>
        /// Funcion encargada de 3 cosas: Leer el sensor, mostrar datos por LCD y controlar el encendido y apagado de los relays.
        /// </summary>
        private static void readTemp()
        {
            OneWire _oneWire = new OneWire(new OutputPort(Pins.GPIO_PIN_D0, false));

            var lcdProvider = new GpioLcdTransferProvider(
                Pins.GPIO_PIN_D11,            // RS
                         Pins.GPIO_NONE,      // RW
                         Pins.GPIO_PIN_D9,    // enable
                         Pins.GPIO_PIN_D2,    // d0
                         Pins.GPIO_PIN_D4,    // d1
                         Pins.GPIO_PIN_D6,    // d2
                         Pins.GPIO_PIN_D8,    // d3
                         Pins.GPIO_PIN_D7,    // d4
                         Pins.GPIO_PIN_D5,    // d5
                         Pins.GPIO_PIN_D3,    // d6
                         Pins.GPIO_PIN_D1);   // d7

            var lcd = new Lcd(lcdProvider);

            lcd.Begin(16, 2);
            // apagamos todos los compomentes externos.
            // Infinite loop that reads the temp and stores it in tempAct
            while (true)
            {
                Double rango     = Datos.tempMax - Datos.tempMin;
                Double limiteSup = 0.50 * rango;
                Double limiteInf = 0.20 * rango;

                try
                {
                    if (_oneWire.TouchReset() > 0)
                    {
                        _oneWire.WriteByte(0xCC);     // Skip ROM, only one device
                        _oneWire.WriteByte(0x44);     // Temp conversion

                        while (_oneWire.ReadByte() == 0)
                        {
                            ;                                //Loading
                        }
                        _oneWire.TouchReset();
                        _oneWire.WriteByte(0xCC);     // Skip ROM
                        _oneWire.WriteByte(0xBE);     // Read

                        ushort temperature = (byte)_oneWire.ReadByte();
                        temperature  |= (ushort)(_oneWire.ReadByte() << 8); // MSB
                        Datos.tempAct = temperature / 16.0;



                        if (Datos.competi && !Datos.finishBattle)
                        {
                            //Debug.Print("------------------------------DENTRO DE PROGRAM.170-------------------");
                            // tanto el secador como el ventilador, operan en FALSE - circuito cerrado
                            if (Datos.tempAct >= (Datos.tempMax - limiteSup))      // FRIO
                            {
                                Secador.Write(false);
                                Ventilador1.Write(true);
                                Ventilador2.Write(true);
                                ledCool.Write(true);
                            }
                            else if (Datos.tempAct <= (Datos.tempMin + limiteInf)) // CALOR
                            {
                                Secador.Write(true);
                                Ventilador1.Write(false);
                                Ventilador2.Write(false);
                            }
                            else                                                   // APAGAMOS TODO
                            {
                                off();
                            }


                            //Datos.tempAct = Microsoft.SPOT.Math.(Datos.tempAct, 1);
                            lcd.Clear();
                            lcd.SetCursorPosition(0, 0);
                            lcd.Write("[" + Datos.tempMin.ToString("N1") + "-" + Datos.tempMax.ToString("N1") + "]");

                            lcd.SetCursorPosition(12, 0);
                            lcd.Write(Datos.roundTime.ToString() + "s");

                            lcd.SetCursorPosition(0, 1);
                            lcd.Write(Datos.tempAct.ToString("N1") + "C [" + Datos.timeInRangeTemp.ToString() + "s" + "]");

                            lcd.SetCursorPosition(13, 1);
                            lcd.Write(Datos.timeLeft.ToString());

                            Thread.Sleep(Datos.refresh);
                        }
                        if (!Datos.competi && !Datos.coolerMode)
                        {
                            lcd.Clear();
                            lcd.SetCursorPosition(0, 0);
                            lcd.Write("Temp War Grupo 1");
                            lcd.SetCursorPosition(0, 1);
                            lcd.Write("[" + Datos.tempAct.ToString("N1") + "C] Pts:" + Datos.timeInRangeTemp + "s");
                            Thread.Sleep(Datos.displayRefresh);
                        }
                        if (Datos.coolerMode)
                        {
                            lcd.Clear();
                            lcd.SetCursorPosition(0, 0);
                            lcd.Write("Cooling Mode.");
                            lcd.SetCursorPosition(0, 1);
                            lcd.Write("Temp: " + Datos.tempAct.ToString("N1") + "C");
                            Thread.Sleep(Datos.displayRefresh);
                            lcd.SetCursorPosition(0, 0);
                            lcd.Write("Cooling Mode..");
                            Thread.Sleep(Datos.displayRefresh);
                            lcd.SetCursorPosition(0, 0);
                            lcd.Write("Cooling Mode...");
                        }
                    }
                    else
                    {
                        Debug.Print("Fallo de sensor");
                        //Could be that you read to fast after previous read. Include
                        Thread.Sleep(1000);
                    }
                }
                catch (Exception ex)
                {
                    Debug.Print("ReadTemperatureToConsole " + ex.Message);
                }
            }
        }
        /// <summary>
        /// Reading temperature
        /// </summary>
        /// <param name="format"> desired format</param>
        /// <returns></returns>
        public override double ReadTemperature(TemperatureFormat format)
        {
            int rawTemperature = 0;

            //if tere is only one device
            if (address == null)
            {
                if (oneWire.TouchReset() > 0)
                {
                    oneWire.WriteByte((int)OneWireCommands.SkipRom);
                    oneWire.WriteByte((int)OneWireCommands.MeasureTemperature);

                    while (oneWire.ReadByte() == 0)
                    {
                        ;
                    }

                    oneWire.TouchReset();

                    oneWire.WriteByte((int)OneWireCommands.SkipRom);
                    oneWire.WriteByte((int)OneWireCommands.ReadRom);

                    rawTemperature = (UInt16)((UInt16)oneWire.ReadByte() | (UInt16)(oneWire.ReadByte() << 8));
                }
                else
                {
                    throw new Exception("Device not found");
                }
            }
            //else if there is device address provided
            else
            {
                bool   match = false;
                byte[] found = null;
                //find device with provided address
                var devices = oneWire.FindAllDevices();
                foreach (byte[] codes in devices)
                {
                    match = true;
                    for (int i = 0; i < codes.Length; i++)
                    {
                        if (codes[i] != address[i])
                        {
                            match = false;
                            break;
                        }
                    }
                    if (match)
                    {
                        found = codes;
                        break;
                    }
                }
                if (found == null)
                {
                    throw new Exception("Device with specified address not found");
                }
                else
                {
                    if (oneWire.TouchReset() > 0)
                    {
                        oneWire.WriteByte((int)OneWireCommands.MatchRom);
                        foreach (byte codePart in found)
                        {
                            oneWire.WriteByte((int)codePart);
                        }
                        oneWire.WriteByte((int)OneWireCommands.MeasureTemperature);

                        while (oneWire.ReadByte() == 0)
                        {
                            ;
                        }

                        oneWire.WriteByte((int)OneWireCommands.MatchRom);
                        foreach (byte codePart in found)
                        {
                            oneWire.WriteByte((int)codePart);
                        }
                        oneWire.WriteByte((int)OneWireCommands.ReadRom);

                        rawTemperature = (UInt16)((UInt16)oneWire.ReadByte() | (UInt16)(oneWire.ReadByte() << 8));
                    }
                }
            }

            //conversion
            double result = 1;

            if ((rawTemperature & 0x8000) > 0)
            {
                rawTemperature = (rawTemperature ^ 0xffff) + 1;
                result         = -1;
            }
            result *= (6 * rawTemperature + rawTemperature / 4) / 100;
            return(result + ((format == TemperatureFormat.KELVIN) ? 273.15 : 0.0));
        }