Login / Register

Arduino / Raspberry Pi Networked Controller – Part 2

Some time has passed since I first wrote about the Raspberry Pi / Arduino Mega2560 Networked Controller, but it has not been time wasted.

The code for the Mega2560 now supports up to thirty-six discrete devices, which can be any combination of Maxim DS18B20 1-Wire Temperature Sensors and Maxim DS2406+ OneWire Switches. The devices are polled round-robin style and updated continuously. There is also an array of twelve “action” structures that allow a specific temperature sensor to be associated with up to two specific switches. Setpoints can be set for each switch, allowing them to be turned on or off, which in turn can be used to drive solid state relays for electrical devices such as heaters, refrigerators, etc. Yo can also introduce a delay, in seconds, that will prevent the switch from activating one the trigger temperature is reached. this will help prevent “short-cycling”, which can damage some older compressors.

The RaspberryPi (“RPi”) has code that communicates through a socket daemon to the Mega2560. It’s a 115200 baud serial link using the GPIO header on the RPi and the TTY1 port on the Mega2560. A series of simple commands are used to interrogate the Mega2560, and a standard LAMP configuration drives the webpages.

The RPi uses the standard Debian “Wheezy” configuration, with some minor adjustments. There is a Raspbian SD card guide that shows how to get the distribution onto an SD card.

Follow this tutorial to get your RPi setup for use as a “headless” webserver. All subsequent work is done via an ssh terminal and an internet or intranet connection.

Once you’ve got the OS working, you need to prevent the serial port “/dev/ttyAMA0” from acting as a console for the OS. To do this you must edit two system files. This must be done as a superuser with the “sudo” command. The first file to edit is /etc/inittab. Once you’ve opened the file with your favorite text editor, comment out the lines that spawn a “getty” on the “/dev/ttyAMA0” device. The line should look something like this:


#Spawn a getty on Raspberry Pi serial line
# T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
# T0:23:respawn:/sbin/getty -L ttyAMA0 9600 vt100

Next, you need to remove references to “/dev/ttyAMA0” from the /boot/cmdline.txt file. Change the file from something like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

to this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Since I’m using the RPi in a “headless” mode, I want to maximize the amount of RAM available to the OS. The normal startup configuration splits the RAM to allow the on-board GPU to run in 1080p mode, something that is not necessary in this configuration. To maximize the RAM available to the web server and other OS programs, use the “sudo cp” command to copy “/boot/arm240_start.elf” to “/boot/start.elf”. Reboot your RPi and continue on.

The RPi’s GPIO pins are 3v3, while the Arduino is typically 5v, so any attempt to connect the Arduino directly to the RPi GPIO pins should be done through some type of level shifter. You can make your own breadboarded level shifter using mosfets and resistors as described here, or you can just buy an Adafruit board and be done with it. I did it both ways, and prefer the latter.

Through the level shifter, connect the Mega2650’s TX1 pin to the RPi’s RX0 GPIO pin, the Mega2650’s RX1 pin to the RPi’s TX0 GPIO pin, and the Mega2650’s D8 pin to the RPi’s GPIO17 pin. You can refer to the following pages for pinouts:
Mega2560
RPi

I also prefer to use the powered mode for the 1-Wire “mini-lan”, which requires a 4k7 ohm resistor tied between the Vcc line and data line of the 1-Wire devices. To this end I connect the 4k7 ohm resistor between D9 and 5v on the Mega2560 and run the Vcc, data, and gnd lines out to a suitable 3-conductor connector.

The entire setup is currently breadboarded, and looks like this:

The Mega2560 is to the left, and has a prototyping shield that contains the level shifter and 1-Wire connections. In the middle is a breadboard with the Maxim chips, while the RPi is situated to the right. Everything is being powered by an active USB hub at the moment, although I did procure a 5v 5A switching power supply for the final product. The breadboard currently has 4 DS18B20s, and 4 DS2406+ switches. At the very top of the photo is a 25A solid state relay (SSR) which is connected to one of the switches.

The Arduino Mega2560 was used to allow me to debug the Arduino code using the Serial Debug Monitor available on Serial port 0, which is not available on the Uno without using a SoftSerial library. The Mega2560 also has 8k of RAM vs the 2K on the Uno. This allows me to have more chips and actions than the Uno. Once I’m satisfied with the code, I may try to port it to an Uno to provide a better perspective on the differences.

I plan on using standard telephone cords for the temperature probes, as it is inexpensive and easily procured. My work recently transitioned to a VOIP-based phone system, so I managed to get a large quantity of 4-conductor phone cords free, which also affected my decision.

