Exemplo n.º 1
0
        /// <summary>
        ///     Make a temperature and humidity reading.
        /// </summary>
        public void Update()
        {
            _si7021.WriteByte(Registers.MeasureHumidityNoHold);
            //
            //  Maximum conversion time is 12ms (page 5 of the datasheet).
            //
            Thread.Sleep(25);
            var data            = _si7021.ReadBytes(3);
            var humidityReading = (ushort)((data[0] << 8) + data[1]);

            Humidity = ((125 * (float)humidityReading) / 65536) - 6;
            if (Humidity < 0)
            {
                Humidity = 0;
            }
            else
            {
                if (Humidity > 100)
                {
                    Humidity = 100;
                }
            }
            data = _si7021.ReadRegisters(Registers.ReadPreviousTemperatureMeasurement, 2);
            var temperatureReading = (short)((data[0] << 8) + data[1]);

            Temperature = (float)(((175.72 * temperatureReading) / 65536) - 46.85);
        }
Exemplo n.º 2
0
        private async Task Begin()
        {
            Console.WriteLine("BMP280::Begin");

            //Read the device signature
            i2cBus.WriteCommand(BMP280_Address, (byte)eRegisters.BMP280_REGISTER_CHIPID);
            var buffer = i2cBus.ReadBytes(BMP280_Address, 1);

            Console.WriteLine("BMP280 Signature: " + buffer[0].ToString());

            //Set the initalize variable to true
            init = true;

            //Read the coefficients table
            CalibrationData = await ReadCoefficeints();

            //Write control register
            await WriteControlRegister();

            //Write humidity control register
            await WriteControlRegisterHumidity();
        }
