Mbed with Magjack

17 May 2011

Hello,

Good Evening.

This morning I just received the MBED.

Following is the testing and preparation that i do it in last weekend.

Testing Step:

1) Just now I did test the Hello World sample code using USB power. The LED1 is blinking every 0.5sec. It is working fine. But i realize the module turns ON after 5 min, it starts getting warm on the module pin and the processor.

2) I’m using USB power. Mbed pin-2, pin-39, and pin-40 voltage Measurement:

Vin (pin-2) = 4.76V (I wonder why it have supply?)

Vo (pin-40) = 3.302V

Vu (pin-39) = 4.93V

3) Testing board and schematic includes: /media/uploads/bc/circuit1.jpg

- 5V regulator and 3V3 regulator

- 5V regulator output ready connect to Mbed Vin pin-2

- 3V3 regulator output ready connect to Magjack either PRT-08534(pin-3 and pin-6) / HR911105A (pin-4 and pin-5)

Magjack PRT-08534 datasheet: http://www.sparkfun.com/datasheets/Prototyping/MagJack.pdf

Magjack HR911105A datasheet: http://www.terraelectronica.ru/pdf/HANRUN/HR911105A.pdf

Ethernet RJ45: http://mbed.org/cookbook/Ethernet-RJ45

4) Magjack resistance and voltage measurement:

- Magjack center tap (TC and RC) 3V3 voltage is supply from external regulator Q5 /media/uploads/bc/circuit2.jpg

5) Prepared

Hardware: - 2meter length LAN cable (Cross cable type. I'm using Wifi modem).

Software: - installed Wireshark – Network protocol analyzer (new on this software, any software can recommend? )

6) Ready for testing…….

Does anyone can guide me what's the next step i can further on? or is there any other information that i miss it and you need me to provide more detail?

Early have one module but i wonder is it the Magjack cause the Mbed dead. I found out the PRT-08534 and HR911105A Magjack all the primary pin have very low resistance. I'm very worry the module will spoil again.

Thanks for your help.

Best Regards,

Wen

16 May 2011

Sorry I don't really have any suggestions.. except...

BUY A BREADBOARD !!

you really don't want to prototyping with soldered connections like that.

Plus..

That MagJack looks like it might short across any of the wires at any minute. Personally, that wiring would scare me. I'd have it all well spaced out on a breadboard.

16 May 2011

Hello David,

Ya, i have breadboard. To attach the Mbed on breadboard it is a bit hard. So i try to do it externally. No, it is just temporary using wiring flying out over the board. After confirm the thing is work, I will fix it properly and slowly add other feature on this PCB board. End of the day this board will become my basic Mbed testing board.

The wire (the blue-wire that you can seen in the photo) that i use is quite hard and trial. Ya, i understand what you worry, the 4 wire connect to the black-header it look like going to short circuit at any time. Don't worry, the wire is solder on male header pin then put it in black-header. So the wire is quite stably wouldn't swing around or left-right! Another reason why i doing that is because the MagJack solder pin is not in parallel row.

Thanks for your remind and advice. I will whilst check the connection before i ON power.

May i know which Macjack you are using?

16 May 2011

Hi,..

I'm afraid I've not done that with my mbed yet ! It thought there was a little breakout board available for MBED with a MagJack and USB and things on it ?

I'll have a look see if I can see what I mean...

EDIT:

This is what I was thinking of...

http://mbed.org/projects/cookbook/wiki/mbed-BoB

there are others around, just search the site for BOB !

16 May 2011

Hi,

Take a careful look at your measurements for p39 & p40, you might have them round the wrong way. There should be no need for a seperate external 3.3V regulator during testing.

Get yourself one of these or something similar from a local source: http://www.coolcomponents.co.uk/catalog/product_info.php?cPath=23_29_41&products_id=528

17 May 2011

Hello David,

Yeah...It is nice development kit. Thanks for your sharing. This DevKit have given me some idea, in future i might do a small DevKit for myself. It is quite interesting.

Hello Carl,

Thanks for your reply.

Opp..sorry, typing error. Noted, thanks for your remind, Edited. At this moment i haven't use on board supply.

