4 years, 8 months ago.

TCPIP Issue

I tried to use the EthernetInterface for a Internet Button application. The button pressed event is sent for the first time to IFTTT. After pressing the button a second time I get blinking LEDs for LED1 to LED4 but no further reaction. Is there any thing wrong with my program logic? I have the IFTTT Key replaced in the source.

#include "mbed.h"
#include "EthernetInterface.h"
#include "IAP.h"

Serial pc(USBTX, USBRX);

DigitalIn btn(P2_13, PullUp); // to press btn connect this pin to GND

int main() 
{
    int ret;
    
    pc.baud(115200);
    pc.printf("-------- InternetButton -----------\n");
    pc.printf("Wait for next btn pressed...\n");
    while(true)
    { 
        if (!btn) 
        {
            pc.printf("Btn pressed\n");
    
            EthernetInterface eth;
            eth.init(); //Use DHCP
            eth.connect();
            pc.printf("IP Address is %s\n", eth.getIPAddress());
    
            TCPSocketConnection sock;
            ret = sock.connect("maker.ifttt.com", 80);
            if (ret == 0) pc.printf("Connected to IFTTT\n");
    
            char http_cmd[] = "POST /trigger/btn_pressed/with/key/IFTTT_KEY?value1=Btn_pressed HTTP/1.1\r\nHost: maker.ifttt.com\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.)\r\n\r\n";
            sock.send_all(http_cmd, sizeof(http_cmd)-1);
    
            char buffer[300];
    
            while (true) 
            {
                ret = sock.receive(buffer, sizeof(buffer)-1);
                if (ret <= 0) break;
                buffer[ret] = '\0';
                pc.printf("Received %d chars from server:\n%s\n", ret, buffer);         
            }
            
            sock.close();
    
            ret = eth.disconnect();
            if (ret == 0) pc.printf("Disconnected.\n");
    
            wait_ms(1000);
            pc.printf("Wait for next btn pressed...\n");      
        }
    }  
}

extern "C" void mbed_mac_address(char *mac) {
    IAP iap;
    
    int *serial_number_array = iap.read_serial();   // 4 int
    char *ptr = (char *)serial_number_array;
    
    mac[0] = ptr[0];
    mac[1] = ptr[1];
    mac[2] = ptr[2];
    mac[3] = ptr[3];
    mac[4] = ptr[4];
    mac[5] = ptr[5];
};

Question relating to:

Arch Pro, an mbed enabled development board for rapid prototyping.
Be the first to answer this question.