The Mega2560 code is available for download at the bottom of this page. Its job is to interrogate and update temperature probes’ status, interrogate and update the switches’ status, check for any actions that require a switch to be turned on or off, and respond to commands from the RPi.

When the Mega2560 powers up, it first checks to see if the chip address/status structures and action structures have been previously saved to its on-board EEPROM. If not, the Mega2560 conducts a search of the 1-Wire mini-lan, saves the addresses of all available sensors and switches to the address/status structure array, initializes the action structures array, and saves both arrays to EEPROM. If the Mega2560 determines that the data has been previously saved, it proceeds to initialize the arrays with that data. It then begins its round-robin status and action checks, while listening for commands from the RPi.

The RPi is responsible for serving web pages and issuing commands to the Mega2560. The inital page simply shows all available sensors and chips:

This page is refreshed about once a second, and the switches can be turned on and off by clicking on the green “ON” or red “OFF” areas of a specific switch. If the switch is being controlled by an action, it will be set based on the action, and will override any selection made by the user. Pressing the “ASSIGN” button for any of the temperature sensors will take you to a page to allow you to assign that sensor to an action:

This page allows you to assign up to two switches that will be controlled by the action. The unique addresses associated with the switch is entered into the appropriate field. You enter the temperature a which you want the action to occur, and a delay in seconds if you wish the action to occur some time after the temperature is reached. The addresses are displayed on the same screen, below the action table. Once all of the information is entered, select the “Enable” radio button and click on “SUBMIT”, and the action array will be updated and saved to EEPROM.

The final page (for the time being) is the Status Action page:

This page is also updated about once a second and show the status of all actions selected, enabled or otherwise. As with the home page, switches can be turned off and on manually, but will be overridden based on the parameters of the action array. Temperature sensors may be assigned to more than one action array, allowing one temperature sensor to have multiple effects, for instance turning on a pump and a cooler at the same temperature.

And now the code:
SerialSlaveRPi.zip contains the Mega2560 code. It uses the EEPROM Anything code, and the OneWire Library, which you can read about here.
RPiWebFiles.zip contains the web code. Place it in your apache document root. In the head.html file, you can replace my logo with your own.
buttcrack_daemon.zip contains the C file and compiler instructions for building the daemon, as well as the compiled code. This was compiled using the WiringPi Library, so make sure you download and install the library. I also have the following code in a shell script that I call up periodically via crontab to make sure that the deamon is still running, as well as getting it running at startup.

#!/bin/sh

if ps aux | grep -v grep | grep buttcrack > /dev/null
then
exit 0
else
rm -f /tmp/bcserver
/"directory to daemon"/buttcrack_daemon
fi

If you add anything cool to the software, find bugs, or just want to comment, please feel free to Contact Me.

3 comments to Arduino / Raspberry Pi Networked Controller – Part 2

Leave a Reply



Charlotte Crosby pulls a series of animated facial expressions in Sydney She complained of a problem with her eyesight earlier in the week. And on Friday Charlotte Crosby was seen pulling a range of facial expressions as she made her way through the Sydney's CBD. The 26-year-old was dressed for comfort in a pair of skinny jeans and a white top, and at one point pulled her long tresses across her face as she made her way through the streets. Scroll down for video Funny girl! Charlotte Crosby has put on an animated display while out in Sydney on Friday pre bonded hairDespite the winter chill, Charlotte completed her casual summery look with black sandals and a grey fringed bag slung over her shoulder. She kept her brunette locks out and straightened with a pair of dark shades on top of her head. The former Geordie Shore starlet previously revealed she was suffering an eyesight problem, earlier in the week. Taking to Instagram on Thursday, Charlotte complained that her eyes were sore after falling asleep in her contact lenses and that she was forced to cancel a trip to the Sydney Opera House. Covering up: The 26-year-old was dressed for comfort in a pair of skinny jeans and a white top, and at one point pulled her long tresses across her face as she made her way through the streets