aaa aaa wrote:

There should be no need for a seperate external 3.3V regulator during testing.

This is what i concern. The Magjack have very low resistance <1 Ohm, i worry it will damage the Mbed. My previous module malfunctioning after power is ON middle LED is not turn ON. Then i measured the LD1117S33 regulator (which is beside the pin-4 to pin-7) Vout just 1.27V only and it slowly getting hot. Because of this i remove the LD1117S33 from the board then i found out the LDO regulator PCB pad Vout and GND is 1.2Ohm. It almost short circuit ya.

By rite we should be no need to separate the 3.3V. At this moment I just try use external supply for my testing and just narrow down the mistake and just focus the Magjack TD+/- and RD+/-.

At this moment i would like to build the hardware by myself and across the embarrass. So that i can learn it. So, in future if i want to build my own development board will be easy.

Carl, may i know which Magjack you are using?

17 May 2011

Hello Wen,

I'm using the Magjack on the cool componets board I linked to earlier. They also show a schematic for the board with Magjack, on their site.

Looking at Magjacks in general you'll need to be sure you have the correct type for your end use, however even if you picked a wrong one, it should not cause your hardware to drop volts and get hot.

http://ww2.pulseeng.com/products/datasheets/J414.pdf shows yet another variety with variation on pin out.

If you are measuring extreme low ohm resistances on all of your Magjacks then they are probably good. A complete short or open cct would be bad. Typically you should have about 0.6 Ohm max across the taps just as you have measured. See this data for reassurance. http://www.belfuse.com/Data/Datasheets/SI-46001-F.pdf

Rgds, Carl

18 May 2011

Hello Carl,

Thanks for your advice. I did take heart of grace to test it. The thing look safe.

Is there any sample program for me to do the testing? eg: Web page control on board 4 LEDs.

I'm using Wifi modem and connect the Mbed to my PC using cross cable. On my PC side it is i have to set fix IP?

Many thanks.

Bst Rgrds, Wen

18 May 2011

Hello Wen,

In a basic setup you would need to use a static IP, just pick one from your router static pool if possible or 'borrow a sensible one' from the DHCP pool ;)

Try this link if you havn't already found suitable code. http://mbed.org/search/?type=&q=nethttpserver

I believe with a little effort you should be able to control the LED's from your PC's browser. A nice little project to begin with.

Good luck, Carl

20 May 2011

Hello Carl,

Ya, thanks for your link. There is a lot of information there.

