Example #1
0
        public static void Main()
        {
            var wifi = new NeonWifiDevice();
            //wifi.EnableDebugOutput = true;
            //wifi.EnableVerboseOutput = true;

            wifi.Booted += WifiOnBooted;  // or you can wait on the wifi.IsInitializedEvent
            //wifi.Error += WifiOnError;
            //wifi.ConnectionStateChanged += WifiOnConnectionStateChanged;

            //var apList = wifi.GetAccessPoints();
            //Debug.Print("Access points:");
            //foreach (var ap in apList)
            //{
            //    Debug.Print("ECN : " + ap.Ecn);
            //    Debug.Print("SSID : " + ap.Ssid);
            //    Debug.Print("RSSI : " + ap.Rssi);
            //    Debug.Print("MAC addr : " + ap.MacAddress);
            //    Debug.Print("Connection is : " + (ap.AutomaticConnectionMode ? "Automatic" : "Manual"));
            //}

            wifi.Connect("XXX","XXX");

            var sntp = new SntpClient(wifi, "time1.google.com");
            sntp.SetTime();

            var uri = new Uri("http://www.example.com");
            var httpClient = new HttpClient(wifi);
            var request = new HttpRequest(uri);
            request.Uri = uri;
            request.Headers.Add("Connection","Keep-Alive");
            request.ResponseReceived += HttpResponseReceived;
            httpClient.SendAsync(request);
            
            bool state = true;
            int iCounter = 0;
            while (true)
            {
                Hardware.UserLed.Write(state);
                state = !state;
                if (++iCounter == 10)
                {
                    Debug.Print("Current UTC time : " + DateTime.UtcNow);
                    iCounter = 0;
                }
                Thread.Sleep(500);
            }
        }
        /// <summary>
        /// Sends a Web Request prepared for Azure Storage
        /// </summary>
        /// <param name="url"></param>
        /// <param name="authHeader"></param>
        /// <param name="dateHeader"></param>
        /// <param name="versionHeader"></param>
        /// <param name="fileBytes"></param>
        /// <param name="contentLength"></param>
        /// <param name="httpVerb"></param>
        /// <param name="expect100Continue"></param>
        /// <param name="additionalHeaders"></param>
        /// <returns></returns>
        public static BasicHttpResponse SendWebRequest(HttpClient client, string url, string authHeader, string dateHeader, string versionHeader, byte[] fileBytes = null, int contentLength = 0, string httpVerb = "GET", bool expect100Continue = false, Hashtable additionalHeaders = null)
        {
            string responseBody = "";
            HttpStatusCode responseStatusCode = HttpStatusCode.Ambiguous;
            HttpRequest request = PrepareRequest(url, authHeader, dateHeader, versionHeader, fileBytes, contentLength, httpVerb, expect100Continue, additionalHeaders);
            try
            {
                HttpResponse response;
                using (response = client.Send(request))
                {
                    responseStatusCode = response.StatusCode;
                    Debug.Print("HTTP " + ((int)responseStatusCode).ToString());

                    responseBody = response.GetBodyAsString();

                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        Debug.Print("Asset has been created!");
                    }
                    if (response.StatusCode == HttpStatusCode.Accepted)
                    {
                        Debug.Print("Action has been completed");
                    }
                    if (response.StatusCode == HttpStatusCode.Forbidden)
                    {
                        throw new WebException("Forbidden", null, WebExceptionStatus.TrustFailure, response);
                    }
                }
            }
            catch (WebException ex)
            {
                if ((ex.Response).StatusCode == HttpStatusCode.Conflict)
                {
                    Debug.Print("Asset already exists!");
                }
                if ((ex.Response).StatusCode == HttpStatusCode.Forbidden)
                {
                    Debug.Print("Problem with signature. Check next debug statement for stack");
                }
            }

            if (responseBody == null)
                responseBody = "There was no body content";

            Debug.Print(responseBody);
            return new BasicHttpResponse() {Body = responseBody, StatusCode = responseStatusCode};
        }
