 Alright, sorry for the delay in the beginning. Okay, I am uncapped and okay. It's just running away from me. Alright, so first of all I would like to know who of you are familiar with Arduino. Like, can you show your fans? So you've already done some kind of Arduino programming before. Any other hardware, any other platform? Alright. And who's not done anything at all and wants to know how to get started? Oh, brilliant. Awesome. Alright, so what I'm going to talk about today will be a little bit about how you can use Arduino and Python and mash it together to make fun connected objects. So for those who have not worked with the Arduino before the Arduino has a bunch of functionality. So you can basically read and write analog and digital signals. Essentially anything that is a continuously varying or a binary varying physical quantity gets converted to an electrical signal. You read it so you can do both analog and digital. You can also set up networks of devices. So there are a few communication protocols which I will come to. You can also use it for timing or counting signals. So the functionality that I will be using mostly in the first half is the serial data. Serial data there are quite a few different protocols, quite a few ways of sending data one after the other. Some of them I have listed them there is UART which is basically communication between two peers. So it can only allow two devices to communicate with each other. The pins are RX and TX. So you basically cross wire them and they keep on sending and receiving data. The other protocol is SPI which lets you have a whole network of devices. So let's say that you have one master and four slaves and you use certain hardware lines to select which device to talk to and then you can basically exchange data and that's full duplex which means that you can send and receive data at the same time. The third one is I2C which is a lot like SPI, lesser wires but with addresses but you don't want to get too much into the detail and then there's hardware which means you manually generate the signals. Basically the signals that you see all of that are the first three are handled by the Arduino itself but the fourth one you can also take it up to you to generate it yourself. So you can write your own protocol if that works for you. So the format I'm going to follow is I'm going to tell you one way to use it then I'm going to walk you through an application and show you the code to do it. The first one is the simplest. It's basically using Pi Serial. It's a library based, built to communicate to Arduino over serial. It lets you send and receive data from your computer. So if you see here, for all our purposes, there will always be two sections. One is code which runs on an Arduino and the other which is Python. So first of all, this code, this section run in here, the Python code runs on a computer and not on an embedded device. So on the left, if you want to start a serial communication all you really need to do is say serial that begin with the speed of communication and your communication channel has already been set up. Now it communicates to the computer over USB. On your Python side, you just write import serial. You create a serial object like this. You select which port which you need to find out from your device manager and the speed of communication. So your communication protocol has already been set. Here if you want to send some data from your computer to your Arduino. So I'll give you an example. Let's say that you have a NFC reader or something which has some data stored on it and you want to send it to the cloud or process it offline. A lot of times your device might not be able to do it on board the device itself or for security purposes you might choose not to. So what you could do is let's say you have a NFC reader. You read it on your Arduino using an NFC shield. It sends the encrypted data to your computer. You upload it to the internet or first you decrypt it to you process the data and then you encrypt it again and send it back. So this would be an application of what you could do with just this basic code. So basically you say serial.printline that sends data from the Python to Arduino and here you say serial.readline and almost similar the other way around. The only difference here is in Python you need to say serial.available. Oh, this is the other way around, is it? No, this is fine. So on the Arduino you need to say serial. Yeah, it's flipped over. But you need to say serial.available because a hardware device is dumb, right? Not as compared to a computer, it's dumb. So it does not know when to stop listening to data or when to start listening. You need to specify that. So here you just say if data is available then read it. Otherwise it's just going to wait forever. All right. That was one example. The other one I am going to show you a demo first. There's no sound for it so. So what I have connected is basically a sensor and I have used matplotlib which is a plotting library in Python. And I just run a simple invocation and that creates a graph. This is a simple light dependent resistor, something that detects if light around it is changing or not. So you could use this to debug your sensor data. So when you just move your hands around, you will see that the graph changes. So you could use this for visualizing data real time like monitoring temperature, humidity, whatever you need to. Let me go back to the presentation. All right. So how we achieve this is fairly simple. On your Arduino side, you are going to be sending just the sensor data. So you see serial dot print line and the value of your sensor. And that's all you need to do. You just send this one line of data. It keeps on sending the data whenever it has it from the sensor. On Python, you need to do a little bit more. You need to create a figure object in matplotlib. You need to specify the axis and the limits. So here we have chosen to use 100 data points. So you will create the limits of the x-axis as 0 to 100, which is basically time. And the y-axis basically is the value that your sensor gives out. So you plot that. Here we're using this animation object basically to create a moving sequence out of a series of images. So what this does is basically smooths it over by calling a function again and again. This line of code converts a simple image. So every time you have a snapshot of 100 values, right? So it just smooths it over and converts it into a transition. It's done. All right. The third way is PyFarmata. So what happened was if you have an Arduino and you have code written for it, you can't see too much unless you've connected a motor or lights or a screen. A lot of times you might not actually need to control it physically. You might actually want to control it via a computer. You might want to remotely unlock or lock or start or stop something. And how do you do that? So PyFarmata was basically written with the idea that there should be a computer interface to control the Arduino or any other microcontroller-based system. So how the system works is there is standard code running on an Arduino, which basically you can modify as well, but it's already established. For different purposes, there are different versions of that code. So if you're just in your Arduino, I'm not sure if I have Arduino installed on this machine, but if you just go to examples, you can just directly upload it. In Python, you need to do this. You just say you need a PyFarmata library. You import the Arduino. You create the board on this connection. So you say that on DevTtyACM0, my Arduino is located. Now, after this, the job is supremely easy. You just have to say board.digital. So you select the pin number 13, which is what I want to trigger. And I just say write. So I can either write a high or a low, a 0 or a 1. And I have that working. The same with read. If you want to do analog, though, it's slightly tricky. So what happens is the WaveFarmata handles an analog read. It continuously reports value, and you need to specify when to start and stop. So you just say enable reporting and read. But that can easily be handled as well. We will now move on to the more fun part of the talk, the one which involves the internet. So the UN is a relatively new Arduino board which has a Linux processor on board. So other than having a standard microcontroller, it also has a processor which can run a modified version of Linux, which means you don't really need to have a computer connected. The first half that we saw, you can eliminate that. So some of the features that you could use are an Ethernet port, a USB cable. It has Wi-Fi connectivity. It can also act as a wireless hotspot. And it also has SD card support. So how it makes your life easier is essentially that we use the serial library to be sending data from an Arduino to the computer and back. Here there is this library called the bridge library. This one here. This handles the communication in between the two sections on the UN. So if I want to send data, all I have to say is bridge dot begin and then dot send. And it will just send that data to the Python side and into the Linux side. On the Linux side, all I have to say is print line. Like in Python, it is a print line or in C, I say C out. And whatever is printed there can be read directly without any additional effort on your side. So let me give you an example. So one thing you could do is, for example, set up a data logger, which basically logs all your data to Dropbox. So how that system works is basically that there is this Linux controller, the ER9331, which is running Python. I have written a script for that. I will walk you through the code. This one, the microcontroller gets data from the sensor. It sends it to the ER9331. This performs two functions. The first one is it saves it into a log file so that we have offline data in case we need it. And since we are using the file API in Dropbox and not the data store, what we are doing is we are taking this file and every time we're just uploading it to Dropbox and using it in replace mode. So you're not appending data. We will just replace the file every time because we do have a new file. Let me show you a demo. Okay, I don't have a demo for that. I have code for that. So there are two parts again. One is basically the Python sketch which runs on an SD card or a USB drive. And the other is the code which runs on a Linux, which runs on the Arduino. The Arduino code you upload via USB. And the Python code you need to plug in a USB drive so you can either SSH into it or you can just put it on a pen drive and then make it run. Let me open this. Can everybody see this? Is it text visible? All right. So there are a few parts to the Dropbox API. The first one is you need to authenticate yourself. You need to get a token. After you have the token, you can just use it to continuously upload the file. You don't need to do anything else after that. So we are saving the authorization in a config file. We are using the config parser library for that, the module. So I have this file called Dropbox underscore auth dot CFG in this location. So this location is where my USB drive or my SD card will be mounted, which you need to set yourself or figure out. Now I have written a simple Dropbox logger class which needs an app key, an app secret, an access token, file name. So when it is created, it tries to fetch the configuration. Let me zoom out a little. So it tries to fetch the config file with the authorization. If it does not exist, it creates it. If it does exist, it checks whether there is a valid authentication or not. Once it finds that, it's really simple. This is all basically code to create a config, fetch the config, get the access token. So one trick with using Dropbox or Facebook or even Gmail is that you need OAuth authorization and you can't do that without having a browser window. It's really, really tricky. So what you need to do is the first time that you run this, you need to have connected to a computer and on the console it prints out this link. You need to go to that link, authorize the app and after that you're good to go. So there is a simple upload file function which basically opens the file and it says put file. The Dropbox already has a Python SDK so it handles everything and we've put it in overwrite mode. The next application is PyDreat. This is much, much simpler and a lot less complicated than Dropbox even. So if you've heard of the Internet of Things or if you're very interested in it, you can implement this without literally any effort. So PyDreat is basically this messaging sort of layer which lets you create an address on the Internet and using get and post requests, actually just get requests, you can send and receive data. So how this would work is you have a sensor, you want to send data. So you just write Python code to post it to DeWit and you can fetch it on your computer or mobile app so you can pretty much connect as many devices as you want to it without much effort. So how you would call this is you could actually test it yourself right now if you like. So you just go to DeWit.io, DeWit for and then you can use your own UI. You can say, you know, MyDreamLand server and then you pass on JSON string where you give the two parameters, the key value pairs, it will return a response which says it succeeded. Then if you want to fetch it, it's exactly as simple as get latest DeWit for and MyDream server and it will fetch that. This is not secured, of course, so if somebody knows what your UID is then they can mess around with you. But you can secure it, they do have a paid service. Let me walk you through the code for that. This is a very simple wrapper actually, it just uses requests. Alright, so the invocation for this is also like this, right? You can use on your computer or on your UN that doesn't matter. You say Python, DeWit.py, you put in your own unique ID and you say get and it will get the response. If you want to send data, you say Python, DeWit.py, your UID, post, you give it a JSON formatted string and it will post the data. This is literally how much code it is. That's really it. Get DeWit and post DeWit. So you use the request library, makes your life a lot simpler, a lot less effort. You also need to write code for your Arduino, of course, to trigger the script. Let me fire up Arduino. Alright, so there is a library called the process library. You say hash include process.h. You have two sensors, a01. You start the communication between the Linux and the microcontroller. While there is no connection, do nothing. Now, you just read the sensor value using analog read. You build up your query string and you send the data. Now, let me show you how sending the data works. You create a process called DeWit. So all of this is highly abstracted. It has already been handled by most of the libraries in C and Python, written on both sides of the Arduino UN. So you create a process. You say run shell command. So it's basically an invocation on the command line. You run it and if there is data available, print it out. You have the data already on the UN site. The same with kit. You run the shell command. If there is any data, you just do a print and that prints out the data on your console. It passes on back to the microcontroller. All right. You could also, for example, use the Google Calendar API and have something like a smart connected calendar lying on your desk, which you can have a small screen with, which will remind you of your next meeting. You could further extend it to beep and you could even extend it to allowing somebody to set alarms for you. So if your friend wants, he can actually bug you and say, hey, you know what you need to get out because we have a meeting. So all of this, of course, you need to give them access, but it's possible. Let me walk you through the code for that. So here we're using the Google Calendar API. We are again built an SDK around it. I cannot share the client secrets because this is being recorded. All right. So the location for this is really, really simple. I've just said Python, sample that pie. So I don't really need any parameters because I know what I'm doing here. I just want to fetch the latest calendar data, get the next one event and display it. So every time there's a new event, what we are going to do is we are going to connect to the Google Calendar API, download the list of calendars that you have, choose a calendar that you want. So there might be a work calendar and home calendar. You might have a lot of different calendars. You use the one that you want. You select the next event that is going to happen. So you specify the current time as a parameter and fetch the events. The next event is what you want to display. So that will be saved in a text file offline. So in case there's no connectivity the next time, you still have accurate enough data. So these are a bunch of imports. We pass arguments. So these are the URLs that we are going to use. GoogleAPI.com auth calendar. All of this is handled by... Most of it is an example code for the Google Calendar API. So you can just grab this. And here is where most of the work happens. Get event file load events. So what we are doing here is we are checking if the time delay. So what we are doing is we have a 900 second delay. So every 15 minutes I want to go and check if there is no events or not. You can change it as much as you want. So you could make it 9000 if you want to check every 150 minutes. If the time duration has passed and the data is old enough, then you list all the events. So basically that gets a list of events from the Google Calendar API. Then you save it in a file. Also this list, out of that you fetch the first element and you save it in next event. And then you just print it out. If you see here, this line here, right? This is basically all you really need to do to send data to the microcontroller. So if you just say print, it of course displays on your standard shell and that can be acquired directly from the microcontroller. This is a bunch of authorizations. So list calendars again, this part of the code handle is the calendar that you have created. This part supplies the time zone, the starting time and a few other things. And it returns if you see here, events.get. This is basically what you need, this and another one. We are saving the events to the file here. You save events using tickle. So you save and load events from a text file using that. Let me show you a demo of how this works. So here it's just waiting, right? You just go to your, I mean just for testing, you go to your Gmail, you create an event, specify the time and it shows up on your desk. So you can do that coming back to the presentation. What you can also do is use the Facebook API, again Facebook as a Python SDK. So you can use the Facebook API, connect to your pages, read your inbox, fetch your page likes, manage your pages even. So you could probably connect a bunch of sensors or physical controls and let people interact with your web page. I unfortunately lost the Python code for that. I do have the Arduino code. So we're using the TFT library here, the TFT screen here, the screen that you see. In here, this is the TFT screen. So this is slightly different from the normal 16 by 2 standard screens that you get. So what we're going to do here is there's a library for that, of course. I mean, just libraries for everything. So this is a bunch of setup stuff. So you set the background as black. You set the color of the text as white. You set the text size as 2. You set the first text in the starting of the screen. So if you see the 00, that specifies the X and Y coordinates of the text. In the loop, we just create a process where we trigger this Python script. And if it is running, then do nothing. After it's done, this one will return true. Wi-Fi check dot available will return true. So if there is any data, then get all that data. This is basically the results that Facebook returned for you. This is what we printed out. And after that, we just convert it into a character array and set the right position, set the right colors, and print it out. If you see here, TFT dot text, it just prints out the value. All right, and I'm going to put up the stock on this URL. So you can just download the code for that as well. So the code, the GitHub, and the presentation will be here. This was my presentation. If you guys have questions, we can take them now. Was it that easy? I mean, nine-year-olds can do it for sure, but they're there. You told the world that a Firmata library for Python. So what's the limitation of what? One big limitation is it's really, really slow. The way that it's been written, there's a lot of delay in between. So how this Firmata library works is that there's a protocol which has already been established. The standard we have communicating. So if you say that digital write, like, turn the spin on or off, then there is a sequence of characters which the Arduino is going to send, or the Python site is going to send. Now, the way that this has been written, it's very, very slow. So there's a lot of delay in between, so it does not really respond as well as you'd like. So for real-time applications, you need to tweak it a lot. Okay, how about, like, you can connect more than one serial device to Arduino, actually. Yeah, you know that, right? I didn't get you. You can connect more than one serial device to Arduino. Yes, you can connect it to any different protocols. Yeah, it was using software serial. So will it support, like, if I want to connect GSM to Arduino, it works on there. Yes, you can do that. So Firmata basically is standard in the sense that there is code running on your computer and running on your microcontroller. So if you want to use software serial, you can do that as well. Okay. So you need to use serial to get data from the Arduino. Yeah. So after that, to convert, to send it to the GSM shield, you can always use software serial. Okay, from Firmata, I mean, from Python code, I can, I mean, I can access that software serial. Yeah, so how you would go about doing this is you have Python code running on your computer. When you run that, that code sends serial data. Now the only way you can send data from a computer to your Arduino is via serial, like serial dot write, serial dot read. Yeah, I can, I can get that. Yeah. And what I'm saying is also we, while using software serial, I'll be inputting the software serial, I'll be into my Arduino ID or whatever Arduino code. So after that, you can communicate with the GSM shield with that. Okay. So you need to use serial in the first part of getting data from the computer, but the second part of connecting to your peripherals, you can use software serial. Okay. Okay. Any more questions? Any questions? Yeah, there. Can you shed some light on 80 tiny processors, microcontrollers? That's not exactly relevant. The 80 tiny is basically a different set of microcontrollers. So a lot of microcontrollers are made for different reasons. Some are meant for cheaper costs. Some are meant for lower number of pins, for higher number of pins, for specific, like, for example, if you want more PWM, less PWM. So these really depend on what they are made for. So that's not exactly relevant to it. But you can, of course, create your own kind of Arduino compatible board around the 80 tiny and then use it along with the same code that we wrote. And also, I have been using PySero for this kind of communication. Okay. So sometimes I get a pipe broken. So that generally happens when you have terminated, so you unplugged it. Okay. Right? So from one end the connection has closed, but the other side has not gotten the communication. Actually, it was showing in the, like, the ID of the board. But the connection was not happening. What used to happen was, like, when I used to send data to PySerial and get the response back from the Python code, so that time it used to break that connection. Yeah. So that would happen because if your OS reevaluates the port, maybe it is unplugged, reset or something, the port on which it is there changes. The OS, the Python does not really know if that happened. So it's like saying that the connection broke in between and before while communication is happening, but the notification did not reach the microcontroller, the Python part. Does the board rate affect this time? No, not really. Okay. Thanks. Yeah. Him? Yeah. Can I directly address the pen on the Arduino through the Python code? Yes, you can. Not via Python, but there is a get request and post request API again. So if you just on your microcontroller, if you see get, and then you put in the URL and you see pin and digital and digital, it will get it directly without you having to run any code even. So it's like a small... Yes, there are quite a few. So because it's Linux, there are quite a few scripts running in the background which are ready to respond back with data. Is there any simulation library for Arduino? Can we simulate in Python? In any... So I mean there are quite a lot of people who offer simulation tools. So for example, they're simulating if you use that. I'm not sure if DINPAI supports that yet, but there are quite a few simulation libraries for that. Yeah. All right. Thank you, Ankit. Yeah. Thanks for your wonderful...