static void Main(string[] args) { bus = I2CBus.Open("/dev/i2c-1"); if (!Detect()) { Console.WriteLine("Some error occurred during communication with the sensor. Please check the sensor."); Console.ReadLine(); Environment.Exit(0); } InitParams(); while(run) { var temp = GetTemperature(); var press = GetPressure(); var r = new Reading { ReadingId = Guid.NewGuid(), Bmp180_Pressure = press, Bmp180_Temp = temp, Time = DateTime.Now, BuildingId = new Guid("79fd1f35-d672-4a6e-9fe0-1cfbadc5aea1"), PiId = new Guid("4c1541ca-e65a-42ca-ba93-5297196bc26e") }; //Task t = PostReading(r).Wait(); PostReading(r).GetAwaiter().GetResult(); Console.WriteLine("Temperature: {0}, Pressure: {1}", temp, press); System.Threading.Thread.Sleep(30000); } bus.Dispose(); //very important! Dispose of the I2CBus instance when you're done. Console.WriteLine("Done."); }
private static async Task PostReading(Reading rd) { client = new HttpClient(); try { string resourceAddress = "http://KilmoSensorApi.azurewebsites.net/api/reading"; //http://localhost:24233/ string postBody = JsonSerializer(rd); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage resp = await client.PostAsync(resourceAddress, new StringContent(postBody, Encoding.UTF8, "application/json")); } catch (HttpRequestException hre) { Console.WriteLine("error: " + hre.Message); string error = hre.Message; } catch (Exception ex) { Console.WriteLine("error: " + ex.Message); string error2 = ex.Message; } }
public static string JsonSerializer(Reading objectToSerialize) { if (objectToSerialize == null) { throw new ArgumentException("objectToSerialize must not be null"); } MemoryStream ms = null; DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType()); ms = new MemoryStream(); serializer.WriteObject(ms, objectToSerialize); ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms); return sr.ReadToEnd(); }