Building Lego Robots with PHP

 5 years ago 12,396 views
Presented by Christopher Pitt (@assertchris)

August 23, 2018

Hands up if you love LEGO. Just kidding, I can’t see your hands. I understand why you love it though. LEGO in general, and especially Technic, are wonderful outlets of creativity and marvels of engineering.

Everything fits together so neatly. Some of the sets also include optional motors and sensors; bringing them to life. The trouble is that they’re closed systems, not easily customised or controlled.

Until now.

Over the past few months, I have been experimenting with custom LEGO electronics. I have built a few different robots, and I’d like to show you how you can do the same.

We’ll begin by looking at a standard Technic set, and how it works out of the box. Then, I’ll show you how to customise its components to connect them to Arduino, and then to PHP, via Low Energy Bluetooth.

You’ll see how to program your robots, using asynchronous PHP code. Finally, I’ll show you how to make a simple React Native application, which you can use to interface with your robots, on Android and iOS.

I am a developer and writer, working at io.co.za. I usually work on application architecture, though sometimes I build compilers or robots.

Transcription (beta):


Thank you everyone for being here. I'm quite excited to talk to you tonight. I get to talk to you about making robots with Lego, which for me is a really exciting topic. I hope it's as exciting for you to partake in as it was for me to prepare and continues to be cause I love Lego. And so what I'd usually do at this point is I would get you all to stand up and do some exercises. If you've been to one of my talks before you might've suffered from this, and it's just a way for me to get rid of the nervous energy and for you to stay awake, but I can't do that. So I'm going to ask you really, really, really nicely. If you could please try and absorb as much of this as you can, because I think you're going to find not only will you benefit from knowing the stuff and from experimenting with this stuff, but those around you and in your family and who you work with might actually find this as as interesting a subject as you will.

And as I do that with that in mind, the most important thing that you need to take away from this talk is that you can make robots with Lego and PHP, and it's good for you and your family. You'll see, as I go through that, there are many examples of taking something that is just traditionally Lego and fun to build and fun, to see and playlists and turning it into an opportunity to teach someone about programming and about electronics and about automation and about making useful stuff in the real world. Just with toys that you have access to and hobby electronics that you have access to. So if you're blown away by the demos that I give, that's cool, but just know you can do this stuff. And I figured out how to do this stuff and I there's no special training involved or special equipment involved for the most part. It's just ordinary Lego and a lot of creativity and fun. The SOC is mostly going to be about LIGO. And what do we know now? I know you've come from a PHP talk and there will be some PHP and some CNC first plus even, but this talk is mostly about Lego and Adrina, it's kind of a bait and switch to talk at nomad PHP about Lego and Audrina, but here we are and it's going to be great. You'll see.

I'd like to start off by telling you a story. This will paint a picture of how I got to the things that I want to show you tonight and give you sort of an idea of the natural progression you can have as you're learning this topic. It all started when I was walking through a toy store, as you tend to do when you have kids and looking at the different boxes of toys that they have available there. And though my kids had Lego from very early age. I never really had Lego of my own because I couldn't find a way to justify it to my wife or why I was going to spend a lot of money on what is essentially just a toy. But then I thought, you know, I saw, I saw a box of Lego, which was this box of Lego.

And I thought, wow, that really looks fun to build. I'm sure I'll have fun doing that. And so I got this box of Lego and I took it home and my son helped me put it together. I say help you, you know how it is when you're building stuff with kids, it's more like you're, you're trying to herd cats, but we put this thing together. And as with most techniques sets, it has some non-standard Lego pieces and the, the, the instructions get you to put weird combinations of things together. And only towards the end, it starts to come together in what most of the time, I, I can only describe as a beautiful design because like technical, really examples of beautiful design. So we put this thing together and I still have it. Actually, it looks like this it's one of my favorite sets and it was the first set that I got.

And it was really fun to bold. Now techniques sets usually have some moveable parts to them. So there's, there's this gear over here that you can turn and that will move the wheels backwards and forwards. There is this gear over here that you can turn and this will raise and lower the arm. There's a gear or there's something to turn at the back here. And as you turn that the arm extends and retracts and has even a little bit over here that you can turn to tilt it, tilt the scoop. This is such a fun set. And I got this and when I was done with it, I just had to get more Lego. But I also had to answer another question that I had, which was now that I've got this, can we make it move on its own? How do I do that? It's great. As a design, it's great in the functionality that it gives you these gears that can turn and make it move. But how do I make it do that on its own?

