Project tutorial
Create Mobile Apps for Android and iOS Connecting to the MKR

Create Mobile Apps for Android and iOS Connecting to the MKR © GPL3+

This simple project shows how to make an app that controls an Arduino MKR1000 board with Evothings Studio by communicating over the TCP.

  • 5,870 views
  • 4 comments
  • 16 respects

Components and supplies

Apps and online services

About this project

MKR1000 is a powerful board that combines the functionality of the Zero and the Wi-Fi Shield. It’s based on the Atmel ATSAMW25 that is part of the SmartConnect family of Atmel Wireless devices, specifically designed for IoT. It offers the ideal solution for makers seeking to add Wi-Fi connectivity with minimal previous experience in networking.

Evothings Studio is a mobile application enabler for the industrial Internet of Things (IoT). It’s really interesting technology to simplify the development process and fun to make apps in using Web Technologies, instead of native languages, is also great to connect faster our cloud services platform to the mobile Apps.

This simple project shows how to make an app that controls an Arduino MKR1000 board with Evothings Studio, by communicating over the TCP networking protocol.

What you Need

This example runs in Evothings Viewer on Android or iOS.

You need to run this example in Evothings Viewer. Alternatively, you can build a Cordova application if you wish to distribute the app. You then need to include the Cordova plugin org.chromium.socket. Read more in the documentation of the Evothings Viewer.

For the Arduino setup, you need:

  • MKR1000
  • LED
  • Resistor 100 Ohm

How to Get up and Running

Follow these steps to get started with this example:

1.       Wire up the Arduino MKR1000 board.

2.       Open arduinowifi.ino in the Arduino development tool.

3.       Enter the name and password for your WiFi network in arduinowifi.ino.

// Your network SSID (network name).
// TODO: Enter the name of your wifi network here.
char ssid[] = "wifi name";
// Your network password.
// TODO: Enter the password of your wifi network here.
char pass[] = "wifi password";

4.       Upload the program to the MKR1000.

5.       Open the Serial Monitor Window and look for the IP address of the MKR1000. (If the IP address is not displayed, check your WiFi network name and password.)

6.       Run Evothings Workbench on your desktop/laptop computer. (If you not install Evothings Workbench, to download click here.)

7.       Run Evothings Viewer on an iOS or Android mobile device and connect to the Workbench.

8.       Launch the example "Arduino LED On/Off TCP" from the Workbench window.

9.       Enter the IP address of the Arduino MKR1000 in the app.

10.      If everything works, you should now be able to turn the LED on and off using the buttons in the app. If it does not work, check your wiring and that the right pins are used (remember pin 2 is used for output).

Code

arduinowifi.inoArduino
/*
  Arduino WiFi Script Server

  Created October 20, 2013
  Mikael Kindborg, Evothings AB

  TCP socket server that accept commands for basic scripting
  of the Arduino board.

  This example is written for a network using WPA encryption.
  For WEP or WPA, change the Wifi.begin() call accordingly.

  The API consists of the requests listed below.

  Requests and responses end with a new line.

  The input parameter n is a pin number ranging from 2 to 9.

  The response is always a 4-character string with a
  hex encoded number ranging from 0 to FFFF.

  Possible response string values:

  H (result from digital read)
  L (result from digital read)
  0 to 1023 - Analog value (result from analog read)

  Set pin mode to OUTPUT for pin n: On
  Response: None
  Example: O5
  Note: O is upper case letter o, not digit zero (0).

  Set pin mode to INPUT for pin n: In
  Response: None
  Example: I5

  Write LOW to pin n: Ln
  Response: None
  Example: L5

  Write HIGH to pin n: Hn
  Response: None
  Example: H5

  READ pin n: Rn
  Response: "H" (HIGH) or "L" (LOW)
  Example: R5 -> H

  ANALOG read pin n: An
  Response: int value as string (range "0" to "1023")
  Example: A5 -> 42
*/

// Include files.
#include <SPI.h>
#include <WiFi101.h>

// Your network SSID (network name).
// TODO: Enter the name of your wifi network here.
char ssid[] = "wifi name";

// Your network password.
// TODO: Enter the password of your wifi network here.
char pass[] = "wifi password";

// Your network key Index number (needed only for WEP).
int keyIndex = 0;

// Server status flag.
int status = WL_IDLE_STATUS;