This few day i did tried a few testing and once of the testing is "Ethernet Testing" ( http://mbed.org/users/no2chem/notebook/ethernet-testing/ ) tutorial, this tutorial working fine for me. In this coding we need to do some IP configuration and the changes

dhcp_start(netif); < Use DHCP

with

netif_set_up(netif);

I wonder why we need to change it to netif_set_up(netif);. Is it i can't DHCP when my Mbed direct connect to PC?

Another testing i was try is the "NetHttpServerHelloWorld" (http://mbed.org/forum/mbed/topic/139/?page=2#comment-3809) tutorial. In this tutorial i can't get the IP address. The testing result is /media/uploads/bc/dhcp.jpg

On 30 Mar 2010 Donatien have said:

Donatien Garnier wrote:

Since your mbed is directly connected to your computer, you can't use the mbed's DHCP client unless you have installed a DHCP server your computer, so I assume that your mbed can't get attributed an IP address and gets stuck there.

You can either install a DHCP server on your computer (but is it really worth it...?), or program the mbed to use a local address (like 192.168.x.y for instance).

Example when you instanciate your server :

HTTPServer("mbed", IPv4(192,168,0,101));

I have lack of DHCP knowledge. Above message seem like i can't connect mbed directly to PC. What does it mean "...program the mbed to use a local address.."? is it i have to add this line HTTPServer("mbed", IPv4(192,168,0,101)); in the coding? How can i do?

Thank you, Wen.

20 May 2011

Wen,

Connect your MBED to your internet router, which will almost certainly have a DHCP service running. Or enable ICS (internet connection sharing) on your PC, that way the internal DHCP service will do its thing.

C.

23 May 2011

Hello Carl,

Now i able to use cross cable to interface with PC and using DHCP service after enable the ICS. At this moment i haven't do the test for direct connect the Mbed to internet router, later i will test it. Thanks a lot ya...

The progress and testing is quite fun and excited. I able to control the LED just create a simple small button in HTMP file, but i wonder why if i simple click the button few times with fast..(keke..) then the Mbed will start hang...need to reset the Mbed. Is it the method that i use is quite slow for Mbed to capture the data?

// Inform TCP that we have taken the data. 
        tcp_recved(pcb, p->tot_len);
        data = static_cast<char *>(p->payload);

When after the button is click the data content will update. I just simply take the content of the data to do the data comparing.

Please can you share your opinion?

Bst Rgds, Wen

23 May 2011

Hi Wen,

I'm afaid I don't have any experience with the particular code base you are working with and besides I am but a noob coder myself.

Did you build the ehternet link using 10BASET or 100BASET magnetics, I can't really tell from your diagram? Just wondering if the ethernet datalink layer is good. That behaviour is typical of a mis matched ethernet link. Make sure you have 10Mb/s half duplex at both ends. Actually I'm not sure of the MBED's default ethernet setting, someone might know or research it from the M3 documentation.

Regards, Carl

26 May 2011

Hello Carl,

Is ok, I'm newbie also... :( Hope we can get someone help from this forum.

Quote:

Did you build the ehternet link using 10BASET or 100BASET magnetics

What do you mean? Do you mean on the hardware part? On the circuit part i didn't add any component in between Mbed to Magjack just the two 100nF cap. How can we check our PC ethernet port duplex parameters?

I just discover the Mbed ethernet speed configuration. Did you seen this link before? http://mbed.org/projects/libraries/changeset/22/mbed/trunk/Ethernet.h#file0

  Ethernet MyEth;
  ...
  ..
  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  MyEth.set_link(Ethernet:: HalfDuplex10); // configure 10Mb/s half duplex
  printf("Setup OK\n");

I having the ethernet connection problem after add the configuration. I did tried others modes AutoNegotiate, FullDuplex10, and so on but still the same.

Another doubt, how can i check Mbed IP on PC? I did tried checked the IP from Command Prompt and type ipconfig/all but can't get the Mbed IP, either using ICS (internet connection sharing) method connection with fix IP and direct cable connection to wireless router with DHCP. I wish my second PC able to link up the Mbed, currently only the first PC able to link.

How can i overcome the problem?

Best Regards, Wen

26 May 2011

Wen,

Your code above is just for connecting the ethernet. In order to get an IP address you need an "IP stack" library (or, if you have lots of time on your hands, write your own!) that handles the TCP/IP protocol. All the Ethernet library allows is to get "data off the wire". It has no idea what that data is. Search this site and Google for LWIP.

regards, Andy

26 May 2011

Hello Andy,

Thanks for your reply.

MyEth.set_link(Ethernet:: HalfDuplex10); // configure 10Mb/s half duplex

Ya, i realize when start running application, the ethernet line will down awhile after that up again but after this i will get error. I'm using getweather ( http://mbed.org/users/elliotb/notebook/weather-checking-webpage-scraper/ ) sample code for this testing. It is very good example code.

To check the IP and display it on Hyperterminal is fine. Just wonder why we can't check the IP from PC and currently just one-to-one interface PC with Mbed.

My programming is not very strong but from the sample code i still able to do some modified. At this moment i don't have much knowledge of LAN but still in learning state. To write my own IP stack library it is very difficult for me now. :(

Can you please kindly give some hint? I'm using "Ethernet Testing" sample code to create a small LED button on HTML. The problem that i facing is why if i simple click the button few times with fast then the Mbed will start hang? Is it the PC and Mbed ethernet speed is not match then will cause this problem?

err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {

    struct netif   *netif = &netif_data;
    ledTCP80 = true;
    printf("TCP callback from %d.%d.%d.%d\r\n", ip4_addr1(&(pcb->remote_ip)),ip4_addr2(&(pcb->remote_ip)),ip4_addr3(&(pcb->remote_ip)),ip4_addr4(&(pcb->remote_ip)));
    char *data;
    // Check if status is ok and data is arrived. 
    if (err == ERR_OK && p != NULL) {
        // Inform TCP that we have taken the data. 
        tcp_recved(pcb, p->tot_len);
        data = static_cast<char *>(p->payload);
        // If the data is a GET request we can handle it. 
        if (strncmp(data, "GET ", 4) == 0) {
        printf("Handling GET request...\r\n");
        printf("Request:\r\n%s\r\n", data);
         
         location = strstr(data,"/?"); // start check LED status 
            if (location != NULL) {
                strncpy(LED_status,location+2,7);
                printf("LED_status: %s\r\n", LED_status);
                location = NULL;
            } 
    
        //generate the test page
        time_t seconds = time(NULL);
        sprintf(temp,     
                          "<form method=\"get\" name=\"LED\">"
                          "<form>LED:<input type=\"submit\" name=\"LED\" value=\"ON\"></form>"
                          "<form method=\"get\" name=\"LED\">"   
                          "<form>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" name=\"LED\" value=\"OFF\"></form><br/>",
         ctime(&seconds),
        (char*) netif->hwaddr[0],
        (char*) netif->hwaddr[1],
        (char*) netif->hwaddr[2],
        (char*) netif->hwaddr[3],
        (char*) netif->hwaddr[4],
        (char*) netif->hwaddr[5],
        inet_ntoa(*(struct in_addr*)&(netif->ip_addr))
        );
        sprintf(buffer, testPage, temp);
            if (tcp_write(pcb, (void *)buffer, strlen(buffer), 1) == ERR_OK) {
            tcp_output(pcb);
            printf("Closing connection...\r\n");
            tcp_close(pcb);
            }
        }
        else
        {
            printf("Non GET request...\r\nRequest:\r\n%s\r\n", data);
        }
        
        pbuf_free(p);
    }
    
     else {
            // No data arrived   
            // That means the client closes the connection and sent us a packet with FIN flag set to 1.    
            // We have to cleanup and destroy out TCPConnection.   
             printf("Connection closed by client.\r\n");
            pbuf_free(p);
        }
    // Don't panic! Everything is fine.   
    ledTCP80 = false;
    return ERR_OK;
}
26 May 2011

Your callback is using a lot of printf()s. Have a read of this first and understand what those are doing to your callbacks. If you must use printf then a) use MODSERIAL with a high TX buffer size and b) use at least 115200 baud for your serial port.