Ouch: The outing comes after Charlotte was forced to cancel a public appearance because she hurt one of her eyes However she later shared a selfie that showed off the windows to her soul adorned in sparkly gold makeup. The reality TV star told Daily Mail Australia on Thursday that her eye felt painful on a flight from Melbourne, earlier that day and she was worried she had scratched her cornea. remy hair extensionsShe was spotted arriving at Sydney Airport concealing her eyes behind sunglasses. Sore? At one during point during her stroll, the former Geordie Shore starlet looked to be trying to cover her eye Enjoying Australia: Since landing Down Under last weekend Charlotte has been busy enjoying the sights of Sydney and Melbourne She previously tweeted: 'I'm going to the OPERA! How sophisticated! And not just anywhere, (the) SYDNEY OPERA HOUSE'. But Charlotte confirmed she wouldn't be watching the performance of French opera Carmen because she could barely keep her lids open. The Ex On The Beach star also said she was disappointed because she'd picked out 'such a nice dress' for the occasion. Recovered? Taking to Instagram on Thursday, Charlotte complained that her eyes were sore after falling asleep wearing her contact lenses ‘I can’t put makeup on this eye because it’s hurting so much and that would irritate it,' Charlotte said. 'So then what am I meant to do when I’ve got to wear my lovely dress and there will be 5,000 people taking photos? I’m just going to feel awful all night. 'It’s also hurting so I don’t think I could sit and watch the opera for that long because I’m in pain and it’s hard to open my eyes.' ‘I can’t put makeup on this eye because it’s hurting so much and that would irritate it,' Charlotte said of her injured eye Change of plan? Charlotte previously tweeted: 'Tonight I'm going to the OPERA! How sophisticated! And not just anywhere, (the) SYDNEY OPERA HOUSE' Charlotte was also forced to close the curtain in her hotel room because the natural light was hurting it. She added: 'It’s a lot of problems and I don’t feel I can get over any of them in time. I’m really sad about it because I have such a nice dress for it. I hope it will be better in the morning.’ Meanwhile, Charlotte announced her departure from MTV's Geordie Shore earlier this month amid clashes with her ex-boyfriend Gary Beadle. Moving on: Charlotte announced her departure from MTV's Geordie Shore earlier this month amid clashes with her ex-boyfriend Gary 'Gaz' Beadle (right). Pictured during a night out in London last year

She impressed audiences with her gorgeous legs as she flirted with Leonardo DiCaprio in Wolf of Wall Street. And Margot Robbie’s pins were on display as she strolled around New York City in a short black slip dress paired with a long pink Kimono, on Wednesday. The 26-year-old showed why she is tipped to be the next Bond girl, showing off her slender physique in the eye-catching number from Spell & The Gypsy Collective. Scroll down for video perruques cheveux naturelsStepping out: Margot Robbie’s trim pins were on display as she wafted around New York City in a short black slip dress paired with a long pink Kimono on Wednesday The oriental-inspired garment featured broderie anglaise in elaborate flower patterns on the sleeves, shoulders and back, and flowed behind her as she strolled around downtown NYC. Leaving a Tribeca hotel, the Australian actress smiled behind oversized round metallic sunglasses with her blonde locks tied back in a bun. The Hollywood starlet’s complexion was characteristically flawless all over, but years in the Northern Hemisphere appeared to have erased her Australian tan. All smiles: Leaving a Tribeca hotel, the Australian actress smiled behind oversized round metallic sunglasses with her blonde locks tied back in a bun High fashion: The 26-year-old showed why she is tipped to be the next Bond girl, showing off her slender physique in the eye-catching number from Spell & The Gypsy Collective Good taste: The oriental-inspired garment featured broderie anglaise in elaborate flower patterns on the sleeves, shoulders and back, and flowed behind her as she strolled around downtown NYC She enhanced her look with natural-look makeup and subtle pink lipstick and carried a small black leather handbag to run her errands.

Margot had been at a fitting for the upcoming NYC premiere of her new film Suicide Squad on Monday night. Tinseltown is abuzz with rumours that the blonde beauty will appear in the next James Bond film as a Bond girl, following a long line of glamorous actresses. Flawles: The Hollywood starlet’s complexion was characteristically flawless all over, but years in the Northern Hemisphere appeared to have erased her Australian tan Going natural: She enhanced her look with natural-look makeup and subtle pink lipstick and carried a small black leather handbag to run her errands Looking her best: Margot had been at a fitting for the upcoming NYC premiere of her new film Suicide Squad on Monday night Online polls have revealed the 26-year-old stunner is at the strongest odds to fill the role, sitting at 12/1, while Emily Blunt sits at 16/1, according to Paddy Power. Others sitting at promising odds to star as the much-desired female character are American model and actress Emily Ratajkowski, who sits at 22/1, with Brit Emma Watson further down the line at 40/1. The Gold Coast girl found her big break in 2013 opposite Leonardo DiCaprio in Martin Scorsese's The Wolf of Wall Street as the feisty and seductive, Naomi. Plenty to smile about: Tinseltown is abuzz with rumours that the blonde beauty will appear in the next James Bond film as a Bond girl, following a long line of glamorous actresses Adoring fans: Margot caused a stir with passersby as she stepped out in NYC Well made: The beautiful garment had an elaborate rose pattern on its back perruques cheveuxMore recently, she starred alongside Alexander Skarsgård in The Legend Of Tarzan as Jane. As well as the blockbuster, Margot has various other projects in the pipeline, including Suicide Squad, in which she plays Harley Quinn. Other females who have starred as the prestigious Bond girl include talented beauties Halle Berry, Rosamund Pike and Eva Green. Next big thing: Online polls have revealed the 26-year-old stunner is at the strongest odds to fill the Bond role, sitting at 12/1, while Emily Blunt sits at 16/1, according to Paddy Power . In demand actress: As well as the blockbuster, Margot has various other projects in the pipeline, including Suicide Squad, in which she plays Harley Quinn Following in footsteps: Other females who have starred as the prestigious Bond girl include talented beauties Halle Berry, Rosamund Pike and Eva Green Although the buzz around who will land the role of Bond girl is growing, the film is not expected to be released until at least 2018. Producer Barbara Broccoli has two other projects firmly underway and is said to be keen to tempt Daniel Craig into reprising his role as the titular character for the fifth time. Holding her own: Her breakout role was as DiCaprio's feisty wife, Naomi, on Wolf of Wall Street, which her portrayal earned her international acclaim - and showed off her gorgeous legs to the world

