Enc28j60 + PIC18F4520: Display temperature value of LM35 on Webpage problems

Home Forums MikroC Pro for PIC Project Discussions Enc28j60 + PIC18F4520: Display temperature value of LM35 on Webpage problems

Viewing 7 reply threads
  • Author
    Posts
    • #5954
      lamnguyenvu98
      Participant

      I use PIC18f4520 and try to read value of Lm35 on webpage. This my code based on Web control and monitoring project. :

      const char HTTPheader[] = “HTTP/1.1 200 OK\nContent-type:”;
      const char HTTPMimeTypeHTML[] = “text/html\n\n”;
      const char HTTPMimeTypeScript[] = “text/plain\n\n”;
      char txt[15] = “”;
      //
      // Define the HTML page to be sent to the PC
      //

      char StrtWebPage1[] =
      “<html><style>.btn {background-color:white;color:#C63;padding:15px 25px;font-size:20px;margin:2px 10px;cursor:cell;}</style><head>\
      <script type=\”text/javascript\”>var test =\
      “;

      char StrtWebPage2[] =
      “</script></head><body bgcolor=#FF9999><center><font size=+5 color=#990066>DAI HOC THUY LOI<br/>Khoa Dien – Dien tu<br/>58KTD-TDH2</font><br/><br/>\
      <table width=400 bgcolor= #F8E0EC>\
      <th colspan=2><font size=+3 color=#5882FA> Temperture:</font></center><td><center><b><font size=+3 color=#FA5858>\
      <script type=\”text/javascript\”>document.write(test)</script>°C </font></b></center></td></th></table></div><form name=input method=get>\
      <table width=600 bgcolor=#FFCCFF><tr><th>\
      <font color=#CC0033>RELAY 1</th> <br /><th><font color=#CC0033>RELAY 2</th><br/></td></tr><tr>\
      <td height=80 bgcolor=#CCCCFF><center><input class=btn name=ON type=submit value=ON>\
      <input class=btn name=OFF type=submit value=OFF><br/></td><td bgcolor=#CCFFCC><center>\
      <input class=btn name=O1 type=submit value=ON><input class=btn name=OF1 type=submit value=OFF><br /></td></tr><tr><td align=center colspan=2><font size=4 color=#CC0033>Design: NHOM 17</font></td></table><br/><br/>\
      </form></center></body></html>”;
      // Ethernet NIC interface definitions
      //
      sfr sbit SPI_Ethernet_Rst at RC0_bit;
      sfr sbit SPI_Ethernet_CS at RC1_bit;
      sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
      sfr sbit SPI_Ethernet_CS_Direction at TRISC1_bit;
      //
      // Define Serial Ethernet Board MAC Address, and IP address to be used for the communication
      //
      unsigned char MACAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ;
      unsigned char IPAddr[4] = {192,168,0,5};
      unsigned char ipgw[4] = {192,168,1,1};
      unsigned char ipmask[4] = {255,255,255,0};
      unsigned char ipdns[4] = {203,113,131,2};
      unsigned char getRequest[10];

      typedef struct
      {
      unsigned canCloseTCP:1;
      unsigned isBroadcast:1;
      }TethPktFlags;

      //
      // TCP routine. This is where the user request to toggle Realy 1 and Relaye 2 are processed
      //
      unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost,
      unsigned int remotePort, unsigned int localPort,
      unsigned int reqLength, TEthPktFlags *flags)
      {
      unsigned int Len;
      for(len=0; len<10; len++)
      getRequest[len]=SPI_Ethernet_getByte();
      getRequest[len]=0;
      if(memcmp(getRequest,”GET /”,5) != 0)return(0);

      if(memcmp(getRequest+6,”ON”,2) == 0) RD0_bit = 0;
      else if(memcmp(getRequest+6,”OFF”,3) == 0) RD0_bit = 1;
      else if(memcmp(getRequest+6,”O1″,2) == 0) RD1_bit = 0;
      else if(memcmp(getRequest+6,”OF1″,3) == 0) RD1_bit = 1;

      if(localPort != 80 && localPort != 8080)return(0); // listen to port 80 and 8080 only
      Len = SPI_Ethernet_putConstString(HTTPheader);
      Len += SPI_Ethernet_putConstString(HTTPMimeTypeHTML);
      Len += SPI_Ethernet_putString(StrtWebPage1);
      Len += SPI_Ethernet_putString(txt);
      Len += SPI_Ethernet_putString(StrtWebPage2);
      return Len;
      }

      //
      // UDP routine. Must be declared even though it is not used
      //
      unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost,
      unsigned int remotePort, unsigned int destPort,
      unsigned int reqLength, TEthPktFlags *flags)
      {
      return(0);
      }

      const unsigned short VREF = 5;

      unsigned int temp_res = 0;
      float temp;

      //
      // Start of MAIN program
      //
      void main()
      {
      LATA = 1; // Configure PORTA as Analog;
      TRISA0_bit = 1; //Configure RA0 as input
      TRISD = 0; // Configure PORTD as output
      LATC = 0; // Configure PORTC as Digital I/O
      LATD = 0; // Configure PORTD as Digital I/O
      PORTD = 0;
      SPI1_Init(); // Initialize SPI module
      SPI_Ethernet_Init(MACAddr, IPAddr, 0x01); // Initialize Ethernet module
      SPI_Ethernet_confNetwork(ipmask, ipgw, ipdns);
      ADC_Init(); // Initialize ADC
      temp_res = 0;
      do {

      temp_res = ADC_Get_Sample(0); // Get 10-bit results of AD conversion // channel AN0/RA0
      temp = (temp_res * VREF)/10.240; // Calculate temperature in Celsuis
      // change Vref constant according
      // to the power supply voltage
      FloatToStr(temp, txt); // Convert temperature to string
      txt[4] = 0;
      SPI_Ethernet_doPacket(); // Process next received packet

      }while(1); // Loop forever

      }

      I tested on real hardware and it did show the value on my web. But problem is I have to refresh page to get new value from Lm35. Could anyone give me a solution?

       

    • #5958
      Bitahwa Bindu
      Keymaster

      Hi,

      You can use the HTML <meta> http-equiv Attribute to refresh your page. For example to refresh your page every 30s, you could do like this:

      <head>
      <meta http-equiv=”refresh” content=”30″>
      </head>

      You can learn more from this page: https://www.w3schools.com/tags/att_meta_http_equiv.asp

       

    • #5959
      lamnguyenvu98
      Participant

      Thanks for your help. I try set it to refresh every 2 seconds and the value give me different numbers ( from 30 degree to 29.6 and then to 32.1 ) It keeps jumping on that range everytime page refresh. I assume it’s about hardware problem.

      What if I want to create 2 webpages: One for controlling relays, one for read temperature. I mean for example: when I type my IP: 192.168.1.5, it show me my page with relay control table and it has a text button “Display Temperature”. When I click on it, it redirect me to a link like: 192.168.1.5/t and I can see temperature value on it. In my web code I add:

      Display Temperature<p>

      then I want to put temperature web, which stored in const char temp, on that site.

      I dont know how to execute code in SPI_Ethernet_UserTCP function. I have tried a few times but what a mess.

       

      Attachments:
      You must be logged in to view attached files.
    • #5961
      Bitahwa Bindu
      Keymaster

      When you refresh the temperature you can’t expect it to be constant. Temperature always changes  it can’t be a fixed value as long as the read temperature is correct.

      Why do you want to create two webpages? Is it not possible to display everything on the same page? remember yo are saving this page in memory and you are using an old PIC with limited memory, so you might run out of memory but it’s your choice. you can try it.

    • #5962
      lamnguyenvu98
      Participant

      Hi. I displayed everything on same webpage. I just try some thing different. Thanks for your help.

      From now I want my friend and my teacher who have their device connected to internet, can access my local web. I’m thinking about paid for static ip. In code, I just replace my ip by that static ip then everyone can come to my web and control things in it. Am I right?

    • #5963
      Bitahwa Bindu
      Keymaster

      Yes you can use a static public IP address or configure your router to forward HTTP port to your local IP address.

      Google how to access my local IP from internet you will get plenty answers.

    • #5964
      lamnguyenvu98
      Participant

      I find paying for static ip is only way. I have to carry this project to my class but unluckily, there is no internet cable in there. So I have to bring my 3G router there and use 3G connection. I do some research about port forwarding but it seems not work with 3G connection.

      It’s all good. I wish you great health and happiness. Thank you so much.

    • #5965
      Bitahwa Bindu
      Keymaster

      You are welcome!

Viewing 7 reply threads
  • You must be logged in to reply to this topic.
Select your currency
EUR Euro