Jump to content
43oh

My first attempt TM4C129


Recommended Posts

Hi everyone,

 

I have just received TM4C129 evaluation board and test with this program I have found on google.

#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x00, 0x1A, 0xB6, 0x02, 0xB1, 0x27 };
IPAddress ip(192,168, 0, 13); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Tiva Web Page</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>Hello from Tiva connected!</h1>");
                    client.println("<p>A web page from the Tiva server</p>");
                    client.println("</body>");
                    client.println("</html>");
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

When I connect the  server for the first time with firefox, everything is ok, but  after no success.  I have to reset LM4C129.  Do I miss something to make server permanently listening ?

 

Thank you for help

 

Salutations

 

 

Link to post
Share on other sites

The Sketch below (Example->Ethernet->EthernetWebserver) should work as expected. The only difference that I see is that "EthernetClient client;" is outside of loop ()rather than inside loop(). I will give you Sketch above a try later today to see if I can reproduce the issue.

/*
  Web  Server
 Created: October 16, 2013 by Robert Wessels (http://energia.nu)
 Derived from example Sketch by Hans Scharler (http://www.iamshadowlord.com)
 
 */
 
#include "Ethernet.h"
// Prototypes
void printConfig();
void printEthernetData();
void printIndex();
void printHelp();

EthernetServer server(80);
int statusConfig = 0;

void setup() {
  Serial.begin(115200);    
   
  pinMode(D1_LED, OUTPUT);
  pinMode(D2_LED, OUTPUT);
  pinMode(PUSH1, INPUT_PULLUP); // released = HIGH, pressed = LOW
  pinMode(PUSH2, INPUT_PULLUP);
  
  Serial.println("Connecting to Ethernet....");  
  IPAddress ip = IPAddress(146,252,242,129);
  IPAddress dns = IPAddress(146,252,242,12);
  IPAddress gw = IPAddress(146,252,242,254);
  IPAddress mask = IPAddress(255,255,255,0);

  Ethernet.begin(0);
//  Ethernet.begin(0, ip, dns, gw);

  server.begin();

  printEthernetData();
}

EthernetClient client;

void loop() {
  client = server.available();
  
  if (client) {                             // if you get a client,
    Serial.print("new client on port ");           // print a message out the serial port
    Serial.println(client.port());
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected

      if (client.available()) {             // if there's bytes to read from the client,    
        char c = client.read();             // read a byte, then
                                            // This lockup is because the recv function is blocking.
        Serial.print(c);
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {  
            break;         
          } 
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }     
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        if (currentLine.endsWith("GET / ")) {
          statusConfig = 0;
          printIndex();
        }
        if (currentLine.endsWith("GET /config.html ")) {
          statusConfig = 1;
          printConfig();
        }
        if (currentLine.endsWith("GET /index.html ")) {
          statusConfig = 0;
          printIndex();
        }
        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /RED_LED_H")) {digitalWrite(D1_LED, HIGH);printConfig();}         
        if (currentLine.endsWith("GET /RED_LED_L")) {digitalWrite(D1_LED, LOW);printConfig();}     
        if (currentLine.endsWith("GET /GREEN_LED_H")) {digitalWrite(D2_LED, HIGH);printConfig();}       
        if (currentLine.endsWith("GET /GREEN_LED_L")) {digitalWrite(D2_LED, LOW);printConfig();}
      }
    }
    // close the connection:
    client.stop();
    //Serial.println("client disonnected");
  }
}

void printIndex()
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:    
//  Serial.println("Index");
  client.println("HTTP/1.1 200 OK");
  
  client.println("Content-type:text/html");
  client.println();
  client.println("<html><head><title>CC3000 Energia Webpage</title></head><body align=center>");
  client.println("<h1 align=center><font color=\"red\">Welcome To CC3000 Web Server</font></h1>");
  client.println("</br><font size=\"4px\"><table border=\"0\" align=center width=1200px height=590px>");
  client.println("<tr><td align=center width=375></td><td width=450px align=left valign=\"top\">");
  
  
  client.println("<p>Using CC3000 WLAN connectivity, Web Server provides ");
  client.println("capability to remotely read and write GPIOs ");
  client.println("on/off.</p></br></br>");
  client.println("<p><a href=\"/config.html\">Click here</a> ");
  client.println("to check status and configure the board</p>");
  client.println("<td align=cneter width=375></td></tr></table></font></body></html>");
  
  client.println();

}

