Control from Android via wifi. Controlling the machine via WiFi using ESP8266 NodeMCU


This article presents step-by-step instruction, which will help you create your own application for an Android smartphone designed to control something via Bluetooth. To demonstrate, we will analyze in detail an example of blinking an LED on Arduino using commands from a phone or tablet. As a result of following our instructions, you will learn to do this:

To control a home robot, you just need to add buttons and process their commands on the Arduino side.

What will it take?

  1. Any Arduino-compatible board
  2. Bluetooth module
  3. Device on which Android OS is installed

It is best to use HC-05 as a Bluetooth module. It is easy to buy in a Chinese online store or on eBay. The module is powered by 3.3V, but its I/O lines can handle 5V logic, allowing its UART to be connected to an Arduino.

Bluetooth module HC-05

Connecting a Bluetooth module to Arduino

So now we need to connect our Arduino with Bluetooth. If the Arduino does not have a 3.3V output, but only 5V, then you will need to install a stabilizer to reduce the power. The assignment of the HC-05 pins is easy to find on the Internet. For use, we recommend that you make a board with power lines, Rx and Tx. Connection to Arduino must be done in the following order:

  • Arduino output 3.3V or (5V through a stabilizer!) - to pin 12 of the Bluetooth module
  • Arduino GND output - to pin 13 of the Bluetooth module
  • Arduino TX output - to pin 2 of the RX Bluetooth module
  • Arduino RX output - to pin 1 of the TX Bluetooth module

After connecting, you need to check the functionality of the Bluetooth module. Connect the LED to pin 12 of the Arduino and upload the following sketch to the board:

Char incomingByte; // incoming data int LED = 12; // LED is connected to pin 12 void setup() ( Serial.begin(9600); // initializing the port pinMode(LED, OUTPUT); // Set pin 12 as output Serial.println("Press 1 to LED ON or 0 to LED OFF..."); ) void loop() ( if (Serial.available() > 0) ( //if data arrived incomingByte = Serial.read(); // read the byte if(incomingByte == "0" ) ( digitalWrite(LED, LOW); // if 1, then turn off the LED Serial.println("LED OFF. Press 1 to LED ON!"); // and print back the message ) if(incomingByte == "1") ( digitalWrite(LED, HIGH); // if 0, then turn on LED Serial.println("LED ON. Press 0 to LED OFF!"); ) ) )

AND HTC Desire with firmware cyanogen 7.1.0 RC1 (Android 2.3.4). Just in case, let me remind you that everything that will be described below only works starting from Android 2.3.4 for phones and Android 3.1 for tablets.

It is worth noting here that this USB Host Shield not entirely successful, especially in combination with Arduino Mega 2560. The first problem was that this fee The extension was made for Arduino UNO, but it differs from Mega in the positions of the SPI contacts, so I had to add jumpers (see photo). The second problem, although quite expected, was the need for external source power supply for the operation of this expansion card. USB Host Shield 2.0 from Circuits@Home is considered more successful, but it is also more expensive.

Board with interconnected SPI contacts

Setting up Arduino software

1. If not already installed, then download and install software for Arduino.
2. Download and unpack the ADK package (contains the DemoKit application). The app, firmware, and hardware folders should appear.
3. Download the CapSense library
4. Copy firmware/arduino_libs/AndroidAccessory and firmware/arduino_libs/USB_Host_Shield to /libraries/ .
5. Create a CapSense directory in /libraries/ and copy CapSense.cpp and CapSense.h from the CapSense archive into it.

Firmware download

Google kindly provides its DemoKit sketch for Arduino. All you need to do is open it from firmware/demokit/demokit.pde, compile it and upload it to the board.

Test Android application

The DemoKit package also contains the sources of the Android application to demonstrate the capabilities. Google invites us to create an Android project ourselves and assemble this application. First, we will need to install API Level 10. Then everything is simple - we create an Android project and specify the path to the app folder, in the Build Target we specify Google APIs (Platform 2.3.3 , API Level 10). We assemble the application and install it on the phone. Those who don’t want to bother with the assembly can download the ready-made APK.

Launch

We simply connect our phone to the USB Host Shield. If we did everything correctly, a request to launch the DemoKit application will appear on the screen.

The application itself contains two tabs - In (buttons, joystick and sensors) and Out (LEDs, relays and servos).

I decided that a couple of LEDs and a button were enough for the demonstration. You can see how this whole miracle works in the video.

Some code

In this example, messages transmitted via USB consist of three bytes:
The 1st byte defines a command or device group, for example LEDs - 0x2
2nd byte indicates specific device, for example green LED - 0x1
The 3rd byte contains the value transmitted to the device, for example maximum brightness - 0xff