Example #3
0
        // In order to create a truly robust long-running solution, you must combine the code below
        //   with proper use of a Watchdog Timer and exception handling on the wifi calls.
        // Hardware note: It has been our experience that to work at 115200 with the ESP8266 and NETMF, you need a 1024 byte serial buffer.
        //   Smaller serial buffers may result in portions of incoming TCP traffic being dropped, which can also break the protocol processing
        //   and result in hangs.

        public static void Main()
        {
            var port = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One);
            var wifi = new Esp8266WifiDevice(port, _rfPower, null);
            // On Oxygen+Neon, you can use use new NeonWifiDevice() without providing a port or power pin definition

            // Enable echoing of commands
            wifi.EnableDebugOutput = true;
            // Enable dump of every byte sent or received (may introduce performance delays and can cause buffer overflow
            //   at low baud rates or with small serial buffers.
            //wifi.EnableVerboseOutput = true;

            Debug.Print("Access points:");
            var apList = wifi.GetAccessPoints();
            foreach (var ap in apList)
            {
                Debug.Print("ssid:" + ap.Ssid + "  ecn:" + ap.Ecn);
            }
            Debug.Print("-- end of list -------------");

#if CREATE_ACCESS_POINT
            wifi.Mode = OperatingMode.Both;
            wifi.ConfigureAccessPoint("serwifitest", "24681234", 5, Ecn.WPA2_PSK);
            wifi.EnableDhcp(OperatingMode.AccessPoint, true);
#else
            wifi.Mode = OperatingMode.Station;
#endif
            wifi.Connect("XXX", "XXX");

            Debug.Print("Station IP address : " + wifi.StationIPAddress.ToString());
            Debug.Print("Station MAC address : " + wifi.StationMacAddress);
            Debug.Print("Station Gateway address : " + wifi.StationGateway.ToString());
            Debug.Print("Station netmask : " + wifi.StationNetmask.ToString());

            Debug.Print("AP SSID : " + wifi.AccessPointSsid);
            Debug.Print("AP Password : "******"AP Channel : " + wifi.AccessPointChannel);
            Debug.Print("AP ECN : " + wifi.AccessPointEcn);

            Debug.Print("AP IP address : " + wifi.AccessPointIPAddress.ToString());
            Debug.Print("AP MAC address : " + wifi.AccessPointMacAddress);
            Debug.Print("AP Gateway address : " + wifi.AccessPointGateway.ToString());
            Debug.Print("AP netmask : " + wifi.AccessPointNetmask.ToString());

            var sntp = new SntpClient(wifi, "time1.google.com");
            // You should repeat this every day or so, to counter clock drift, or use .Start()
            // to update the clock against and SNTP server automatically in the background.
            sntp.SetTime();

            wifi.CreateServer(80, OnServerConnectionOpened);

            var httpClient = new HttpClient(wifi);
            var request = new HttpRequest(new Uri("http://www.example.com/"));
            request.ResponseReceived += HttpResponseReceived;
            httpClient.SendAsync(request);

            int iCounter = 0;
            bool state = true;
            while (true)
            {
                _userLed.Write(state);
                state = !state;
                ++iCounter;
                if (iCounter % 10 == 0)
                {
                    Debug.Print("Current UTC time : " + DateTime.UtcNow);
                }
                // Every 15 seconds
                if (iCounter % 30 == 0)
                {
#if CREATE_ACCESS_POINT
                    Debug.Print("Clients connected to this AP");
                    var clientList = wifi.GetConnectedClients();
                    foreach (var client in clientList)
                    {
                        Debug.Print("IP:" + client.IpAddress.ToString() + "  MAC:" + client.MacAddress);
                    }
                    Debug.Print("-- end of list -------------");
#endif
                    iCounter = 0;
                }
                Thread.Sleep(500);
            }
        }
        private void SendBacklog()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            bool fFirst = true;
            foreach (var item in _backlog)
            {
                if (!fFirst)
                    sb.Append(",");
                fFirst = false;
                sb.Append(item);
            }
            sb.Append("]");

            //TODO: With large backlogs and large property sets, this will exceed the ESP8266 buffer. To fix that, send each backlog item separately

            // Send the data
            var adapter = (INetworkAdapter)DiContainer.Instance.Resolve(typeof(INetworkAdapter));
            var httpClient = new HttpClient(adapter);
            var request = new HttpRequest(HttpMethod.Post, new Uri("http://192.168.1.236/"));
            request.Body = Encoding.UTF8.GetBytes(sb.ToString());
            //request.ResponseReceived += HttpResponseReceived;
            httpClient.SendAsync(request);

            //TODO: We're always assuming the tranmission worked, and it doesn't. We're not building up the backlog of failed transmissions
            _backlog.Clear();
        }
Example #5
0
 public BlobClient(INetworkAdapter adapter, CloudStorageAccount account)
 {
     _adapter = adapter;
     _account = account;
     _client = new HttpClient(adapter);
 }