// Create WiFi server listening on the given port.
WiFiServer server(3300);

void setup() {
  // Start serial communication with the given baud rate.
  // NOTE: Remember to set the baud rate in the Serial
  // monitor to the same value.
  Serial.begin(9600);

  // Wait for serial port to connect. Needed for Leonardo only
  while (!Serial) {
    ;
  }

  // Check for the presence of the WiFi shield.
  if (WiFi.status() == WL_NO_SHIELD) {
    // If no shield, print message and exit setup.
    Serial.println("WiFi shield not present");
    status = WL_NO_SHIELD;
    return;
  }

  // Connect to Wifi network.
  while (status != WL_CONNECTED) {
    Serial.print("Connecting to Network named: ");
    Serial.println(ssid);

    // Connect to WPA/WPA2 network. Update this line if
    // using open or WEP network.
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // Start the server.
  server.begin();

  // Print WiFi status.
  printWifiStatus();
}

void loop() {
  // Check that we are connected.
  if (status != WL_CONNECTED) {
    return;
  }

  // Listen for incoming client requests.
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("Client connected");

  String request = readRequest(&client);
  executeRequest(&client, &request);

  // Close the connection.
  //client.stop();

  Serial.println("Client disonnected");
}

// Read the request line. The string from the JavaScript client ends with a newline.
String readRequest(WiFiClient* client) {
  String request = "";

  // Loop while the client is connected.
  while (client->connected()) {
    // Read available bytes.
    while (client->available()) {
      // Read a byte.
      char c = client->read();

      // Print the value (for debugging).
      Serial.write(c);

      // Exit loop if end of line.
      if ('\n' == c) {
        return request;
      }

      // Add byte to request line.
      request += c;
    }
  }
  return request;
}

void executeRequest(WiFiClient* client, String* request) {
  char command = readCommand(request);
  int n = readParam(request);
  if ('O' == command) {
    pinMode(n, OUTPUT);
  } else if ('I' == command) {
    pinMode(n, INPUT);
  } else if ('L' == command) {
    digitalWrite(n, LOW);
  } else if ('H' == command) {
    digitalWrite(n, HIGH);
  } else if ('R' == command) {
    sendResponse(client, String(digitalRead(n)));
  } else if ('A' == command) {
    sendResponse(client, String(analogRead(n)));
  }
}

// Read the command from the request string.
char readCommand(String* request) {
  String commandString = request->substring(0, 1);
  return commandString.charAt(0);
}

// Read the parameter from the request string.
int readParam(String* request) {
  // This handles a hex digit 0 to F (0 to 15).
  char buffer[2];
  buffer[0] = request->charAt(1);
  buffer[1] = 0;
  return (int) strtol(buffer, NULL, 16);
}

void sendResponse(WiFiClient* client, String response) {
  // Send response to client.
  client->println(response);

  // Debug print.
  Serial.println("sendResponse:");
  Serial.println(response);
}

void printWifiStatus() {
  Serial.println("WiFi status");

  // Print network name.
  Serial.print("  SSID: ");
  Serial.println(WiFi.SSID());

  // Print WiFi shield IP address.
  IPAddress ip = WiFi.localIP();
  Serial.print("  IP Address: ");
  Serial.println(ip);

  // Print the signal strength.
  long rssi = WiFi.RSSI();
  Serial.print("  Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Schematics

Sketch using the MKR1000
Sketch

Comments

Similar projects you might like

Room temperature sensor with display on Mobile

Project showcase by Kaustubh Agarwal

  • 23,926 views
  • 8 comments
  • 54 respects

Program MKR Over-the-Air + Goodies: Voice Control, etc.

Project tutorial by Team Flower Platform

  • 10,136 views
  • 7 comments
  • 37 respects

Blinking LEDs on Arduino from iOS and Android

Project tutorial by Alex Jonsson

  • 14,192 views
  • 8 comments
  • 46 respects

Basic Arduino + JavaScript (Workshop)

by Alex Glow

  • 11,873 views
  • 1 comment
  • 27 respects

Connecting "stuff" via Bluetooth / Android / Arduino

Project tutorial by MJRoBot

  • 12,720 views
  • 20 comments
  • 32 respects

Morse Code Translator

Project showcase by Team ONYX

  • 11,614 views
  • 2 comments
  • 16 respects
Add projectSign up / Login