Nov 19

I recently acquired a DS1616 IC.  This device offers the following:

DS1616 on Arduino proto shield

DS1616 on Arduino proto shield

  • Measures four channels of data:
    • Integrated 8-bit temperature sensor
    • Integrated 8-bit analog-to-digital converter (ADC) with a three-input mux
  • Real -time clock/calendar
  • Unique, factory lasered 64-bit serial number
  • A whole bunch of other logging and alarm functions

This device was crying out to be interfaced with the Arduino.

The DS1616 allows 2 forms of serial communication, 2 wire asynchronous and 3 wire synchronous.  I decided to go with the 2 wire configuration, what with Arduino pins being a precious commodity.  Connecting the DS1616 to the Arduino is a simple affair.  Two pins for the serial communcations.  It’s a good idea to include a battery, so that the time remains correct when power is removed.

Great schematic drawing, eh?

Great schematic drawing, eh?

That’s the hardware out of the way.  Let’s move onto the software.  Communication with the DS1616 is established using the NewSoftSerial library.  Getting data is essentially a case of lots of bit banging.  The DS1616 library (downloadable below) provides all the functions needed to gather data from the DS1616.  The best way to show the functions provided is to view the sample sketch below.

Sample code

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
/*
    DS1616 library sample code.
    Used for communicating with the DS1616 RTC and temperature device/
    Created by Pete Foster (http://www.xmob.co.uk)
 
    REQUIRES NewSoftSerial library.  (http://arduiniana.org/libraries/NewSoftSerial/)
*/


#include <NewSoftSerial.h>
#include <DS1616.h>

NewSoftSerial ds(8, 9); // The serial port that the DS1616 is connected to
DS1616 chip; // The class

void setup() {
    Serial.begin(9600);
    ds.begin(9600); // DS1616 works at 9600 baud
   
    byte page[35]; // A buffer for the class to work with
 
    // The DS1616 uses an internal Y2K flag to determine the
    // century.  This means that only years between 1900 and
    // 2099 can be used.  The set_current_date function
    // automatically sets the flag based on the supplied year.
    char new_date [] = "2009-11-19"; // The date string.  Format is YYYY-MM-DD.
    // set_current_date(char working_buffer, char new_date, NewSoftSerial serial_port)
    chip.set_current_date(page, new_date, ds); // Set the date
 
    // The DS1616 RTC can operate in 12 or 24 hour mode.
    // This can be set, depending on the format of the string
    // used to set the time.
    // If the string ends with an 'A' or a 'P' then 12 hour
    // format is used.  Otherwise, 24 hour will be used.
    // e.g.
    // "01:30:00P"
    // "13:30:00"
    char new_time [] = "12:00:00"; // The time string
    // set_current_time(char working_buffer, char new_time, NewSoftSerial serial_port)
    chip.set_current_time(page, new_time, ds); // Set the time
}

void loop() {
    // DS1616 returns data in 32 byte pages plus a 2 byte CRC.
    // CRC is ignored in this library.
    byte page[35];  // A buffer for the class to work with

    // The vars where data from requests will be stored
    float temp;
    int analog1, analog2, analog3;
    char time[] = "00:00:00A";
    char date[] = "0000-00-00";
    char serial_num [] = "0000000000000000";
 
    // Get the serial number of the chip.
    // This is represented as a string of hex.
    // get_serial_number(char working_buffer, char serial_num_buffer, NewSoftSerial serial_port)
    // Serial number is returned in serial_num_buffer.
    chip.get_serial_number(page, serial_num, ds);
 
    // Get the current time from the chip.
    // get_current_time(char working_buffer, char time_buffer, NewSoftSerial serial_port)
    // Time us returned in time_buffer
    chip.get_current_time(page, time, ds);
 
    // Get the current date from the chip.
    // get_current_date(char working_buffer, char date_buffer, NewSoftSerial serial_port)
    // Date us returned in date_buffer
    chip.get_current_date(page, date, ds);
 
    // Get the current reading from the internal temperature sensor.
    // get_current_temp(char working_buffer, NewSoftSerial serial_port)
    // Returns float
    temp = chip.get_current_temp(page, ds);
 
    // Read the 3 analog channels.  These are 8-bit.
    // 0v on input is 0
    // 5v (Vcc) on input is 255
    // read_analog(char working_buffer, int analog_channel, NewSoftSerial serial_port)
    // Returns int
    analog1 = chip.read_analog(page, 1, ds);
    analog2 = chip.read_analog(page, 2, ds);
    analog3 = chip.read_analog(page, 3, ds);
 
    // Display the data
    Serial.print(serial_num);
    Serial.print(',');
    Serial.print(date);
    Serial.print(',');
    Serial.print(time);
    Serial.print(",");
    Serial.print(temp);
    Serial.print(',');
    Serial.print(analog1);
    Serial.print(',');
    Serial.print(analog2);
    Serial.print(',');
    Serial.print(analog3);
    Serial.print("\n");
    delay(1000);
}

Download

Current release: arduino_DS1616-0.0.1.tar.gz

If you use this library, I would love to hear about it.

Tagged with:
Nov 13

I recently needed to add promotional discounts for one of my web sites.  I was unable to find an off the shelf function to generate unique (user specific) codes.  So, I created the code myself.

The PHP code presented here allows you to encode any string of your choice into a familiar looking serial number/promotional code.  The produced codes are restricted to a limited subset of characters minimising the risk of human typing mistakes.  Basic encryption is also supported.

Typical Scenario 1

An online store wants to provide a registered customer the opportunity to purchase with a 10% discount.  The discount code should be valid for that customer only, and should be valid until a certain date.  The store generates a code that consists of the users ID, the offer and an expiry date:

  • 123,10off,091212

This code is easily decipherable and open to abuse by other store customers.  By using the functions presented here, the following code can be generated:

  • WGK4P-H7JV9-S6XXW-PWQYL-TJPDU-2

The generated promotional code is far more difficult to decipher.  When the customer enters this code, the online store can decode it, check it is valid (customer and date) and apply the discount.

Typical Scenario 2

A software developer has released an application that has multiple levels of functionality, depending on the license purchased by the user.  The application detects supported functionality, based on the serial number provided by the user:

  • pro,noexpire
  • lite,101112

Allowing a user to enter such codes is again open to rampant abuse.  The software developer provides the following serial numbers:

  • 6DK8F-H9F55-M732G-L6ZNA
  • 5VFQR-YFDWC-TKMJN-UWW

Each serial number is valid, but allows the application to offer different levels of functionality.

Usage Example

The code below demonstrates how to use the functions:

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
<?php
    // GENERATE A CODE
    // Include the functions
    include_once('promo_gen.php');

    // The key used for encryption
    $key = "password";

    // The plaintext promotional code
    $promo = "123,10off,091212";

    // Encrypt the plaintext
    $promo = promo_encrypt($promo, $key);

    // Encode the encrypted plaintext
    $promo = promo_encode($promo);

    // $promo is now:
    // WGK4P-H7JV9-S6XXW-PWQYL-TJPDU-2

    // DECODE A SERIAL NUMBER
    // The serial number
    $serial = "6DK8F-H9F55-M732E-P7RAQ-PWJ";

    // Decode the number
    $serial = prom_decode($serial);

    // Decrypt the serial number
    $serial = promo_decrypt($serial, $key);

    // $serial is now:
    // pro,noexp,xMob
?>

Download

Current release: php_promo_gen.current.tgz

If you use this code generator, I would love to hear about it.

Tagged with:
Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2009 - Pete Foster
preload preload preload