So I asked myself this question, and then I made effect or mistake. Then I said to myself, well, I'm sure I can find something to make this move on its own. Let me go and look what is in the shop. So I went on Amazon first and then a few local sites. And then I discovered that you can get this box of what's called power functions. There's this weird arrangement inside Lego, where a bunch of different departments seem to be making different and competing mechanization components. But this looked like the simplest part functions. And though it didn't it wasn't advertised as part of my red crane, since it was advertised as parts of other sets, it looks like this, this isn't the original one that I got. It's a, it's a another that I got, but basically you have this box and you get to the battery pack in it.

And there's like a directional switch and some LEDs and some motors, few other bits and pieces. And so I saw this and I said to Liz, Hey, let's get this. And so she was out of the shops and she got it from me and she brought it home. And I immediately upset her because the very first thing I did when I got it was I started to cut into the pieces and make my own connections for its and try and reverse engineer it and figure out how I could get these different things, connecting to each other in a customizable way. It totally upset her. I mean, the thing was like $50 or something for like an experiment. And I immediately basically destroyed it. But anyway, this is what these components look like. Here's the battery pack. And there's like a, a little directional switch at the top of it.

And then they have these connectors and power functions and you can attach them in sort of a Daisy chain of all way to each other. You can actually make some interesting things with this. I got these components and then I started to attach them to my red crane. I thought, okay, what if I attach the motor here? Then when the motor turns, I mean, just imagine the motor turning on its own. Like if it had its own power, when the motor turns this crane arm would extend or retract, and I could attach this motor to various points on the crane, like maybe I could attach a motor there. I could attach it there as we saw previously. What if I could attach one over there, over there at the top, I could make all these parts of the crane move on their own, but it raised another question because though I can find these components and buy them and use them as Lego intended to make technic models move.

How do I deal with the wires? All of these things connect to each other, but I can't switch the switch on and off programmatically or with the remotes. I have to be at the model to turn it on, to make it move. That's a bit annoying. How do I deal with this question of why I made another mistake, which was to go back to the shop and buy some or Lego? Some would consider it a mistake. I actually thought it was quite an inspired decision at the time. And I found this an RC track racer. Now this was getting a bit pricey because now this was almost a hundred dollars. Well, the equivalents in South Africa, but what did allow me to do was remotely control the device. Let me show you what that looks like up close. Yes.

Okay. Now you can get the set on Amazon or at a Lego store. I'm assuming, and it is designed to be able to be built in one of two ways. This is the first way. That's the picture on the box. And this actually opens up to reveal some of the electronic components available. There's that battery box that we saw before, and you can switch it on and off as you can. When it's loose. There's a motor over here connected to one of the wheels. There's a motor at the back here connected to one of the wheels. Now this thing over here is an infrared receiver and what you can do actually I'll turn it on. And I'll show you is that you get with this RCC, you a little remote, you'll notice there's two colored notches on this remote, red and blue, and then map to colored pads over here, where this is connected because each of these two motors is independent of each other. And so you can use the remote to turn one wheel at a time. You can turn them both, and this thing will move forwards or backwards and you can turn them in opposite directions and get it to spin. It's actually able to look at what that looks like quickly. Cause I think it's pretty interesting.

I can control this just by using this control. That's pretty fun, right? That's pretty fun. How is that? Just before we get into the slide on the following it is worth noting that this infrared remote and the receiver works with line of sight as infrared does. So if you're in a different room or you go too far away from the RC call, it stops working. And this was a whole other issue that led to some more interesting questions and discoveries. Now, in order to do any more interesting stuff from this point needs to take a detour into hobby electronics. I'm going to show you some components and talk about them because you're likely to encounter them. If you start to do the circuitry or, or the projects that I demonstrate to you, but don't worry. You don't need to remember all the details or understand how the things work internally. This is just a brief primer, as you've seen, I've got a workspace out here and what I'm gonna try and do is hold components up and let this camera focus on them because a lot of them are quite small and finicky.

And so it would be good to see them close up. We're going to cover a few of these. We'll start off with the Adrina. Now this is a kind of Audrina. I'll hold it up so that you can see it's not an official Ardwino it's made by DF robot, which is a company that produces generic electronic circuits. But this board is modeled after a kind of official Ardwino called the UNO. Now Arduino is a company that pioneered the creation of open standards for hardware, like the design of the sport and the software that runs on it. And so they released these open standards into the wild and other companies can come along like DFR robots and create generic devices that are slightly cheaper. There are different models of Arduino. So as I said, this is the model of the owner, but there are smaller ones and there are larger ones and they have slightly different limitations of memory and inputs and upwards, but they all have the same basic idea.

