Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody.

Something very strange happens in my code and I really need your help.

So, my setup is an arduino uno with an ethernet shield and a vb net application. What app does is to give some data to the arduino in a local net . It works but there is an issue. Sometimes i v got rapidly response from the arduino some others with a delay which is max approx 4 sec. I cant find why is that happen.

When i try the same procedure through the web browser or my mobile it works without any delay.

What I have tried:

Here is my codes.

arduino code :

<pre lang="c#"><pre>//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>
#define HC12 Serial
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
    HC12.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 200) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//   

String stringOne = readString;
  //Serial.println(stringOne);
  // replace() changes all instances of one substring with another:
  // first, make a copy of the original string:
  String stringTwo = stringOne;
  // then perform the replacements:
  stringTwo.replace("HTTP/1.1", "");
  // print the original:
  //Serial.println("Original string: " + stringOne);
  // and print the modified string:
  //Serial.println("Modified string: " + stringTwo);


 // String stringOne = readString;
  //Serial.println(stringOne);
  // replace() changes all instances of one substring with another:
  // first, make a copy of the original string:
  String stringThree = stringTwo;
  // then perform the replacements:
  stringThree.replace("%20",  " ");
  // print the original:
  //Serial.println("Original string: " + stringOne);
  // and print the modified string:
  //Serial.println("Modified string: " + stringTwo);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          

          ///////////////
          HC12.println(stringThree); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 ");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
         client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on1\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
} 




and the vb code :

<pre lang="vb"><pre>
 
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    
    


    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If SerialPort1.IsOpen Then
            SerialPort1.WriteLine("m")
        End If
        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "m"
        WebBrowser1.Navigate(TextBox10.Text)
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
       

        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "c" & Label10.Text
        WebBrowser1.Navigate(TextBox10.Text)
        ' Threading.Thread.Sleep(20)
        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "a" & Label11.Text
        WebBrowser1.Navigate(TextBox10.Text)
        ' Threading.Thread.Sleep(20)
        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "k"
        WebBrowser1.Navigate(TextBox10.Text)
        ' Threading.Thread.Sleep(20)
    End Sub

   

    

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        Label8.Text = TrackBar1.Value
        If SerialPort1.IsOpen Then

            SerialPort1.WriteLine("z" & Val(Label8.Text))
        End If
        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "z" & Val(Label8.Text)
        WebBrowser1.Navigate(TextBox10.Text)
    End Sub

   

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label5.Text = "ΕΞΕΛΙΞΗ"
        Label10.Text = TextBox1.Text * 3600 + TextBox2.Text * 60
       

        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "c" & Label10.Text
        WebBrowser1.Navigate(TextBox10.Text)


        If Label10.Text > 0 Then
            Timer2.Start()
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label15.Text = "ΕΞΕΛΙΞΗ"
        Label11.Text = TextBox3.Text * 3600 + TextBox4.Text * 60
      
        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "a" & Label11.Text
        WebBrowser1.Navigate(TextBox10.Text)



        'Label11.Text = Label11.Text + 1
        If Label11.Text > 0 Then
            Timer3.Start()
        End If
    End Sub
  

 
   

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
       
       
        TextBox10.Text = TextBox11.Text
        TextBox10.Text = TextBox10.Text & "s" & RichTextBox1.Text
        WebBrowser1.Navigate(TextBox10.Text)
    End Sub

   

    
End Class
Posted
Comments
Gerry Schmitz 6-Apr-19 15:40pm    
Too much code for anyone to bother with. You need to "narrow things down".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900