Next week will see the conclusion of the 12th season of The Bachelorette, as Joelle 'JoJo' Fletcher gets engaged to either Jordan Rodgers or Robby Hayes. And according to a new report, there's set to be a dramatic break-up ahead of the romantic proposal. Us Weekly claims that JoJo comes to the difficult decision of choosing between the two suitors after one of them makes a big mistake with her parents Joseph and Soraya. lace front wigsFamily matters: Joelle 'JoJo' Fletcher apparently picks her fiance in next week's Bachelorette finale after one of the men makes a big mistake with her parents Who will it be?: Jordan Rodgers (L) and Robby Hayes are the two men left in the competition, and one of them allegedly gets sent packing after failing to ask JoJo's parents for her hand in marriage The runner-up gets dumped after failing to ask the 25-year-old's parents' for her hand in marriage, and during the emotional rejection, JoJo confesses that she wanted things to turn out differently. 'JoJo was sobbing,' a source told Us Weekly. 'She told him, “I wanted it to be you.”' The insider added: 'It was important to JoJo that the guys get her parents’ permission before proposing. JoJo viewed it as disrespectful that he didn’t.' Us Weekly claims that JoJo's family liked both of the finalists, but thought that the man she eventually picks was better suited to her. Favourite from the start: Jordan received the First Impression Rose and his relationship with JoJo has gone from strength to strength ever since

Not true: The 27-year-old former quarterback has denied that he cheated on his last girlfriend 'Everyone thought he was husband material,' a source said. 'Later, they told JoJo that they liked the other man too, but they thought the second would be more loyal and a better fit.' And speaking about the family's meeting with the runner-up, the insider revealed: 'After he left, Soraya said she thought he was a player.' It seems that JoJo did make the right decision however, as she is apparently still very happily engaged to the winner. She likes what she sees: JoJo has also fallen in love with former competitive swimmer Robby, 27 cosplay wigsDenying the rumours: Robby has refuted claims that he dumped his girlfriend to appear on the show Former quarterback Jordan, 27, has been a favourite to win the show from night one, when JoJo presented him with the First Impression Rose. Show secrets: Us Weekly revealed details of the dramatic finale And while on Monday's episode of the show he was seen telling the real estate developer that asking her father's permission to propose would be very important to him, a clip for the finale showed JoJo asking if he is ready to get down on one knee. Both of the finalists have faced rumours about past relationships, with Jordan's ex Brittany Farrar claiming that they broke up because she cheated on him. He denied this to JoJo on the show. The footballer is estranged from his famous NFL star brother Aaron, whose girlfriend Olivia Munn is close pals with Jordan's ex-girlfriend. Aaron finally addressed his younger brother speaking about their fraught relationship on the show during an interview with WISN on Tuesday. 'As far as those kinds of things go, I've always found that it's a little inappropriate to talk publicly about some family matters, so I'm not going to speak on those things,' he said. 'But I wish him well in the competition.' Meanwhile a friend of Robby's ex-girlfriend Hope Higginbotham has claimed that he dumped her in order to go on The Bachelorette, something which he denied during last week's hometown dates. The Bachelorette finale followed by the After The Final Rose special will air on ABC on Monday. She made the right choice: JoJo is apparently still happily engaged to the winner, who popped the question in May. The finale airs on ABC on Monday