And it is us. It's an abstracted implementation of hobby electronics. It has rows of inputs and outputs like there and like there. And what it does is allows someone who doesn't understand maybe all the inner workings of an electronic circuit board, but wants to get started a way to interact with the physical world. You can, for instance, use these outputs to switch on a light bulb or to open a valve. You can use these inputs as a way to read from sensors like temperature and humidity and touch. And so you can start to create interesting projects without knowing a lot of really, really complex electronic circuitry theory. There are some interesting components on top of this, like this little shiny cylinder over here, which is a crystal, which allows this to do iterative execution. I'll explain a little bit about what that is in just a second, but basically it enables a loop.

There's this chip over here, different models of this board have different implementations of the ship, but it's basically storage to allow you to write a program and store it on the Arduino so that when it gets powered again, it can start to execute that program. What do we know is a basically small, cheap automation, computers. They're not fully fledged computers like some we'll see, but they're really cool. The generic ones tend to be cheaper. They tend to all be expensive in South Africa because they don't have a lot of people who input them. But as a way of creating an autonomous projects or an automation system, they are golden.

Before we look at some other components, it's also worth noting that maybe you've come across something like this. This is called a raspberry PI. It is basically a credit card sized computer. It has a let's see it has an HTMI port. It's got an Ethan airport and some USB ports. There's alien audio. There's an SD card slots at the bottom for you to be able to stall the operating system and files that you work with. And so this is actually, you can install a 64 bit operating system on here like Linux, and you can run this as a computer. Now, the reason this is interesting is because it also has a row of these pins over here, which functions similarly to the input and output ports on an Adrina. So if you have one of these and you want to get going with these experiments, you actually can because you've got the same input and ports you need for the projects that I show you like Adrina that has different models in different sizes with slightly different capabilities.

Raspberry pies also come in different flavors. This is a smaller raspberry PI. It's called a PI zero. There are some many USB ports. There's a many HTMI ports. There's still an SD card slot at the top there. So it's got very similar capabilities to the bigger raspberry PI. But likewise, we know there are different sizes to these devices and all of them do generally what I'm going to show you. That's cool because you can start with a range of different hardware. You don't have to feel like you need exactly this model to get going next. Let's look at a component called a diode. It looks like this very small. This one. Nope.

Let me just try and get it to focus on that. This is hilarious. Okay. Well, it's a little black cylinder and it has a shiny lime on one of the ends. And the diode is interesting because it's like a one way valve for current. When you put this in a circuit, the voltage or the current is only going to flow in one direction through it, it caught flow the other way. And that's important because sometimes you can damage your circuits. If current is flowing in the direction, you don't plan for it to flow in dyads or cool like that. There are one way evolves for current.

Okay? Then there's this. You've probably seen a lot of these before. In fact, you might even have some shining domino in your house. It's called an led and Ned is short for light emitting diode. Now that's why I started with diet in the first place. This has a lot of things in common with the diet. It's directional, just like a diet, which means you need to place it in a certain orientation for a current to flow through it. If you turn it the wrong way round, it won't work. It also emits light, which is useful because it needs to be light-emitting. And what you should remember when you use these two IMiD lights in your circuits is that you should generally use them with resistors because they aren't meant to handle potentially the current and the voltage that you want to put through them. Unlike light bulbs, if you attach a 1200 light bill to your main power, the light bulb would explode it work. They'll probably just fizzle a little bit. Maybe makes the smoke get really hot and stop working. So just remember when you use these in your circuits that you need to use them with a resistor, resistors are really cool. They're like a mystery. Have a look at this one.

Yeah. It's a tiny, tiny cylinder with little colored lines on, in a really weird configuration. Now, resistors a non-directional. You can put them anywhere around in your circuit. There'll be fun. Current can flow through any to them. But what they do is they stop or they reduce the flow of current, which is useful. They create resistance so that not the full current goes through that path in the circuit. It's useful because sometimes you want to channel current through different parts of your circuit and you would use resistors to do that. So to help with that, it's also useful when you combine it with things like LEDs so that you don't burn the led ALS or other components that have a limit to the current and the voltage they can handle. Now, the lines on a resistor have a story of their own.

Nope. The configuration of lines on a resistor. Explain what resistance the resistor has. Resistors were standardized in a time when it was too expensive to print on the resistance itself, it's resistive value. So instead standards bodies decided on a configuration of different colored lines in different orders and with different thicknesses to signify how much resistance this resistance has. It's like a code or on its own. And so it can be kind of daunting. If you don't understand the code to know what resister you're actually working with, but I've got a solution for you, which we'll see in a minute. Anyway, what do you need to know is resist is act as kind of a gateway to slow down the flow of current in a circuit. I think this is the last component I'm going to show you tonight. And it is a transistor. It looks like this.

