×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Egypt
    Posts
    47

    Post WP8 & Arduino Uno communicating Problem

    Hello
    I am having trouble with WP8 & Arduino Uno
    I use Bluetooth module, Arduino Uno, Proximity sensor, lumia 920
     Use this code in the Arduino :

    Code:
    #include <SoftwareSerial.h>
     
    const int TX_BT = 10;
    const int RX_BT = 11;
     
    SoftwareSerial btSerial(TX_BT, RX_BT);
     
    //Frequency to send periodic messages to Windows Phone, in milliseconds.
    //Core code.
    const unsigned long periodicMessageFrequency = 5000;
    unsigned long time = 0;
     
    //Process the incoming command from Windows Phone.
    //It should be changed according to what you want to do.
    void processCommand(char* command) {
    }
     
    //Send a message back to the Windows Phone.
    //Is can't be changed.
    void sendMessage(char* message) {
      int messageLen = strlen(message);
      if(messageLen < 256) {  
        btSerial.write(messageLen);
        btSerial.print(message);
      }
    }
     
    //Send a set of periodic messages to the Windows Phone.
    //It should be changed according to what you want to do.
    //This message could be a sensor data, like a thermometer data.
    void sendPeriodicMessages() {
    }
     
    //Setup Arduino function
    void setup() {
      Serial.begin(9600);
      Serial.println("USB Connected");
      btSerial.begin(9600);
    }
     
    //Loop Arduino function
    //It can't be changed
    void loop() {
      if(btSerial.available()) {
          int commandSize = (int)btSerial.read();
          char command[commandSize];
          int commandPos = 0;
          while(commandPos < commandSize) {
            if(btSerial.available()) {
              command[commandPos] = (char)btSerial.read();
              commandPos++;
            }
          }
          command[commandPos] = 0;
          processCommand(command);
      }
      unsigned long currentTime = millis();
      if((currentTime - time) > periodicMessageFrequency) {
        sendPeriodicMessages();
        time = currentTime;
      }
    }
    And this code in WP8 App :

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Networking;
    using Windows.Networking.Sockets;
    using Windows.Storage.Streams;
     
    namespace BluetoothConnectionManager
    {
        /// <summary>
        /// Class to control the bluetooth connection to the Arduino.
        /// </summary>
        public class ConnectionManager
        {
            /// <summary>
            /// Socket used to communicate with Arduino.
            /// </summary>
            private StreamSocket socket;
     
            /// <summary>
            /// DataWriter used to send commands easily.
            /// </summary>
            private DataWriter dataWriter;
     
            /// <summary>
            /// DataReader used to receive messages easily.
            /// </summary>
            private DataReader dataReader;
     
            /// <summary>
            /// Thread used to keep reading data from socket.
            /// </summary>
            private BackgroundWorker dataReadWorker;
     
            /// <summary>
            /// Delegate used by event handler.
            /// </summary>
            /// <param name="message">The message received.</param>
            public delegate void MessageReceivedHandler(string message);
     
            /// <summary>
            /// Event fired when a new message is received from Arduino.
            /// </summary>
            public event MessageReceivedHandler MessageReceived;
     
            /// <summary>
            /// Initialize the manager, should be called in OnNavigatedTo of main page.
            /// </summary>
            public void Initialize()
            {
                socket = new StreamSocket();
                dataReadWorker = new BackgroundWorker();
                dataReadWorker.WorkerSupportsCancellation = true;
                dataReadWorker.DoWork += new DoWorkEventHandler(ReceiveMessages);
            }
     
            /// <summary>
            /// Finalize the connection manager, should be called in OnNavigatedFrom of main page.
            /// </summary>
            public void Terminate()
            {
                if (socket != null)
                {
                    socket.Dispose();
                }
                if (dataReadWorker != null)
                {
                    dataReadWorker.CancelAsync();
                }
            }
     
            /// <summary>
            /// Connect to the given host device.
            /// </summary>
            /// <param name="deviceHostName">The host device name.</param>
            public async void Connect(HostName deviceHostName)
            {
                if (socket != null)
                {
                    await socket.ConnectAsync(deviceHostName, "1");
                    dataReader = new DataReader(socket.InputStream);
                    dataReadWorker.RunWorkerAsync();
                    dataWriter = new DataWriter(socket.OutputStream);
                }
            }
     
            /// <summary>
            /// Receive messages from the Arduino through bluetooth.
            /// </summary>
            private async void ReceiveMessages(object sender, DoWorkEventArgs e)
            {
                try
                {
                    while (true)
                    {
                        // Read first byte (length of the subsequent message, 255 or less). 
                        uint sizeFieldCount = await dataReader.LoadAsync(1);
                        if (sizeFieldCount != 1)
                        {
                            // The underlying socket was closed before we were able to read the whole data. 
                            return;
                        }
     
                        // Read the message. 
                        uint messageLength = dataReader.ReadByte();
                        uint actualMessageLength = await dataReader.LoadAsync(messageLength);
                        if (messageLength != actualMessageLength)
                        {
                            // The underlying socket was closed before we were able to read the whole data. 
                            return;
                        }
                        // Read the message and process it.
                        string message = dataReader.ReadString(actualMessageLength);
                        MessageReceived(message);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
     
            }
     
            /// <summary>
            /// Send command to the Arduino through bluetooth.
            /// </summary>
            /// <param name="command">The sent command.</param>
            /// <returns>The number of bytes sent</returns>
            public async Task<uint> SendCommand(string command)
            {
                uint sentCommandSize = 0;
                if (dataWriter != null)
                {
                    uint commandSize = dataWriter.MeasureString(command);
                    dataWriter.WriteByte((byte)commandSize);
                    sentCommandSize = dataWriter.WriteString(command);
                    await dataWriter.StoreAsync();
                }
                return sentCommandSize;
            }
        }
    }
    But the code you used it in the WP8 App to connect with the Arduino code is unsafe because it allows for Arduino to the implementation of the orders of any Windows Phone device has this application
    Is there any suggestions on how to create a safe environment for communication between them
    I probably used it as a key to my car

  2. #2
    Registered User
    Join Date
    Mar 2007
    Location
    ..., Earth, Brazil, Paraiba, Campina Grande
    Posts
    72

    Re: WP8 & Arduino Uno communicating Problem

    Hi Ahmed,

    It's a critical security scenario and deserves a special attention. I guess that you can think about 2 levels of security:

    1) Bluetooth pairing: The pin code is already a password to access the device. In the article we used the default one "1,2,3,4". But you can change it following this site: http://ecno92.blogspot.com.br/2012/1...ange-name.html

    2) Application layer: You can implement any security mechanism in application layer. The simpler one is a statical password in Arduino code that you have to type in WP8 application in order to access the Arduino. Every command must contains this password and the Arduino compare them (the statical saved and the received from WP8).

    Obs: You can implement both of them to make your solution more safe.


    PS: A key to car is an interest use for this. Could you show me some pictures when finished?
    PS2: How about your NFC shield? Already received the new one?

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,763

    Re: WP8 & Arduino Uno communicating Problem

    Quote Originally Posted by bintk View Post
    it allows for Arduino to the implementation of the orders of any Windows Phone device has this application
    On the lower level you could mandate pairing. Then the Arduino would accept commands from your device only. But it is a question for Arduino development forums if you can either mandate pairing, or at least verify the BT address of the sending device (from software).
    If there is no hardware-level pairing you can still implement something from software, and encrypt (or at least, sign) communication between the devices.

    EDIT: sorry, I did not notice the already existing answer, this tab was open for a longer time apparently.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Location
    Egypt
    Posts
    47

    Re: WP8 & Arduino Uno communicating Problem

    I'm already begun to work with a different PIN
    I plan to create a database on the Internet and put a statical code in the database and after login via e-mail and password you can obtain the code from the database
    I will set the Arduino to get this statical code verification and verification of the PIN before implementation of the order
    NFC shield so far did not reach the to Egypt
    It takes 20: 30 to get to the Egyptian customs and 5 days to issue a permit communications

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    Egypt
    Posts
    47

    Re: WP8 & Arduino Uno communicating Problem

    Quote Originally Posted by mfabiop View Post
    PS: A key to car is an interest use for this. Could you show me some pictures when finished?
    * I will send you all the pictures and the code for your application Once completed, tomorrow I have exams in college and I will finish exams on 22/5 ^_^

  6. #6
    Registered User
    Join Date
    Jun 2011
    Location
    Egypt
    Posts
    47

    Re: WP8 & Arduino Uno communicating Problem

    Quote Originally Posted by wizard_hu_ View Post
    On the lower level you could mandate pairing. Then the Arduino would accept commands from your device only. But it is a question for Arduino development forums if you can either mandate pairing, or at least verify the BT address of the sending device (from software).
    If there is no hardware-level pairing you can still implement something from software, and encrypt (or at least, sign) communication between the devices.

    EDIT: sorry, I did not notice the already existing answer, this tab was open for a longer time apparently.
    Do not worry
    I actually working by your advice
    I hope to get high safely through the code in a database on the Internet and demand from the user to login before accessing any information within the application
    I'll also used SSL system - https to get more security

Similar Threads

  1. Problem in communicating two running apps
    By esanchis in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2009-09-20, 20:21
  2. is it possible to connect an n95 8gb to an arduino like n810?
    By McLightning in forum Mobile Java General
    Replies: 2
    Last Post: 2009-05-07, 12:08
  3. Replies: 5
    Last Post: 2009-02-14, 17:02
  4. problem of communicating an internetradio over rstp
    By miqitangguo in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2008-05-13, 14:54
  5. Problem in communicating with 5110
    By Nokia_Archive in forum General Messaging
    Replies: 1
    Last Post: 2002-05-20, 15:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×