If the above makes sense then the penny might drop as to why pressing the button too fast causes a hang.

26 May 2011

Also, I'm having a hard time wrapping my head around this:-

        sprintf(temp,     
                          "<form method=\"get\" name=\"LED\">"
                          "<form>LED:<input type=\"submit\" name=\"LED\" value=\"ON\"></form>"
                          "<form method=\"get\" name=\"LED\">"   
                          "<form>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" name=\"LED\" value=\"OFF\"></form><br/>",
         ctime(&seconds),
        (char*) netif->hwaddr[0],
        (char*) netif->hwaddr[1],
        (char*) netif->hwaddr[2],
        (char*) netif->hwaddr[3],
        (char*) netif->hwaddr[4],
        (char*) netif->hwaddr[5],
        inet_ntoa(*(struct in_addr*)&(netif->ip_addr))
        );
        sprintf(buffer, testPage, temp);

The format string:-

                          "<form method=\"get\" name=\"LED\">"
                          "<form>LED:<input type=\"submit\" name=\"LED\" value=\"ON\"></form>"
                          "<form method=\"get\" name=\"LED\">"   
                          "<form>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" name=\"LED\" value=\"OFF\"></form><br/>",

contains no tokens. So the parameters:-

         ctime(&seconds),
        (char*) netif->hwaddr[0],
        (char*) netif->hwaddr[1],
        (char*) netif->hwaddr[2],
        (char*) netif->hwaddr[3],
        (char*) netif->hwaddr[4],
        (char*) netif->hwaddr[5],
        inet_ntoa(*(struct in_addr*)&(netif->ip_addr))

