String sent to javascript is undefined

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
microchip22
Posts: 41
Joined: 31 Mar 2012 19:13

String sent to javascript is undefined

#1 Post by microchip22 » 20 Mar 2015 02:01

String values sent to web pages show up as undefined. If I send a string that contains a number, such as “123” the javascript works ok and shows the value. The javascript processes a character string such as "Door" differently than "123". I did not change the web page code or my MB code except for the MB code line SWL1="Door". I am using the ETH_click (Demonstration of mikroBUS ETH click code from Libstock with modified webpages.

The web page is essentially the same as the Libstock example except the following line was added :

Code: Select all

"SWL1=<script> document.write(SWL1)</script>"+
Any value that looks like text shows up as undefined, but if it looks like a number, even though it is defined as a string in MB code it works ok.

Setup as follows:
Circuit Board:
-MCU: PIC18F45K22
- ETH click board with ETHERNET controller ENC24J60

Software as follows:
-Windows 7
- MikroBasic Pro for Pic version 6.5.0

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: String sent to javascript is undefined

#2 Post by darko.minic » 20 Mar 2015 11:37

Hi,

Can you please send me some simple project which describes the problem so I can examine it?

Regards,
Darko

microchip22
Posts: 41
Joined: 31 Mar 2012 19:13

Re: String sent to javascript is undefined

#3 Post by microchip22 » 21 Mar 2015 13:56

Here is the sample project file.

I am receiving the following errors in Firefox browser (Firebug debugger) :

ReferenceError: Text_String is not defined

ReferenceError: swl1 is not defined

How do I correct these errors in javascript and still let Microbasic replace the value of swl1? I have tried several things. I can not define the javascript variable with out messing up Microbasics replacement.

Code: Select all

'     MCU:             PIC18F45K22

' * NOTES:
'     - ETH click board with ETHERNET controller ENC24J60 is placed to the mikroBUS socket 1.
'       Program uses SPI1 perpheral to comunicate with ETHERNET controller.
'     LED RB0 will always be ON since it is connected to INT pin of ENC24J60.
' *

program ETH_Sample
'**********************************************
include "DS1307_ulcd2"

include "timer"
include "ulcd-32PTU"


' ***********************************
' * RAM variables
' *
' Declarations section
symbol lm34     = PORTA.0
'symbol N/C     = PORTA.1
symbol clk      = PORTA.2
symbol LOAD     = PORTA.3
symbol QH    = PORTA.4
'symbol N/C     = PORTA.5
'
symbol WP   = PORTB.0
symbol MB2_INT   = PORTB.1
symbol ALARM  = PORTB.2
symbol CD     = PORTB.3 'cd input for SD memory
'symbol N/C = PORTB.4     '
'symbol N/C    = PORTB.5
symbol PGC     = PORTB.6
symbol PGD     = PORTB.7
  '
symbol ZPIN  = PORTC.0
symbol MPIN  = PORTC.1
symbol XIN   = PORTC.2
symbol SCL   = PORTC.3   'for DS1307
symbol SDA   = PORTC.4   'for DS1307
symbol SD0  = PORTC.5
symbol RC6      = PORTC.6
symbol RC7      = PORTC.7
  '
symbol SCK2   = PORTD.0
symbol SDI2  = PORTD.1
symbol LCD_BUFFER   = PORTD.2
symbol SS2  = PORTD.3
symbol SDO2  = PORTD.4
symbol WIFI_BUFFER  = PORTD.5
symbol TX2      = PORTD.6
symbol RX2      = PortD.7

symbol RE0    = PORTE.0
symbol RE1    = PORTE.1
'symbol N/C    = PORTE.2
  '
  '
' Declarations section
' mE ehternet NIC pinout

dim sw as byte
dim sw_txt as string[6]'value of sw converted to string


dim adc as word
dim k as string[23]
dim adc2 as float
dim swl1 as string[25]'location 1


dim
  SPI_Ethernet_Rst as sbit at LATE1_bit  ' for writing to output pin always use latch (PIC18 family)
  SPI_Ethernet_CS  as sbit at LATE0_bit  ' for writing to output pin always use latch (PIC18 family)
  SPI_Ethernet_Rst_Direction as sbit at TRISE1_bit
  SPI_Ethernet_CS_Direction  as sbit at TRISE0_bit
' end ethernet NIC definitions

dim myMacAddr   as byte[6]   ' my MAC address
    myIpAddr    as byte[4]   ' my IP address
    gwIpAddr    as byte[4]   ' gateway (router) IP address
    ipMask      as byte[4]   ' network mask (for example : 255.255.255.0)
    dnsIpAddr   as byte[4]   ' DNS server IP address

' ************************************************************
' * ROM constant strings
' *
const httpHeader as string[30]         = "HTTP/1.1 200 OK"+chr(10)+"Content-type: "  ' HTTP header
const httpMimeTypeHTML as string[11]   = "text/html"+chr(10)+chr(10)                 ' HTML MIME type
const httpMimeTypeScript as string[12] = "text/plain"+chr(10)+chr(10)                ' TEXT MIME type
const httpMethod as string[5]          = "GET /"
' *
' * web page, splited into 2 parts :
' * when coming short of ROM, fragmented data is handled more efficiently by linker
' *
' * this HTML page calls the boards to get its status, and builds itself with javascript
' *

'------------------------------------------------------------------------------------------------------
const indexPage as string[723]=
"<!DOCTYPE html>"+
"<html>"+
"<head>"+
"<title>Index</title>"+

"<style type=" + chr(34) + "text/css" + chr(34) + ">"+
".auto-style1 {"+
"        font-weight: bold;"+
"        color: #0000FF;"+
"}"+
"</style>"+
"</head>"+
"<body>"+
"<a href=" + chr(34) + "/" + chr(34) + ">Reload</a>"+
"<script src=/s></script>"+
"<br/>"+
"<br/>"+

"<br/>"+
"<br/>"+

"<br/>"+

"<br/>"+

"<br/>"+
"<h4>The Date is: "+
"<script type=" + chr(34) + "text/javascript" + chr(34) + ">"+
"<!--"+
"var currentTime = new Date()"+
"var month = currentTime.getMonth() + 1"+
"var day = currentTime.getDate()"+
"var year = currentTime.getFullYear()"+
"document.write(month + " + chr(34) + "/" + chr(34) + " + day + " + chr(34) + "/" + chr(34) + " + year)"+
"//-->"+
"</script>"+
"</h4>"+
"<br/>"+
"<br/>"+
"<span class=" + chr(34) + "style1" + chr(34) + "><strong>Status</strong></span>"+
"<br/>"+
"Temperature =<script>document.write(k)</script> Degrees F"+
"<br/>"+
"SWL1=<script>document.write(swl1)</script>"+
"<br/>"+
"SW =<script>document.write(sw_txt)</script>"+
"<br/>"+
"</body>"+
"</html>"

'------------------------------------------------------------------------------------------------------
'*************************************************************************************************
                    
dim    getRequest  as byte[15]   ' HTTP request buffer
       dyna        as char[30]   ' buffer for dynamic response
       httpCounter as word       ' counter of HTTP requests
       txt         as string[11]

' *******************************************
' * user defined functions
' *

' *
' * this function is called by the library
' * the user accesses to the HTTP request by successive calls to Spi_Ethernet_getByte()
' * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
' * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit
' *
' * if you don't need to reply to HTTP requests,
' * just define this function with a return(0) as single statement
' *
' *
sub function Spi_Ethernet_UserTCP(dim byref remoteHost as byte[4],
                              dim remotePort, localPort, reqLength as word, dim byref flags as TEthPktFlags) as word
  dim  i as word        ' my reply length
       bitMask as byte  ' for bit mask
       txt         as string[11]
    result = 0

    ' should we close tcp socket after response is sent?
    ' library closes tcp socket by default if canClose flag is not reset here
    '  flags.canClose = 0 ' 0 - do not close socket
                    ' otherwise - close socket

    if(localPort <> 80) then          ' I listen only to web request on port 80
      result = 0
      exit
    end if

    ' get 10 first bytes only of the request, the rest does not matter here
    for i = 0 to 10
      getRequest[i] = Spi_Ethernet_getByte()
    next i

    getRequest[i] = 0

    ' copy httpMethod to ram for use in memcmp routine
    for i = 0 to 4
      txt[i] = httpMethod[i]
    next i

    if(memcmp(@getRequest, @txt, 5) <> 0) then  ' only GET method is supported here
      result = 0
      exit
    end if

    Inc(httpCounter)                           ' one more request done


    if(getRequest[5] = "s") then               ' if request path name starts with s, store dynamic data in transmit buffer
      ' the text string replied by this request can be interpreted as javascript statements
      ' by browsers
      
      result = SPI_Ethernet_putConstString(@httpHeader)                    ' HTTP header
      result = result + SPI_Ethernet_putConstString(@httpMimeTypeScript)   ' with text MIME type
     '------------------------
      ' add temperature (string temperature value to reply
      FloatToStr(adc2,dyna)
      txt  = "var k="
      result = result + Spi_Ethernet_putString(@txt)
      result = result + Spi_Ethernet_putString(@dyna)
      txt  = ";"
      result = result + Spi_Ethernet_putString(@txt)
      '------------------------
      ' add sw_txt value to reply
      dyna=sw_txt
      txt  = "var sw_txt="
      result = result + Spi_Ethernet_putString(@txt)
      result = result + Spi_Ethernet_putString(@dyna)
      txt  = ";"
      result = result + Spi_Ethernet_putString(@txt)
      '------------------------
      ' add SW integer to reply
      dyna=sw_txt
      txt  = "var SW="
      result = result + Spi_Ethernet_putString(@txt)
      result = result + Spi_Ethernet_putString(@dyna)
      txt  = ";"
      result = result + Spi_Ethernet_putString(@txt)
      '------------------------

      dyna=swl1
      txt  = "var SWL1="
      result = result + Spi_Ethernet_putString(@txt)
      result = result + Spi_Ethernet_putString(@dyna)
      txt  = ";"
      result = result + Spi_Ethernet_putString(@txt)
       '------------------------

    else
      if(getRequest[5] = "t") then                         ' if request path name starts with t, toggle PORTD (LED) bit number that comes after
        bitMask = 0
        if(isdigit(getRequest[6]) <> 0) then               ' if 0 <= bit number <= 9, bits 8 & 9 does not exist but does not matter
          bitMask = getRequest[6] - "0"                    ' convert ASCII to integer
          bitMask = 1 << bitMask                           ' create bit mask
          LATD   = PORTD xor bitMask                      ' toggle PORTD with xor operator
        end if
      end if
    end if

    if(result = 0) then ' what do to by default
      result = SPI_Ethernet_putConstString(@httpHeader)                  ' HTTP header
      result = result + SPI_Ethernet_putConstString(@httpMimeTypeHTML)   ' with HTML MIME type
      result = result + SPI_Ethernet_putConstString(@indexPage)          ' HTML page first part
      'result = result + SPI_Ethernet_putConstString(@indexPage2)         ' HTML page second part
    end if
    
    ' return to the library with the number of bytes to transmit
end sub

' *
' * this function is called by the library
' * the user accesses to the UDP request by successive calls to Spi_Ethernet_getByte()
' * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
' * the function must return the length in bytes of the UDP reply, or 0 if nothing to transmit
' *
' * if you don't need to reply to UDP requests,
' * just define this function with a return(0) as single statement
' *
' *
sub function Spi_Ethernet_UserUDP(dim byref remoteHost as byte[4],
                              dim remotePort, destPort, reqLength as word, dim byref flags as TEthPktFlags) as word
    result = 0            ' back to the library with the length of the UDP reply
end sub
 sub procedure buffers_off()
  LCD_Buffer=1
  WIFI_Buffer=1
end sub

sub procedure LCD_on()
  UART1_Init(9600)
  LCD_Buffer=0
  WIFI_Buffer=1
end sub
sub procedure DS1307_INIT()
  I2C1_Init(100000)     ' initialize full master mode
  I2C1_Start()          ' issue start signal
  I2C1_Wr(0xD0)         ' address DS1307
  I2C1_Wr(0)            ' start from word at address (REG0)
  I2C1_Wr(0x80)         ' write $80 to REG0. (pause counter + 0 sec)
  I2C1_Wr(0)            ' write 0 to minutes word to (REG1)
  'I2C1_Wr(0x17)         ' write 17 to hours word (24-hours mode)(REG2)
  I2C1_Wr(0x12)         ' write 12 to hours word (24-hours mode)(REG2)
  I2C1_Wr(0x02)         ' write 2 - Monday (REG3)
  I2C1_Wr(0x04)         ' write 4 to date word (REG4)
  I2C1_Wr(0x05)         ' write 5 (May) to month word (REG5)
  I2C1_Wr(0x09)         ' write 09 to year word (REG6)
  I2C1_Stop()           ' issue stop signal
  '
  I2C1_Start()          ' issue start signal
  I2C1_Wr(0xD0)         ' address DS1307
  I2C1_Wr(0)            ' start from word at address 0
  I2C1_Wr(0)            ' write 0 to REG0 (enable counting + 0 sec)
  I2C1_Stop()           ' issue stop signal

end sub

sub procedure beep()
  ALARM=1
  Delay_ms(100)  'pause
  ALARM=0
end sub

sub procedure pulse_clock()
  Delay_us(50)
  clk=1
  Delay_us(50)
  clk=0
  Delay_us(50)
end sub
sub procedure read_sw()
  load=0
  Delay_ms(1)
  load=1
  Delay_ms(1)
  sw.7 = QH          'load bit
  pulse_clock       'bring clk pin high, then low
  sw.6 = QH          'same as above
  pulse_clock
  sw.5 = QH
  pulse_clock
  sw.4 = QH
  pulse_clock
  sw.3 = QH
  pulse_clock
  sw.2 = QH
  pulse_clock
  sw.1 = QH
  pulse_clock
  sw.0 = QH
  ByteToStr(sw, sw_txt)
end sub
main:
' ******************************************************
 ANSELC = 0x00
  ANSELD = 0x00
  ANSELA = %00000001
  ADCON1=0x0E 'RA0=analog rest of port A digital
  CM1CON0 = 0 ' comparator off
  CM2CON0 = 0 ' comparator off
  TRISA  = %11110011  ' PORTA is input
  TRISB = %11101011 'needs work
  Intcon2.rbpu = 0   'turn on PORTB weak-pullups
  TRISC =%10010101
  TRISD = %00000010 'port D
  TRISE = %11000000
  Delay_ms(200)  'pause
  'buffers_off()
  Delay_ms(100)
  UART1_Init(9600) 'setup hardware uart1
  Delay_ms(500)
  beep()
  Delay_ms(1000)
  LCD_on()'turn on lcd buffers all others off
  Delay_ms(1000)
  gfx_Cls()

  Delay_ms(1000)  '1 second pause
  gfx_Cls()
  Delay_ms(500)
  putch("X")
  txt_MoveCursor(1,0)
  putch("Y")
  putst("TEST")
  ''Delay_ms(500)


  LOAD=0  'start load low

  Delay_ms(1000)  ' 1 second pause
'*******************************************************

  httpCounter = 0
  ' set mac address
  myMacAddr[0] = 0x00
  myMacAddr[1] = 0x14
  myMacAddr[2] = 0xA5
  myMacAddr[3] = 0x76
  myMacAddr[4] = 0x19
  myMacAddr[5] = 0x3F

  ' set IP address
  myIpAddr[0] = 192
  myIpAddr[1] = 168
  myIpAddr[2] = 1
  myIpAddr[3] = 21
  
  ' set gateway address
  gwIpAddr[0]  = 192
  gwIpAddr[1]  = 168
  gwIpAddr[2]  = 1
  gwIpAddr[3]  = 1

  ' set dns address
  dnsIpAddr[0] = 192
  dnsIpAddr[1] = 168
  dnsIpAddr[2] = 1
  dnsIpAddr[3] = 1

  ' set subnet mask
  ipMask[0]    = 255
  ipMask[1]    = 255
  ipMask[2]    = 255
  ipMask[3]    = 0

'   *
'   * starts ENC28J60 with :
'   * reset bit on PORTC.B0
'   * CS bit on PORTC.B1
'   * my MAC & IP address
'   * full duplex
'   *
'**************************************
' must have the following 4 lines
'SPI1_Init()
'SPI_Ethernet_Init(myMacAddr, myIpAddr, _SPI_Ethernet_FULLDUPLEX)           ' init ethernet module
'SPI_Ethernet_setUserHandlers(@SPI_Ethernet_UserTCP, @SPI_Ethernet_UserUDP) ' set user handlers
'SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr)



'**************************************


  SPI1_Init()
  Delay_ms(500)
  SPI_Ethernet_Init(myMacAddr, myIpAddr, _SPI_Ethernet_FULLDUPLEX)           ' init ethernet module
  SPI_Ethernet_setUserHandlers(@SPI_Ethernet_UserTCP, @SPI_Ethernet_UserUDP) ' set user handlers

  ' dhcp will not be used here, so use preconfigured addresses
  SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr)

  while TRUE                      ' do forever


    SPI_Ethernet_doPacket()       ' process incoming Ethernet packets

