Networking and Communications
29 November 2017 | By Casey Evans
“A brand is no longer what we tell the customer it is--it is what customers tell each other it is.”-Scott Cook, founder of Intuit
Neil says that the point of this class is to get rid of the sentence that “I’m MechE so I don’t know how to program.” As well as “I’m CompSci so I don’t know how to make.” But all this messy compsci guru stuff is intimidating.
Work not by demand on your time but by the supply of your time. Recitation one floor down. Sam will cover the XMega. PSoC. Processor core with soft modules around it. Arm programming. Build a wired or wireless network connecting at least two processors. Many reasons for this. The obvious one being talking to something that’s not where you are. Another is an interface on a device for a device somewhere else. Parallelism. AVR runs at 10s of 10^6 cycles per second but costs 10s of cents. Also can help with performance. Modularity is another good reason (separate subsystems to test independently). Interference. One circuit with high noise and all that is hard but if you have separate boards for each system then you can limit interference. Start with simple networks and end with radios. RS-232. Extensions for multiple devices. Bridge board, Tx and Rx lines onto a bus. Tx to Rx. Each board connected to the Tx and Rx lines. Tristated Tx. Give an ID to each board. Each board listens but only one board matches the message, when it hears its ID it changes to an output and sends a message. Simple serial bus for no more than 10 processors probably but they should be pretty easy to understand. “It’s really trivial to make a serial bus.”-Neil. Have to be well behave to take turns talking. I2C is a standard protocol. RS-232 is asynchronous, you just blurt out the data. I2C has a clock line as well as a data line. It’s a standard protocol for interprocessor communication. Prioritizes who gets to talk. Accelerometer speaks SPI or I2C. Atmel has app notes. TWI or USI modules. TWI is the same thing as I2C (Philips IP battle). Bit banging it where you use software to turn pins on and off. Programmer speaks SPI. Roughly equally useful SPI to I2C. SPI in circuit programming, I2C device networking. Sociotechnical history no deep reason. SPI is faster at really high speeds. If he controlled both sides Neil wouldn’t use either. USI section of the ATTiny44 data sheet for more info. V-USB is USB in software. An extreme version of bit-banging. It’s slow (USB 1.0) USB has tight timing requirements but it adaptively clocks in software by listening to the host. USB to FTDI project has been done. With MegaU2. LUFA is a good third party library using AVRs with hardware USB to implement various devices. Bus v network: network designed so anybody can talk to anybody and (ideally) know where each other are. Mac how you decide who can talks…oh, there’s loads of OSI layer.
Modulation: PCM (pulse code modulation), PPM (where the pulse occurs), OOK (presence or absence of a signal), FSK (two freqs, one for zero or one, needs a radio for more than one freq), PSK (change phase, only needs a radio at one freq but you need a reference local source to understand), QAM (phases and amplitudes to store lots of bits), spread spectrum (modulate to look random), DSSS (uses random noise), goal is to use bandwidth as effectively as possible, The Comminications Handbook by Gibson.
Channel Sharing: who gets to talk, Aloha you try and if you collide you try again, master-slave has one person tell when to talk, token ring token can get lost, CSMA how wifi and ethernet work (cocktail party, listen and if someone else is talking you don’t talk, pause in silence for a random backoff time before starting to talk; fails if network heavily loaded), CDMA (spread signal to not interfere with each other), MIMO (antenna thickets to steer the beams to get them spacially localized).
Error correction: expect to make errors so add redundancy.
Networking: there’s a good book on it, he’ll fix the link, topology to get from one point to another, internet engineering task force, IPV4 moving to IPV6, packets have a structure and IPv6 has a larger address space, NAT reuses addresses, unrouted private space you can use and assign yourself, IP is address on a letter, UDP adds ports (rooms in the building), TCP splits a big message into small messages, http is tcp to port 80, telnet, sockets are a standard protocol, JS and Python easy to talk to sockets, C is harder, IP over serial dates back to modems (Slip)
Radios: ARRL Handbook, oscillator to send signal, mixer to modulate data, low noise amplifier to receive, intermediate freq stage for partial amp, intermediate signal, filter baseband data. Picture a heavy chain and a skinny thread connected, vibrating thread will reflect back—illustrates need for impedance matching. Antenna gain focuses energy. Single chip radios. IS1871. BLE. System in chip. ~$7. Speaks a simple protocol. Antenna is happier if there’s nothing underneath it. Speaks simple serial. Nordic BLE sniffer. Castellated so pasted on the side of the board. Standard services. Event driven model. nRF24 has all kinds of great parts mixed in. nrf52. Care about it because it’s a very fast ring oscillator. Good radio performance. It’s a general-purpose processor that happens to have a radio. Sam is writing a page on using the nrf52. Requires a new toolchain and all that. Long term goals. Wifi is a power pig. 100s of mA. Requires a big regulator. TCP can go in either direction. Software radio – bit bang radio in software.
Takeaway – at least get the serial bus. Aim it at your final project. Not enough for everyone to have every one. ARM programming, nontrival learning curve.
None. But I did go to recitation this week.
So. I got my hands on a Bluetooth module. But that's about it specific to this week. I also spent time working through getting the serial connection to work (see week 12) and cleaning up my output week to be a bit cleaner and better documentation. But the real culprit here is the massive paper due this Friday that has taken me more hours than I'd admit and is still only 11/20 pages in the works and my signal processing project (where I was able to write 11 pages in only a few hours, go figure). Point is, weeks 7, 9, 11, 12 and 13 are all kind of intermeshed for me right now and I will be taking the weekend to sort them out (after the paper, project and pset have been turned in and forgotten).
So it turns out the module I grabbed (the nRF24L01+) really doesn't do BLE as I thought it would. Or if it does, the datasheet isn't very helpful with it and I, with my limited experience, would not be able to pull it off in a reasonable amount of time. So I grabbed a RN4871. This chip wants 3.3V when using FTDI, so I'm thinking to just make a step down voltage divider so I can keep using my 5V FTDI (which I need to buy off Gavin or somehow otherwise aquire). But I'm not sure if the Tx and Rx lines will complain about that. I think for the Rx line I'd have to make sure that the 3.3V will pull up to 5V on the other end. Maybe I only need them to program though in which case I don't need to worry about anything except power, which the voltage divider should work well for. Next step is to read the datasheet so I'll let you know.
Okay, so the User's Guide is here. The other link is the datasheet which wasn't especially helpful. But moving forward I can't find a 3.3V FTDI so I'll try to make it 5V compatible with a voltage divider and an opamp. It's been a while since I've used opamps but here's a simulation I did to see what I would need from the BLE Tx pin to the FTDI Rx pin:
The ground can be common and the Vcc and Tx from the FTDI can be stepped down with a voltage divider. The datasheet says it can operate from 1.9V to 3.6V so a 499ohm and 1kohm voltage divider (the parts available) which gives about a 3.33V output should be fine. I did a simulation to check:
In order to figure out the configuration of the opamp I needed to use a pin diagram. THe one for the fablab opamp is shown below:
Now to draw up a board and print it out. Also, just for reference, I like this version of the fablab inventory. Right now I'm reading through the BLE documentation for App Inventor. I think the way this works is that I could send the BLE commands from the app and then write to the Tx line which could go to the Rx line on the ATTiny controlling the LED bulb. Not totally sure there. I just ran that by with Natalie and she said that's pretty accurate. I can also use GPIO pins. It took me a bit to get the layout working for the modified 5V FTDI compatible chip (at least, I hope it's FTDI 5V compatible - I worry that the BLE Tx line might get pulled up by the opamp but I don't think it will matter too much - at worst it will bring it to 3.3V probably which the 3.3V FTDI would have also done). Well see. I'm an experimentalist and the time has come to try. Actually, I not really either but I think I should just try it rather than worry about it. Well I machined it and the size is just ever so slightly/majorly off that it won't work. And I accidentally ended Priyanka's job while it was calculating so I'm letting her have a turn while I debug. But Niki had a 3.3V cable so I'm going to just try using Neil's native board first then. If it helps my almost board was:
Remember that the scaling is a bit off for the parts that came from Neil's board originally. The parts I ported from Eagle were scaled as I set them. Oh well, that would take time to fix. I'll see how the original board works with a 3.3V cable. I did find the ratio Neil's parts were off by at 1000 dpi. It should be 0.614585 in tall. So I tried that and it was giving me funny issues with the traces so I left it at 1000 dpi and had the same problem. Geez, I gotta learn from my mistakes. And the lab's closing again so I'm going to go home and get some CADing done for tomorrow (bulb, lampshade, stand) and maybe start working with App Inventor. I may have to tell my advisor that I can't come in tomorrow morning. I dunno. Such stress. Very wow. So I did a lot of sketching out things and
Okay, so after much troubles I was able to build the Bluetooth board Neil laid out. Looks great, right?
I plugged it into my computer with FTDI and was confused for a bit until I read the user's manual a few times with Mike. I used PuTTY as a terminal simulator to talk to the thing. After a bit of flopping about I discovered typing $$$ into the terminal gets you into command mode with the BLE. After that you can type commands and hit enter to send them to the device. A good start is the command '+'. That activates echo so you can see what you're typing. Then 'SS,C0' enables the 'UART Transparent service' which is basically how the thing works. You can name your device with 'S-,string'. I called mine ILoveLamp since that's what my roommates have been calling it. Microchip has two great apps for development: SmartDiscover and SmartData (which you need to search in the app store with "Bluetooth Smart Data." These are Apple apps. They look like this:
I'm not sure if they have Android equivalents. The first one finds your device and the second one is a little terminal where you can send messages back and forth. I did that a bit. Nearby BLE devices come up on your screen like so:
Then you just tap to connect. And you get a screen like this:
And when you change the name of the device with "S-,name" it can look like this (note same UUID):
You can send the device data via the SmartData app. I sent a few messages that came through. This is the PuTTY terminal simulator output of what I sent:
Then I tried out the pin setting in the command line. The pinout of the RN4871 is:
I was switching pin 1_2 which you can do with '|O,08,08' to set and '|O,08,00' to reset. There's a table I still have to see if I can enter command mode from the phone terminal, figure out how to send things back to the phone (ie get to the serial terminal of the device) and how to get it to work with App Inventor. I think my networking segment might be to connect the Tx on the BLE to the ATTiny just to see hello echo, but I'll look into that when I have a working lamp. Okay, so sending it '$$$' via the app doesn't cause it to go to the command mode.
In looking into App Inventor I found this helpful site. And it's been a while so I'll remind myself of the BLE documentation. This also might be helpful. I started making the app with the notifications and connection settings. Still trying to figure out reading and writing. Mostly I worked on the relay and logistics of the physical part of the lamp. I'll come back to Bluetooth after I finish updating the documentation.
Okay. I'm looking more into the User's Guide now to see if I can figure this out. I want to be able to send it commands from the phone if possible. D gives he device information which I will need since app inventor's code uses MAC addresses to connect to BLE. This means I'll also need the &C command which will make my device use the MAC address instead of a random address. I'm not really sure what all that is about. And I plugged it in backwards to the FTDI and I think I've killed it. FML. 2 hours later (because I'm much less competent than Neil), I have a second board that works. It's MAC address is D88039F75654. Don't plug these guys in backwards. They are not fans. I need to do some back tracking to explain other things but for now I'm going to jump ahead a bit because I found this great YouTube video to help with AppInventor and BLE. I also found this site very helpful in understanding how BLE works. Here's another link I found helpful.
And now I'll go back. So fun fact about the new board. It can talk to my phone when I type in the terminal emulator. So cool! I think it was one of the settings on my other board that I was playing around with and didn't understand that was preventing me from talking to the phone using the other board but now I'll never know. Anyways, here's a picture of the SmartData app listening to the BLE talk to it:
That whole setup looked kinda like this by the way:
In preparation for switching my lamp I modified the original BLE board to accept two pinheaders (with much soldering patience). That new board looked like this, though I eventually added hot glue to the pin headers and FTDI headers:
I used the new skill I learned a few days back from Natalie's friend to check electrical consistency and make sure my board wasn't shorting. Even under the microscope it was kind of hard to tell. Anyways, after reading those resources above I updated my app (detailed in week 12) and was able to connect to my BLE via the app. On the terminal side it looked like this:
Super cool. Then, as I tried to figure out how to flip the pin, I read in the user's manual about running a script that would tie a handle to setting a pin. I set that up but I don't think I really understand how to run commands and a script at the same time. You might not be able to. That is something I still could try messing around with. Anyways, it took me a bit but I finally figured out what handles were by learning about UUIDs and characteristics and services. There are some good references in the links above, especially this Nordic site and this textbook site. I ended up defining a local characteristic as a read, write and notify characteristic (since that was the example and I knew I wanted reading and writing). I'm still not sure about notification but I figured it might not hurt if I don't use it. I also don't understand the difference between write and write without notification. It might be one of the sources of my problems in actually flipping the pin. Here's a picture of the list of my services and characteristics (listed using the command 'LS':
When I used the new app I developed to try to write to my characteristic, I was able to see the write request in the terminal:
I was curious about the threes and tried many different numbers and strategies in the app but still got them in the values I was trying to write. Not sure what's up with that but I can send and recieve stuff from an interface I designed. Here's a picture of the terminal after I went a bit deep into the 'why are there 3s' question:
It's been an interesting project and I look forward to working with Bluetooth again in the future.