 Hi and welcome to our session making it easier to make things with WebAssembly and the Internet of Things There'll be two of us presenting today. My name is Jonathan Barry. I'm the CEO of Goliath an IoT platform company I'm joining my good friend Alvaro. Hello everyone. My name is Alvaro I'm a Google developer expert for IoT and also work as a software engineer at leverage working on IoT projects there You'll be hearing from Alvaro later today Now let's talk about our agenda Four major sections. We'll start with defining. What is WebAssembly or Lawsum as we use it interchangeably Then we'll move on to how what WebAssembly has moved beyond the browser and Next we'll be talking about the meat of this conversation Which is WebAssembly and IoT a great combination and the last part of the talk. We'll be showing you code All right, let's get into it So what is WebAssembly if We go to WebAssembly.org the main website for the specification. You'll see this definition I'm going to highlight a few key points. Basically WebAssembly is an instruction format like an ISA to be run in a specialized virtual machine and That means that you can take languages that can compile to WebAssembly and run it in a bad environment And the first use case is really focusing on the web hence the name and With that definition comes some goals and first the most well-known goal of WebAssembly is to be a fast and efficient because it's not just important to have a compiled target in a VM, but it needs to be able to Be efficient for let's say the browser and it needs to be more efficient than Things like JavaScript and other virtual machine technologies before it. The second goal is to be safe and that also means security and Just like the web has some basic constructs for safety and security like tab isolation WebAssembly has to support those as well and in some parts of WebAssembly they try to even increase safety and security. The third goal is being open and debuggable And that's really about enabling developers to build complicated applications with WebAssembly But also to have room to experiment and grow and evolve the specification The last goal is being part of the open web platform And that's about not just being able to execute code in a VM But to interact with his environment to interface with in this case the web platform like JavaScript APIs and browser capabilities As we're talking about browsers WebAssembly on the web has broad browser adoption It's actually been supported by every major web browser for quite some time and what's notable about WebAssembly as a Standard is that it's one of the few that got unified across the browser vendors All at once Actually Microsoft, Google, Mozilla and Apple all contributed to the original founding specification So there's something special about WebAssembly from its early beginnings Now there's a broad adoption because of those browser support and you'll find hundreds of examples on the internet I tend to bucket into four major categories and here's just some examples from those major categories So I'll go through them quickly the most common use case for using WebAssembly on the web is For games and game engines support compiling to WebAssembly And so as a game developer you can build your game using the platform of your choice Targeting mobile or desktop or console and then also get a web app Pretty much for free, which has been one of the early success stories of WebAssembly second large category is Taking legacy desktop applications that you that was written in the millions of lines of probably C++ code and porting that to the web similar to game engines and there's a success story With Autodesk and AutoCAD where they basically took their You know 30 year old application and now they have a web app and as they continue to develop AutoCAD They get that web app with all the greatest latest greatest features They didn't have to build a purpose built web app as well. It was is almost they got that web app for free The next category one that I think is the most relevant and useful role of WebAssembly at large is to take it and Apply it almost like a scalpel to improve specific areas of web applications where performance or security is critical And let's say JavaScript can't meet those those those performance needs So one example is Figma, which is a web-based visual design tool They use WebAssembly for their layout engine very computationally intensive And another example is eBay They have a QR code scanner and they need that that speed that JavaScript just couldn't give The last category I want to touch on is code reuse and that's not quite the same as porting a desktop library But actually taking let's say a module library that might be complicated And compile it to WebAssembly so you can use it in different environments There's a write-up from the Google Earth team of how they have a very sophisticated rendering engine that they wanted to reuse between the web and mobile and through WebAssembly they can compile their their desktop Library and bring it into the web and then build a this you know native Natural you know web-based application around it. So let's talk about WebAssembly beyond the browser and Now the creators of WebAssembly While they chose the name to the web I thought about the use cases that might be relevant beyond just Beyond this the browser and in fact if you go to the WebAssembly website You'll see a whole section about non-web embeddings they talk about how the same goals of WebAssembly could be applied to other environments and They did they they actually split up the spec to have a core spec Which is about the compilation target low-level primitives of the spec and then the JavaScript bindings to separate that out so that in the future other bindings can be brought in as as they become relevant and And some of these use cases are cloud computing where we have traditional virtual machines Maybe you could replace that with WebAssembly blockchain which runs third-party code on top of a blockchain stack tooling and CLIs which have pretty bespoke implementation Requirements and the thing that we're interested in the most which is the Internet things by the way whenever you see a cartoon That's blueish like this in my slides That's credit to Ling Clark over at Missile Research Which is a fantastic job taking deeply complicated specs and making them accessible to developers like ourselves and Actually WebAssembly is being adopted beyond the browser There's a handful of categories of use cases that I'll go through some of them now Your cloud is probably the most Interesting in the first to come on the scene and that's everything from Cloudflare and Flastly and even Microsoft Taking traditional server workloads and running it in a WebAssembly VM Or even extending the cloud Projects like Envoy, which is a popular proxy can be extended and create new functionality using using WebAssembly And then there's blockchain and I think it really got started when the theorem foundation Said that they're going to take the proprietary bespoke EVM the theorem VM and move it to a WebAssembly based implementation and a ton of other projects started to follow suit as well a Lot of activity in the blockchain world with WebAssembly Of course to run it outside the browser you need run times that can run beyond the browser And there's a an array of different types of runtimes with different characteristics Some optimized for for cloud based some of them for IoT. We'll go into some of those later And the last category I want to talk about is languages themselves And that's not to say taking in one language and compiling to WebAssembly and running it somewhere But actually extending the capability of the language itself. So Node Node.js for example has the ability to pull in a WebAssembly modules first-class support and add new capabilities to node without having to deal with native bindings and cross compilation challenges and by the way other languages can be Extended in the same way with WebAssembly just through a third-party extension framework Now this is all about running code and it's the VM part of WebAssembly But in all these use cases there is there needs to be some interaction with the host environment with the server serving infrastructure with the blockchain Etc. And there needs to be some interface to be defined to have that interaction And that's that's called wazi the WebAssembly system interface And it's a spec and a work stream to define what those interfaces should look like and that's a whole Talking of itself, but this is a key piece to WebAssembly story And actually a noble quote is from Solomon Hikes who is the creator of Docker And he said that if wasm and was existed prior to 2008 they probably wouldn't have to have created Docker And so There's a lot to be a lot to be lots to be done here, but it's super interesting that that's so powerful and Like I mentioned, it's a whole other topic and we don't have time for that But you can think of wazi as an API contract Between the WebAssembly runtime and wherever it's running and the environment you're trying to work at So there's a wazi core which has some of these interface defined for things like file system networking And the community is then expanding upon and proposing new use cases blockchain folks talking about smart contracts VR 3D graphics, etc And so this is one area that is under active development and actually an opportunity to to come in and be part of the story Now to the meat of what we're here to talk about WebAssembly and IoT and before we get into the details I Want to start just to find what is IoT or the Internet of Things when we talk about machines and computers with connectivity We think of smartphones Laptops and servers, but when when we say IoT we don't mean these kinds of things We mean more like these kind of things. So door locks digital assistants medical devices signage and effectively I would categorize those as traditional embedded systems and the main difference between an embedded system and maybe a general-purpose computing platform is that the embedded systems Really have constraints and we call these environments constrained environments or constrained devices That's everything from its processing power and computing capabilities To other resources on the device and just as a point of comparison the the machine You're probably watching us on has multiple cores with gigahertz of compute power where your traditional IoT device might have 64 megahertz on a single core Maybe 128 kilobytes of RAM half a mega flash It also extends to power and power usage So if it's a battery power device, it might be running two hours two or two years on a single AA Even as networking might be constrained. So only 50 kilobytes per second for upload speeds And so to build systems with IoT devices with all these constraints You actually have to have very specialized software custom networking stacks, etc. And it's requires a very specialized skill set And so whenever I talk about IoT devices and meet developers interested in the space the first question I usually get is can I run Java scripts on an Arduino or an IoT device more generally? And I think it's reasonable questions. They they come from that world and experience They have those tools and I tripped this a lot to to Atwood's law and Jeff Atwood famous software developer coined this phrase Any application that can be written in JavaScript will eventually be written in JavaScript And to me that's really about Software platform where there's an influx of new developers. They are always looking for some level of abstraction And in the IoT world, that's that's certainly the case And there's a whole bunch of folks working on Specialized implementations that can be run in a constrained environments In fact, there are multiple implementations of JavaScript Runtimes that can work on microcontroller devices and there are implementations in other languages that can run on constrained devices now the big challenge with these is besides being hard to make there's no room for code reuse and Because each language needs to support array devices all the work to make micro Python For example work on our Arduino class devices that can't be used in Jerry script the job script implementation And as you build libraries to talk to sensors and other devices those two cannot be used and reuse So a lot of us start to ask the question. Well, can we use web assembly potentially to create an abstract layer? So there's some common low-level implementation that we can all share and each language can build on top of it And if you reflect back on to the goals of what web assembly in general, they play really well to IoT devices Everybody's are constrained devices. So what was simply being efficient really helps out here Safety and security. That's a critical part of some of these mission critical devices being open and debuggable would allow us to create the complicated IoT software, but also extend it to all the different use cases that might come up And really the only difference is not just supporting the web ecosystem, but also the hardware ecosystem and in fact, that's what's being done today and It's not just theory. It's it's really something you can grab it with your own projects And actually the rest of this talk will be talking through how you can start using web assembly with IoT devices From here, I'll hand it over to Alroad to give us all the details Hello everyone. So I'm here to show you a quick little demo of using web assembly To run multiple high-level languages on an IoT device. So the approach that we are going to show here is more like an Extentual approach. So as we are going to see we are not like fully replacing all the like embedded system development with web assembly because the devices still have to provide like device drivers networking and memory management So we are going to like handle most of the logic of the system using web assembly But we still need to implement some things on the device itself. So this is how it's going to work So we are going to get like three languages in this case tiny go assembly script and rust we are going to compile to assembly script and The device that we chose is an ESP32 and it is providing an OTA interface that basically we can post web assembly files to it It is going to save on the device internal flash and then every time the device reboots It's going to like run that web assembly file using the web assembly runtime. In this case we are using the UASM3 library and then for that runtime into work and provide the interfaces and Access to the underlying things on the ESP32 We need to implement a bridge to have access to GPIO memory and Wi-Fi connections So we are going to show that how it works But first let's go through the demo and I'm going to show the code and sending some code to the device itself Cool. So here I have my device already running assembly script and also it's like blinking in an LED here having access to the device pins and Connecting to connected to Wi-Fi This here is the assembly script code. It looks a lot like an Arduino code, but in this case, of course, it's written in In a like flavor of type script But assembly script is actually like a smaller subset because it compiles with this like more low-level thing Which is a web assembly But the code basically does have some functions to connect to Wi-Fi check if it is connected or not Connect to a given SSID password. We try print the local IP address We have like a setup function that set up those pins to output so I can toggle the LED on and off and we have our like Normal loop function that we usually have on our Some of our Arduino code and prototypes to do something on the loop in this case It's just blinking in the LED and printing some messages So we are going to compile the assembly script code here. Let me enter the folder here I'm gonna delete the old code and then I'm gonna run the npm script here that uses the assembly script Commands to compile the code that we are seeing here. Oh Actually to show that it's different. Let's change the message here like a low open source summit Europe Let's compile So let's see the Web assembly file size. So in this case for the assembly script Code compile is just like four kilobytes So it's pretty small if you think about because when you have to do like a full TA on an ESP 32 device For example, it usually takes like one megabyte for a full code. So let's update the device So we have like a HTTP interface on it. So we can post some Post the file to it and it's also using MDNS. So we don't need to type in the IP address So I'm gonna call that file and after the device we seize that file is going to reboot. So yeah, it's rebooting now And yeah, it's now running the new code. So it's connecting to Wi-Fi getting the IP address and Printing out the new message and let's do that with another language. Let's get now tiny go And let me open the tiny go code here So it's the same logic. We just written the same code, but in different language. This case is go So it's connecting to Wi-Fi set up the GPIOs and our loop function just checks the Wi-Fi Connection and prints the local IP address, but in this case is hello, tiny girl So I'm gonna remove the old file And let's run a bit script here. Just using tiny go to compile it to web assembly And in this case the web assembly file is much bigger probably because the tiny go compiler is not as optimized for the You web assembly world, but it's still like super small. It's just like eight kilobytes. So let's send the OTA So the device Same thing the device is gonna receive that file And after it saves it's gonna reboot running the new code Okay, and rebooting hello from tiny go and it's doing the same thing connecting to Wi-Fi Getting the IP address in such a sense. And now last one. Let's do for rust Let me delete the old file Let's run my boot script here In this case for rust the actual compile code is super small like the press compilers super optimize it So it's like a really small code. So it's just one kilobyte. So we are going to OTA that to the device again Let me show the rust code here is the same thing But yeah, I'm connecting to Wi-Fi and running the loop as we presented before and yeah, it's now running rust Cool so another cool thing about web assembly that we talked before is that it opens The possibilities to making like some debugging or even simulation tools Because it's more like a standard way of doing things and for example, we can actually Implement the same apis that the device is implemented on the web and then we can run those same web assembly files To be simulated on the web. So let me show that to you So I'm going to start my server here And there you go. This is the the same This is the web interface that is simulating Uh, the same apis that is provided by the device in this case is running the assembly script code is connecting to the Wi-Fi network Uh, and I mean I'm providing apis for serial console in this case is just bringing to Element here on the dawn And for example, I can simulate that the ip address is different For example, the answer with 10 and then I can change to run rust code And then it's going to get like this simulated ip address But it's too connected to the same wi-fi interface and I can test like the tiny go code Uh, etc. So, yeah, that's a nice thing. So maybe we can have like some debugging tools For that so we can simulate the actual hardware like blinking the led or maybe connecting to other things If we build those interfaces Cool. So back to the slides here. Let's show how this works under the hood So basically you need to build that bridge between the web assembly world and the iot and the embedded system world So we need to implement that glue to make both Work together. So right now there's no standard way to to build that yet So we had to define our own contract to make that work So how we define an interface or contract to make that work So first we need to define which functions are going to be implemented by the hardware side Those functions are known as the sporting functions So basically for example, if I want to make gpio available or wi-fi interface available I need to define those methods That are going to be available for the web assembly world to use And those functions are going to be implemented in cnc plus plus because we are talking about the Hosted the embedded system side and for the high level language like we presented with rust tiny go An assembly script those functions are going to be imported or external functions So basically they're just going to be used by those high level language. They don't need like extra Implementation although we are going to talk about some complex types handling But most of the work is on the embedded system side And the cool thing is after we define those interfaces and implement them on the hardware side Other languages that have targets for web assembly can be used And use those external functions later So for example, we didn't pretend here c plus plus but c plus plus also can be compelled to web assembly and c plus plus Can also use those functions In this scenario So this is our pseudo contract. I'm just showing this to to give an idea think of that as a protocol buffer type of definition Maybe this can generate some code in this case not doing but it's just to present the whole idea on how We define the contract to make those two work So, uh, basically they find three modules. The first one is just the Arduino module They have like the milis function that returns kind of the uptime of the system A delay function that receives some milliseconds to like sleep for a while Or just stop a bit pin configuration gpio configuration toggle those pins And also, uh, I added a function to return like maybe an internal LED configuration on a pin this did the full pin to to be toggle here Uh, we have a serial interface and the wi-fi interface that we can have like the status connect wi-fi and get the local ip And as you can see here and also as a reminder, web assembly is a low level, uh thing So they only provide you like basic types. So we only have numbers and pointers So for example for the wi-fi connection function, we need to To pass strings for the ss id and password, but we don't have that So we have to pass like a pointer to a byte buffer and that buffer Is filled with like a given size and the password is the same thing. We're passing a pointer and the size of that buffer and for the local wi-fi Although it returns a string. We don't have a way to return a string by itself We just pass a pointer that is a buffer that's going to be filled out by the host OS like by the s peter too In this case and then on the the high level language. We can have access to that I'm going to show how this is implemented on both sides. So this is uh, the definitions of those functions on the uosme 3 World or in the s peter two side in this case So we are just declaring here the modules like the Arduino modules the functions And also this library have this cool notation that you define Like the return method of those functions and also the parameters So for example the millisecond functions here Returns a number and they're have no parameters The delay function returns void doesn't return anything But receives a number as a parameter and we have more complex examples like the wi-fi connect Function that doesn't return anything but receives a pointer and a number a point in the number Which is the ssid and password pointers that we are going to pass through the strings later And for the actual implementation of those functions Here's the basic example of the system of time or the millis Function we just declared the return type Which is a number and then we call the Millis function for the Arduino framework for example in this case here For the gpio implementation We also have a way to get like parameters that are coming from the The high level language or the thing that was compiled to have assembly So in this case we're receiving two parameters the pin number and the value and we just call the digital write function On the Arduino framework and as we are not returning anything we just call the functions of success And on the high level language in this case is assembly script we just declare their functions They are coming from the host thing that this is going to run. So in this case i'm just Uh getting uh importing those functions that are already implemented on the device itself So for the assembly script world those functions already exist. I just use them But for more complex Examples like the wi-fi connection that we need to pass strings. Like I said, we don't have strings natively so in this case the arguments are going to be like Points pointers for like memory Spaces on that device. So we receive like the pointer to the ssid the size of the ssid Pointer to the password and the size of the password and then we convert that to in the string And then we call the wi-fi function on the Arduino framework to connect to wi-fi And then again, this function doesn't return anything. We just call success And on the high level language side again showing assembly script Although assembly script supports the strings as we talk, there's no support for that Only wasmy word. So we need to do the conversion between the strings and the byte buffers So in this case on the assembly script, I need to convert to utf 8 and code into a byte array Get the length and then pass those pointers to the underlying Wasmy word here and the same thing for the local wi-fi in this case the host os is returning in the string So we need to like allocate some memory pass that pointer to the host os And then that host os is going to fill out with the local wi-fi in this case and then we convert back to in the string So to summarize we need to define an interface and a contract with those methods that are going to be Used on our use case. We need to implement that contract on the host. So most of the work Right now is to the to implement that on the host system and the embedded system And we need to always like remember that we need to handle like those complex types And we need to do some some allocation and memory management because web assembly only defined basic types So we only have numbers end pointers So yeah, I mean what is the future for web assembly and iot the thing that I just presented here and the not so good news is Someone has to build those bridges. So we as a community need to define more standard ways to like bridge Make bridges between web assembly and iot and as we talked before maybe was is the solution But right now wasi is defining more things for more common use cases like file system access Um crypto apis and those kind of things But for the iot world we need more than that. So we need access to gpio's networking that can be wi-fi, internet, ble or other types of hardware access And also for example for iot. Maybe we need a Thank for power management, which is really specific for iot and a lot of use cases need that So yeah, we need to define those standard ways to access that And the cool thing is that we have some really good rtos and embedded system projects that are trying to standardize Those layers to support multiple devices But they are doing on the like embedded world is still like c and c++ And a really good example of that is like zaffer artos So maybe what we can do is build some layers on top of those Good abstractions for multiple devices. So maybe building something on top of zaffer for example And leverage those really good apis that they are creating So yeah, I mean it's something that we can discuss and maybe use And one good example of a company that is building some set of Interface and apis to do that with web assemblies and is intel They have something called rammer application framework that they find they define some interfaces contracts like we did here How to build applications using web assembly for iot but they defining Not so so not so standard way those interfaces, but they have that example And you can check later with some of the links that we are going to make available here But yeah, we are excited with the future of web assembly and iot There are still a lot of things to do and Things to be standardized and maybe apis to be read them So we invite you all to join this cause and maybe Help us make things easier to be able to be using web assembly and iot And as I mentioned before here are some links about web assembly in general like the web assembly dot org and rosy dot that for the wealthy spec We have the discord channel that You can follow like some things that are happening on the web assembly world like some projects and new specs coming up All the demo that are presented here is available on github So if you want to check the code in more details, you can just go to That github repository and check the code and also jonathan. I Provide this google docs with some other links about web assembly, of course in the web assembly and iot in general So you can definitely just access that link and and see that google docs. We are going to post more content in there But yeah, thanks everyone for joining and jonathan. I will be here available to answer all of your questions