'     *
'     * add your stuff here if needed
'     * SPI_Ethernet_doPacket() must be called as often as possible
'     * otherwise packets could be lost
'     *
  LCD_on()'turn on lcd buffers all others off
  '*************************************

  read_sw()

  Delay_ms(10)  ' pause
  adc = Adc_Read(0)
  adc2 = 0.49*adc

  FloatToStr(adc2,k)
  LCD_putSt(5,1 ,"Temp =")
  LCD_putSt(5,8 ,k)
  Delay_ms(100)  'pause
  sw=StrToInt(sw_txt)

  swl1="Text_String"

  wend
end.

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: String sent to javascript is undefined

#4 Post by darko.minic » 23 Mar 2015 10:39

Hi,

Can you please send me whole zipped project, so I can compile it and try to reproduce the problem?

Best regards,

microchip22
Posts: 41
Joined: 31 Mar 2012 19:13

Re: String sent to javascript is undefined

#5 Post by microchip22 » 27 Mar 2015 22:27

Here is the complete sample project file.
Attachments
ETH_Sample.zip
(10.5 KiB) Downloaded 147 times

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: String sent to javascript is undefined

#6 Post by darko.minic » 30 Mar 2015 10:26

Hi,

I will examine your project and let you know the results.

Regards,
Darko

