The stock Debian image for the Raspberry Pi uses the UART as a serial console. I was able to connect to it from my Ubuntu laptop via my 3.3-volt USB FTDI TTL-232 cable. Using the Raspberry Pi’s serial port – Clayton's Domain. The stock Debian image for the Raspberry Pi uses the UART as a serial console. I was able to connect to it from my Ubuntu laptop via my 3. Footnote 1 Note: The SunJSSE implementation uses the Java Cryptography Extension (JCE) for all its cryptographic algorithms. Footnote 2 Cipher suites that use AES_256. A definite guide for you to understand the LinQ C# lambda expression and start working with it confidently. Feel the do more, write less power of lambda. Using Java for Serial Communication Introduction. Because of Java's platform-independence, serial interfacing is difficult. Serial interfacing requires a.USB FTDI TTL- 2. 32 cable. I connected Raspberry Pi’s ground pin to the ground pin of the FTDI, the Rasberry Pi’s TX pin to the FTDI’s RX pin and vice versa. The Raspberry Pi’s pinout is available here.) Then on my Ubuntu laptop I installed minicom (sudo apt- get install minicom) and fired it up with: minicom - b 1. D /dev/tty. USB0. After typing in a username, I got a password prompt and was able to log in. Also, the serial console allowed me to see all the kernel output during boot, which could be handy someday. But I wanted to use the Raspberry Pi’s UART for my own purposes, not as a serial console. To achieve that, I did the following. First, I made a backup of the /boot/cmdline. Then I edited it: sudo vi /boot/cmdline. Originally it contained: dwc_otg. AMA0,1. 15. 20. 0 kgdboc=tty. AMA0,1. 15. 20. 0 console=tty. I deleted the two parameters involving the serial port (tty. AMA0) to get the following: dwc_otg. I rebooted (sudo reboot) to confirm that kernel output was no longer going to the serial port. But the serial console was still available. So I edited /etc/inittab: sudo vi /etc/inittab. I commented out the following line: 2: 2. L tty. AMA0 1. 15. Finally, I rebooted again and confirmed that nothing was touching the serial port anymore. Then, to test it out I installed minicom on the Raspberry Pi: sudo apt- get install minicom. And ran it: minicom - b 1. D /dev/tty. AMA0. After firing up minicom on my Ubuntu laptop again, I was able to send data in both directions! Now to get the Raspberry Pi talking to an Arduino…. Connect Arduino Uno to Android Via Bluetooth: 6 Steps Command and message structure as described in the previous step// Serial Parameters: COM1. N 1// \r or \n to end command line// Bluetooth is on Pin 0 & 1 @ 9. Command structure // CMD RED|GREEN|YELLOW=ON|OFF// CMD TMAX|SECONDS=value// CMD SECONDS=value// CMD STATUS// Status message structure// STATUS RED|GREEN|YELLOW|TMIN|TMAX|SECONDS|TEMP|THIGH=value Initialization of variables needed for temperature controlfloat max. Temp = 3. 0. 0; // switch on led when temp > max. Tempint max. Temp. Sensor = (int) ((max. Temp / 1. 00 + . 5) * 2. Temp can later be changed, but the program needs a default value to start with. Temp. Sensor is the conversion of max. Temp to the 0- 1. Arduino ADC converter; temperature comparison will be performed by an interrupt routine that we want as fast as possible: it is more efficient to directly compare the integer Pin output value rather than the float temperature. We still want to report the temperature and the program will store it in the variable with the same name. If you are not familiar with the temperature conversion formula, you can have a look here. Seconds can also be changed with a command but again we need a defaultint max. Seconds = 1. 0; // send status message every max. Seconds Declarations of Pin constantsconst int led. Pin = 1. 3; // temperature ledconst int temp. Pin = A0; // T3. 6 temperature sensor analog input pinconst int led. Pin = 3; // Yellowconst int led. Pin = 4; // Greenconst int led. Pin = 5; // Red Variables used in the interrupt routine and accessed from outside of itvolatile int temp. Val; volatile int seconds = 0; volatile boolean temp. High = false; volatile boolean status. Report = false; Volatile is a special keyword that prevents the compiler from performing certain optimizations: all variables that are modified within an interrupt routine and are also accessed outside of it must be declared as volatile to signal that their value can change at any time and to make sure the latest, correct, value is read from memory when needed. Command string variables (they will be explained later)String input. String = ""; String command = ""; String value = ""; boolean string. Complete = false; The setup() functionvoid setup(){//start serial connection. Serial. begin(9. 60. Serial. print("Max T: "); Serial. Temp); Serial. print(" Sensor: "); Serial. Temp. Sensor); input. String. reserve(5. Mode(led. Pin, OUTPUT); digital. Write(led. Pin, LOW); pin. Mode(led. 1Pin, OUTPUT); pin. Mode(led. 2Pin, OUTPUT); pin. Mode(led. 3Pin, OUTPUT); digital. Write(led. 1Pin, LOW); digital. Write(led. 2Pin, LOW); digital. Write(led. 3Pin, LOW); The reserve method of a string allocates the number of bytes provided as argument. The following code is needed to initialize the timer interrupt and set it to fire every second, the slowest that Arduino can do; for detailed information see here. Timer. 1 for interrupt @ 1. TCCR1. A = 0; // set entire TCCR1. A register to 0. TCCR1. B = 0; // same for TCCR1. B// set compare match register to desired timer count: OCR1. A = 1. 56. 24; // turn on CTC mode: TCCR1. B |= (1 < < WGM1. Set CS1. 0 and CS1. TCCR1. B |= (1 < < CS1. TCCR1. B |= (1 < < CS1. TIMSK1 |= (1 < < OCIE1. A); sei(); // enable global interrupts} The timer interrupt routine: we cannot change its name, but the content is entirely customizable. ISR(TIMER1_COMPA_vect){temp. Val = analog. Read(temp. Pin); if (temp. Val > max. Temp. Sensor) {digital. Write(led. Pin, HIGH); temp. High = true; }else {digital. Write(led. Pin, LOW); temp. High = false; } The temperature value - or, as discussed above its 0- 1. LED is lit and temp. High is set to true, otherwise the LED is switched off andtemp. High is set to false. Seconds) {status. Report = true; seconds = 0; }} Remember that the interrupt is fired every second, but we want to report the system status less frequently: the seconds variable is incremented at each iteration until it reaches the values when the report is due; this will be done later in the main loop by checking status. Report flag. As a rule, never never perform something so slow such writing data to serial from within an interrupt routine. The loop() function interprets and executes commands when received, it then reports status if flag is raised by timer interrupt. In order to read a string from the serial buffer, loop() relies upon the serial. Event() function that will be defined at the end: this routine is run between each time loop() runs. It is not widely documented and it probably doesn't apply to all Arduino models; in any case, it's not difficult to nest its content within the main loop (see the end of thi step). Value = 0; if (string. Complete) {Serial. String); boolean string. OK = false; if (input. String. starts. With("CMD ")) {input. String = input. String. First we check if the received string starts with "CMD ": if so we can discard the first four characters, otherwise we'll later raise an error. String. index. Of('='); if (pos > - 1) {command = input. String. substring(0, pos); value = input. String. substring(pos+1, input. String. length()- 1); // extract command up to \n exluded There are two types of commands: those setting a value, where we'll find "=" separating the variable+value pair, and those where the command is a single directive (STATUS). If "=" is present at pos, the string is split into command (left part) and value (right part), dropping both the "=" in between and the end- of- line character at the end. RED")) { // RED=ON|OFFvalue. ON") ? digital. Write(led. Pin, HIGH) : digital. Write(led. 3Pin, LOW); string. OK = true; }else if (command. GREEN")) { // GREEN=ON|OFFvalue. ON") ? digital. Write(led. Pin, HIGH) : digital. Write(led. 2Pin, LOW); string. OK = true; }else if (command. YELLOW")) { // YELLOW=ON|OFFvalue. ON") ? digital. Write(led. Pin, HIGH) : digital. Write(led. 1Pin, LOW); string. OK = true; } We examine and execute the LED commands; note that the code only checks for value ON: if you write GREEN=ASD it will be interpreted as GREEN=OFF. It's not perfect, but it keeps things a lot simpler. OK=true is set every time a command is recognized and executed so that wrong commands will be flagged later. TMAX")) { // TMAX=valueint. Value = value. to. Int(); if (int. Value > 0) {max. Temp = (float) int. Value; max. Temp. Sensor = (int) ((max. Temp / 1. 00 + . 5) * 2. OK = true; }}else if (command. SECONDS")) { // SECONDS=valueint. Value = value. to. Int(); if (int. Value > 0) {max. Seconds = int. Value; string. OK = true; }} When value should be a number, we need to convert it and test it really is a number. In the case of Max. Temp, we also compute the sensor value as explained in the variable definition section} // pos > - 1else if (input. String. starts. With("STATUS")) {Serial. STATUS RED="); Serial. Read(led. 3Pin)); Serial. STATUS GREEN="); Serial. Read(led. 2Pin)); Serial. STATUS YELLOW="); Serial. Read(led. 1Pin)); Serial. STATUS TMAX="); Serial. Temp); Serial. print("STATUS SECONDS="); Serial. Seconds); Serial. STATUS TEMP="); Serial. Serial. print("STATUS THIGH="); Serial. High); string. OK = true; } // input. String. starts. With("STATUS") If command is STATUS, the program simply outputs all information to serial.} // input. String. starts. With("CMD ")string. OK ? Serial. println("Command Executed") : Serial. Invalid Command"); Signal if a valid or invalid command has been received.// clear the string for next iterationinput. String = ""; string. Complete = false; } // string. Complete Variable housekeeping for the next command iteration. Report) {temperature = (temp. Val * 0. 0. 04. 88. Serial. print("STATUS TEMP="); Serial. Serial. print("STATUS THIGH="); Serial. High); status. Report = false; }} If the interrupt routine has raised the status. Report flag, some information is printed to serial and the flag is cleared. Note that the current temperature value is calculated at this point: therefore, if you issue a STATUS command in between the status. Report interval, you'll get the old temperature value. As already noted, serial. Event() occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available. Event() {while (Serial. Char = (char)Serial. String: input. String += in. Char; // if the incoming character is a newline or a carriage return, set a flag// so the main loop can do something about it: if (in. Char == '\n' || in. Char == '\r') {string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2017
Categories |