get passed for no good reason. I would offer you some code to try but I can't see testPage in your snippet. It is a global?

26 May 2011

Hello Andy,

May i know what do you mean contain no tokens?

Following is the whole complete test code. I didn't modified much in the code, just add some coding that you have highlighted.

#include "mbed.h"
#include "lwip/opt.h"
#include "lwip/stats.h"
#include "lwip/sys.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#include "netif/etharp.h"
#include "netif/loopif.h"
#include "device.h"

Ethernet ethernet;
DigitalOut ledLink(p30);
DigitalOut ledActivity(p29);
DigitalOut ledStage0 (LED1);
DigitalOut ledStage1 (LED2);
DigitalOut ledStage2 (LED3);
DigitalOut ledTCP80 (LED4);

volatile char stage = 0;

Ticker stage_blinker;

struct netif    netif_data;

const char testPage[] = "HTTP/1.1 200 OK\r\n"
                        "Content-Type: text/html\r\n"
                        "Connection: Close\r\n\r\n"
                        "<html>"
                        "<head>"
                        "<title>mbed test page</title>"
                        "<style type='text/css'>"
                        "body{font-family:'Arial, sans-serif', sans-serif;font-size:.8em;background-color:#fff;}"
                        "</style>"
                        "</head>"
                        "<body>%s</body></html>\r\n\r\n";

char buffer[1024];
char temp[1024];

char * location;
char LED_status[7];


err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
    struct netif   *netif = &netif_data;
    ledTCP80 = true;
    printf("TCP callback from %d.%d.%d.%d\r\n", ip4_addr1(&(pcb->remote_ip)),ip4_addr2(&(pcb->remote_ip)),ip4_addr3(&(pcb->remote_ip)),ip4_addr4(&(pcb->remote_ip)));
    char *data;
    /* Check if status is ok and data is arrived. */
    if (err == ERR_OK && p != NULL) {
        /* Inform TCP that we have taken the data. */
        tcp_recved(pcb, p->tot_len);
        data = static_cast<char *>(p->payload);
        /* If the data is a GET request we can handle it. */
        if (strncmp(data, "GET ", 4) == 0) {
        printf("Handling GET request...\r\n");
        printf("Request:\r\n%s\r\n", data);
        
        location = strstr(data,"/?");
        if (location != NULL) {
           strncpy(LED_status,location+2,7);
           printf("LED_status: %s\r\n", LED_status);
           location = NULL;
        }
            
            
        //generate the test page
        time_t seconds = time(NULL);
        sprintf(temp,     "<form method=\"get\" name=\"LED\">"
                          "<form>LED:<input type=\"submit\" name=\"LED\" value=\"ON\">"
                          "<input type=\"submit\" name=\"LED\" value=\"OFF\"></form><br/>",
         ctime(&seconds),
        (char*) netif->hwaddr[0],
        (char*) netif->hwaddr[1],
        (char*) netif->hwaddr[2],
        (char*) netif->hwaddr[3],
        (char*) netif->hwaddr[4],
        (char*) netif->hwaddr[5],
        inet_ntoa(*(struct in_addr*)&(netif->ip_addr))
        );
        sprintf(buffer, testPage, temp);
            if (tcp_write(pcb, (void *)buffer, strlen(buffer), 1) == ERR_OK) {
            tcp_output(pcb);
            printf("Closing connection...\r\n");
            tcp_close(pcb);
            }
        }
        else
        {
            printf("Non GET request...\r\nRequest:\r\n%s\r\n", data);
        }
        
        pbuf_free(p);
    }
    
     else {
            /* No data arrived */
            /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */
            /* We have to cleanup and destroy out TCPConnection. */
             printf("Connection closed by client.\r\n");
            pbuf_free(p);
        }
    /* Don't panic! Everything is fine. */
    ledTCP80 = false;
    return ERR_OK;
}
/* Accept an incomming call on the registered port */
err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) {
    LWIP_UNUSED_ARG(arg);
    /* Subscribe a receive callback function */
    tcp_recv(npcb, &recv_callback);
    /* Don't panic! Everything is fine. */
    return ERR_OK;
}

