programming for the internet of things
TRANSCRIPT
![Page 1: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/1.jpg)
Programming for the
Internet of ThingsPeter Hoddie@phoddie@kinoma
#ieeeJanuary 9, 2016
![Page 2: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/2.jpg)
@kinoma
Overview• Looking ahead five years, based on what is happening today.• What does the code we program need to do?• How will we be writing that code?• Who will be doing the programming?
![Page 3: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/3.jpg)
@kinoma
![Page 4: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/4.jpg)
@kinoma
Consumer IoT
![Page 5: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/5.jpg)
@kinoma
Consumer expectations• These things are better than their predecessors
• Do more• More configurable• More reliable
• These things can work together with other things to do even more useful stuff
![Page 6: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/6.jpg)
@kinoma
Two kinds of standards• To underpin markets
where massive investment needed• DVD (manufacturing
factories)• 5G (cell towers)• Wi-Fi (chips)• MPEG compression
(silicon, software, toolchain)
• To formalize (and clean-up) existing practice• HTTP• JSON• JavaScript• HTML• MPEG-4 file format
![Page 7: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/7.jpg)
@kinoma
Standards in IoT• Industry impulse is to create
a new standard• Define boundaries of
new product categories• Ensure interoperability
![Page 8: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/8.jpg)
@kinoma
Everyone is trying
![Page 9: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/9.jpg)
@kinoma
Too much. Too soon.• It isn’t obvious what we want to do in the big picture
• Trying to create “underpinning” standards• Not necessary for this market – investment level is
already unbelievably high• Leading to bad standards • Too much functionality• Allow for too many possible futures• Too big and complex to be practical
![Page 10: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/10.jpg)
@kinoma
IoT needs time to evolve• Experiments to discover what is possible
• Experience to know what works in the real world
• Too early for new standards• Plenty of existing standards to build on
![Page 11: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/11.jpg)
• Many suggest sending everything through the cloud• Cloud acts as intermediary between devices and services
• Problems• Too much data• Internet isn’t always available• Who’s cloud?• Security – moving data around unnecessarily @kinoma
The cloud
![Page 12: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/12.jpg)
• Devices must be able to communicate directly with
• Any cloud service
• Any other IoT device
• Any mobile app
@kinoma
Direct
![Page 13: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/13.jpg)
@kinoma
![Page 14: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/14.jpg)
@kinoma
![Page 15: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/15.jpg)
The Killer App for IoT is the same as the Killer App for PC and mobile:
The ability to run the apps you choose.
@kinoma
No single killer app
![Page 16: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/16.jpg)
@kinoma
User-installed apps on IoT devices?
• Devices aren’t powerful enough. • Too difficult for anyone but the most experienced
embedded programmers.• It won’t be reliable.• A security nightmare.
Insanity!
![Page 17: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/17.jpg)
Let’s explore the insanity
![Page 18: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/18.jpg)
@kinoma
Let’s use a standard to help• JavaScript is the closest thing we have to a
universal programming language
Web (Desktop)
Mobile (Apps and Web)
Server
Embedded
![Page 19: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/19.jpg)
@kinoma
High level programming languages on embedded systems
Relatedly, writing software to control drones, vending machines, and dishwashers has become as easy as spinning up a website. Fast, efficient processors … are turning JavaScript into a popular embedded programming language—unthinkable less than a decade ago.
![Page 20: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/20.jpg)
JavaScript for IoT
@kinoma
• JSON built in – de facto data format of the web• Exceptionally portable – OS independent• Helps eliminate memory leaks so devices can run
for a very long time – garbage collector
![Page 21: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/21.jpg)
Secure foundation
@kinoma
• Sandbox• Core language provides no access to network, files, hardware, screen,
audio, etc.• Scripts can only see and do what the system designer chooses to
provide• Secure – many classes of security flaws in native code are nonexistent
• Uninitialized memory• Stack overflow• Buffer overruns• Mal-formed data injection
![Page 22: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/22.jpg)
First truly major enhancements to the language. ES6 contains more than 400 individual changes including: • Classes – familiar tool for inheritance• Promises – clean, consistent asynchronous operation• Modules – reusable code libraries• ArrayBuffer – work with binary data
JavaScript 6th Edition – Features for IoT
@kinoma
![Page 23: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/23.jpg)
@kinoma
How small a system can run JavaScript?• 512 KB RAM• 200 MHz ARM Cortex M4• Wi-Fi b/g• Most complete ES6 implementation anywhere• Open source
![Page 24: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/24.jpg)
What does JavaScript forIoT devices look like?
![Page 25: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/25.jpg)
@kinoma
HTTP Client
let HTTPClient = require("HTTPClient");let http = new HTTPClient(url);
http.onTransferComplete = function(status) {trace(`Transfer complete : ${status}\n`);
};http.onDataReady = function(buffer) {
trace(String.fromArrayBuffer(buffer));};
http.start();
![Page 26: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/26.jpg)
@kinoma
HTTP Server
let HTTPServer = require("HTTPServer");let server = new HTTPServer({port: 80});
server.onRequest = function(request) {trace(`new request: url = ${request.url}\n`);request.addHeader("Connection", "close");request.response();
};
![Page 27: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/27.jpg)
@kinoma
I2C Accelerometerlet accel = new I2C(1, 0x53);let id = accel.readChar(0x00);if (0xE5 != id)
throw new Error(`unrecognized id: ${id}`);
accel.write(0x2d, [0x08]);accel.write(0x38, [(0x01 << 6) | 0x1f]);
let status = accel.readByte(0x39);let tmp = accel.readByte(0x32);let x = (tmp << 8) | accel.readByte(0x33);tmp = accel.readByte(0x34);let y = (tmp << 8) | accel.readByte(0x35);tmp = accel.readByte(0x36);let z = (tmp << 8) | accel.readByte(0x37);
![Page 28: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/28.jpg)
@kinoma
Adding ES6 to your product• Just a few steps to get the basics working• Get XS6 from GitHub• Build it with your product
• Entirely ANSI C – likely builds as-is• All host OS dependencies in three files xs6Host.c,
xs6Platform.h, and xs6Platform.6• Update as needed for your host OS / RTOS
![Page 29: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/29.jpg)
@kinoma
Hello World
/* test.js */trace("Hello, world!\n");
![Page 30: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/30.jpg)
@kinoma
Hosting scripts in your code#include <xs.h>int main(int argc, char* argv[]) {xsCreation creation = {
128 * 1024 * 1024,/* initial chunk size */16 * 1024 * 1024, /* incremental chunk size */8 * 1024 * 1024, /* initial heap slot count */1 * 1024 * 1024, /* incremental heap slot count */4 * 1024, /* stack slot count */12 * 1024, /* key slot count */1993, /* name modulo */127 /* symbol modulo */};xsMachine* machine = xsCreateMachine(&creation, NULL,"my virtual machine",
NULL);xsBeginHost(machine);xsRunProgram(argv(1));xsEndHost(machine);xsDeleteMachine(machine);return 0;
}
![Page 31: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/31.jpg)
Reading environment variablesTo allow a script to do this trace(getenv("XS6") + "\n");
trace(getenv("XSBUG_HOST") + "\n");
xsResult = xsNewHostFunction(xs_getenv, 1);xsSet(xsGlobal, xsID("getenv"), xsResult);
void xs_getenv(xsMachine* the){ xsStringValue result = getenv(xsToString(xsArg(0))); if (result) xsResult = xsString(result);}
Implement xs_getenv in C
Add getenv function to the virtual machine
![Page 32: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/32.jpg)
Going deeper• JavaScript is also great for building the product
• App logic• Communication
• Network protocols• Hardware
@kinoma
![Page 33: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/33.jpg)
@kinoma
Why use JavaScript to build your product?• Get it working faster• Iterate incredibly fast• Leverage code and techniques
developed by other JS developers• Hardware independent; easy to re-use
in your next generation• Re-use JavaScript code with Node.js cloud
service, mobile apps, and web pages• Much easier to find JavaScript programmers
to work on your project
![Page 34: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/34.jpg)
Avoid the “100% pure” trap• It doesn’t make sense to code
everything in script• Native code is great
• Fast• Access to native functionality• Access to hardware functions• Re-use of proven, reliable code• Secure
![Page 35: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/35.jpg)
@kinoma
But, you may say
JavaScript isn’t type safe. My manager
insists….JavaScript isn’t good
for big projects. Google told me… Modules
JavaScript isn’t fast
![Page 36: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/36.jpg)
Programming is for more people than you may imagine
![Page 37: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/37.jpg)
Everyone can configure IoT devices with mobile apps
![Page 38: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/38.jpg)
IFTTT goes the next step with simple rules
![Page 39: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/39.jpg)
Visual programming is powerful,an on-ramp to “real” coding
![Page 40: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/40.jpg)
JavaScript has proven to be accessible to designers, students, and engineers
![Page 41: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/41.jpg)
@kinoma
Scriptable is scalable• Your organization can’t implement everything itself
• Interactions with other devices• Mobile experience• Interactions with cloud service
• Building partnerships directly is slow, expensive, and limited• Opening your product to Apps let’s individuals and companies
integrate your product with theirs• Brings new abilities, new customers, access to new markets
![Page 42: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/42.jpg)
@kinoma
Scriptable IoT will lead us to theright standards
• New “standard objects” for IoT to augment JavaScript built-ins • Common programming models• Modules / libraries that are common across devices• Perhaps enhancements to JavaScript for needs of IoT
![Page 43: Programming for the Internet of Things](https://reader031.vdocuments.net/reader031/viewer/2022030217/58879bd11a28ab5b1a8b6c53/html5/thumbnails/43.jpg)
@kinoma
Scriptable will realize potential of IoT• We can’t organize to connect all these
devices and services together• This is not a central design / control
problem• Organic exploration and growth• Consumers will get the magic they
expect, just as the mobile app ecosystem snapped into place