Android Multiwii remote Bluetooth/Wifi

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

howardhb wrote:What WiFi hardware do you use on the copter?



Howard, I believe any Wifi to Serial device will work such as this one.

http://www.ebay.com/itm/281205805036?ss ... 1423.l2649

User avatar
howardhb
Posts: 189
Joined: Tue Oct 11, 2011 7:10 pm
Location: Port Elizabeth, South Africa

Re: Android Multiwii remote Bluetooth/Wifi

Post by howardhb »

Howard, I believe any Wifi to Serial device will work such as this one.
http://www.ebay.com/itm/281205805036?ss ... 1423.l2649

Thanks, I will acquire one and give this a try.

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

howardhb wrote:What WiFi hardware do you use on the copter?


I'm using a wr703n router with ser2net installed. Costs ~20$


howardhb wrote:Howard, I believe any Wifi to Serial device will work such as this one.


Cool, I did not know about them. I would still recommend the wr703n since you can add stuff later like a camera and it is 5$ cheaper.

copterrichie wrote:Ok, I installed the updated version and was able to access the menu and sub-menus. I set the IP Address and port for my copters and upon pressing the connect, I received: "Unfortunately, Multiwii Remote has Stopped". I have a newer Tablet, going to test it there and see if I receive the same response.

Thank you and I am LOVING This.

Richard


What android version are you running and what tablet?

copterrichie wrote:On the second tablet, I received the following: "failed to connect to /192.168.1.20 (port 8899) after 90000ms: isConnected failed: ECONNREFUSED (Connection refused)


Hmm, that means there is a problem with the wifi link, nothing I can do from this end. Did you connect to your copter wifi network before connecting with the app? If you are using an openwrt router, I had the same issue and reset it with the command "fastboot", installed ser2net and disabled the console.

I have to go now, I'll be back in about an hour.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Theelectronicguy wrote:
What android version are you running and what tablet?

Hmm, that means there is a problem with the wifi link, nothing I can do from this end. Did you connect to your copter wifi network before connecting with the app? If you are using an openwrt router, I had the same issue and reset it with the command "fastboot", installed ser2net and disabled the console.

I have to go now, I'll be back in about an hour.


I think it may be a permission issue, I had this problem in the very early stage with my MultiWii Port to Android, both of my tablets are not rooted.

Tablet #1: Version 4.0.4
Tablet #2: Version 4.2.2

My MultiWii Port connects and is very stable however, I like your graphical interface and touch control. Really would make life much easier not having to use a joystick.

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Hi,

What permission issues? It's kind of hard to debug since the AVD does not support wifi :P. Could you please get "Log collector" from the market and send the log to "allaboutblender@gmail.com" when you try the app. I can't think of anything that could cause it to fail...

This is the wifi class, if you have any idea of what is different from yours, pray tell:

Code: Select all

public class Wifi extends Communication {
   private Activity myActivity;
   private CommunicationInterface wifiInterface;
   private Socket mySocket;
   private BufferedInputStream input;
   private OutputStream out;
   private WifiManager wifiManager;
   
   public Wifi(Activity myActivity) {
      super(myActivity.getApplicationContext());
      this.myActivity = myActivity;
      wifiInterface = (CommunicationInterface) this.myActivity;
   }

   public boolean Write(byte[] data) {
      super.Write(data);
      try {
         out.write(data);
      } catch (IOException e) {
         e.printStackTrace();
         Connected = false;
      }
      return Connected;
   }
   
   Handler handler = new Handler();
   Runnable inputRunnable = new Runnable() {
      @Override
      public void run() {   
         if(input != null)
            try {
         while(input.available() > 0)
            wifiInterface.onReceive((byte) (input.read() & 0xFF));
            }
         catch(IOException e) {
            e.printStackTrace();
         }
         if(Connected)
         handler.postDelayed(this, 10);
      }
   };   
   
