Exemplo n.º 1
0
        /// <summary>
        ///     Send a single value to AdafruitIO
        /// </summary>
        /// <param name="Value">Value to send to AdafruitIO.</param>
        public void PostValue(SensorReading Value)
        {
            string apiUri = $"{URI}{UserName}/feeds/";

            if (Group != null)
            {
                apiUri += $"{Group}.";
            }
            apiUri += $"{Value.Key}/data";

            string dateString = Value.CreatedAt.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");

            string postBody = "{\"value\":\"" + Value.Value + "\",\"created_at\":\"" + dateString + "\"}";

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = new TimeSpan(0, 5, 0);
                httpClient.DefaultRequestHeaders.Add("X-AIO-Key", IOKey);
                StringContent content = new StringContent(postBody, Encoding.UTF8, "application/json");

                try
                {
                    HttpResponseMessage response = httpClient.PostAsync(apiUri, content).Result;
                }
                catch (TaskCanceledException)
                {
                    Console.WriteLine("Request timed out.");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Request went sideways:\n - Source: {e.Source}" +
                                      $"\n - Type: {e.GetType()}" +
                                      $"\n - Message: {e.Message}\n - InnerException:\n{e.InnerException}" +
                                      $" - StackTrace:\n{e.StackTrace}");
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Send a single value to AdafruitIO
 /// </summary>
 /// <param name="value">Value to send to AdafruitIO.</param>
 public void PostValue(SensorReading value)
 {
     PostValues(new SensorReading[] { value });
 }