Feb
4
One Wireduino made easy
16 years ago, at the start of February | 12 Comments
For a very long time now I’ve been playing with the Arduino boards but it wasn’t until I recently discovered some DS1820 1-wire chips in my parts box that I started even thinking about 1-wire stuff. Connecting 1-wire devices to the Arduino is, well, amazingly simple. Using the sample circuit from the datasheet we can see the connections are few and far between.
Please note that this is the corrected circuit, previously the VDD was not tied to ground. While the circuit would work it would only give reliable results on very short runs. This circuit allows longer runs to the sensor.
Coding this up is also made extremely easy with the OneWire library. I’ve put together a little application below that will read all of the 1-wire devices on a bus. If you press L in the Arduino IDE serial monitor it will list all the 1-Wire device id’s and if you press T you’ll get the id and temperature reading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | /* Simple 1-wire device reading Pass T over serial to get temperatures Pass L to list 1-wire devices */ #include <OneWire.h> #include <stdio.h> #define CONVERT 0x44 #define READSCRATCH 0xBE #define SKIP_ROM 0xCC #define MATCH_ROM 0x55 OneWire ds(10); // The DS18S20 is connected on pin 10 int ledPin = 13; // flash an led on 13 - we all like flashing lights void setup(void) { // initialize inputs/outputs // start serial port Serial.begin(9600); pinMode(ledPin, OUTPUT); //we'll use the led to output a heartbeat } void loop(void) { int incomingByte = 0; if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // say what you got: //Serial.print("I received: "); //Serial.println(incomingByte, DEC); if (incomingByte == 84) { getStuff(); Serial.print("\r\n^"); } else if (incomingByte == 76) { listDevices(); } } } void listDevices(void) { byte addr[8]; ds.reset_search(); while(ds.search(addr)) { digitalWrite(ledPin, HIGH); // sets the LED on if ( OneWire::crc8( addr, 7) == addr[7]) { if ( addr[0] == 0x10) { // Make sure it is a DS18S20 device char buffer[512]; sprintf(buffer,"Id:%02x%02x%02x%02x%02x%02x%02x%02x\r\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); Serial.print(buffer); } } } Serial.print('^'); } void getStuff(void) { byte i; byte present = 0; byte data[12]; byte addr[8]; char buff[15]; float real_temp; float temp_count; float read_temp; ds.reset_search(); while(ds.search(addr)) { digitalWrite(ledPin, HIGH); // sets the LED on if ( OneWire::crc8( addr, 7) != addr[7]) { // Check CRC is valid Serial.print("CRC is not valid"); } if ( addr[0] != 0x10) { // Make sure it is a DS18x20 device Serial.print("Device is not a DS18x20 family device."); } ds.reset(); // Reset device ds.select(addr); // Select device ds.write(CONVERT,1); // Issue Convert command delay(1000); // maybe 750ms is enough, maybe not digitalWrite(ledPin, LOW); // sets the LED off present = ds.reset(); // Reset device ds.select(addr); // Select device ds.write(READSCRATCH); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } if(OneWire::crc8( data, 8) == data[8]) { // Check CRC is valid // CRC is ok // Divide the temperature by 2 - note the » and // « need to be replaced with double < and > // wordpress kills the server when you try to // save otherwise. read_temp=((data[1]«8) | data[0]) » 1 ; // Convert to real temperature temp_count=float(data[7] - data[6])/(float)data[7]; real_temp = ((float)read_temp-0.25)+temp_count; // Convert float to ascii tempToAscii(real_temp,buff); char buffer[512]; sprintf(buffer,"%02x%02x%02x%02x%02x%02x%02x%02x %s\r\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], buff); Serial.print(buffer); } else { Serial.println("CRC Failed"); } } void tempToAscii(double temp, char *buff) { int frac; //get three numbers to the right of the decimal point frac=(unsigned int)(temp*1000)%1000; itoa((int)temp,buff,10); strcat(buff,"."); //put the frac after the decimal itoa(frac,&buff[strlen(buff)],10); } |
There’s also a copy of the .pde file that you can download if you feel more inclined. It’s not an exact copy, I removed the commented out xml for the above version. It’s worth pointing out that this circuit is using what is called parasitic power, which means your cable run should be shortish. If you want to use an external power supply then pin 3 (VDD) on the DS18S20 should go to your external supply. This would allow for much longer runs of cable. Below is an example graph generated by cacti with 2 DS1820’s connected the the arduino. As you can see the temperatures aren’t exactly the same but this is to be expected since the accuracy of the DS1820 is +/- 0.5 ° C
The breaks in the graph were caused my me removing the USB plug from the PC it was connected to so that I could make some minor alterations to the circuit.
Josh asked how I was getting the input for Cacti, here’s the perl script I use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/usr/bin/perl $output = `echo -en "T" >>/dev/ttyUSB0 && cat /dev/ttyUSB0`; @lines = split(/\r\n/,$output); $t=1; foreach $line (@lines) { @spl = split(/\s+/,$line); $lc = @spl; if ($lc gt 1) { $node=$spl[0]; $val=$spl[1]; print "Temperature_$node:$val "; } $t++; } |
This outputs Temperature_x:n where x is the 1-Wire id and n is the temperature. It could probably do with tidying up and if you wanted to you could pass the 1-Wire id to the script to and only output the temperature for that particular 1-Wire device. A copy of the exported Cacti template can be downloaded, I’m hoping that you just need to import that into your Cacti after placing the script in the scripts directory. Just remember, under linux, if you unplug and replug in your Arduino you may need to set the tty port speed to 9600 (or whatever you put in your Serial.begin(); ). You can use the following command for that;
1 2 3 | stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts |
Make sure that ttyUSB0 is what your Arduino is connected to first. If not you will need to change it in the perl script and on the line above.
![[del.icio.us]](http://images.del.icio.us/static/img/delicious.small.gif)
Tagged with: 1-Wire • arduino • hardware • opensource • software
February 4, 2009 21:46
Jan
12
WordPress tweetbacking
16 years ago, mid-January | 4 Comments
Tweetbacking, it sounds like some sort of deviant sport or something but isn’t. The idea behind Tweetbacks, similar to pingbacks, is that you can see who is commenting (tweeting) about your blog on Twitter. There are a number of plugins available for use in the end I went for Joost de Valk‘s version. While it worked nicely I decided that I really wanted to approve the tweets before they appeared in my blog so I modified the code a little. The diff is available for download. It allows you to enable and disable the automatic approval of Tweets form the plugin’s admin page. Hopefully Joost will add this option in the official version sometime.
Tweetbacks are saved in the comments table and can be displayed however you like for example, in my comment loop I have:
<?php if (strtolower($comment->comment_type) == 'tweetback') { $avatarurl = str_replace( "twitter:", "http://s3.amazonaws.com/twitter_production/profile_images/", $comment->comment_author_email ); echo "<img align=middle" ." src='$avatarurl' border=none" ." width=40 height=40 alt=''>"; } ?> |
A word or two of caution about the plugin. Personally I’ve removed the bit.ly url shortening link parsing because it just didn’t work properly – I ended up with bogus tweetbacks until I did. The other issue is with the date saving from the tweetback, it doesn’t work properly either giving dates in the 1970’s!!!
![[del.icio.us]](http://images.del.icio.us/static/img/delicious.small.gif)
Tagged with: opensource • software
January 12, 2009 22:09
Nov
28
iPhone proximity sensing IS in the API
17 years ago, at the end of November | Leave a Comment
There seems to have been a lot of misinformation flying about of late. With Google releasing a new version of their app incorporating proximity sensing, some blogs have claimed this is breaking AppStore rules by using undocumented hidden (non public) API calls. I’m here to tell you that’s just rubbish. All that has happened is the people reporting it haven’t even bothered to check. Want to prove this to yourself?
1. Start Xcode and pick one of the templates.
2. Load up the AppDelegate code and skip to ‘applicationDidFinishLaunching’.
3. Type ‘application.’ (note the full stop after the word application)
4. Hit escape and you’ll get a list, scroll down to items starting with P
5. Oh look there it is…
For those without Xcode here’s a picture.

Oh, that must be the non-public public api...
For those that want to see the what the documentation says
proximitySensingEnabled
A Boolean value that determines whether proximity sensing is enabled.
@property(nonatomic, getter=isProximitySensingEnabled) BOOL proximitySensingEnabled
Discussion
YES if proximity sensing is enabled; otherwise NO. Enabling proximity sensing tells iPhone OS that it may need to blank the screen if the user’s face is near it. Proximity sensing is disabled by default.
Availability
Available in iPhone OS 2.0 and later.
Declared In
UIApplication.h
Unfortuneately once some people grab hold of false information no facts will get in their way. Anyone that knows me will tell you that I’m really not fond of either Google or Apple, but let’s just get stuff right shall we.
Update: I’ve had some discussion on irc and it was felt that I should point out that proximityStateChanged is used and following some additonal chatter on IRC with UncleBob who pointed out that “It’s not private in the OBjC sense just the Apple documented SDK sense. ” he then went on to say “the whole thing is definately a peanut-gallery cockfest” which sums it all up really.
![[del.icio.us]](http://images.del.icio.us/static/img/delicious.small.gif)
Tagged with: api • Apple • google • iphone
November 28, 2008 11:43
Nov
11
Concept iPhone Application
17 years ago, mid-November | 3 Comments
I’ve been messing about trying to learn how to write iPhone apps. There’s a lot to take in but at the moment I’ve managed a concept application. A little shop (of horrors ;). At the moment it simply reads data from an sqlite database and then displays information on the product. The buy now button currently just jumps to the product url at Linitx.com.
Update: Added a screenshot when rotated. The iPhone SDK handles the rotation automatically, all you need to do is specify which parts of which objects move or resize. All that can be done from Interface Builder. Easy as pie. Also note the changed title bar colour, in keeping with LinITX, and the title text. The shop title (LinITX) and shop base product URL are read from a plist file. I suppose they could be read from the sqlite db but I am experimenting so learning how to read a plist file is useful too.
Update 2: Spent ages last night trying to work out why some of the text descriptions for products came out of the database as null. Discovered that when importing the data file it wasn’t using UTF8 encoding, which is apparently all sqlite deals with. Loading up the source data file into TextEdit under OSX and saving it as UTF8 then reimporting solved the problem. I also modified how things are displayed. The image and text are inserted into a UIWebView so are now scaleable by the user. The images are also base64 encoded and in the database. This increases the database size but means the application can still be used offline. Before none of the pictures would display if there was no network connectivity.
Update 3: Added the ability to search the list. The search bar appears when the search button is pressed and hides again when the search is complete.
![[del.icio.us]](http://images.del.icio.us/static/img/delicious.small.gif)
Tagged with: development • iphone • software
November 11, 2008 20:35
Oct
19
Fullscreen iPhone WebApps
17 years ago, mid-October | 1 Comment
Web applications on the iPhone have always been second class citizens without access to the really interesting features. To add insult to injury web applications lost a large chunk of screen real estate to the top and bottom Safari toolbars.
Well, now they don’t need to. For users with firmware 2.1, if you add the following lines to the <head> section of your web application page then, when someone adds your web application to their home screen and then uses it all they will see is the standard bar showing carrier name, signal and battery level. As ever the pictures should make it much clearer. Notice that there is no address bar or lower toolbar at all, even when scrolling.
Read more…
Tagged with: Apple • iphone
October 19, 2008 16:44
Current Electricity Use (15min)
iPhone/Webkit RSS Reader
Links
- automated home
- Automated It Technology News
- awooga!!!
- LinITX
- My Acer page
- My Asterisk pages
- My Work in progress (old)
- Noble Race Car
- openmoko / neo 1973 wiki
- planet openmoko
- Spadgecock Cumpants
Tags
1-Wire android api Apple arduino currentcost DDAR development DVD FIC freerunner G1 google Google Phone gphone gprs GPS hardware image image builds inspiration iphone jailbreak kiosk linux Mac monitoring Music neo 1973 Nokia openmoko opensource OSX Pachube personal qtopia rhubarb rikki Rio slimp3 slimserver software tracking Trolltech u-bootTwitpic
Graphy Stuff




