$crystal = 20000000 $regfile = "m168def.dat" $baud = 9600 '-------------- Config --------------------------------------------------------- 'config LCD Config Portd = Output Config Lcdbus = 4 Config Lcd = 20 * 2 'configure lcd screen Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2 Dim Freetext As String * 30 'all neccessary for interrupt input of serial chars Dim Ser_rx_char As String * 1 Dim Ser_rx_char_byte As Byte Dim Serialinput As String * 100 'int routine is collecting input string in this variable Dim New_input_string As String * 100 'if complete string received it will be saved here Dim Parse_string As String * 100 'parser gets this string Dim New_input_string_received As Bit Dim Input_zaehl As Byte Dim String_len As Byte 'variables for parser Dim Parser_string_len As Byte Dim Parser_split_pointer As Byte Dim Parse_wd(8) As String * 25 Dim Parse_wd_counter As Byte 'config timer which is used for system tick Config Timer1 = Timer , Prescale = 64 On Timer1 Timer_irq Const Timer_reload = 49911 '20 Hz timertick Dim Timertick As Byte Dim Stopwatch As Word 'is incremented with 20 Hz. 1 sec delay: Test for stopwatch = 20 Dim Systick As Long Enable Timer1 'activate Timer1 'hardware UART is used Config Serialin = Buffered , Size = 250 'PD0 = TXO, PD1 = RXI '250 Byte input buffer Enable Interrupts 'neccessary because uart fires interrupt if new char received 'ADC config Config Adc = Single , Prescaler = Auto , Reference = Avcc Start Adc Dim Analogmess As Word Dim Level As Bit 'config I/Os Config Portb.3 = Output Blue Alias Portb.3 Config Portb.4 = Output Red Alias Portb.4 Config Portb.5 = Output Green Alias Portb.5 Red = 1 Green = 1 Blue = 1 Config Portc.4 = Output Weiss_0 Alias Portc.4 Config Portc.5 = Output Weiss_1 Alias Portc.5 Dim Temp As String * 10 Dim Temp_b As Byte '-------------- INIT ----------------------------------------------------------- Systick = 0 Cls Upperline '---"01234567890123456789" Lcd "WEB client" Lowerline Lcd "0133" Cursor Off Noblink Serialinput = "" 'Inputpuffer reset New_input_string = "" 'input string reset reset New_input_string_received = 0 'flag reset ' connect remote BT module Blue = 0 Wait 10 'wait for bt module to finish startup Blue = 1 Red = 0 Gosub Bt_connect Red = 1 Green = 0 '-------------- MAIN ----------------------------------------------------------- Do Cls Gosub Get_analog If Pinc.5 = 1 Then 'test for port set by remote host Level = 0 'if used in hackerspace use different port Else Level = 1 End If 'define url encoded freetext to be displayed on website ------------------ Freetext = "Hallo%20das%20ist%20ein%20Text" Green = 1 Blue = 0 'send GET string via arduino to webserver -------------------------------- Print "GET /index.php?"; Print "0=" ; Systick ; Print "&3=" ; Freetext ; Print "&4=" ; Level ; Print " HTTP/1.1 " Blue = 1 Green = 0 Stopwatch = 0 While Stopwatch < 200 '-------------- fetch waiting chars If Ischarwaiting() = 1 Then 'poll buffer of uart routine Gosub Get_serial_char End If '-------------- test for new input string If New_input_string_received = 1 Then 'complete string was received New_input_string_received = 0 'flag reset Parse_string = New_input_string 'prepare string for parser Gosub Parse_input_string 'analyze words in received string End If Wend Loop '-------------- end MAIN ------------------------------------------------------- '-------------- subroutines ---------------------------------------------------- Parse_input_string: '-------------- parse input string For Parse_wd_counter = 1 To 8 'strip leading blanks While Left(parse_string , 1) = " " Parser_string_len = Len(parse_string) Parser_string_len = Parser_string_len - 1 Parse_string = Right(parse_string , Parser_string_len) Wend 'find first blank char Parser_split_pointer = Instr(parse_string , " ") 'look for first blank character If Parser_split_pointer > 0 Then '= 0 if no blank found Parser_split_pointer = Parser_split_pointer - 1 End If 'isolate first parse word If Parser_split_pointer > 25 Then 'avoid to set Parse_wd(n) with strings longer than 25 chars Parser_split_pointer = 25 End If Parse_wd(parse_wd_counter) = Mid(parse_string , 1 , Parser_split_pointer) 'reduce parse string to remaining chars which are still due to parse If Parser_split_pointer > 0 Then ' Parser_split_pointer = Parser_split_pointer + 1 Parser_string_len = Len(parse_string) Parser_string_len = Parser_string_len - Parser_split_pointer Parse_string = Right(parse_string , Parser_string_len) Else 'last command word in parse string found Parse_string = "" End If 'Print "[" ; Parse_wd(parse_wd_counter) ; "]"; Next '-------------- interprete commands with arguments Select Case Parse_wd(1) Case "at" : Gosub Attention Case Else 'Print "missing at string, received: [" ; Parse_wd(1) ; "]" 'Print "send [at command argument]" End Select Return '-------------- commands Attention: Select Case Parse_wd(2) Case "line1" : Gosub Line1 Case "line2" : Gosub Line2 Case "led" : Gosub Led1 Case Else End Select Return Line1: Upperline Lcd Parse_wd(3) Return Line2: Lowerline Lcd Parse_wd(3) Return Led1: If Parse_wd(3) = "0" Then Weiss_1 = 0 Else Weiss_1 = 1 End If Return Get_analog: 'get analog voltage and find threshold ----------------------------------- Analogmess = Getadc(0) If Analogmess > 512 Then Level = 1 Else Level = 0 End If Return Bt_connect: 'send connect string to remote BT module Print "atd 1234567890ff2" 'use adress of your BT Module to connect Wait 5 ' to do: test for carrier detect signal Return Bt_hangup: 'verbindungsabbau Wait 5 Print "+++"; Wait 5 Print "ath" Return '-------------- receive serial characters -------------------------------------- ' collect received chars and assemble them to input string Get_serial_char: If Ischarwaiting() = 1 Then Ser_rx_char = Inkey() Ser_rx_char_byte = Asc(ser_rx_char) String_len = Len(serialinput) Select Case Ser_rx_char_byte Case 13 'return chra was recognized New_input_string = Serialinput 'copy string for main routine New_input_string_received = 1 'set flag that new string is ready Serialinput = "" 'reset all for new string Ser_rx_char = "" Case 127 'backspace - char If String_len > 0 Then String_len = String_len - 1 If String_len = 0 Then 'because "left" returns even with (string,0) one char Serialinput = "" Else Serialinput = Left(serialinput , String_len) End If End If Case Is < 32 'drop all ather control chars Ser_rx_char = "" Case Is > 127 Ser_rx_char = "" Case Else 'valid input char If String_len < 100 Then 'room left in input string? Serialinput = Serialinput + Ser_rx_char 'append received char 'Print Ser_rx_char ; 'and echo via serial 'no echo at all! Else 'abort if 99 chars without enter received New_input_string = Serialinput 'then string will be closed without enter Serialinput = "" New_input_string_received = 1 Ser_rx_char = "" End If End Select End If Return Timer_irq: '20 Hz interrupt Timer1 = Timer_reload Stopwatch = Stopwatch + 1 Timertick = Timertick + 1 If Timertick = 20 Then Timertick = 0 Systick = Systick + 1 Toggle Weiss_0 End If Return