Arduino

... /* initialization */ AndroidAccessory acc("Google, Inc.", "DemoKit", "DemoKit Arduino Board", "1.0", "http://www.android.com", "0000000012345678"); void setup() ( .... acc.powerOn(); ) void loop() ( byte msg; /* check connection */ if (acc.isConnected()) ( /* receive message from Android device */ int len = acc.read(msg, sizeof(msg), 1); if (len > 0) ( /* message for LEDs */ if (msg == 0x2) ( if (msg == 0x0) analogWrite(LED3_RED, msg) ; else if (msg == 0x1) analogWrite(LED3_GREEN, msg); else if (msg == 0x2) analogWrite(LED3_BLUE, msg); ) ) msg ​​= 0x1; b = digitalRead(BUTTON1); if (b != b1 ) ( msg = 0; msg = b ? 1: 0; /* send button state */ acc.write(msg, 3); b1 = b; ) ) )

Android

import com.android.future.usb.UsbAccessory; import com.android.future.usb.UsbManager; ... public class DemoKitActivity extends Activity implements Runnable ( private UsbManager mUsbManager; UsbAccessory mAccessory; FileInputStream mInputStream; FileOutputStream mOutputStream; ... private void openAccessory(UsbAccessory accessory) ( mFileDescriptor = mUsbManager.openAccessory(accessory); if (mFileDescriptor != null ) ( mAccessory = accessory; FileDescriptor fd = mFileDescriptor.getFileDescriptor(); mInputStream = new FileInputStream(fd); mOutputStream = new FileOutputStream(fd); Thread thread= new Thread(null, this, "AccessoryThread"); thread.start(); ) ) public void run() ( int ret = 0; byte buffer = new byte; int i; while (ret >= 0) ( // receiving incoming messages ret = mInputStream.read(buffer); i = 0; while ( i< ret) { int len = ret - i; switch (buffer[i]) { case 0x1: // сообщение от кнопки if (len >= 3) ( Message m = Message.obtain(mHandler, MESSAGE_SWITCH); m.obj = new SwitchMsg(buffer, buffer); mHandler.sendMessage(m); ) i += 3; break; ) ) ) ) // example of use - turn on the red LED at full brightness: // mActivity.sendCommand((byte)2, (byte)0, (byte)255) public void sendCommand(byte command, byte target, int value) ( byte buffer = new byte; if (value > 255) value = 255; buffer = command; buffer = target; buffer = (byte) value; if (mOutputStream != null && buffer != -1) ( try ( mOutputStream. write(buffer); ) catch (IOException e) ( ... ) ) ) )

conclusions

Opening such Google features first of all, of course, he hopes for the appearance large number active Android accessories, but far from last place is occupied by the fact that in essence we get a convenient device for interacting with various sensors, sensors and actuators. Such a device could easily become the brain of something robotic.

Also, we must not forget that Android device for Arduino can act as an expansion board that has GPS, Bluetooth, WiFi, accelerometer and much more.

Would you like to send text message from your Android smartphone to your Arduino board? This article tells you how to do it!

What you need

  • Android smartphone with support USB mode host (i.e. OTG support) - most devices running Android 3.1 and higher support this mode. Check your phone with via USB Host Diagnostics App from Play Store;
  • Arduino- any version. I will use Uno R3 ;
  • USB cable for Arduino;
  • USB OTG cable- you need it to connect the Arduino USB cable to the micro-USB port of the phone;
  • Android Studio - you need to install it. It's quite easy to do. Android Studio makes application development easier with its assumptions and code generation. This is one of best IDEs. You can also use this article as a guide to Android installation IDE.

Basic components of an Android application

IN Android application there are three main files:

MainActivity.java This is where the executable Java code is located that controls how the application will function. activity_main.xml Contains the application layout, that is, components: buttons, text display components, etc. AndroidManifest.xml Here you define when the application should run, what permissions it needs, and to what hardware he needs to gain access.

There are many more files, but they are all connected to each other using these three.

An activity can be characterized as a screen where the user interacts with the phone. Activities contain widgets such as buttons, text fields, images, etc. that help in conveying information. This tutorial will only use one activity, MainActivity, which will accept user input to send to the Arduino and also display the received text.

Layout

We will use the same layout as the USB App and Bluetooth App. It is simple and contains the minimum widgets needed to test the connection between devices.

As you can see, it contains an EditText widget to receive data from the user, buttons to start the connection, transfer data, end the connection, and clear the TextView . The received data is displayed in a TextView ( empty space under the buttons).

Here is part of the XML code. Because the code for the buttons is similar, it is not shown here. The full code can be downloaded from the link at the end of the article.







2024 gtavrl.ru.