microchip22
Posts: 41
Joined: 31 Mar 2012 19:13

Re: String sent to javascript is undefined

#7 Post by microchip22 » 01 Apr 2015 11:32

Any progress ?

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: String sent to javascript is undefined

#8 Post by darko.minic » 02 Apr 2015 09:45

Hi,

Unfortunately i was unable to get your example to get work. Code is huge, and is hard to tell where problem occurs.

Can you please rewrite our example with as little as possible lines of code which represents the problem,
so we can be able to isolate the problem and reproduce it?

Regards,
Darko

microchip22
Posts: 41
Joined: 31 Mar 2012 19:13

Re: String sent to javascript is undefined

#9 Post by microchip22 » 02 Apr 2015 14:03

I have simplified the project as much as I can. I removed code for the DS1307 clock chip and LCD display. The html code will display the temperature variable (k) and SWL1
Attachments
ETH_example.zip
(4.12 KiB) Downloaded 148 times

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: String sent to javascript is undefined

#10 Post by darko.minic » 03 Apr 2015 15:17

Hi,

I have tried your project through debug and SWL1 variable receives values as expected.
Indeed there is some issue with getting values in Java script, but unfortunately this is application level of using Ethernet library,
so this is out of scope of our support.

I can recommend you to try with some other browsers, and see if problem remains the same.
Also you should sniff Ethernet communication (you can download some sniff application) to see in which moment occurs.

