private async Task Log_Event(string DataValue, string Name, string Sensor, string DataType, string UnitOfMeasure)
        {
            //Debug
            DateTime localDate = DateTime.Now;
            System.Diagnostics.Debug.WriteLine("Event: " + Name + "-" + Sensor + "-" + DataValue + "-" + localDate.ToString());
    
            //Flash LED 
            TrasmitLEDPin.Write(GpioPinValue.High);
            await Task.Delay(100);
            TrasmitLEDPin.Write(GpioPinValue.Low);
           
            //to Azure
            if (AzureMode == "Transmit")
                if (internetConnected == "True")
                {
                    {

                        //Init httpClinet:
                        //var httpClient = new HttpClient();

                        System.Diagnostics.Debug.WriteLine("Starting Azure Transmit");


                        MySensorData SensorInstance = new MySensorData();
                        SensorInstance.Name = Name;
                        SensorInstance.SensorType = Sensor;
                        SensorInstance.TimeStamp = DateTime.Now.ToString();
                        SensorInstance.DataValue = DataValue;
                        SensorInstance.DataType = DataType;
                        SensorInstance.UnitOfMeasure = UnitOfMeasure;
                        SensorInstance.MeasurementID = Guid.NewGuid().ToString();
                        SensorInstance.Location = "NULL";

                        string jsoncontent = JsonConvert.SerializeObject(SensorInstance);
                       
                        DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString);

                        string dataBuffer;
                        dataBuffer = jsoncontent;

                        System.Diagnostics.Debug.WriteLine(jsoncontent);


                        Message eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
                        await deviceClient.SendEventAsync(eventMessage);


                        System.Diagnostics.Debug.WriteLine("Azure Transmit Done");



                    }
                }


        }
Example #2
0
        private async Task Log_Event(string DataValue, string Name, string Sensor, string DataType, string UnitOfMeasure)
        {
            //Debug
            DateTime localDate = DateTime.Now;
            System.Diagnostics.Debug.WriteLine("Event: " + Name + "-" + Sensor + "-" + DataValue + "-" + localDate.ToString());

            //to Azure
            if (AzureMode == "Transmit")
                if (internetConnected == "True")
                {
                    {
                        
                        //Init httpClinet:
                        var httpClient = new HttpClient();

                        System.Diagnostics.Debug.WriteLine("Starting Azure Transmit");


                        MySensorData SensorInstance = new MySensorData();
                        SensorInstance.Name = Name;
                        SensorInstance.SensorType = Sensor;
                        SensorInstance.TimeStamp = DateTime.Now.ToString();
                        SensorInstance.DataValue = DataValue;
                        SensorInstance.DataType = DataType;
                        SensorInstance.UnitOfMeasure = UnitOfMeasure;
                        SensorInstance.MeasurementID = Guid.NewGuid().ToString();
                        SensorInstance.Location = DeviceLocation;




                        //     string jsoncontent = "[{\"Name\":\"" + SensorInstance.Name + "\",\"SensorType\":\"" + SensorInstance.SensorType + "\",\"TimeStamp\":\"" + SensorInstance.TimeStamp + "\",\"DataValue\":\"" + SensorInstance.DataValue + "\"}]";


                        // Name, SensorType, TimeStamp, Datavalue, UnitOfMeasure, Location, MeasurementID, Datatype
                        string jsoncontent = JsonConvert.SerializeObject(SensorInstance);

                        System.Diagnostics.Debug.WriteLine(jsoncontent);






                        /*
                        //Azure Check
                        var eventHub1url = new Uri("https://bergeventhub1.servicebus.windows.net");
                        HttpRequestMessage msgLoc = new HttpRequestMessage(new HttpMethod("GET"), new Uri(eventHub1url.ToString()));
                        HttpResponseMessage response = await httpClient.SendRequestAsync(msgLoc).AsTask();
                        string AzureResponce = response.StatusCode.ToString();
                        if(AzureResponce == "OK")
                        {
                            AzureMode = "Transmit";
                            LabelAzureStatus.Text = "Connected!";

                        }
                        else
                        {
                            AzureMode = "NoTransmit";
                            LabelAzureStatus.Text = "Disconnected";

                        }
                        */




                        var uri = new Uri("https://bergeventhub1.servicebus.windows.net/scudemo1/publishers/mydevice/messages?timeout=60");


                        //NameSpace bergeventhub1
                        //Hub Name scudemo1
                        //Publisher mydevice
                        //Mode http  
                        //Sender Key Name  AccessPolicyTest
                        //Sender Key Lb0v8as6rBaRu8r2uN959Y+VzBuLQsUP3Mz4ibiYUiY=
                        //TTL 

                        //var sas = "SharedAccessSignature sr=https%3a%2f%2fbergeventhub1.servicebus.windows.net%2fscudemo1%2fpublishers%2fmydevice%2fmessages&sig=9XPKGtAS%2bKUpZOTg5MtNO5x08DHD1qkrqeDxOEk5gOg%3d&se=1439877541&skn=AccessPolicyTest";
                        //var sas = "SharedAccessSignature sr=https%3a%2f%2fbergeventhub1.servicebus.windows.net%2fscudemo1%2fpublishers%2fmydevice%2fmessages&sig=frEcVrgQYE0ggV3ET7J6aBKM12yX%2f9KQcnmSvlkKKS8%3d&se=1440346027&skn=AccessPolicyTest";
                        //var sas = "SharedAccessSignature sr=https%3a%2f%2fbergeventhub1.servicebus.windows.net%2fscudemo1%2fpublishers%2fmydevice%2fmessages&sig=dtqpbWqiRVWRB98JK7%2fuR%2faWoCmczVHvHCqZs0QRpFI%3d&se=1440346027&skn=AccessPolicyTest";




                        //var sas = "SharedAccessSignature sr=https%3a%2f%2fbergeventhub1.servicebus.windows.net%2fscudemo1%2fpublishers%2fmydevice%2fmessages&sig=629PRqS4g%2bcAfq3sbLBzhAlVslpI3yQDHtfOZWHdbyg%3d&se=1441099364&skn=AccessPolicyTest";

                        //httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", sas);

                       // HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), new Uri(uri.ToString()));
                       // msg.Content = new HttpStringContent(jsoncontent);
                     //   msg.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json");


                    //    HttpResponseMessage responseEVPOST = await httpClient.SendRequestAsync(msg).AsTask();

                        httpClient.Dispose();
                        System.Diagnostics.Debug.WriteLine("Azure Transmit Done");



                    }
                }


        }