They typically have three legs and they're sort of like a half cylinder. If I can show you what that looks like, it's kind of like a cylinder cut down the middle and what transistors are, are a way of electronically controlling a circuit, okay. In hobby electronics, at least this is how we use them. There are three legs on the transistor. And so we connect a circuit on the two outer legs, and this can be a circuit with mains power going through it. If there is this, it can handle that. And then what we do is we connect a smaller charge through the middle pen. And when the charge is on that middle pin, it will connect the two occupants. It's like an electronic switch where the middle pin controls the connection between the two auto pens. Now, this is really useful for our circuits, because again, going back to the example of of your mains power and light bulbs, what if he wanted to control the lights in your office based on an API or with API?

So you make requests to it and your lights go on or off or change different colors. You would need to use something like a transistor, the lights in your office use mains power, which for a starters, AC not DC, like most hobby electronics use, but also they have a lot more voltage and current than your Arduino can handle. So what you need to do is you need to connect your Arduino with the transistor so that it can control a circuit without using the same voltage and current of the circuit. You connect your Adrina to the middle point of a transistor or a series of transistors, and you connect the occupants to your Maine's power so that you can control things with a much higher current and voltage using a much lower current and voltage. At least that's what they used for, with the hobby electronics that I'm going to show you.

Okay. Really cool. Now it can be difficult to remember the details that I've just gone over. And sometimes you may need some more info, like how you read the colored bands on a resistor. If you try to work out what resistance it is. And so to help with that, I read an article quite a lot, quite a while ago, which describes all of these components and a few more ones. And it has useful info, like how to read the colored bands on a resistant, and you can find it at that link. And so if you want more details about these components or a couple of other ones that I haven't spoken about now, you can go to this article and you'll get that info there. You can also ask me if you don't get this link, now you can ask me on Twitter later.

And I've also posted the link to these slides in the slack channel. So you can find this info there as well. All that is to say, we need to use an assortment of these components. If we want to make more interesting Lego robots. If we have time, I'll show you exactly a very complex circuit that I've built to enable us to, or not complex, but a very intricate circuit boards, which will enable us to control these Lego things in a cool way. But for now, let's talk about programming and Arduino. The steps I'm going to talk to you about now are Arduinos specific. If you're using a raspberry PI, you'll use slightly different software, and I can I can point you in the direction of that software. Maybe if you ask me on Twitter about this or in the slack channel, but for now, let's just assume you're going to start with an Arduino or at least I'm going to demonstrate with one.

And we'll talk about how we program this. I think when next slide is a webcam view. Yes it is. Okay. So for now, I'm going to show you Arduino specific stuff. If I open up this ID, okay. So we know this company that popularized the open source hardware and software also release amongst other things. And ID ID is stretching it a bit. It's a text editor with the ability to upload code to an Arduino, and it looks like this. You can get it. If you go to do we know that CC, I couldn't even spell that. Hold on. Oh, do we know that CC, you can get this IDE from here, there's a software download section. You can also use an online IDE, which is much better than this offline one, but it's it's been a bit temperamental today. So I'm not going to use it for this demo.

I'm just going to use the idea that you can download from that site. It looks like this. And the first thing you'll notice is this is not PHP code. This is actually see the IDE is capable of doing C plus plus as well. So when you start out with Adrina, you start programming in C or C plus plus suspend your belief for just a second, because I want to explain how this code works. And then I want to talk about how we don't have to write this code ever again. When you create an Arduino script, you have two functions that get executed. The first happens when the Adrina gets powered. So if you need to initialize something, you would do that within the setup function. And then the second function happens continuously. It's an infinite loop. And that that goes back to the crystal that I pointed out on the board earlier that crystal allows this loop to work.

It'll constantly run the code within here, inside this IDE, there are all sorts of examples. And the very first one that you can go to. One of the first ones you can go to is basic blink. And it is this, it says, take the Ardwino. And depending on the model of what we know that you've got get the pin that has a built-in led on it. I'll point this out to you in just a second on video, but most of the Arduino models have an led that is embedded in the board. And that is connected to one of the pins for upward on the board. So the setup function says, get me the one that is the built-in led open and set its mode to output. In other words, I'm going to put a signal out of this pin, into the world. And in this case, it's going to lead to the edit.