void printConfig()
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:    
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  client.println("<html><head><title>CC3000 Energia Webpage</title></head><body align=center>");
  client.println("<h1 align=center><font color=\"red\">Welcome To CC3000 Web Server</font></h1>");
  
  // the content of the HTTP response follows the header:
  // Added: nicer buttons
  client.print("<font color='green'>GREEN_LED</font> <button onclick=\"location.href='/GREEN_LED_H'\">HIGH</button>");
  client.println(" <button onclick=\"location.href='/GREEN_LED_L'\">LOW</button><br>");
  client.print("<font color='red'>RED_LED</font> <button onclick=\"location.href='/RED_LED_H'\">HIGH</button>");
  client.println(" <button onclick=\"location.href='/RED_LED_L'\">LOW</button><br><br>");

  client.println("PUSH1 ");
  if(digitalRead(PUSH1))client.print("is HIGH<br>");
  else client.print("is LOW<br>");
  client.println("PUSH2 ");
  if(digitalRead(PUSH2))client.print("is HIGH<br>");
  else client.print("is LOW<br>");  
  
  client.println("<a href=\"/config.html\" >refresh</a> <br>");
  client.println("<a href=\"/index.html\" >home</a> <br>");
  client.println("</body></html>");
  // The HTTP response ends with another blank line:
  client.println();
  // break out of the while loop:
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    switch(inChar) {
      case 'h':
        printHelp();
        break;
      case 'i':
        printEthernetData();
        break;
      default:
        Serial.println();
        Serial.println("Invalid menu option");
    }
  } 
}

void printHelp() {
  Serial.println();
  Serial.println("+++++++++++++++++++++++++++++++++++++");
  Serial.println("Help menu:");
  Serial.println("\th: This help menu");
  Serial.println("\ti: IP address information");
  Serial.println("+++++++++++++++++++++++++++++++++++++");
}

void printHex(int num, int precision) {
      char tmp[16];
      char format[128];

      sprintf(format, "%%.%dX", precision);

      sprintf(tmp, format, num);
      Serial.print(tmp);
}

void printEthernetData() {
  // print your IP address:
  Serial.println();
  Serial.println("IP Address Information:");  
  IPAddress ip = Ethernet.localIP();
    Serial.print("IP Address:\t");
  Serial.println(ip);
  
  // print your MAC address:
  
  IPAddress subnet = Ethernet.subnetMask();
  Serial.print("NetMask:\t");
  Serial.println(subnet);

  // print your gateway address:
  IPAddress gateway = Ethernet.gatewayIP();
  Serial.print("Gateway:\t");
  Serial.println(gateway);

  // print your gateway address:
  IPAddress dns = Ethernet.dnsServerIP();
  Serial.print("DNS:\t\t");
  Serial.println(dns);

}
Link to post
Share on other sites
  • 2 months later...

I don't know if you're still around or not, but I got a IoT launchpad a couple days ago and have been having similar issues with a webserver program based heavily on the demo/example program in Energia (BarometicPressureWebServer), which uses the same core code as in the original post of this thread.

 

With possibly excessive use of Serial and LED debugging, I think I found the problem.

 

If I'm correct, it's due to the while loop.

It calls

while (client.connected()) {
if (client.available()) {

and then goes on and does things.

 

The break; statement that ends that while is inside the if (client.available). If the client never sends anything, or if the Launchpad misses it, client.available() never returns true, the IF never happens, and the break; statement never happens either. Leaving you stuck in the while loop for all eternity.

 

I've been banging my head against this issue for a couple days, and finally figured out the above. I put in a timeout to kill the connection (call break; and let it trundle off to client.stop()) after 1000ms.

void listenForEthernetClients() {

  // listen for incoming clients


  boolean activeConnection = false;


  EthernetClient client = server.available();



  if (client) {

    Serial.println("Got a client");

    // an http request ends with a blank line

    boolean currentLineIsBlank = true;

    char c;

    

    while (client.connected()) {

      if (!activeConnection){

        connectionActiveTimer = millis();

        activeConnection = true;

      }

      

      if (activeConnection && connectionActiveTimer + connectionTimeout < millis()){

        break;

      }

        

      if (client.available()) {

I've seen it hang in the while loop for a second and then reset and start working again, which leads me to believe that I've found and fixed the problem.

Bug in the demo problem rather than a bug with Ethernet or the IoT.

 

 

 

As a note, I'm absolutely loving the board and Energia support for it. Without Energia I'd be completely lost trying to do anything with this board.

Link to post
Share on other sites
  • 2 weeks later...
  • 1 month later...

Just a heads up for anyone interested, but the current EthernetWebserver.ino file for the LM4F on GitHub has some issues. The biggest is that the newConnection flag is a global variable and never gets reset to true after being set to false for the first time. To fix the issue, either move the newConnection declaration into the loop() function before the while() loop statement or add a line of code before the while() loop that sets the newConnection variable to true.

Link to post
Share on other sites
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...