chipkit mx7ck calling "hello world" from webserver

aabfm
Posts: 31
Joined: Mon Aug 14, 2017 4:28 pm
Location: UK

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Thu Aug 24, 2017 3:40 pm

Hi everyone,
Here is another update.

Back on my Windows machine, where I also installed UECIDE, I have now a running call (with intervals of 5 seconds) to the webserver.
Enjoy the code (this time much tidier ;) ).

Code: Select all

#include <IM8720PHY.h>
#include <DEIPcK.h>

int use_dhcp=0;
int debug01=1;
int debug02=0;
int phy_con=0;

IPv4 ip = {193,0,0,171};
IPv4 subnet = {255,255,255,0};
IPv4 gatew = {193,0,0,3};

const char * szIPServer = "216.58.206.100"; // Google
unsigned short portServer = 80; 

typedef enum
{
    NONE = 0,
    CONNECT,
    TCPCONNECT,
    WRITE,
    READ,
    CLOSE,
    DONE,
} STATE;

STATE state;
IPSTATUS status;

byte rgbRead[1024];
int cbRead = 0;

unsigned tStart = 0;
unsigned tWait = 3000;

TCPSocket tcpClient;

void setup()
{
		int con;
		  
		Serial.begin(38400);
    if (debug01)
    {
        Serial.println("WebClient 1.0 @ AM Copyright 2017");
        Serial.println("");
    }

    // Initialize PHY
		if (use_dhcp==1)
		{
		    // use DHCP
		    con=deIPcK.begin();
		}
		else
		{
		    // use fix IP
		    con=deIPcK.begin(ip,gatew,subnet);
		}

    if(con)
    {
        if (debug01) {Serial.println("PHY Connected");}
    }
    else if(IsIPStatusAnError(status))
    {
    		if (debug01)
    		{
        		Serial.print("Unable to connect, status: ");
        		Serial.println(status, DEC);
    		}
    }
}

void loop()
{
		String message = "GET / HTTP/1.1\nHost: www.google.com\nConnection close:\n\n";
    byte rgbWriteStream[message.length()];
    message.getBytes(rgbWriteStream, message.length() + 1);
    int cbWriteStream = sizeof(rgbWriteStream);
    int result;

		state = TCPCONNECT;
    result=call_server(rgbWriteStream, cbWriteStream);

    Serial.print("Size out:");
    Serial.println(result, DEC);
    Serial.println("Wait 5s...");
    Serial.print("");
    delay(5000);
}

int call_server(byte rgbWriteStream[], int cbWriteStream)
{
		int not_finished=1;
		int sent;
		
		while (not_finished)
		{
				switch(state)
	  		{
	  				case TCPCONNECT:
		            if(tcpClient.isEstablished())
		            {
				            state = WRITE;
		            }
		            else
		            {
		            		// connect to webserver szIPServer on port portServer and return result into tcpClient socket
				            if(deIPcK.tcpConnect(szIPServer, portServer, tcpClient))
				            {
				                state = WRITE;
				            }
		            }
	  						break;
	  				case WRITE:
	  						// send command to the server
		            if(tcpClient.isEstablished())
		            {
		                tcpClient.writeStream(rgbWriteStream, cbWriteStream);
		                state = READ;
		                tStart = (unsigned) millis();
		            }
	  						break;
	  				case READ:
	  						// receiving results from server
								if((cbRead = tcpClient.available()) > 0)
		            {
		                cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
		                cbRead = tcpClient.readStream(rgbRead, cbRead);
		                // print results DEBUG ONLY
		                for(int i=0; i < cbRead; i++)
		                {
		                    Serial.print((char)rgbRead[i]);
		                }
		                sent=cbRead;
		            }
		            else if( (((unsigned) millis()) - tStart) > tWait )
		            {
		                state = CLOSE;
		            }
	  						break;
	  				case CLOSE:
	  						// closing socket
	  						tcpClient.close();
	  						state = DONE;
	  						break;
	  				case DONE:
	  						not_finished=0;
	  				default:
	  						break;
	  		}
	  		DEIPcK::periodicTasks();
		}
		return sent;
}
I am now proceeding to the post parameters.
Until my next post (possibly on a new topic) enjoy this easy-to-understand (I think) piece of code.
Thanks.

aabfm
Posts: 31
Joined: Mon Aug 14, 2017 4:28 pm
Location: UK

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Thu Aug 24, 2017 10:22 pm

Hi,
Here comes the first question: how do I use a domain such as http://www.mydomain.com/script.php instead of the server IP address?
I suppose that this is an item related to the DNS servers, but how do I configure them? and how/where do I use it?
Thanks in advance.

PS: BTW, after the previous posts the mx7ck is now calling different websites (which respond in xml) to retrieve different information based on which button I am pressing (ex: BTN1 -> currency exchange, BTN2 -> local weather and BTN3 -> current unread items on my mailbox) and the info is being displayed on a 40x4 LCD, nice! ;) 8-)

Post Reply