He lighting up, set that built in, led up then in an infinite loop while this device has power, give that built-in led pin a high signal, give it a high charge or high voltage level. Okay. Talk in relative terms to Audrina. Now it's not actually that high, but it is better than off completely. Right? So in other words, get the entity open and switch the led on then delay for a thousand milliseconds or in this case just to lay for a second and then take that led pin and set the up the voltage to low. In other words, switch the led on, wait a second. Switch that led off. Wait another second. And then this function executes again on, wait a second, second off, wait a second on. And so now I haven't actually got this connected yet. I will go to this video view quickly.

And so here's my odd Mino. You connected by default with USB. So here I have a USB cable. And when I connect this, you're going to see a whole bunch of lights go on. I hold this up to the camera in just a moment. So you can see a bit more accurately, but now this is connected. Now I can go to the Arduino IDE and I can say my board, the type of board is an Arduino UNO. That's the, that's the model of it. It might not be an official Audrina, but this is the one that it, that it is modeled after. And the port, well, out of all these ports that are available, the one that's going to match it is the one that is closest to USB modem. And then we can see it's actually put the suffix here. So in my case, it automatically selected these, but in order to get it to work every time, you're probably just going to want to check that these look right to you.

Okay. So we've got our program here. We've selected the Arduino model and the port. Now we have a couple of buttons here in the IDE. The first is that we can verify this program to see if it compiles successfully. It's a compiled language. So we're probably going to want to do that. We didn't see any error messages. Okay. Another button that we can click is upload that compiles the program and it uploads it to the Arduino. Now, apart from the red power led that you can see on here, let me hold this even closer because I want you to see very accurately cool. Oh, that is too close for the auto-focus to work. Do you see that led that switching on for a second and offer a second on of, on of it's doing that because our program is executing on the Adrina actually, you can see it from down here as well.

It's just not as clear on, off, on, off. Great. We've written our first Arduino program. It's blinking a belt built in led. That's connected to pin 13 on this model. That's exactly what our pro our program said. We wanted to do. Now, hands up, if you want to write C for the rest of your life, while you make robots, I can't see you, but I can guess no, one's actually got their hand up because writing C to do automation, doesn't sound like a fun thing to do the community around Ardwino realized this. And so they came to the rescue by developing a kind of open standard called fermata. Now, probably the easiest way to describe what fermata is like in our PHB context is to describe it as a kind of web server protocol that runs on the Arduino. It's a C or C plus plus program.

And I'll just find it here by going to file examples for Marta standard for Alta it's, a C program or C plus plus program that is huge. There is so much code in here, but what it does is it acts as a kind of server running on the Arduino that can start to listen for and act upon dynamic instructions sent over the wire. So we no longer need to program in C in order to give our OD winos instructions. We can upload this C plus fast program, and then we can issue dynamic instructions from any number of languages that adhere or libraries that adhere to the fermata protocol. I'm gonna upload this quickly to the board, because I want to show this in action.

Okay. So it's done uploading. Let me go to my slides. Now, there are a few different PHP libraries that you can use to communicate with fermata the best by far. And my favorite is the character for Marta library, and it's got a stable release. So you don't even need to do any funny composer business to get this running. You can just say composer require character for fermata, and that'll install a bunch of libraries. That'll let us be able to start using this protocol. Okay. Now I have a very simple example about how to use this. At least I think it's simple compared to some of the code I've seen to begin with, and we'll make this slightly bigger. We need to get a reference to the Arduino board, have a look at the string over here. If you go into the Arduino IDE, you can see it matches this over here. So when we're writing this PHP code, we need to know what the string is. One way, probably the easiest ways to go to the ID and to see which ports are there in which ports it is selected. That's one way to know if you don't have this IDE, what you can do is you can set Ellis slash dev, correct USB loaded.

And so that will present you with a few options. And these are most likely references to your Arduino. If you're doing this development on Linux, use TTY dot USB murder. If you're using this on Mac, you see you, but USB modem, slide, protocol differences. There, you need to see you to use it on a Mac. Okay? So here I said, slash dev slash C U dot USB modem. Then I've got this magical number down here, which is called a board rate. Hands up. If you're from a time when you used a dial up modem, that term might be familiar to you board. And it is it B a U D. It is the rate at which serial communication happens over the wire to the device.

The computer is communicating with us OD we know many, many, many times a second. And that specific number is the frequency at which it does that. We don't really need to change that or know much more about it. But I thought I would explain why we're using a magic number there. This is fairly explanatory. We are referencing the device, but this might be unfamiliar to you only. Now, you know, it's something to do with how fast we communicate with the device. You don't really need to understand that or change it much beyond this point. So we get a reference to the Arduino board. We also get a reference to this, which looks like an event loop. Now, surprise, surprise. I know you weren't expecting this from someone like me, but PHP has the ability to simulate it and clips. So this library allows the simulation of an event loop.

