Conf42 Golang 2022 - Online

Go to the Edge

Video size:

Abstract

Are you interested in IoT devices but you’re unsure how or where to start when coding them?

With IoT devices now forming a fundamental part of this world and fuelling the move forward in the technology evolution I will break down how I have become an IoT hobbyist using the language I am most fond of, Go. However, this is not the regular form of Go we are all used to, but rather an abstracted version called TinyGo. By using this, we can break down barriers and begin to unlock the potential of embedded systems and web assembly, join me and i’ll show you how!

Summary

  • Andela has matched thousands of technologists across the globe to their next career adventure. Now the future of work is yours to create. Anytime, anywhere. The world is at your fingertips.
  • Liam Hampton is a Microsoft regional cloud advocate here in the UK. He is also an IoT enthusiast, hence why he's doing this talk today. He will be talking about Tinygo, essentially a version of Golang, but for IoT quick agenda.
  • The next step for this is to have it water itself. I would like to have a sort of a self watering pump. So if I was away, then I could log in and I could press a button and it would be able to trigger the water pump for me.
  • Now, another one is a post notification. I want a notification where the postie comes, drops the post off, lands in the letterbox. This would then trigger the Arduino to do something for me. I'm using visual studio code and the Tinygo vs code plugin. The syntax is no different to go code.
  • The next one is a Lego wheel for GitHub notifications. The third is a dashboard with some youre home sensors. And if you want to give tinygo a go, you can. There is a tinygo playground much like the go playground.

Transcript

