Ejemplo n.º 1
0
        static void Main()
        {
            led = GpioController.GetDefault().OpenPin(FEZ.GpioPin.Led1);
            led.SetDriveMode(GpioPinDriveMode.Output);

            string host = "ghi-test-iot.azure-devices.net"; //"192.168.1.152" "ghi-test-iot.azure-devices.net" "a1uyw4e5ps7wof.iot.us-east-2.amazonaws.com"
            int    port = 8883;                             // 1883 8883

            var serial = SerialDevice.FromId(FEZ.UartPort.Usart1);

            serial.BaudRate    = 115200;
            serial.ReadTimeout = TimeSpan.Zero;

            wifi = new AMW007Interface(serial);

            mqtt = new MQTT();
            wifi.Run();

            //wifi.SetTlsServerRootCertificate("azure.pem"); //aws.pem

            mqtt.Connect(wifi, host, port, clientID, 60, true);
            mqtt.Publish("devices/FEZ/messages/events/", "HELLO!");       //devices/FEZ/messages/events/    $aws/things/FEZ/shadow/update

            mqtt.Subscribe("devices/FEZ/messages/devicebound/#", "test"); // devices/FEZ/messages/devicebound/# $aws/things/FEZ/shadow/update

            Thread reader = new Thread(ReadStream);

            reader.Start();

            while (true)
            {
                wifi.ReadSocket(0, 500);
                Thread.Sleep(200);
            }
        }
Ejemplo n.º 2
0
        public void Connect(AMW007Interface wifi, string host, int port, string clientID, int keepAlive, bool cleanSession, string username = null, string password = null)
        {
            int fixedHeaderSize = 0;
            int varHeaderSize   = 0;
            int payloadSize     = 0;
            int remainingLength = 0;

            byte[] MQTTbuffer;
            int    index = 0;

            this.wifi     = wifi;
            this.clientID = clientID;

            wifi.OpenSocket(host, port);

            Thread reader = new Thread(ReadStream);

            reader.Start();

            byte[] clientIdUtf8 = Encoding.UTF8.GetBytes(clientID);
            byte[] usernameUtf8 = ((username != null) && (username.Length > 0)) ? Encoding.UTF8.GetBytes(username) : null;
            byte[] passwordUtf8 = ((password != null) && (password.Length > 0)) ? Encoding.UTF8.GetBytes(password) : null;

            varHeaderSize += (Constants.PROTOCOL_NAME_LEN_SIZE + Constants.PROTOCOL_NAME_SIZE);

            varHeaderSize += Constants.PROTOCOL_VERSION_SIZE;
            varHeaderSize += Constants.CONNECT_FLAGS_SIZE;
            varHeaderSize += Constants.KEEP_ALIVE_TIME_SIZE;

            payloadSize     += clientIdUtf8.Length + 2;
            payloadSize     += (usernameUtf8 != null) ? (usernameUtf8.Length + 2) : 0;
            payloadSize     += (passwordUtf8 != null) ? (passwordUtf8.Length + 2) : 0;
            remainingLength += (varHeaderSize + payloadSize);

            fixedHeaderSize = 1;

            int temp = remainingLength;

            // increase fixed header size based on remaining length
            // (each remaining length byte can encode until 128)
            do
            {
                fixedHeaderSize++;
                temp = temp / 128;
            } while (temp > 0);

            MQTTbuffer          = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
            MQTTbuffer[index++] = (Constants.CONNECT << Constants.TYPE_OFFSET) | Constants.CONNECT_FLAG_BITS;
            index = this.encodeRemainingLength(remainingLength, MQTTbuffer, index);

            MQTTbuffer[index++] = 0;                            // MSB protocol name size
            MQTTbuffer[index++] = Constants.PROTOCOL_NAME_SIZE; // LSB protocol name size
            Array.Copy(Encoding.UTF8.GetBytes(Constants.PROTOCOL_NAME), 0, MQTTbuffer, index, Constants.PROTOCOL_NAME_SIZE);
            index += Constants.PROTOCOL_NAME_SIZE;
            MQTTbuffer[index++] = Constants.PROTOCOL_VERSION_V3_1_1;

            byte connectFlags = 0x00;

            connectFlags       |= (usernameUtf8 != null) ? (byte)(1 << Constants.USERNAME_FLAG_OFFSET) : (byte)0x00;
            connectFlags       |= (passwordUtf8 != null) ? (byte)(1 << Constants.PASSWORD_FLAG_OFFSET) : (byte)0x00;
            connectFlags       |= (cleanSession) ? (byte)(1 << Constants.CLEAN_SESSION_FLAG_OFFSET) : (byte)0x00;
            MQTTbuffer[index++] = connectFlags;

            MQTTbuffer[index++] = (byte)(keepAlive / 256);                     // MSB
            MQTTbuffer[index++] = (byte)(keepAlive % 256);                     // LSB

            MQTTbuffer[index++] = (byte)((clientIdUtf8.Length >> 8) & 0x00FF); // MSB
            MQTTbuffer[index++] = (byte)(clientIdUtf8.Length & 0x00FF);        // LSB
            Array.Copy(clientIdUtf8, 0, MQTTbuffer, index, clientIdUtf8.Length);
            index += clientIdUtf8.Length;

            if (usernameUtf8 != null)
            {
                MQTTbuffer[index++] = (byte)(usernameUtf8.Length / 256); // MSB
                MQTTbuffer[index++] = (byte)(usernameUtf8.Length % 256); // LSB
                Array.Copy(usernameUtf8, 0, MQTTbuffer, index, usernameUtf8.Length);
                index += usernameUtf8.Length;
            }

            if (passwordUtf8 != null)
            {
                MQTTbuffer[index++] = (byte)(passwordUtf8.Length / 256); // MSB
                MQTTbuffer[index++] = (byte)(passwordUtf8.Length % 256); // LSB
                Array.Copy(passwordUtf8, 0, MQTTbuffer, index, passwordUtf8.Length);
                index += passwordUtf8.Length;
            }

            int count = 0;

            do
            {
                wifi.WriteSocket(0, MQTTbuffer, MQTTbuffer.Length);
                Thread.Sleep(500);
                wifi.ReadSocket(0, 1000);
                count++;
                if (count > 5)
                {
                    throw new ArgumentOutOfRangeException("Can't connect"); //TODO Add exceptions class
                }
            } while (connack == false);
            // TODO implement ConnackHandler()
        }