And it does that because the way we communicate with this library to the Arduino is an event loop based system. So we had event listeners in this event loop, and it runs as an infinite loop. So the moment we start, the scripted we'll continue running until we exited to what Fagles or whatever, but we can listen for events based on like connections to the device or reading, sensors, and so forth. Okay. We get a reference to the event loop, and then we say, we want to activate the board. Now it's not actually going to do any of this stuff until we run the event loop, but we set that code up to say, okay, let's activate the board. And when you are done activating it, just give us a message to say, Hey, we've connected to it. Then we're going to say, get pin 13 on the board.

Now, based on this model, pin 13 is the built in led pen. As you'll see, when we execute this code, the led is going to light up as it did before. So it's going to do exactly the same thing as we expect. But again, this is a bit of a magic number, which the Arduino IDE defines a constant four. They called us led on the school button, depending on the board that you plug in. So for us, okay, we're using a number here which represents the specific board model, but that's okay. Get pin 13, set it's mode, two outputs, and then in a set interval, which is very, Javascripty like code in a set into of one second, get the pins, digital signal and inverters. If the digital signal was zero set the digital signal to one. And if it was one set it to zero, this is exactly the same thing as saying digital rights, high and digital, right?

Low, except novice is all in PHP code. And if there's an error, let us know what that is. Okay. Will this work time to see first off, I'm going to point out that I'm running this in PHP 5.6, and I will explain why in just a moment, but don't hate me for doing this. It's part of the demo. And it's actually going to help you deal with a bug that you were likely to encounter. If you try this experiments at home, okay. PHP 5.6 PHP index. I'm not even in the rascal spread folder, LIGO PHB index, connecting to the board. This'll take a second or two connected to the board. Now, if I hold this up a little bit closer, you see that led, that goes on for a second off on, off, on off, there are a couple of LEDs next to that. And if I hold this a little bit closer, you might be able to read what the labels are next to them.

Yes. Okay. The levels next to those other two that are going on are TX and RX. And those are lighting up a lot because we're getting serial communication over the wire, telling this board what it needs to do. Dynamic instructions that happen a lot. And so these lights are just actually showing us that there's some network serial traffic to give the Arduino board instructions because for Marta is running on it and it is taking dynamic instructions. Okay? Our code listing works. This is great news because now we can program and our demeanor and it can do it with PHP instead of C plus plus a for away what next I mentioned, sorry, let me just stop this quickly. I mentioned that's I'm using PHP 5.6. If you're using this on a Mac and you want to run these experiments or make these projects, you are likely to encounter a bug.

If you're using a USB cable to program or to communicate with them or to on back then there is a problem in the way that PHP connects to fermata, just with a default implementation of serial communication, which causes it not to work. Basically PHP wants to give it instructions before the serial connection is completely open. And so it just never connects it censored. It sends it in an infinite loop or it hangs or some or other bug that happens. But basically if you try and connect this with a cable or the USB, and you're using a Mac, this word work. And so what I had to do was I had to find an extension that replaced the default serial communication protocols in PHP on Mac fulfill martyr. There's an extension called PHP makes serial port. The extension itself is called gorilla. What you need to do is install PHP 5.6.

I know it's gross, but hear me out because we're just prototyping something here for the moment. Oh, and also I'm assuming if you're a developer in your own back, you have Homebrew. So you can install PHP 5.6 with Homebrew, and then you can clone this repo to get the serial port extension source code. There's a few steps here to compile the the extension source code into a dot Sol file. And then once this is all done, you need to go into your PHP INI file and you need to add extension equals gorilla dot. So if you get stuck at the step or you want me to repeat it, I'm happy to chat about it on Twitter. I don't want to dwell on it too far, too long, except to say that if you can't connect it for mater on Mac, this is probably why now you know where to look and ask me questions about it.

If you need to, okay. Demo time, demo, time, time. Let me see what this camera configuration is quickly. Right? So I took some of these components and I went back to my problem about how I make the technique set and move without wires. Remember I had the RC Rover and I had an infrared remote infrared control, and I could get the Rover to move backwards and forwards, but it was infrared, which means in a different room or too far away. It didn't work. Also. How do I control that in a slightly different way? I can only push buttons on the remote to get it to work. Maybe I can attach wires to the remote itself and try and trigger those switches via transistors and Adrina circuits, baby, but that's kind of messy, right? Maybe I could attach wise to the Rover itself and control the motors that way again, kind of messy. And it doesn't, it takes me a step back because now I have wires again. And so what I did was I looked at these adapters and I wondered how I could tie into them and come to find you actually can. You can, if you're willing to get a few spares and cut them up and make connections on them yourself, you can noninvasively create Arduino controlled circuitry that interfaces with Lego components. Let me show you what one of those circuits looks like quickly. I'll just go and get it.