void stageblinker()
{
    switch (stage)
    {
        case 0:
        ledStage0 = !ledStage0;
        ledStage1 = false;
        ledStage2 = false;
        break;
        case 1:
        ledStage0 = true;
        ledStage1 = !ledStage1;
        ledStage2 = false;
        break;
        case 2:
        ledStage0 = true;
        ledStage1 = true;
        ledStage2 = true;
        stage_blinker.detach();
        break;
    }
}

int main() {
    printf("mBed Ethernet Tester 1.0\r\nStarting Up...\r\n");
    stage = 0;
    struct netif   *netif = &netif_data;
    struct ip_addr  ipaddr;
    struct ip_addr  netmask;
    struct ip_addr  gateway;
    Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine;
    stage_blinker.attach_us(&stageblinker, 1000*500);
    
    char *hostname = "my-mbed";
    
    printf("Configuring device for DHCP...\r\n");
    /* Start Network with DHCP */
    IP4_ADDR(&netmask, 255,255,255,255);
    IP4_ADDR(&gateway, 0,0,0,0);
    IP4_ADDR(&ipaddr, 0,0,0,0);
    /* Initialise after configuration */
    lwip_init();
    netif->hwaddr_len = ETHARP_HWADDR_LEN;
    device_address((char *)netif->hwaddr);
    netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input);
    netif->hostname = hostname;
    netif_set_default(netif);
    dhcp_start(netif); // <-- Use DHCP
    
        /* Initialise all needed timers */
    tickARP.attach_us( &etharp_tmr,  ARP_TMR_INTERVAL  * 1000);
    tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000);
    tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000);
    dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000);
    dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000);
    dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000);
    stage = 1;
         while (!netif_is_up(netif)) { 
         ledLink = ethernet.link();
         device_poll(); 
     } 

/*
    while (!(netif->dhcp->state == DHCP_BOUND || netif->dhcp->state == DHCP_PERMANENT))
    {
        ledLink = ethernet.link();
        device_poll();    
        //printf("Waiting for DHCP response, state = %d\r\n", netif->dhcp->state);
        //wait_ms(100);
        }
    */
    stage = 2;
            printf("Interface is up, local IP is %s\r\n", 
inet_ntoa(*(struct in_addr*)&(netif->ip_addr))); 

    printf("Starting Web Server...\r\n");

        /* Bind a function to a tcp port */
    struct tcp_pcb *pcb = tcp_new();
    if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) {
        pcb = tcp_listen(pcb);
        tcp_accept(pcb, &accept_callback);
    }
    
    printf("Waiting for connection...\r\n");
    while(1) {
        device_poll();
        ledLink = ethernet.link();    
    }
}

Quote:

I can't see testPage in your snippet. It is a global?

To test the program. First we have to open the browser and type in the Mbed IP. After that you will see this button /media/uploads/bc/button.jpg on html. When everything we click the button the hyperterminal will show the output message "LED_status: LED=ON" or "LED_status: LED=OFF". Please correct me if i have mis-understand your question.

Best Regards, Wen

26 May 2011

By token I mean:-

    char s[] = "Hello world";
    printf("The string is: %s\n", s);

In the above, %s is the token. printf takes the first parameter s (in this case a string) and embeds it at the first token, %s

Output...

The string is: Hello world

Look again at what I posted. There are no tokens in teh format string so the long list of passed parameters do nothing for the printf() function. You just spend longer building the call to printf by places all those parameters on the stack which aren't used.

However, the global var const char testPage does have a token in it, a %s

Since your temp can actually const also and put directly into testPage and you can avoid having to use the sprintf entirely. Only if you needed to modify the text in temp would you need to use what you have. Look at this for example:-

