Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 6 ■ IOT PATTERNS: REMOTE CONTROL


//is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();


//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}


return super.onOptionsItemSelected(item);
}
}


Finally, you need to update AndroidManifest.xml under the App ➤ Manifests folder.
Your app needs to access the Internet for connecting to the MQTT broker, so you need to
add Internet permissions in AndroidManifest.xml as well. Listing 6-11 provides the code
that needs to be updated in AndroidManifest.xml.


Listing 6-11. Add App Permissions in AndroidManifest.xml



Code (Arduino)


Next, you are going to write code for connecting Arduino to the Internet using WiFi,
subscribing to an MQTT broker, and controlling the attached LED.
Start your Arduino IDE and either type the code provided here or download it from
the site and open it. All the code goes into a single source file ( *.ino ), but in order to
make it easy to understand and reuse, it has been divided into five sections.



  • External libraries

  • Internet connectivity (WiFi)

  • MQTT (subscribe)

  • Control LED

  • Standard functions

- Building Arduino Projects for the Internet of Things - free download pdf - issuhub">
Free download pdf