PICDEM Net 2: Display Analogue Temperature Sensor on Web Page

Background:

The PICDEM Net 2 has an analogue temperature sensor connected to Pin AN3 of the onboard PIC18F97J60.  This reading of this sensor is not included on the demonstration Web Site.

Assumptions:

PICDEM Net 2 programmed with V4.11 of the TCPIP Demo Application.
V4.11 of the Microchip TCPIP Stack installed on your PC
Use of a PICKIT 2 Programmer for Firmware download to the onboard PIC18F97J60

Notes on the PICKIT 2:  This programmer can also supply power to the PICDEM Net 2 via your PC's USB connection (a very handy feature).  However the instructions below assume you have independantly powered the PICDEM Net 2.

Note on the approach taken to resolving this challenge:

As the Temperature sensor on AN3 is a simple analogue device, it uses the PIC18F97J60's onboard Analogue to Digital Conversion to get a voltage reading as a relative 10bit (0-1023) reading.  We will simply copy the AN2 code within the demo application as this is already reading an Analogue potentiometer and displaying it's value.  We will make no attempt to convert the 10bit value to a sensible Celsius value. (But we did that later here


Part 1: Install required Software:

Software:

Install Microchip Software:

MPLAB 7.62
C18 Compiler Student Edition v3.12
TCPIP Stack V4.11
PICKIT 2 Programmer Software (Install Software before connecting PICKIT 2 for the first time)


Part 2: Upgrade PICNET 2 Demonstration application to v4.11 (if necessary)

The PICDEM Net 2 will display the version on the LCD panel under normal operation.  Mine was supplied with v3.75 but v4.11 is available and is a significant update.  The way variables are setup in v4.11 is incompatible with the 3.75 version.  These instructions are for v4.11 only.

Upgrading:

  1. Power up your PICDEM Net 2 boardConnect PICKIT 2 to your PC
  2. Connect PICKIT 2 to your PICDEM Net 2 ICSP connector (Use supplied SIL6 - RJ12 adaptor)
  3. Start PICKIT 2 software
  4. PICKIT 2 Software should be able to recognise that a PIC18F97J60 is connected otherwise you have a connection problem.
  5. File -> Import Hex -> C:\Microchip Solutions\TCPIP Demo App\TCPIP Demo App-C18 PICDEMNET2 18F97J60.hex
  6. Click Write to download the HEX file (firmware) to your PICDEM NET 2.  After a minute or so you should see the green programming successful message.

This will result in the previous demo website sitting in the onboard EEPROM being incompatible with the new HTTP server & File system etc loaded into the Firmware by the steps above.

To update the EEPROM with the new Demo web site:

(Note: The new V4.11 demo firmware includes a website upload page so no need to use FTP to get your pages onto the EEPROM):

With the PICDEM Net 2 powered up and connected to your network, it should show the V4.11 message and it's IP number.

In your Browser enter the PICDEM Net 2's IP address like this:  192.168.1.102\mpfsupload

This will call the new mpfsupload function in the firmware allowing you to browse to the required .bin mpfs2 formatted image.

On the displayed page Browse to C:\Microchip Solutions\TCPIP Demo App\MPFSImg2.bin and then Upload.  You should eventually see an Upload Successful message.

Congratulations, you are now up to date with the V4.11 application.  Have a look around.  Note that it has a combination of active and static pages.  The instructions below are for the static page dynvars


The TCPIP Stack is installed to C:\Microchip Solutions\Microchip

The DEMO application (Website & Firmware) is installed to: C:\Microchip Solutions\TCPIP Demo App

To modify the Default Website to display the Analogue Temperature Sensor on the home page we will:

  1. Update the dynvars.htm to include a new dynamic variable
  2. Convert the webpages into the format required by the embedded HTTP server
  3. Update the firmware to include code to handle our new variable

Take a copy of the DEMO application in case it all goes to custard (C:\Microchip Solutions\TCPIP Demo App)

Note: pot0 is the dynamic variable used in the demo v4.11 application to display the onboard Potentiometer.  By searching for this we can use it as a starting point for all our required changes.

1. We need to add a new Dynamic Variable:

Note that that Section 4 will automatically upload the result to the EEPROM on the PICDEM Net 2 which is a nice touch.

Now we need to start changing the firmware to link in the new "temp1" reference we have added above by adding a new HTTPPrint_...() function to handle this variable, using the existing pot0 as a template:

void HTTPPrint_temp1(void)
{
char AN0String[8];
WORD ADval;
#if __C30__
ADval = (WORD)ADC1BUF0;
//ADval *= (WORD)10;
//ADval /= (WORD)102;
uitoa(ADval, (BYTE*)AN0String);
#else
// Wait until A/D conversion is done
ADCON0bits.GO = 1;
while(ADCON0bits.GO);

// Convert 10-bit value into ASCII string
ADval = (WORD)ADRES;
//ADval *= (WORD)10;
//ADval /= (WORD)102;
uitoa(ADval, AN0String);
#endif
TCPPutArray(sktHTTP,(void *)AN0String, strlen((char*)AN0String));
return;
}

void HTTPPrint_pot0(void)
{
char AN0String[8];
WORD ADval;
#if __C30__
ADval = (WORD)ADC1BUF0;
//ADval *= (WORD)10;
//ADval /= (WORD)102;
uitoa(ADval, (BYTE*)AN0String);
#else
// Wait until A/D conversion is done
ADCON0bits.CHS0 = 0;
ADCON0bits.CHS1 = 1;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;

ADCON0bits.GO = 1;
while(ADCON0bits.GO);

// Convert 10-bit value into ASCII string
ADval = (WORD)ADRES;
//ADval *= (WORD)10;
//ADval /= (WORD)102;
uitoa(ADval, AN0String);
#endif
TCPPutArray(sktHTTP,(void *)AN0String, strlen((char*)AN0String));
return;
}

void HTTPPrint_temp1(void)
{
char AN0String[8];
WORD ADval;
// Wait until A/D conversion is done
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 1;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;

ADCON0bits.GO = 1;
while(ADCON0bits.GO);

// Convert 10-bit value into ASCII string
ADval = (WORD)ADRES;
//ADval *= (WORD)10;
//ADval /= (WORD)102;
uitoa(ADval, AN0String);

TCPPutArray(sktHTTP,(void *)AN0String, strlen((char*)AN0String));
return;
}

Refer to the Microchip PIC18F97J60 Datasheet for details on the A-D control registers

Thats it!  After the firmware has updated the board will restart and you should be able to see the temperature on dynvars page.



Now for extra credit we will try to get the same value working on the home page renewing automatically.

<p>Potentiometer: <span id="pot0" style="font-weight:normal">0</span>
<p>Temperature: <span id="temp1" style="font-weight:normal">0</span>

//update the POT value
document.getElementById('pot0').innerHTML = getXMLValue(xmlData, 'pot0');
//update the temp1 value
document.getElementById('temp1').innerHTML = getXMLValue(xmlData, 'temp1');

Hey hey it works!


 

Notes on the v4.11 help file:

The instructions above re customisation were from my following the v4.11 Users Guide installed to your PC when you installed the v4.11 TCPIP Stack earlier.  You can find it at: Start Menu\Programs\Microchip\TCPIP Stack 4.11\Microchip TCPIP User's Guide, however:

    void HTTPPrint_led0(void)
    {
        TCPPut(curHTTP.socket, (LED0_IO?'1':'0'));
        return;
    }

should be (I think..):

void HTTPPrint_led0(void)
{
TCPPut(sktHTTP, (LED0_IO?'1':'0'));
return;
}


For using the Temperature sensor we need to turn on/off Analogue to Digital conversion so here's the existing control register definitions I found to know what to add to the HTTPPrint_pot0 & HTTPPrint_temp1 functions..:

typedef struct
{
unsigned char ADON:1;
unsigned char GO:1;
unsigned char CHS0:1;
unsigned char CHS1:1;
unsigned char CHS2:1;
unsigned char CHS3:1;
unsigned char :1;
unsigned char ADCAL:1;
} ADCON0bits;
typedef struct
{
unsigned char ADCS0:1;
unsigned char ADCS1:1;
unsigned char ADCS2:1;
unsigned char ACQT0:1;
unsigned char ACQT1:1;
unsigned char ACQT2:1;
unsigned char :1;
unsigned char ADFM:1;
} ADCON2bits;


Where to from here:


My TCPIP V4.13 with temp1 on dynvars.htm

Webseminars (look under Connectivity)