Regards,
Darko

Darklakebridge78
Posts: 136
Joined: 12 May 2007 14:04
Location: Italy
Contact:

Re: String sent to javascript is undefined

#11 Post by Darklakebridge78 » 25 Apr 2020 16:42

Hi! The problem is the string you send to the browser.
If it is not correctly filled in it returns the value "undefined". It's normal, it's not a number!
If you want to correctly display a string, you must format the output correctly.

Example:
in the html you write:

Code: Select all

"<script src = / s> </script>"
<Script>document.write(VRL)</ script>
in the Spi_Ethernet_UserTCP routine (...)

Code: Select all

... normal routine code ...

if(GetRequest[5] = "s") then
   result = SPI_Ethernet_putConstString(@httpHeader)
   result = result + SPI_Ethernet_putConstString(@httpMimeTypeScript)

   txt  = "var VRL=" + chr(34) + "your text" + chr(34) + ";"
   result = result + Spi_Ethernet_putString(@txt)
end if
Where "txt" is:

Code: Select all

dim txt as string[35]
At that point the browser will make you the string to be published.

In fact, if you look at javascript tutorials (https://www.w3schools.com/js/js_variables.asp), the code needed to publish a string is the following:
var VRL = "your text";

For Network WiFi library the code synthesis is this:

Code: Select all


tmp = "var DVN="
PutString(@tmp)
dyna = chr(34) + "your text" + chr(34)
PutString(@dyna)
tmp = ";"
PutString(@tmp)

Obviously VRL and DVN are the names of the variables to be inserted, in my case are a fancy name! :-)
MikroC PRO for PIC, MikroC PRO for dsPIC, MikroC PRO for PIC32, MikroC PRO for ARM, Visual TFT, Visual GLCD,
http://www.teolab.it

Post Reply

Return to “mikroBasic PRO for PIC General”