   public boolean flush() {
      try {
         out.flush();
         return true;
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
   }
   
   @Override
   public boolean Connect(String ip, int port) {
      address = ip + ":" + port;
      setState(STATE_CONNECTING);
      try {
          mySocket = new Socket(ip, port);
          input = new BufferedInputStream(mySocket.getInputStream());
            out = mySocket.getOutputStream();
            handler.post(inputRunnable);
            setState(STATE_CONNECTED);
            Connected = true;
         }
         catch(Exception e) {
            e.printStackTrace();
            setState(STATE_NONE);
            setState(e.getMessage());
            Connected = false;
         }
      return Connected;
   }

   @Override
   public boolean dataAvailable() {
      try {
         if(input == null)
            return false;
         return input.available() != 0;
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
   }

   @Override
   public void Close() {
      Connected = false;
      if(mySocket != null && mySocket.isConnected())
         try {
            mySocket.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
   }

   @Override
   public int getStrength() {
      return 100 + wifiManager.getConnectionInfo().getRssi();
   }
}


And the permissions:

Code: Select all

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Hmmm, let me go though my code and compare. Back with you shortly.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

The following is how I am doing it for myself, exclude the ControlP5 Stuff.

Code: Select all

import android.net.wifi.WifiManager; 
import android.net.wifi.WifiInfo;

private WifiManager wifiManager;          // instance of the WifiManager for getting network details
private String wifiState;                 // state of the wifi connection
private String IpAddress;
//private String Network;

Socket socket;
ObjectOutputStream out;
DataInputStream in;

private String HostAddress = "192.168.1.77";
private int port = 8899;
private boolean NetworkFlag = false;



Code: Select all

wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 
  int wifiStateCode = wifiManager.getWifiState();

  switch (wifiStateCode) {
  case 1:
    wifiState = "Wifi disabled";
    break;
  case 3:
    wifiState = "Wifi enabled";

WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    IpAddress = ReadIpAddress(wifiInfo.getIpAddress());
    controlP5.addTextlabel("IpAddress",IpAddress,1,90).setFont(font15);
   
    //init_com=true;
    //graph_on=true;
      try {
 
    Socket socket=new Socket(HostAddress,port);
    socket.setKeepAlive(true);
    NetworkFlag = socket.isConnected();
   out=new ObjectOutputStream(socket.getOutputStream());
    in=new DataInputStream(socket.getInputStream());
   controlP5.addTextlabel("HostIP",HostAddress,1,120).setFont(font15);
 } catch (IOException e) {}
   
    break;
  case 4:
    wifiState = "Wifi state unknown";
    break;
  default:
    wifiState = "Other wifi state detected: " + wifiState;
  }
  controlP5.addTextlabel("WiFiStatus",wifiState,1,75).setFont(font15);
  if(NetworkFlag==true){
    buttonSTART.setColorBackground(green_);buttonSTOP.setColorBackground(green_);buttonREAD.setColorBackground(green_);
    buttonRESET.setColorBackground(green_);
    buttonCALIBRATE_ACC.setColorBackground(green_); buttonCALIBRATE_MAG.setColorBackground(green_);
}
   
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

And here is the AndroidManifest

Code: Select all

[/code<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="processing.test.multiwiiconfusb">
  <uses-sdk android:minSdkVersion="14"/>
  <application android:debuggable="true" android:icon="@drawable/icon" android:label="MultiWiiConfUSB">
    <activity android:name=".MultiWiiConfWifi">
      <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
      </activity>
  </application>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
</manifest>

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Ok, Fixed all warnings and added the one permission missing. Give it a try: http://www.filedropper.com/multiwiiremote_3

If It still doesn't work, either send me the log as described in the previous post or plug your phone into the computer and filter the logcat in eclipse by application name "com.multiwii.multiwiiremote".

Thanks

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Murphy's Law, the .apk is corrupt. Tried numerous times on both tablets including re-downloading it. :cry:

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Ok, I kept trying to redownload a good copy and Eureka!! Success, it works!! I will give it a complete test in the morning.

Thank you!!!

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Glad to know it finally worked! Enjoy!

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Thank you for the INSPIRATION, will keep you posted on the testing.


Image

Harnas
Posts: 2
Joined: Mon Feb 03, 2014 9:28 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by Harnas »

I developed similar app for Windows Phone. Now works on my Nokia Lumia 820, and connected with multiwii board via bluetooth. I think in next 2-3 weeks app will be published on windows phone store.

Image
Last edited by Harnas on Wed Feb 26, 2014 9:46 pm, edited 1 time in total.

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

copterrichie wrote:Thank you for the INSPIRATION, will keep you posted on the testing.


Image


Looks good. How does it fly?

Harnas wrote:I developed similar app for Windows Phone. Now works on my Nokia Lumia 820, and connected with multiwii board via bluetooth. I think in next 2-3 weeks app will be published on windows phone store, and source code will be published.
Image


Nice. I think it would look better if you remove aux1 = 1000 and "illuminate" the 1000/1500/2000 values when they are selected. I never programed for the windows phone. How hard is it and what do you use?

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Theelectronicguy wrote:Looks good. How does it fly?


One of the issues that I have at present with two joysticks, both can not have the focus at the same time. So I will have to write some code (in the process) to pole each joystick separately, the update then MSP_SET before sending that date set to the copter. I am not expecting to get this project into the air just yet, still need to finish up other elements of the copter first.

Thanks for asking. :)

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Just discovered there is an inherited problem with the touch screens on Tablets and maybe cell phones as well. The touch screen is unable to receive two simultaneous inputs. So, the built in Accelerometer may have to be used in place of one of the sticks.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

So what I need is a multiple touch screen sensitive zones tablet: http://en.wikipedia.org/wiki/Multi-touch

Rats, there is always something. :(

Harnas
Posts: 2
Joined: Mon Feb 03, 2014 9:28 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by Harnas »

Nice. I think it would look better if you remove aux1 = 1000 and "illuminate" the 1000/1500/2000 values when they are selected. I never programed for the windows phone. How hard is it and what do you use?


Great idea :) It should be easy. I use Visual Studio Express 2012 for Windows Phone, and write code in C#.

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Re: Android Multiwii remote Bluetooth/Wifi

Post by ezio »

copterrichie wrote:Just discovered there is an inherited problem with the touch screens on Tablets and maybe cell phones as well. The touch screen is unable to receive two simultaneous inputs. So, the built in Accelerometer may have to be used in place of one of the sticks.

Well I think it is the problem of the implementation. Each android device should support multi touch. With new devices it is up to 10 fingers.
http://developer.android.com/training/g ... multi.html

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

ezio wrote:Well I think it is the problem of the implementation. Each android device should support multi touch. With new devices it is up to 10 fingers.
http://developer.android.com/training/g ... multi.html


AWE!!! Thank you!!!

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

ezio wrote:
copterrichie wrote:Just discovered there is an inherited problem with the touch screens on Tablets and maybe cell phones as well. The touch screen is unable to receive two simultaneous inputs. So, the built in Accelerometer may have to be used in place of one of the sticks.

Well I think it is the problem of the implementation. Each android device should support multi touch. With new devices it is up to 10 fingers.
http://developer.android.com/training/g ... multi.html


Hey ezio,

Do you think you could add support for wifi in the next version of your app? I would love to use it, but as mentioned, I don't have bluetooth.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Java's mousePressed was the source of the problem I was having with reading multiple inputs. Moving forward. :D

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Odp: Android Multiwii remote Bluetooth/Wifi

Post by ezio »

Theelectronicguy wrote:
ezio wrote:
copterrichie wrote:Just discovered there is an inherited problem with the touch screens on Tablets and maybe cell phones as well. The touch screen is unable to receive two simultaneous inputs. So, the built in Accelerometer may have to be used in place of one of the sticks.

Well I think it is the problem of the implementation. Each android device should support multi touch. With new devices it is up to 10 fingers.
http://developer.android.com/training/g ... multi.html


Hey ezio,

Do you think you could add support for wifi in the next version of your app? I would love to use it, but as mentioned, I don't have bluetooth.

If you can give me an example code and some description I can try. I don't have the hardware so you will be a tester :)

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Gladly:

Code: Select all

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Handler;

public class Wifi extends Communication {
   private Activity myActivity;
   private CommunicationInterface wifiInterface;
   private Socket mySocket;
   private BufferedInputStream input;
   private OutputStream out;
   private WifiManager wifiManager;
   
   public Wifi(Activity myActivity) {
      super(myActivity.getApplicationContext());
      this.myActivity = myActivity;
      wifiInterface = (CommunicationInterface) this.myActivity;
      wifiManager = (WifiManager) myActivity.getSystemService(Context.WIFI_SERVICE);
      Enable();
   }
   
   //TODO connectToNetwork not working
   public boolean connectToNetwork(String ssid, String key) {
      WifiConfiguration wifiConfig = new WifiConfiguration();
      wifiConfig.SSID = String.format("\"%s\"", ssid);
      wifiConfig.preSharedKey = String.format("\"%s\"", key);
      wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
      
      int netId;
      WifiConfiguration currentConf = getConfiguredNetwork(wifiManager, ssid);
      if(currentConf == null)
         netId = wifiManager.addNetwork(wifiConfig);
      else
         netId = currentConf.networkId;
      
      if(wifiManager.getConnectionInfo().getSSID().equalsIgnoreCase(ssid))
         return true;
      
      wifiManager.disconnect();
      wifiManager.enableNetwork(netId, true);
      return wifiManager.reconnect();
   }
   private WifiConfiguration getConfiguredNetwork(WifiManager wifiManager, String ssid) {
      List<WifiConfiguration> myList = wifiManager.getConfiguredNetworks();
      for(WifiConfiguration current: myList) {
         if(current.SSID.equalsIgnoreCase(ssid))
            return current;
      }
      return null;
   }
   
   public void setState(String state) {
      this.state = state;
   }
   public String getConnectionState() {
      return state;
   }
   
   public boolean Write(byte[] data) {
      super.Write(data);
      try {
         out.write(data);
         Connected = true;
      } catch (IOException e) {
         e.printStackTrace();
         Connected = false;
      }
      return Connected;
   }
   
   Handler handler = new Handler();
   Runnable inputRunnable = new Runnable() {
      @Override
      public void run() {   
         if(input != null)
            try {
         while(input.available() > 0)
            wifiInterface.onReceive((byte) (input.read() & 0xFF));
            }
         catch(IOException e) {
            e.printStackTrace();
         }
         if(Connected)
         handler.postDelayed(this, 10);
      }
   };   
   
   public boolean flush() {
      try {
         out.flush();
         return true;
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
   }
   
   @Override
   public void Enable() {
   if(!wifiManager.isWifiEnabled())
   wifiManager.setWifiEnabled(true);
   }

   @Override
   public boolean Connect(String ip, int port) {
      address = ip + ":" + port;
      setState(STATE_CONNECTING);
      try {
          mySocket = new Socket(ip, port);
          mySocket.setKeepAlive(true);
          input = new BufferedInputStream(mySocket.getInputStream());
            out = mySocket.getOutputStream();
            handler.post(inputRunnable);
            setState(STATE_CONNECTED);
            Connected = true;
         }
         catch(Exception e) {
            e.printStackTrace();
            setState(STATE_NONE);
            setState(e.getMessage());
            Connected = false;
         }
      return Connected;
   }

   @Override
   public boolean dataAvailable() {
      try {
         if(input == null)
            return false;
         return input.available() != 0;
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
   }

   @Override
   public byte Read() {
      return 0;
   }

   @Override
   public void Close() {
      Connected = false;
      if(mySocket != null && mySocket.isConnected())
         try {
            mySocket.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
   }

   @Override
   public void Disable() {
   if(wifiManager.isWifiEnabled())      
   wifiManager.setWifiEnabled(false);
   }

   @Override
   public int getStrength() {
      return 100 + wifiManager.getConnectionInfo().getRssi();
   }
}

I actually tried to add it on the source you published on github, but couldn't get it to run >.<. Thanks for adding it :)

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

There is no reason I can see to not have one at $49 dollars.

Ematic 7" Pro Series Google Android 4.0

http://www.ebay.com/itm/Ematic-7-Pro-Se ... 0990768817

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Here is a much better version for $49 bucks:

HyperTAB 7" Google Android 4.2 Tablet 4GB Dual Core A23 Jelly Bean PC

http://www.ebay.com/itm/HyperTAB-7-Goog ... 1088431544

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Re: Android Multiwii remote Bluetooth/Wifi

Post by ezio »

Theelectronicguy wrote:I actually tried to add it on the source you published on github, but couldn't get it to run >.<. Thanks for adding it :)

I have sent you a message.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

copterrichie wrote:Here is a much better version for $49 bucks:

HyperTAB 7" Google Android 4.2 Tablet 4GB Dual Core A23 Jelly Bean PC

http://www.ebay.com/itm/HyperTAB-7-Goog ... 1088431544


I purchased this one for testing and tried it with the EZ-GUI, works perfectly with the exception, No wifi support and some screen formatting.

Image

Image

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

Just a little heads up on the HyperTAB 7" Google Android, the one that I received, the battery seems to be small. Meaning the battery life is short about an hour to hour 1/2 with wifi enabled. There is a reason why they are only 50 bucks. ;)

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Re: Android Multiwii remote Bluetooth/Wifi

Post by ezio »

WiFi support has been added to EZ-GUI and it has been released as Beta version.
To try it go here:
https://plus.google.com/communities/117 ... 6739210133

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by copterrichie »

I have not personally used this one but reading over the specs, makes it a better candidate to use with MWC because it is 5 volts verse 3.3 volts as the previous one posted.


Serial to Embedded WiFi module,TTL UART, support TCP/UDP protocol + Antenna

http://www.ebay.com/itm/Serial-to-Embed ... 1322563574

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Re: Android Multiwii remote Bluetooth/Wifi

Post by ezio »

Theelectronicguy wrote:Gladly:

Code: Select all

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Handler;

public class Wifi extends Communication {
   private Activity myActivity;
   private CommunicationInterface wifiInterface;
   private Socket mySocket;
   private BufferedInputStream input;
   private OutputStream out;
   private WifiManager wifiManager;
   
   public Wifi(Activity myActivity) {
      super(myActivity.getApplicationContext());
      this.myActivity = myActivity;
      wifiInterface = (CommunicationInterface) this.myActivity;
      wifiManager = (WifiManager) myActivity.getSystemService(Context.WIFI_SERVICE);
      Enable();
   }
   
   //TODO connectToNetwork not working
   public boolean connectToNetwork(String ssid, String key) {
      WifiConfiguration wifiConfig = new WifiConfiguration();
      wifiConfig.SSID = String.format("\"%s\"", ssid);
      wifiConfig.preSharedKey = String.format("\"%s\"", key);
      wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
      
      int netId;
      WifiConfiguration currentConf = getConfiguredNetwork(wifiManager, ssid);
      if(currentConf == null)
         netId = wifiManager.addNetwork(wifiConfig);
      else
         netId = currentConf.networkId;
      
      if(wifiManager.getConnectionInfo().getSSID().equalsIgnoreCase(ssid))
         return true;
      
      wifiManager.disconnect();
      wifiManager.enableNetwork(netId, true);
      return wifiManager.reconnect();
   }
   private WifiConfiguration getConfiguredNetwork(WifiManager wifiManager, String ssid) {
      List<WifiConfiguration> myList = wifiManager.getConfiguredNetworks();
      for(WifiConfiguration current: myList) {
         if(current.SSID.equalsIgnoreCase(ssid))
            return current;
      }
      return null;
   }
   
   public void setState(String state) {
      this.state = state;
   }
   public String getConnectionState() {
      return state;
   }
   
   public boolean Write(byte[] data) {
      super.Write(data);
      try {
         out.write(data);
         Connected = true;
      } catch (IOException e) {
         e.printStackTrace();
         Connected = false;
      }
      return Connected;
   }
   
   Handler handler = new Handler();
   Runnable inputRunnable = new Runnable() {
      @Override
      public void run() {   
         if(input != null)
            try {
         while(input.available() > 0)
            wifiInterface.onReceive((byte) (input.read() & 0xFF));
            }
         catch(IOException e) {
            e.printStackTrace();
         }
         if(Connected)
         handler.postDelayed(this, 10);
      }
   };   
   
   public boolean flush() {
      try {
         out.flush();
         return true;
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
   }
   
   @Override
   public void Enable() {
   if(!wifiManager.isWifiEnabled())
   wifiManager.setWifiEnabled(true);
   }

   @Override
   public boolean Connect(String ip, int port) {
      address = ip + ":" + port;
      setState(STATE_CONNECTING);
      try {
          mySocket = new Socket(ip, port);
          mySocket.setKeepAlive(true);
          input = new BufferedInputStream(mySocket.getInputStream());
            out = mySocket.getOutputStream();
            handler.post(inputRunnable);
            setState(STATE_CONNECTED);
            Connected = true;
         }
         catch(Exception e) {
            e.printStackTrace();
            setState(STATE_NONE);
            setState(e.getMessage());
            Connected = false;
         }
      return Connected;
   }

   @Override
   public boolean dataAvailable() {
      try {
         if(input == null)
            return false;
         return input.available() != 0;
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
   }

   @Override
   public byte Read() {
      return 0;
   }

   @Override
   public void Close() {
      Connected = false;
      if(mySocket != null && mySocket.isConnected())
         try {
            mySocket.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
   }

   @Override
   public void Disable() {
   if(wifiManager.isWifiEnabled())      
   wifiManager.setWifiEnabled(false);
   }

   @Override
   public int getStrength() {
      return 100 + wifiManager.getConnectionInfo().getRssi();
   }
}

I actually tried to add it on the source you published on github, but couldn't get it to run >.<. Thanks for adding it :)


BTW
You should use this code to get signal strength in percent:

Code: Select all

public int getRSSI() {
      try {
         int rssi = wifi.getConnectionInfo().getRssi();
         int level = WifiManager.calculateSignalLevel(rssi, 10);
         int percentage = (int) ((level / 10.0) * 100);
         Log.d("aaa", "WiFI RSSI=" + String.valueOf(percentage));
         return percentage;

      } catch (Exception e) {
         return 0;
      }
   }

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Thanks for the heads up. Actually, I was trying to get serial data from the copter but I always get -22 returned (234 in int) in the Read() method. Do you have any idea why?

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Nevermind, it seems I wasn't sending the request correctly. It's working now.

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Re: Android Multiwii remote Bluetooth/Wifi

Post by ezio »

What is the default IP and port number in wifi to serial modules?

mustafacey
Posts: 1
Joined: Mon Mar 24, 2014 12:46 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by mustafacey »

I can not download the apk file from server. Could you please send me or upload another location?

Theelectronicguy
Posts: 49
Joined: Tue Dec 06, 2011 3:12 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by Theelectronicguy »

Updated link. Also added the github project page.

mearshen
Posts: 5
Joined: Sun Sep 07, 2014 9:38 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by mearshen »

hello,i add a wifi module to the control board so that i can use wifi. But when i open the app and connect the control board,it displays connected,but the motors don't move when i touch the screen. Do you know what's wrong?

bajvulcho
Posts: 1
Joined: Sun Feb 08, 2015 9:56 pm

Re: Android Multiwii remote Bluetooth/Wifi

Post by bajvulcho »

Hello! I am building a quadcopter. I am using Arduino Mega ADK, MPU 9250(gyro + acc + mag), baro BMP180. I configured the MultWii config.h and uploaded it. I have a Bluetooth HC-06 with 115200 baud rate set on it. I am connecting to it with Multiwii Remote but the board does not receive any commands. BT Module is connected on pins 0,1. What am i doing wrong?

jaked0120@gmail.com
Posts: 2
Joined: Sat Mar 14, 2015 2:48 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by jaked0120@gmail.com »

I'm guessing the tx on the HC-06 module goes to the rx on the arduino and vise-versa?

jaked0120@gmail.com
Posts: 2
Joined: Sat Mar 14, 2015 2:48 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by jaked0120@gmail.com »

When I connect to the Bluetooth module, the app gives me this "No data received"

User avatar
ezio
Posts: 827
Joined: Sun Apr 01, 2012 11:03 pm
Location: Paris
Contact:

Post by ezio »


rbanga01
Posts: 3
Joined: Wed Apr 15, 2015 8:22 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by rbanga01 »

Do I need a receiver if I'm using a BT module(HC-06) with the MultiWii v2.5 Flight Controller to control via this BT remote?

rbanga01
Posts: 3
Joined: Wed Apr 15, 2015 8:22 am

Re: Android Multiwii remote Bluetooth/Wifi

Post by rbanga01 »

Could anyone please help me! I'm not able to implement flight via this remote. I'm using a MultiWii Flight v2.5 Controller and a HC-05 Bluetooth module for communication between Android and MultiWii

Post Reply