Exemplo n.º 3
0
        public double GetPressure()
        {
            double current_temperature = GetTemperature();

            byte[] bytes = new byte[] { 0xF4, (byte)(0x34 + (3 << 6)) };
            bus.WriteBytes(deviceID, bytes);
            System.Threading.Thread.Sleep(28);
            bus.WriteByte(deviceID, (byte)0xF6);
            byte ms = bus.ReadBytes(deviceID, 1)[0];

            bus.WriteByte(deviceID, (byte)0xF7);
            byte ls = bus.ReadBytes(deviceID, 1)[0];

            bus.WriteByte(deviceID, (byte)0xF8);
            byte   xs = bus.ReadBytes(deviceID, 1)[0];
            double pu = (ms * 256.0) + ls + (xs / 256.0);
            double s, x, y, z;
            double x0 = AC1;
            double x1 = 160.0 * Math.Pow(2, -13) * AC2;
            double x2 = Math.Pow(160, 2) * Math.Pow(2, -25) * B2;
            double c3 = 160.0 * Math.Pow(2, -15) * AC3;
            double c4 = Math.Pow(10, -3) * Math.Pow(2, -15) * AC4;
            double b1 = Math.Pow(160, 2) * Math.Pow(2, -30) * B1;
            double y0 = c4 * Math.Pow(2, 15);
            double y1 = c4 * c3;
            double y2 = c4 * b1;
            double p0 = (3791.0 - 8.0) / 1600.0;
            double p1 = 1.0 - 7357.0 * Math.Pow(2, -20);
            double p2 = 3038.0 * 100.0 * Math.Pow(2, -36);

            s = current_temperature - 25.0;
            x = (x2 * Math.Pow(s, 2)) + (x1 * s) + x0;
            y = (y2 * Math.Pow(s, 2)) + (y1 * s) + y0;
            z = (pu - x) / y;
            double P = (p2 * Math.Pow(z, 2)) + (p1 * z) + p0;

            return(P);
        }
        /// <summary>
        /// Get metadata from slaves, store in _assimSlaves and JSON files
        /// </summary>
        public void getMetadata()
        {
            Debug.Print(MethodNames.GET_METADATA);
            bool   i2cNodeProcessed;
            string name = "", value = "";
            ushort speed   = 200; // 1000 breaks ReadBytes
            ushort timeout = 200;

            foreach (Slave slave in _assimSlaves)
            {
                byte      slaveAddress = slave.address;
                Hashtable rootPairs    = new Hashtable();
                ArrayList userMetas    = Config.deserializeUserMetas(slaveAddress);
                I2CBus    i2C          = new I2CBus(slaveAddress, speed, timeout);
                while (true)
                {
                    i2cNodeProcessed = false;
                    for (byte segment = 0; segment < 3; segment++)   // 3 requests per meta
                    {
                        try {
                            var byteArray = i2C.ReadBytes(16);
                            var packet    = getPacketFromBytes(byteArray);
                            switch (segment)
                            {
                            case 0:
                                name = packet;
                                break;

                            case 1:
                                if (name == packet)
                                {    // this is the main symptom of message sequence is out of sync - not experienced on Netduino node
                                    Debug.Print(LogMessages.OUT_OF_SYNC);
                                    Debug.Print(LogMessages.RESTARTING);
                                    // ToDo: restart the device
                                }
                                value = packet;
                                getUserMetaOrDefault(userMetas, name, ref value);
                                setMetaOfInterest(slave, rootPairs, name, value);
                                break;

                            case 2:
                                Debug.Print(name + (name.Length > 6 ? LogMessages.TAB_1 : LogMessages.TAB_2) + value);
                                // check if last metadata
                                if (packet == I2cMessages.SEG3_DISCONTINUE)      // 0 on last property
                                {
                                    i2cNodeProcessed = true;
                                    break;
                                }
                                break;

                            default:
                                break;
                            }
                        } catch (Exception ex) {
                            Debug.Print(ex.InnerException.Message);
                            return;
                        }
                    }// end for segment
                    if (i2cNodeProcessed)   // break out if last metadata
                    {
                        break;
                    }
                }// end while true
                Debug.Print(LogMessages.CONFIRM_METADATA + slave.name);
                i2C.WriteByte(I2cMessages.WRITE_CONFIRM_METADATA);
                // SAVE /config/metadata/<i>.json
                if (!Config.serializeSlaveMetas(slaveAddress, rootPairs))
                {
                    Debug.Print(LogMessages.ERROR_SAVING_FILE + slaveAddress.ToString());
                }
            }// end foreach slave
        }
        /// <summary>
        /// Get properties from slaves, raise events for individual properties and completion
        /// </summary>
        public void getProperties()
        {
            Debug.Print(MethodNames.GET_PROPERTIES);
            bool   i2cNodeProcessed;
            ushort speed = 200; // 1000 breaks ReadBytes
            ushort timeout = 200;
            string name = "", value = "";

            foreach (Slave slave in _assimSlaves)
            {
                byte slaveAddress = slave.address;
                byte propIndex    = 0;
                //ToDo: clock stretching
                I2CBus i2C = new I2CBus(slaveAddress, speed, timeout);
                while (true)
                {
                    i2cNodeProcessed = false;
                    for (byte segment = 0; segment < 3; segment++)
                    { // 3 requests per meta
                        try
                        {
                            var byteArray = i2C.ReadBytes(16);
                            var packet    = getPacketFromBytes(byteArray);
                            switch (segment)
                            {
                            case 0:
                                name = packet;
                                break;

                            case 1:
                                value = packet;
                                break;

                            case 2:
                                var args = new PropertyReceivedventArgs {
                                    slaveAddress  = slaveAddress,
                                    propertyIndex = propIndex,
                                    role          = slave.role,
                                    name          = name,
                                    value         = value
                                };
                                PropertyReceived?.Invoke(this, args);
                                propIndex++;
                                if (packet == I2cMessages.SEG3_DISCONTINUE)
                                {    // 0 on last property
                                    i2cNodeProcessed = true;
                                    break;
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.Print(ex.Message);
                        }
                    }
                    if (i2cNodeProcessed)
                    { // break out if last property for slave
                        break;
                    }
                }
            }
            SlaveCyleComplete?.Invoke(null, EventArgs.Empty);
        }