const char testPage[] = "HTTP/1.1 200 OK\r\n"
                        "Content-Type: text/html\r\n"
                        "Connection: Close\r\n\r\n"
                        "<html>"
                        "<head>"
                        "<title>mbed test page</title>"
                        "<style type='text/css'>"
                        "body{font-family:'Arial, sans-serif', sans-serif;font-size:.8em;background-color:#fff;}"
                        "</style>"
                        "</head>"
                        "<body>"
                            "<form method=\"get\" name=\"LED\">"
                            "<form>LED:<input type=\"submit\" name=\"LED\" value=\"ON\">"
                            "<input type=\"submit\" name=\"LED\" value=\"OFF\"></form><br/>"
                        "</body></html>\r\n\r\n";

Notice how, because temp is actually fixed you might aswell just embed it directly into testPage (show indented above). Now you can get rid of the sprintf() and temp entirely:-

err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
    struct netif   *netif = &netif_data;
    ledTCP80 = true;
    printf("TCP callback from %d.%d.%d.%d\r\n", ip4_addr1(&(pcb->remote_ip)),ip4_addr2(&(pcb->remote_ip)),ip4_addr3(&(pcb->remote_ip)),ip4_addr4(&(pcb->remote_ip)));
    char *data;
    /* Check if status is ok and data is arrived. */
    if (err == ERR_OK && p != NULL) {
        /* Inform TCP that we have taken the data. */
        tcp_recved(pcb, p->tot_len);
        data = static_cast<char *>(p->payload);
        /* If the data is a GET request we can handle it. */
        if (strncmp(data, "GET ", 4) == 0) {
            printf("Handling GET request...\r\n");
            printf("Request:\r\n%s\r\n", data);
        
            location = strstr(data,"/?");
            if (location != NULL) {
               strncpy(LED_status,location+2,7);
               printf("LED_status: %s\r\n", LED_status);
               location = NULL;
            }
            
            
            //generate the test page
            // No need for the following anymore:-
            //time_t seconds = time(NULL);
            //sprintf(temp,     "<form method=\"get\" name=\"LED\">"
            //                 "<form>LED:<input type=\"submit\" name=\"LED\" value=\"ON\">"
            //                  "<input type=\"submit\" name=\"LED\" value=\"OFF\"></form><br/>",
            // ctime(&seconds),
            //(char*) netif->hwaddr[0],
            //(char*) netif->hwaddr[1],
            //(char*) netif->hwaddr[2],
            //(char*) netif->hwaddr[3],
            //(char*) netif->hwaddr[4],
            //(char*) netif->hwaddr[5],
            //inet_ntoa(*(struct in_addr*)&(netif->ip_addr))
            //);
            //sprintf(buffer, testPage, temp);
            //if (tcp_write(pcb, (void *)buffer, strlen(buffer), 1) == ERR_OK) {
            //                         vvvvvvvv <-- just use testPage rather than buffer because it's a const char now.
            if (tcp_write(pcb, (void *)testPage, sizeof(testPage), 1) == ERR_OK) {
                tcp_output(pcb);
                printf("Closing connection...\r\n");
                tcp_close(pcb);
            }
        }
        else
        {
            printf("Non GET request...\r\nRequest:\r\n%s\r\n", data);
        }
        
        pbuf_free(p);
    }
    
     else {
            /* No data arrived */
            /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */
            /* We have to cleanup and destroy out TCPConnection. */
             printf("Connection closed by client.\r\n");
            pbuf_free(p);
        }
    /* Don't panic! Everything is fine. */
    ledTCP80 = false;
    return ERR_OK;
}

27 May 2011

Hello Andy.

Very good morning.

Ya, your explanation is very good and in detail. Now i understand, thanks.

Combining your hint ( using a lot of printf()s ) and improved the coding then i tried to comment out all the printf() in recv_callback just leave

printf("LED_status: %s\r\n", LED_status);

So, compile and run the application. I realize the button only can click 27 time's with slow click after this the Mbed will start hang. Is it have some limitation there?

P/s: You have provide very good tutorial for MODSERIAL. I will look into this feature after this. I learn a lot of thing and appreciated the (tutorial, sample code, forum, manufacturer, and so on..) support, before using Mbed that just was a dream! Those support is very impotent for Newbie (as me). Thanks a lot.

Best Regards, Wen