//the meat - called by event handler - read sensor, format data and update screen and AIO private async void ReadI2CTemp() { byte TempRawMSB, TempRawLSB; //use later byte[] ReadBuf = new byte[2]; // MSB first, then LSB byte[] WriteBuf = new byte[] { TEMP_REG_ADDR }; // 0 selects data register - write it out first byte[] ConfBuf = new byte[] { CONFIG_REG_ADDR, 0xE1, 0xA0 }; //hit the OS bit (assume rest unchanged) //should read and mask bit ... //byte[] WriteBuf = new byte[] { CONFIG_REG_ADDR, 0xE1, 0xA0 }; try { /* * Read from the temp sensor */ I2CTemp.Write(ConfBuf); I2CTemp.WriteRead(WriteBuf, ReadBuf); //I2CTemp.Read(ReadBuf); } catch (Exception e) { /* If WriteRead() fails, display error messages */ Text_X_Axis.Text = "Error"; Text_Status.Text = "Exception: " + e.Message; return; } /* * In order to get the raw 16-bit data values, we need to concatenate two 8-bit bytes from the I2C read for each axis. * We accomplish this by using bit shift and logical OR operations */ //TempRaw = (Int16)(ReadBuf[0] | ReadBuf[1] << 8); TempRawMSB = ReadBuf[0]; TempRawLSB = ReadBuf[1]; /* Convert raw values to G's */ //AccelerationX = (double)AccelerationRawX / UNITS_PER_G; int temp = ((TempRawMSB << 8) | TempRawLSB) >> 4; //double celsius = temp * 0.0625; String Celsius = (temp * 0.0625).ToString("F2"); /* Display the values */ Text_X_Axis.Text = "Temp (RAW): " + TempRawMSB.ToString("X2") + " " + TempRawLSB.ToString("X2"); Text_Y_Axis.Text = "Celsius: " + Celsius; Text_Status.Text = "Status: Running"; //Adafruit IO Web update //Call function to update AIO with temp data. A simple GET. Returns status code String resCode = await UpdateAIO(Celsius); //Report the status on screen/console. Should be 200/OK AIO_Status.Text = "AIO Status: " + resCode; //Blip the LED so we know something happening. FlashLED(); }
private void ReadI2CTemp() { byte TempRawMSB, TempRawLSB; //use later byte[] ReadBuf = new byte[2]; // MSB first, then LSB byte[] WriteBuf = new byte[] { TEMP_REG_ADDR }; // 0 selects data register - write it out first byte[] ConfBuf = new byte[] { CONFIG_REG_ADDR, 0xE1, 0xA0 }; //hit the OS bit (assume rest unchanged) //should read and mask bit ... //byte[] WriteBuf = new byte[] { CONFIG_REG_ADDR, 0xE1, 0xA0 }; try { /* * Read from the temp */ I2CTemp.Write(ConfBuf); I2CTemp.WriteRead(WriteBuf, ReadBuf); //I2CTemp.Read(ReadBuf); } catch (Exception e) { /* If WriteRead() fails, display error messages */ Text_X_Axis.Text = "Error"; Text_Status.Text = "Exception: " + e.Message; return; } /* * In order to get the raw 16-bit data values, we need to concatenate two 8-bit bytes from the I2C read for each axis. * We accomplish this by using bit shift and logical OR operations */ //TempRaw = (Int16)(ReadBuf[0] | ReadBuf[1] << 8); TempRawMSB = ReadBuf[0]; TempRawLSB = ReadBuf[1]; /* Convert raw values to G's */ //AccelerationX = (double)AccelerationRawX / UNITS_PER_G; int temp = ((TempRawMSB << 8) | TempRawLSB) >> 4; //double celsius = temp * 0.0625; String Celsius = (temp * 0.0625).ToString("F2"); /* Display the values */ Text_X_Axis.Text = "Temp (RAW): " + TempRawMSB.ToString("X2") + " " + TempRawLSB.ToString("X2"); Text_Y_Axis.Text = "Celsius: " + Celsius; Text_Status.Text = "Status: Running"; //Adafruit IO Web PUT String req = "https://io.adafruit.com/api/groups/RPi/send.json?x-aio-key=" + AIO_Key + "&temp=" + Celsius + "&status=" + "Running"; HttpWebRequest g = (HttpWebRequest)WebRequest.Create(req); //WebRequest.Timeout = 1000; //Assuming codes are OK, not 400, 500 var r = g.GetResponseAsync(); AIO_Status.Text = "AIO Status: OK"; }