Communication Arduino-Android

Hi, i am trying to make an app working with my arduino but i don’t know how. I am running a project about home automation and i have completed my code which includes a script. Does anybody know how to translate this script to the android language? Below is the code so that you can see it for yourself. Any advice about the code is accepted! :slight_smile: :slight_smile:

Smart_Home.ino (48.7 KB)

There are no scripts in C/C++, which is the language that Arduino uses.

gibsonlp: Does anybody know how to translate this script to the android language?

The programming language for Anrdoid app is Java.

well from what I gather your code returns a huge web page on which you can trigger actions.

you wait for clients to connect, when someone clicks somewhere, you parse the input through extract_commands() and if all is fine you trigger the right command by calling commands(extracted_data) which act on some global variables such as the temperature

then you go back to the begining of the loop and in update_sensors() call, you read your sensors and take necessary actions as defined by the global variables

The way the web page is programmed is indeed by having a unload calling get_ajax and a massive request.onreadystatechange function which helps build a GET request by setting cmd1 and cmd2 to relevant values

I suppose you want to transform what the script do in the web page into a native app on Android?

well then I would suggest you start from scratch, learn how to send a GET request in JAVA to a server, build a UI in Android that build a relevant GET request which will be routed to your Arduino.

J-M-L: well from what I gather your code returns a huge web page on which you can trigger actions.

you wait for clients to connect, when someone clicks somewhere, you parse the input through extract_commands() and if all is fine you trigger the right command by calling commands(extracted_data) which act on some global variables such as the temperature

then you go back to the begining of the loop and in update_sensors() call, you read your sensors and take necessary actions as defined by the global variables

The way the web page is programmed is indeed by having a unload calling get_ajax and a massive request.onreadystatechange function which helps build a GET request by setting cmd1 and cmd2 to relevant values

I suppose you want to transform what the script do in the web page into a native app on Android?

well then I would suggest you start from scratch, learn how to send a GET request in JAVA to a server, build a UI in Android that build a relevant GET request which will be routed to your Arduino.

Exactly! I just want to use my android to give orders to my arduino. But i have no experience on it, that's why i need help. Could you give me more info?

In straight JAVA You would do something like this.

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class HttpURLConnectionExample {

    private final String USER_AGENT = "Mozilla/6.0";

    public static void main(String[] args) throws Exception {

        HttpURLConnectionExample http = new HttpURLConnectionExample();

        System.out.println("Testing 1 - Send Http GET request");
        http.sendGet();

    }

    // HTTP GET request
    private void sendGet() throws Exception {

        String url = "http://www.google.com/search?q=gibsonlp";
                // in your case you need to put a string that goes to your Arduino.

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

    }

}

and in the sendGet method, you need to craft the proper URL. (here I hardcoded a lookup of your pseudo in google)

to craft something like this in Android, you need to learn how to create an Android App, Android user interface, attach actions to the user interface which will help build the GET call that you will pass to the connection..

if you don't know Java and don't know how to program for android, well you have a few interesting discovery nights ahead

good luck!

an alternative is to see what BLYNK can do for you. You won't have a native UI totally crafted by you in your Android phone, but you will already be able to do a lot, and in a better way than the web interface - and that's pretty easy.

i was thinking of phonegap, did anybody use this program in order to create an android app?

search MIT INVENTOR videos on Youtube..its an easy userfreindly software that connect Android with Arduino in easy way..

yes but could it be used with a webserver that has ajax-javascript?

May be following link helpful for you: Convert App Inventor to Java https://www.youtube.com/watch?v=FqNTc4DVYW8

I used Cordova (branch of Phonegap) to build an Android app. This app used to call REST api hosted in a RaspberryPi. To build the Android app I used AngularJs, CSS and HTML.

When I moved to Arduino I used HC-06 Bluetooth connected to Arduino and Android App communicated via Bluetooth instead of Wifi.

I assume you can host simple REST service in Arduino (never tried it) and in your Android app just call this REST service.