This transcript was autogenerated. To make changes, submit a PR.
What if you could work with some of the world's most innovative companies, all from the comfort of a remote workplace? Andela has matched thousands of technologists across the globe to their next career adventure. We're empowering new talent worldwide, from sao paulo to Egypt and Lagos to Warsaw. Now the future of work is yours to create. Anytime, anywhere. The world is at your fingertips. This is andela. Hello everybody, and become to my talk today. I'm super excited to be here. I'm really, really excited to show you what I've been working on over the past couple of months. So without further ado, let's jump into it. Today my talk is called go to the Edge. And I'm going to be talking about Tinygo, essentially a version of Golang, but for IoT quick agenda. So I'm going to tell you a little bit about me, who I am, where I've come from, and what I do today, where I found Tinygo, what I do with Tinygo, and the projects that I create in Tinygo, with a little bit of a hands on to show you how I do that as well. So who am I? What do I do? So, my name is Liam Hampton and I'm a Microsoft regional cloud advocate here in the UK, based at the London reactor. I'm an open youre contributor and through that I have been a software engineer. Prior to this role as an advocate, I was working at IBM and I wrote a lot of golang, a lot of node, and I was technical blogging. I'm also an IoT enthusiast, hence why I'm doing this talk today. And I'm a public speaker. I have spoken at a number of conferences and meetups around the globe over the past couple of years. So how did I find Tinygo? Now that's a really interesting question, because I actually knew nothing about Iot about a year ago. That's a bit crazy if you think about it, somebody in tech not really knowing anything about IoT. All I thought it was was just smart tech and little sort of event driven boards, essentially. But I actually wanted to get into home and smart tech. Now, I didn't know how to do it. I'd never written c language, I've never written micropython. I just wanted to learn. So the best way to do that was I went and bought a whole bunch of these boards. I went and bought a number of arduinos. Pretty expensive for someone who knew nothing about them, whether I was even getting the right things, but I went and bought them nonetheless. And that kind of forced my hand into learning now, I didn't want to learn a new language, so I ended up like every other developer would, went onto Google and said, how do I put go on an Arduino? And lo and behold, I found something called Tinygo, which is essentially a subversion of go being a go developer. I was pretty familiar with this and I was kind of like, hallelujah, I have saved it. I am now able to do what I've wanted to do. So what is Tinygo now? Tinygo, if you go to the website, it is a go compiler for small places, and you're going to see this scattered everywhere. Whether it's on the GitHub repository, whether it's on the website, whether it's in people's blogs, it is a compiler for small places, it's still a go compiler. So like I said, if you go to the website, you'll be faced with something that looks a little bit like this, where you'll see the sort of landing page and you can see the boards, the documentation for it. You can even go to the GitHub because IoT is open youre like most things now, which is fantastic. And there's even quick getting started guides as well. So what is it? It is essentially what this slide shows you. You have a smaller compiler which comprises, sorry, excuse me is a smaller compiler which comprises of a number of libraries from the main language itself. So for example, you have got IoT, you have got the bytes package, you have got archive and zip, but you don't have things like HTML and template or the net package because you're not likely to use them with a microcontroller or webassembly or anything else that you're likely to build with Tinygo. So it is very compact, it is very quick, and it kind of just takes what you need from the main language. It uses the LLVM technologies. So what LLVM is, is a strategy to support static and dynamic compilation for languages. It is essentially a toolchain of technologies, modular and reusable. It's open youre and it kind of works a little bit like the diagram. So this is very overhead and very simplistic, but kind of get the gist of it. Language you type goes through LLVM, which then allows you to run it on an architecture x 86 arm power pc, et cetera. If you want to read more about this, and I really sincerely hope you do really interesting read, you can go to the website llvm.org. So what is Tinygo used for itself? Where do you most commonly see it used, well, microcontrollers is the main use at the moment. It is anyway. And a microcontroller is something that looks a little bit like these. It's a board. It's like an input output board. So an arduino or an microbit or an artifruit playground or an x nine smartwatch, for example, something that you can code, something which will take in, you can manipulate with it, you can write a lot of programs for, is essentially a small board. And this is where tiny go really excels. Being a really small compiler, it is able to fit and execute or run code on these small chipsets. And with that being said, it supports over 70 boards now. And this is what I love about this technology or this project, should we say. It has got a fantastic community and with that you get fantastic support. So every single release something new is happening and the boards are growing. I think I started looking into this maybe about a year ago and maybe we were up to like 50 boards. We're now way above that, up to 70 plus boards, which is fantastic. The second use case is webassembly. Now, webassembly is essentially this diagram here where you can write your program in the code that you like, I. E. In this instance we're going to say Tinygo. You can pass it through a webassembly compiler, which will then change that and sort of manipulate the code to be interpreted by a web browser. And a web browser will interpret code as like HTML, Javascript, CSS, your standard sort of interpretations for web. However, wasm allows you to do that with different code. So let's take a dashboard. If I want to build a dashboard or something, I could write that in tinygo and it could then be interpreted by the web browser with a Wasm compiler. Thirdly, and finally, you may see people write small lightweight clis with tiny go. It's not so common now, but it definitely was about a year ago when I was looking into this. And a Cli is a command line interface, and it just allows you to have maybe lower level control at a system level if youre want to be sort of interacting with your host machine, for example. So where do I use tinygo? And this was kind of looping back to the beginning. Why did I buy all those Arduinos now? I had a number of problems in my home that I wanted to solve. One of them most, well, one of the bigger ones being dead plants. Now, I went away for a month with a trip and I came back and my plant looked a little bit like this. Wilted, dead, looking under the weather. Pretty far gone, really. And I always forgot to water them. So I wanted something which would tell me when I needed to water my plants. Therefore, I made a soil moisture sensor. So the sensor goes in the plant pot, takes a reading, sends it to the arduino, who then calculates it and sends it back to me for human readable output. And in this example, it is saying soil moisture is 71%. Fantastic. And for this plant, I think it needed to be around 50% or above. 50 is the ideal, which. So this is good. So the plant ended up looking, or still does look like this, nice and fresh. And it's behind me now. You can still see it poking behind my head. So that's just one example of where I use tiny go, really simple project, really small, but it allows me to get my foot in the door and build bigger and better things. The next step for this is to have it water itself. So, like I said, I want to build a dashboard. I would like to have a sort of a self watering pump. So if I was away, then I could log in and I could press a button, and it would be able to trigger the water pump for me and water the plant itself. So that's the next step for this one. Now, another one is a post notification. Now, this is the bane of my life. I do not live in the Burj Khalifa by any shot, but I do live in a block of flats, which are pretty tall, right? So for this sake, or for this, we're just going to pretend that I live in the top, right? And when the post comes, it's at the bottom. So every single day, I have to run up and down the stairs and I have to go and collect my post. And sometimes it isn't there. There's no standard time for my post delivery, which to me, is pretty annoying. So I want a post notification where the postie comes, drops the post off, lands in the letterbox, which would then trigger the Arduino to do something for me that would then tell me that I have some post in my letterbox, and then I can run down to the bottom and collect it. Now, that is a really simple sort of diagram here, and this is a really simple project to be building, but it's really fun and actually really useful for me. So I'm excited to get into phase two. And I say phase two because I've already created phase number one. Now, phase one is an Arduino, which is connected to a breadboard, like we can see on this diagram, it's got a couple of resistors, it's got a photoresistor to detect light in the environment, and a number of leds. So essentially a binary on off switch depending on the light in the environment. And I am going to show you how I have done that. To do that, I'm going to open up some code in a second. But the syntax is no different to go. So I want to stress it is exactly the same as what you would typically be writing if you're writing a Go program. I'm using an Arduino uno r three, so it doesn't have any wifi. So anything I do would need to be connected through a third party board or like a connector hat or something. I'm using visual studio code and the Tinygo vs code plugin. And the versions are here on the screen. I'm using Go version 1.17 and Tinygo version 00:22 now, you can't have tinygo without go installed because it kind of piggybacks off of it. It cherry picks the libraries, remember? So I am going to hide my slides and get up some code, like so. So vs code is open and what I can do here. So before we get going into the code, I'm just going to show you the plugin that I'm using. So over here on the vs code marketplace, if I was to type in tiny go, you can see here, this is the plugin that I'm using, and it is installed and it is globally available for me, which is fantastic. And secondly, to use that, I then sort of jump down to the bottom. You can see I'm using go version 1.17.6 and next to it we have tiny go, and it already says Arduino. But if I was to click on that, you can see a drop down of a number of boards that I can choose from. So there's a whole load of them in vs code or in this plugin which it supports. And all you do is you select the one that you're using and the one that you want to flash. Now let's run through some code. As you can see, it is really no different to go code. We have Iot, a package main, and some imports. But the timely thing that I need to make you aware of is this is using machine. This is the one library which is kind of detrimental to this project. Without it, Iot would just be regular go code. So here we can see in the function main we are initializing some analog pins. I have then got a light sensor and some leds which are here and I am sort of attributing them to the corresponding pins on the board. So the light sensor is in ADC zero and the led pins are in d seven, eight, nine and ten. After that I'm going to configure the leds as outputs. So it's going to loop around that slice and it's going to say hey, for every single LEd I'm going to make sure you're in output so you can emit some light when sort of an event happens, when it's triggered. After that I'm going to check the environment for the light in the room. So this is where I'm looking at the light sensor now. And I'm saying if there's over 8000, and I believe this might be nits, but whatever this sensor is actually measuring in, I just found 8000 to be perfect for my environment. It's going to say if there is enough light or if the light is over 8000, then make sure those leds are turned off. I. E set the LEd to false otherwise, or else set them to true. It's a real simple statement saying if, else, if it's greater than this, make sure they're off. If it's not, make sure they're on. And then I'm just going to do a timer. So I'm going to sleep for a couple of seconds or a second here. And this is just so that the sensor isn't completely overloading me with data every millisecond because without this it would just consistently run super fast, there'd be too much data and it would just not be good for me. I don't need that, it's not necessary for this project. So to flash it we need to find out what ports we're connected to. And to do that I am going to type in the command ls Devcustar and that's going to tell me where or all the ports that are connected to my machine. As we can see here, I've got the Bluetooth incoming port, I've got some headphones and I've got a USB modem. The USB modem is what the Arduino is connected to via a USB cable. And the next step is to flash it by using that port. So I need to connect to it. Now the command for that is Tinygo flash and then you have target and then the board name and in this case I'm saying Arduino. Then you have the port name or the port number. And in this case it is Devcu USB modem 11301. And as you can see, that corresponds to the port above. And then I'm just going to use or specify the file that I want to flash the board. And in this case it is conf 42 demo go. So if I press enter, that should flash the board for me. And it takes about a second or two Iot, doesn't take too long. There we are, it's done. Now if I was to hide this and show a different camera over here, you can see the leds are turned off. But if I was to cover up the sensor, they turn on. And that is essentially what this project is doing. It's on off for some leds. Now if we go back to my slides, we can see how that kind of correlates to the phase one of my project. Now imagine the postee wants to open up the letter box, puts the letter in. When they open up the letter box, some light is going to come in with the letter as well. And that would just be a triggering point for this project. So when there's post, send a trigger, send me a notification, whether that's via the cloud, through maybe Azure Iot hub or some pub sub event driven architecture that I'd like to build out with this, this is how it's correlating to what I want it to do. So this is a first pass. Again, this is phase number one, and I'm excited to take this a little bit further. So what are my coding projects now? I've actually got a number of things that I want to play with. One of them is in front of me, I've got a macro pad. So when I'm streaming I can sort of hook this up to obs or a different program and have some shortcuts while I'm coding or streaming or something like that. And that uses RP 200:40 board, so I believe that is actually a raspberry PI. And again, Tinygo supports that. If I was to look in vs code, I can see the board that I could flash and it is under that as well. The next one is a Lego wheel for GitHub notifications. I'm a sucker for always missing out on notifications. Whenever I get asked to do a pull request or review some code, I always miss it. I get the email, but it just always goes over my head. And I'm so busy with some certain things that I'm playing with at the moment sometimes don't get around to doing it. So I'd love a physical notification very much. If I saw a lego wheel moving on my desk, then I would know that I've got a GitHub notification and to go and check it out. I've done this before with text messages, but the text messages, I'm really good at ignoring those. So I thought, let's have something really obtrusive on my desk. So that's the second one, and the third one is a dashboard with some youre home sensors. I would love to be able to tell the moisture, the temperature, all these smart things that the average smart home has. Now I'd like to build that myself and build a dashboard with that. I can then hook all the other existing things up that I've got my post notification, my smart plant, all these kind of things and have a dashboard which is just sort of on my wall in my hallway, and I can see everything that's going on. And I'd love to do that. And if you want to give tinygo a go, you can as well, because there is a tinygo playground much like the go playground. All you have to do is go to play tinygo.org and you can use this as a sandbox or a simulator. You don't need any equipment. I just chose to go out and buy it myself because then I can learn it sort of hands on. That's how I learn. That's how I'd love to do it. But you can do this yourself through a web browser, really quick and simple and it's really, really fun. You can create some really cool projects in the playground, much like you can with the go playground. And with that, I'd like to say thank you very much for listening to my talk. It's an absolute pleasure and I can't wait to see where I'm going next with this. If you have any questions, please do feel free to reach out to me on social media, and I will always get back to you as soon as possible. My dms messages are always open. Thank you very much. Take care.
...

Liam Hampton

Cloud Advocate @ Microsoft

Liam Hampton's LinkedIn account Liam Hampton's twitter account



Awesome tech events for

Priority access to all content

Video hallway track

Community chat

Exclusive promotions and giveaways