{"id":174,"date":"2012-12-16T17:11:51","date_gmt":"2012-12-16T17:11:51","guid":{"rendered":"https:\/\/www.thebillplease.io\/play\/?p=174"},"modified":"2012-12-16T17:11:51","modified_gmt":"2012-12-16T17:11:51","slug":"fixing-arduino-ethernet-rfm12-communication","status":"publish","type":"post","link":"https:\/\/www.thebillplease.io\/play\/2012\/12\/16\/fixing-arduino-ethernet-rfm12-communication\/","title":{"rendered":"Fixing Arduino Ethernet and RFM12 Communication"},"content":{"rendered":"<p>Unfortunately, Arduino&#8217;s Ethernet shield does not play very nice with other devices that want to use its select pins. After many searches, I came across jcw&#8217;s discussion board and Martin&#8217;s implementation doc.<\/p>\n<p>Because the ethernet shield uses digital pin 10 select, RF12 select line needs to use digital pin 9. You also need to disable interrupts. As such, you need to modify both your w5100 and rf12 libraries.<\/p>\n<p>Based on jcw and Martin&#8217;s posts.<br \/>\n<a title=\"Martin\" href=\"http:\/\/harizanov.com\/2012\/04\/rfm12b-and-arduino-ethernet-with-wiznet5100-chip\/\" target=\"_blank\" rel=\"noopener\">Martin&#8217;s post<\/a><br \/>\n<a href=\"http:\/\/forum.jeelabs.net\/node\/291\" target=\"_blank\" rel=\"noopener\">jcw post<\/a><br \/>\n<a href=\"http:\/\/tronixstuff.wordpress.com\/2011\/05\/13\/tutorial-arduino-and-the-spi-bus\/\" target=\"_blank\" rel=\"noopener\">Another great post explaining SPI<\/a><\/p>\n<p>Step 1:<br \/>\nChange w5100.h<\/p>\n<p>From:<\/p>\n<pre lang=\"c\" line=\"1\">#else\ninline static void initSS() { DDRB |= _BV(2); };\ninline static void setSS() { PORTB &amp;= ~_BV(2); };\ninline static void resetSS() { PORTB |= _BV(2); };\n#endif\n#else\ninline static void initSS() { DDRB |= _BV(2); };\ninline static void setSS() { cli(); PORTB &amp;= ~_BV(2); };\ninline static void resetSS() { PORTB |= _BV(2); sei(); };\n#endif<\/pre>\n<p>Step 2:<br \/>\nChange RF12.cpp<\/p>\n<p>From:<\/p>\n<pre lang=\"c\" line=\"1\">\/\/ ATmega168, ATmega328, etc.\n#define RFM_IRQ 2\n#define SS_DDR DDRB\n#define SS_PORT PORTB\n#define SS_BIT 2 \/\/ for PORTB: 2 = d.10, 1 = d.9, 0 = d.8\n<\/pre>\n<p>To:<\/p>\n<pre lang=\"c\" line=\"1\">\n\/\/ ATmega168, ATmega328, etc.\n#define RFM_IRQ 2\n#define SS_DDR DDRB\n#define SS_PORT PORTB\n#define SS_BIT 1 \/\/ for PORTB: 2 = d.10, 1 = d.9, 0 = d.8\n\/\/Changed from 2 = d.10 to a = d.9\n<\/pre>\n<p>Step 3:<br \/>\nYou also need to change the order of your includes. You must include Ethernet.h before JeeLib.h<\/p>\n<pre lang=\"c\" line=\"1\">\n#include &lt;Ethernet.h&gt;\n#include &lt;JeeLib.h&gt;\n#include &lt;avr\/sleep.h&gt;\n#include &lt;SPI.h&gt;\n<\/pre>\n<p>You also need to change your initialization sequence. You must initialize rf12 before ethernet.<\/p>\n<pre lang=\"c\" line=\"1\">\nSerial.begin(57600);\nrf12_initialize(26, RF12_433MHZ, 4);\nEthernet.begin(mac); \/\/initialize ethernet\nSerial.println(\"\\n[TimedReceive]\");\nSerial.println(\"connecting...\");\ndelay(1000); \/\/ give it time to boot\n<\/pre>\n<p>Example of timedRecv that works:<\/p>\n<pre lang=\"c\" line=\"1\">\n\/\/\/ @dir timedRecv\n\/\/\/ Experiment with time-controlled periodic reception.\n\/\/ 2011-06-24 &lt;jc@wippler.nl&gt; http:\/\/opensource.org\/licenses\/mit-license.php\n#include &lt;Ethernet.h&gt;\n#include &lt;JeeLib.h&gt;\n#include &lt;avr\/sleep.h&gt;\n#include &lt;SPI.h&gt;\n\nstruct {\nbyte start; \/\/ time at which we started listening for a packet\nbyte later; \/\/ how long we had to wait for packet to come in\n} payload;\n\nISR(WDT_vect) { Sleepy::watchdogEvent(); }\n\nbyte mac[] = { 0x90,0xA2,0xDA,0x00,0x55,0x8D}; \/\/Replace with your Ethernet shield MAC\nEthernetClient client;\nIPAddress server(192,168,1,101); \/\/localhost\n\nvoid setup () {\nSerial.begin(57600);\nrf12_initialize(26, RF12_433MHZ, 4);\nEthernet.begin(mac); \/\/initialize ethernet\nSerial.println(\"initialized mac\");\nSerial.println(\"\\n[TimedReceive]\");\nSerial.println(\"connecting...\");\ndelay(1000); \/\/ give it time to boot\n}\n\nvoid loop () {\nif (rf12_recvDone() &amp;&amp; rf12_crc == 0) {\n\/\/ process incoming data here\nSerial.println(\"rf12_recDone = true\");\n\nif (RF12_WANTS_ACK) {\nSerial.println(\"rf12 wants ack\");\npayload.later = (byte) millis() - payload.start;\nrf12_sendStart(RF12_ACK_REPLY, &amp;payload, sizeof payload);\nrf12_sendWait(1); \/\/ don't power down too soon\n}\n\nString data;\ndata+=\"\";\ndata+=\"uid=\";\ndata+=\"12345\";\ndata+=\"&amp;tid=\";\ndata+=\"eeefffeee\";\ndata+=\"&amp;ql=\";\ndata+=\"333444111\";\n\nif (client.connect(server,80)) {\nSerial.println(\"connected\");\nclient.println(\"POST \/tupper\/data.php HTTP\/1.1\");\nclient.println(\"Host: www.your-website.com\");\nclient.println(\"Content-Type: application\/x-www-form-urlencoded\");\nclient.println(\"Connection: close\");\nclient.print(\"Content-Length: \");\nclient.println(data.length());\nclient.println();\nclient.print(data);\nclient.println();\nSerial.println(data);\n\n\/\/Prints your post request out for debugging\nclient.println(\"POST \/tupper\/data.php HTTP\/1.1\");\nclient.println(\"Host: www.your-website.com\");\nclient.println(\"Content-Type: application\/x-www-form-urlencoded\");\nclient.println(\"Connection: close\");\nclient.print(\"Content-Length: \");\nclient.println(data.length());\nclient.println();\nclient.print(data);\nclient.println();\n\n}\ndelay(2000);\nif (client.connected()) {\nSerial.println();\nSerial.println(\"disconnecting.\");\nclient.stop();\n}\n\/\/ power down for 2 seconds (multiple of 16 ms)\nrf12_sleep(RF12_SLEEP);\nSleepy::loseSomeTime(2000);\nrf12_sleep(RF12_WAKEUP);\n\n\/\/ just woke up, start listening for a packet again\npayload.start = millis();\n} else {\n\/\/ switch into idle mode until the next interrupt\nSerial.println(\"setting sleep mode\");\nset_sleep_mode(SLEEP_MODE_IDLE);\nsleep_mode();\n}\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Unfortunately, Arduino&#8217;s Ethernet shield does not play very nice with other devices that want to use its select pins. After many searches, I came across jcw&#8217;s discussion board and Martin&#8217;s implementation doc. Because the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-arduino","category-internet-of-things"],"_links":{"self":[{"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":0,"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thebillplease.io\/play\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}