/// <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); }
public static void Main() { // setup some configs ushort speed = 100; ushort timeout = 100; ushort numberOfDevices = 0; // loop forever while (true) { numberOfDevices = 0; // loop through all the possible I2C addresses (0-127) for (byte i = 0; i < 127; i++) { I2CBus i2C = new I2CBus(i, speed, timeout); try { i2C.WriteByte(0); Debug.Print("Found I2C device at: " + i.ToString("X")); numberOfDevices++; } catch (Exception e) { } } if (numberOfDevices == 0) { Debug.Print("No I2C devices found."); } // wait five seconds before scanning again. Thread.Sleep(5000); } }
private void scanBusAddresses() { Debug.Print(MethodNames.SCAN_BUS_ADDRESSES); if (loadAddressWhitelist()) { Debug.Print(LogMessages.LOADED_WHITELIST); return; } ushort speed = 100; ushort timeout = 200; for (byte i = SLAVE_SCAN_LOW; i < SLAVE_SCAN_HIGH; i++) { I2CBus i2C = new I2CBus(i, speed, timeout); try { i2C.WriteByte(0); var slaveAddressOnly = new Slave { address = i, clock_stretch = 40000, name = "not_assigned", role = Role.UNDEFINED }; _assimSlaves.Add(slaveAddressOnly); } catch { } } if (_assimSlaves.Count == 0) { //Debug.Print("No I2C devices found."); } else { //Debug.Print(_numberOfDevices.ToString() + " I2C devices found."); } }
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 }