Okay. So this is a VRC races set that I showed you earlier. It has two different configurations. This is the one that's not on the box. That's a little bit top heavy, but it's more of a Rover design. And this works better for my example, because I have space on the back to put all this wonderful, wonderful circuitry. Now, without going too into detail about how this circuit looks apart from the fact that it looks amazing, but without going into too much detail about how it works, there is one Reno at the bottom, and this is an official Adrina. You can see it as like some style, some writing on the side there. And it's cool. It's really cool. On top of the side, Wiener is a project board, which is basically a way for me to connect wires without soldiering them onto the Audrey note itself.

Then I have this little chip over here, which is a Bluetooth, low energy transmitter and receiver. And then I have a chip in the middle here, this little black chip that's in the middle there. Now, the reason I need that chip is because I want to control motors, but I can't connect them directly to the Ardwino instead of having my own configuration of these transistors and a massive, massive circuits, I can use this chip, which is called a motor drive, a chip, and it has transistors embedded in itself. So it has a whole bunch of inputs and outputs, and basically putting current into different pins on this chip will allow me to control the motion of motors with up connecting them in series two, mild Reno. It's basically a pre-made transistor circuit to control motors. Okay? So I've got this chip sitting on the back of Arduino. And if you look in there, you'll see there's a little blue and a little red cap. Those are connected to these wires, plugged into the Adrina. They do a couple things. They allow me to control the motors by switching them on and off with the Arduino. And they also allow me to draw battery power from the battery pack connected to this RC Rover.

So if I switch this on, I just want to show you that this is really, really non-invasive. I haven't destroyed anything about the RC Rover kits. All I've done is I've gotten some additional connectors like this, and I've wiped them up to my Adrina's circuits. In other words, I can take my normal infrared remotes. And once the Rover has power, I can move the motors independently. That all still functions exactly like it did before. Only now have a look. The Bluetooth sender and receiver is pinging with different colors. Okay. It's not a look at what that's actually going to allow us to do because no, just to make totally clear that the still works, noninvasively, I can use this remote to make this RC rather move. It's completely functional. Exactly. Like Lego intended the set to be booked. However, now I can write some code. I can execute that code and this is going to be fun. Oh, oh, by the way. Because I'm not using the Bluetooth module. I actually bypass that bug that I had before with Mac. So although I have PHB five on here, I'm actually going to switch to PHP seven quickly because I would prefer to use that for the following code examples. So what I'll do is I'll just take these path variables.

I should have PSP again, view. Now I have PHP seven running here. Okay. So if I go back to my letter folder and a racer and make this a little bit bigger. Okay. No, I should be able to say PHP source, spin, connecting connected. It's going to spin for two and a half seconds, freeze for two and a half spin the other way for two and a half frees for two and a half. And it'll keep on doing that while it has power lift in its batteries and the Bluetooth connection.

[Inaudible]

That is awesome. Tell me that's not awesome. You liar. Tell me that is awesome. Let me show you the PHP code that gets this working and it really hit home. How really, really cool this is. Okay. I have a bootstrap file. That's loads of the composer auto loader and sets up the initial board and event loop. And so the first line that executes, this is connecting, which is this code that runs over here. And once the board is activated, it says connected, which is this line over here. And then I do some kind of control pins. Now these are outputs on the Arduino that are connected to that motor driver chip. So control one a and control two a and enable a are three output pins that are involved with just one of the motors on the RCU Rover control one, B two B and enable B are three occupants on the Arduino, the control, just the other side of the ICU Rover.

We need six of these upwards to successfully use the motor driver chip that is on that circuit board on the back of the RC Rover. This is kind of a setup operation, right? We set these up and then we have another one of these set interval things. So over here, I've got set interval. And then if I scroll a little bit down, you can see this happens every two and a half seconds that this function is executed. I started off by saying, okay, mode is zero. If mode is zero set, these control pins and enable that side.

So this is just the left hand side. And this is just the right hand side. Notice how though I'm doing control. One a is equal to one and control two. A's equal to zero. And over here, I'm doing control. One B is equal to zero and control. Two B is equal to one. These are going in the opposite direction. That's how it spins individual motors, one going forwards, and one going backwards. We'll make this RC Rover spin in position. Okay. Then I said, motor one, the next time in two and a half seconds at the executes, we'll see. Okay, I'm disabling both of the motors. That's what makes it stand still for two and a half and set mode to two. When in mode two, I swap these around, have a look up here where I say, one is one to a zero here. I'm saying one, a zero, two A's one.

