ESP8266: Custom iOS App for LED Control
Overview
Time to complete: 10–15min; Level of difficulty: Beginner
This guide will show you how to program an ESP8266 Serial to Wi-Fi module to run a web application that will control two LEDs in response to requests over http. Then, we’ll use Xcode and Swift 2 to build a basic iOS App that will allow us to control the state of the LEDs.
We've made a video following the process described in this tutorial, if that's your preferred media for tutorials go right ahead:
Simple iOS App For Controlling The ESP8266!
Obtaining the Code
For this activity, we'll be loading a program to our ESP8266 Development Board, it's available in our Github repository:
- ESP8266 LED Control (firmware)
- iOS LED Control for ESP8266 (app)
We can open a new window in the Arduino environment and copy-paste the firmware. We'll of course need to setup the Arduino IDE to program the ESP8266. We'll also need to have Xcode 7 running on our system.
Wiring: ESP8266 LED Control
The wiring for this tutorial is fairly straight-forward. The two anodes (+) of the LEDs shoud be connected to pins D1and D2 on our ESP8266 Development Board, and the cathodes should be connected to a current-limiting resistor (330Ohm) and the resistor, in turn, should be connected to ground.
Walkthrough: ESP8266 LED Control (firmware)
The firmware on the ESP8266 needs to do two things, control the low-level hardware (LEDs) and respond to requests sent to it over http. Fortunately this can be accomplished very easily using the built-in libraries for the module. These libraries will allow us to 1) run a webserver that will listen to http requests on a specific port (80), and 2) build a web application with an API (a set of URL paths) to execute control commands requested by a client.
As with most programs, we start our application code by including a few libraries and defining constants and variables that we'll be using later in our program. Remember to change the values for the constants ssid and password so that they match your own network settings!
#include#include #include // Define the ID and password of your Wi-Fi network const char* ssid = "Network ID"; const char* password = "Password"; // Instantiate the ESP8266WebServer class, passing the argument 80 for the // port that the server will be listening. ESP8266WebServer server(80); // Define the variables with the pin values where the LEDs are connected const int led1_pin = D1; const int led2_pin = D2;
The next section of our program includes the definitions for the functions that will run when users access specific URLs of our web application. Our minimalistic web application will only respond to the following http requests:
GET /
GET /setleds
Everything other request will return a 404 (Not Found) error, which we also define in one of our functions. The first function handles all GET requests to our ESP8266 root-level path ('/'). This function simply returns a string describing how to use our web application using the send() method defined in the ESP8266WebServer class:
// User-defined function that will be called when a client accesses the root // directory path of the ESP8266 host void handleRoot() { // Simply sends an 'OK' (200) response to the client, and a plain text // string with usage. server.send(200, "text/plain", String("Hello from esp8266! Usage: navigate to") + String(" /setleds?led1=XX&led2=YY changing XX/YY to ON or OFF.")); }
The next function is the main one of our web application. It handles the requests that attempt to change the status of the LEDs. These requests will be hadnled throuth the path /setleds and the function expects that the requests include parameters specifying the desired state of the LEDs. These parameters are appended to the path using the standard URL syntax ?parameter1=value1¶meter2=value2&... We can access the parameter values with the arg() method of the ESP8266WebServer class. Based on the values (ON or OFF) defined for the parameters we expect (led1 and led2), we use digitalWrite() to change the status of the LEDs.
// User-defined function that will be called when a client accesses the /setleds // path of the ESP8266 host void handleSetLeds() { // We can read the desired status of the LEDs from the expected parameters that // should be passed in the URL. We expect two parameters "led1" and "led2". String led1_status = server.arg("led1"); String led2_status = server.arg("led2"); // Check if the URL include a change of the LED status bool url_check = false; if((led1_status == "ON")||(led1_status == "OFF")||(led2_status == "ON")||(led2_status == "OFF")) url_check = true; // It's not required to pass them both, so we check that they're exactly equal to // the strings ON or OFF by our design choice (this can be changed if you prefer // a different behavior) if (led1_status == "ON") digitalWrite(led1_pin, HIGH); else if (led1_status == "OFF") digitalWrite(led1_pin, LOW); if (led2_status == "ON") digitalWrite(led2_pin, HIGH); else if (led2_status == "OFF") digitalWrite(led2_pin, LOW); if (url_check) // If we've set the LEDs to the requested status, we have the webserver // return an "OK" (200) response. We also include the number of milliseconds // since the program started running. // Note: This number will overflow (go back to zero), after approximately 50 days. server.send(200, "text/plain", "LEDs' status changed! (" + String(millis()) + ")"); else server.send(200, "text/plain", "LEDs' status unchanged! (" + String(millis()) + ")"); }
Note that both parameters need not be specified simultaneously on a request. Once the LEDs are set the server sends an OK (200) response notifying whether the LEDs' status was changed. For this, we simply check to see if at least one of the four expected parameters were included in the request.
The next function includes our response to any request not sent to either of the URLs described above. We simply construct a message that includes information that would be helpful for debugging (in case the request included a mistake) and send it as part of our 404 (Not Found) response:
// If the client requests any other URL than the root directory or the /setled path: void handleNotFound() { // We construct a message to be returned to the client String message = "File Not Found\n\n"; // which includes what URI was requested message += "URI: "; message += server.uri(); // what method was used message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; // and what parameters were passed message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } // the response, as expected, is a "Not Found" (404) error server.send(404, "text/plain", message); }
After these three user-defined functions, we define the standard setup() and loop() functions. In setup() we initialize the pins as outputs and set their initial state to LOW. We also connect our ESP8266 to our Wi-Fi network. We start our webserver to listen for http requests. And, we associate specific URLs with the 3 functions described above:
// In the setup function we initialize the different things // that will be needed in our program, as well as set up the hardware void setup(void) { // Set the LED pins to act as digital outputs and set them to a LOW // state initially. pinMode(led1_pin, OUTPUT); pinMode(led2_pin, OUTPUT); digitalWrite(led1_pin, HIGH); digitalWrite(led2_pin, HIGH); // Start the Serial communication for debugging purposes Serial.begin(115200); // Initialize the WiFi client and try to connect to our Wi-Fi network WiFi.begin(ssid, password); Serial.println(""); // Wait for a successful connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // For debugging purposes print the network ID and the assigned IP address Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Associate the URLs with the functions that will be handling the requests server.on("/", HTTP_GET, handleRoot); server.on("/setleds", HTTP_GET, handleSetLeds); server.onNotFound(handleNotFound); // Start running the webserver server.begin(); Serial.println("HTTP server started"); }
Finally, our loop function only needs to call the handleClient() method of the ESP8266WebServer class:
// The loop function is straight-forward, simply handle any incoming requests to the // our ESP8266 host! void loop(void) { server.handleClient(); }
Testing our ESP8266 LED Control Firmware
We can go ahead and upload the code to the ESP8266 using the Arduino IDE. We can then open up a Serial Monitor window to view that the application runs as we'd expect. In addition, we can get the IP Address assigned by our wireless router (or whatever device in our network is acting as a DHCP server).
Using the IP Address we can use our computers, or any other device connected to the same network to test the proper operation of the ESP8266. In our case, the IP Address is 192.168.0.107, thus we use the browser in our laptop to navigate to:
http://192.168.0.107/setleds?led1=ON&led2=ON
And we verify that both LEDs, in fact, turn on. After doing so, we proceed with opening Xcode and starting a new project to build our iOS app.
Setting Up Our New App in Xcode 7
To write our iOS App, we're going to start a new project in Xcode 7
File → New → Project...
that is going to be a Single-View Application for iOS:
In the next configuration screen, you can set the organization name identifier to anything of your choosing (you can always use your name and "personal" for the identifier). As we'll be using the Swift language (opposed to Objective-C) and developing our app for iPhones, we make sure those options are selected from the drop-down menus. We name the project ESP8266LED:
After clicking next our project is created and we're faced with several configuration options separated in different tabs. The only changes we'll make to our app are in the General tab. Specifically, we select iPhone from the Devices drop-down menu, and Portrait from the Device Orientation options.
Note: if you're unfamiliar with Xcode 7, looking through the documentation on the Apple Developers site is a must(!). We're going to open our main storyboard in the Interface Builder, and make the changes to the View Controller so that the Size is set to one of the iPhone options, and the Orientation is set to Portrait.
Now that we have most things set up, we're ready to start adding some objects to our storyboard, and write some code to add the desired functionality to our app.
Walkthrough: ESP8266 LED Control (app)
Next, we're going to add a few objects to the View Controller's View. Ensuring that the Object library is selected, we can use the search bar at the bottom of the utilities area to find our first object, a Web View, which we'll position at the bottom of the View Controller's View. We then proceed to
Ctrl+Click
and drag to add an Outlet in our ViewController.swift
file. We name the Outlet for the Web View web, and leave the rest of the options set to their default values:
We can then proceed to add the first of four Button objects repeating the process we followed to add the Web View. However, instead of adding an Outlet, we'll add an IBAction that we'll name led1on. We'll modify the code of this IBAction in the ViewController.swift file with the function calls that will allow us to access the API of our web application running on the ESP8266.
To set led1 to ON we need to include the parameter led1=ON in the URL expected by the API (i.e., http://ip-address/setleds?). To do this we can add:
// URL paths including the ESP8266's IP address and arguments var led1on_URL = "http://192.168.0.107/setleds?led1=ON" // IBAction connected to the "LED1 ON" button @IBAction func led1on(sender: AnyObject) { // Instantiate an NSURL object using the // led1on_URL string let requestURL = NSURL(string: led1on_URL) // Instantiate an NSURLRequest object using the // requestURL NSURL let request = NSURLRequest(URL: requestURL!) // Use the webview to send the request to the // request NSURLRequest web.loadRequest(request) }
Comments, questions, or concerns? Drop us a line!.
沒有留言:
張貼留言