So I turn this side's motor around. If it was going forwards and I could be going backwards and vice versa, I reversed both directions. So that's after turning one direction for two and a half seconds. And in standing still for two and a half seconds, it will spin in the opposite direction for two and a half set mode three, if mode three turned them both off again, this is very similar to the concept of the blinking code, except now, instead of controlling an led, I'm controlling the motor driver, which is controlling two motors connected to an RC Rover noninvasively, just with LIGO clips, making this ICU Rover controllable with PHP code or with a mind explosion thing is what I'm doing at the moment. You've probably heard it. Okay. Now I've got a few more demos that I want to go through quickly. So let me rush to do them. I want to show you what we can do to get even more crazy with this. But what I'm going to do is I'm going to put the RC rose on its head because I want to make it do stuff. And I don't want to make a drive off the scene. You'll understand why in just a minute. So I'm going to flip it over quickly. [inaudible]

Okay. Don't try that with a real car. You will die right now. Let's look at some more example, code character, not only ships with, for Marta, but it also ships with an asynchronous HTTP obstruction. We can connect to the board in exactly the same way as we did before. Only now we can create an HTTP and we can create some routes. In other words, some end that we can call with HTTP requests, which we'll get it to move the exact digital configuration. Doesn't really matter except to say, we're moving them. We're turning the both on we're making them configured exactly the same way, which means it'll run in a direction it'll go forwards or it'll go backwards. So when someone calls forward slash forward, they're over, we'll move forward. And when someone pulls forward slash stop, the Rover will stop. Okay. PHP source, character serve connecting to the board and let's tile. These as best we can connect it to the board, know what I should be able to do is cool. Hep. And I set this port as 80. Okay. So cool. ACDP once you seven zero zero 180 80 slash forward.

Now it's going to move forward continuously. It'll do that since like all this stuff, HTTP endpoints, we've just created an HTTP API that someone can call from an app or just from call. Someone can go to a website and click a button. And this API is connected to an OD. We know via Bluetooth connected to a physical device that will move in the real world when someone makes a web request. How cool is that? Okay. I think that's very cool. I think that's awesome. There are some okay. It's worth noting that in this bootstrap file, I'll talk about this quickly because it's awesome. Character has its own event loop system. It also supports a kind of adaptive system that will allow us to implement a different event loop implementation, and use that inside character. It ships with a react PHP adapter. So let me talk about that for a second.

You can use character to connect to Arduino and to run an HTTP server asynchronously, and you can put react PHPs event loop implementation inside character, and it will use that come to find, there are loads of people writing asynchronous, PHP libraries. One of them, my favorites in fact, is the AAM PHP library, which is a collection of asynchronous libraries amongst other things, including an asynchronous HTTP server. So if you prefer to use that and you don't want to use character's HTTP server, what you can do is that the a and PHP event group has a reacted adapter. You can get that back to debtor. So it's ANPs event loop to later look like in reactive in group given to character characters event live system. And so when you call loop is IO event, you factor yet what you're getting is the AAM PHP event loop.

That means that you can write code that looks like this aside. The fact that I'm using pre processes, which allows me to do really cool things like this or using short arrow functions in PHP. This code is using eras, which is a and PHPs asynchronous web framework. So I create a new host at port 80 80. I tell him which public folder to use. I created a writer. I defined some ruts. If I call slash it should just tell me, hello world. I've defined a new method called connect, which will tell me when it's connecting to something and it will connect to the board, same code as I had with the character stuff before and now it's running inside Eris inside and code again, I have my forward end points and I have my stop end points. Let's stop the character server and then we can use, okay.

So we can use something for vendor pre heiress, running it in debug mode, using the configuration file of source observed or pre it should boot up successfully. Okay. It looks like it has booted up successfully now because I have this new end point to connect to the Arduino. I can say connect and that'll tell me, okay, it's connecting to the racer. Some reason that's not giving me the exact feedback that I wanted. It's not the end of the world. I assume by now it's connected because the light is no longer blinking on the racer. If I go back to the camera, you'll see that.

No, I can pull forward on this stuff on this. Think about this for a moment. We've managed to run multiple web frameworks. Let's see. Final, put that camera back on. I'm sorry. We've managed to run multiple web frameworks, asynchronous HTTP servers, first characters, one amps, one to create HTTP API APIs to communicate over the web through Bluetooth, to an Arduino sitting on the back of an RC Rover. Wow, that's awesome. Because now we can create technology stacks that we understand things that run in...

SPONSORS

SPONSORS

SPONSORS

PHP Tutorials and Videos