chipkit mx7ck calling "hello world" from webserver

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

chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Tue Aug 22, 2017 3:52 pm

Hi,
My target is to have the mx7ck calling a php script with post arguments and read the answer from the webserver, i.e. I want to build a webclient that sends post parameters to a php script.
So far I've created the a very basic program which is supposed to access Google and print via serial monitor whatever it gets but it just doesn't return anything.
Here is the code based on a few examples:

Code: Select all

#include <IM8720PHY.h>                      // This is for the the Internal MAC and SMSC 8720 PHY
#include <DEIPcK.h>

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

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

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

STATE state = CONNECT;
IPSTATUS    status;

unsigned tStart = 0;
unsigned tWait = 5000;

TCPSocket tcpClient;
byte rgbRead[1024];
int cbRead = 0;

void setup() {
  
    Serial.begin(9600);
    Serial.println("TCPEchoClient 3.0");
    Serial.println("Digilent, Copyright 2014");
    Serial.println("");

    deIPcK.begin(ip,gatew,subnet);
}

void loop() {
    int cbRead = 0;
    int contry = 1;

    switch(state)
    {
        case CONNECT:
            if(deIPcK.tcpConnect(server, portServer, tcpClient, &status))
            {
                state = WRITE;
            }
            break;

       // write out the strings  
       case WRITE:
            if(tcpClient.isEstablished())
                {     
                state = READ;
                tStart = (unsigned) millis();
                }
            break;

        case READ:

            // see if we got anything to read
            if((cbRead = tcpClient.available()) > 0)
            {
                cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
                cbRead = tcpClient.readStream(rgbRead, 12);
                 
                for(int i=0; i < cbRead; i++) 
                {
                    Serial.print((char) rgbRead[i]);
                }
            }

            // give us some time to get everything echo'ed back
            else if( (((unsigned) millis()) - tStart) > tWait )
            {
                Serial.println("Closing!");
                Serial.println("");
                state = CLOSE;
            }

            break;

        // done, so close up the tcpClient
        case CLOSE:
            tcpClient.close();
            Serial.println("Closing TcpClient, Done with sketch.");
            state = DONE;
            break;

        case DONE:
        default:
            break;
    }

    // keep the stack alive each pass through the loop()
    DEIPcK::periodicTasks();
}
and here is the result on the serial monitor:
TCPEchoClient 3.0
Digilent, Copyright 2014

Got Connection
Bytes Read Back:
No bytes read back!
...
No bytes read back!
Closing!

Closing TcpClient, Done with sketch.
Any ideas on how to sort out this?
BTW I have my own webapp already with the php "hello world", how can I use the local DNS to retrieve the server's IP?
Note: It has been a nightmare just to have some IDE working with this board. Right now I have MPIDE0150.
Thanks in advance.

lstandage
Posts: 17
Joined: Fri Sep 06, 2013 6:03 pm

Re: chipkit mx7ck calling "hello world" from webserver

Post by lstandage » Tue Aug 22, 2017 6:53 pm

MPIDE is definitely not the recommended IDE. You can switch to the Arduino IDE and install the chipKIT-core, which will give you everything you need.

You can also use MPLAB X, and our new plugin, chipKIT Sketch Importer, which will allow you to do debugging (as long as you have a debugger, like the chipKIT PGM).

I notice that you establish the connection with the server, but you don't send anything to the server. I think you have to send something to the server, via tcpClient.writeStream, before you will get anything back.

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

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Tue Aug 22, 2017 9:07 pm

Any suggestion on what to send to the server?
Unfortunately the only, and closest, example I could find is the TCPEchoClient where it shows a command inside the WRITE state:

Code: Select all

byte rgbWriteStream[] = {'*','W','r','o','t','e',' ','f','r','o','m',' ','t','c','p','C','l','i','e','n','t','.','w','r','i','t','e','S','t','r','e','a','m','*','\n'};
int cbWriteStream = sizeof(rgbWriteStream);
tcpClient.writeStream(rgbWriteStream, cbWriteStream);
Is it me or everyone is looking basically for webservers? Example after example there is only a few examples about clients, and, after a some days looking for it, I could only find a single example of a webclient that sends post parameters... :( but it doesn't work... (sorry for the steam...)
Thx

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: chipkit mx7ck calling "hello world" from webserver

Post by majenko » Tue Aug 22, 2017 9:22 pm

You won't get anything back from a web server without sending it a request for a web page first.

For instance, for Google, send it:

Code: Select all

GET / HTTP/1.1
Host: www.google.com
Connection close:
[extra blank line]
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

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

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Tue Aug 22, 2017 9:33 pm

I'll give it a try, thanks for your prompt answer.
In the meantime, and sorry for my ignorance, if I were to send a couple of post parameters such as $_POST['name'] and $_POST['age'] to a script, for instance abcde.php and wanted to read the result from it such as the associated name stored in the database here are a couple of questions:
- how would I send it?
- what would be the best way to read it? xml result? json result? other?...
Thx

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: chipkit mx7ck calling "hello world" from webserver

Post by majenko » Tue Aug 22, 2017 9:42 pm

The content would look like this:

Code: Select all

POST /path/to/page.php HTTP/1.1
Host: www.mywebsite.com
Connection: close
Content-Length: 21
Content-Type: application/x-www-form-urlencoded

name=Bob+Smith&age=34
Note that the "Content-Length" must match what you put in the "body" (everything that follows the blank line).

The response can be in whatever form you find easiest to parse. The simpler the better.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: chipkit mx7ck calling "hello world" from webserver

Post by majenko » Tue Aug 22, 2017 9:43 pm

Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

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

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Tue Aug 22, 2017 9:56 pm

Thanks a lot. I'll give a try first thing in the morning.

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

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Wed Aug 23, 2017 5:19 pm

GREAT NEWS!!!! IT IS ALIVE AND KICKING!!!!
(sorry for the enthusiasm)

In order to avoid doing what I was complaining about here is the COMPLETE CODE to use the mx7ck board as a webclient.
IDE: MPIDE0150
Board: CEREBOT MX7ck (DO NOT FORGET THE JUMPER ON JP11 AND USE USB UART PORT AND OF COURSE CHECK THE JUMPER JP3 FOR PSU SETTINGS)
OS: Windows 7 64 (I'll give it a try later on with my Linux Mint machine)

And finally a big thanks to these friends: majenko and lstandage for guidance, ideas and examples.

Note: There are loads of "Serial.println"s and "Serial.print"s that were just for my debugging.

Code: Select all

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

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 = CONNECT;
IPSTATUS    status;

unsigned tStart = 0;
unsigned tWait = 5000;

TCPSocket tcpClient;
byte rgbRead[1024];
int cbRead = 0;

void setup() {
    Serial.begin(38400);
    Serial.println("TCPEchoClient 3.0");
    Serial.println("Digilent, Copyright 2014");
    Serial.println("");
}

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);
    Serial.print("Data prepared to send:");
    Serial.println(cbWriteStream, DEC);
    
    if(boolean bi = state_machine(rgbWriteStream, cbWriteStream))
    {
      Serial.println("Posted");
    }
    else
    {
      Serial.println("NOT Posted");
    }
}
  
boolean state_machine(byte rgbWriteStream[], int cbWriteStream)
{

  byte rgbRead[1024];
  unsigned tStart = 0;
  unsigned tWait = 3000;
  int flag=0;
  for(;;)
  {
  int cbRead = 0;
  IPSTATUS status;
  switch(state)
    {

        case CONNECT:
            if(deIPcK.begin(ip,gatew,subnet))
            {
                Serial.println("Connected");
                state = TCPCONNECT;
            }
            else if(IsIPStatusAnError(status))
            {
                Serial.print("Unable to connection, status: ");
                Serial.println(status, DEC);
                state = CLOSE;
            }
            break;

        case TCPCONNECT:
            if(deIPcK.tcpConnect(szIPServer, portServer, tcpClient))
            {
                Serial.println("Connected to server.");
                Serial.print("Data to send:");
                Serial.println(cbWriteStream, DEC);
                state = WRITE;
            }
        break;

        case WRITE:
            if(tcpClient.isEstablished())
                {
                tcpClient.writeStream(rgbWriteStream, cbWriteStream);
                Serial.println("Bytes Read Back:");
                state = READ;
                tStart = (unsigned) millis();
                }
            break;

            case READ:

                if((cbRead = tcpClient.available()) > 0)
                {
                    Serial.println("TCP Available");
                    cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
                    Serial.print("Size 1:");
                    Serial.println(cbRead, DEC);
                    cbRead = tcpClient.readStream(rgbRead, cbRead);

                    for(int i=0; i < cbRead; i++)
                    {
                        Serial.print((char)rgbRead[i]);
                    }
                    Serial.println(" -- END --");
                    Serial.print("Size 2:");
                    Serial.println(cbRead, DEC);
                }

                else if( (((unsigned) millis()) - tStart) > tWait )
                {
                    Serial.println("Next step: Closing");
                    state = CLOSE;
                }
                break;

        case CLOSE:
            tcpClient.close();
            Serial.println("Closing TcpClient!");
            state = DONE;
            break;

        case DONE:
            state = CONNECT;
            return cbRead;
        default:
            break;

    }
    DEIPcK::periodicTasks();
  }
}
Here is the result from the serial monitor:
TCPEchoClient 3.0
Digilent, Copyright 2014

Data prepared to send:55
Connected
Connected to server.
Data to send:55
Bytes Read Back:
TCP Available
Size 1:503
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Location: http://www.google.co.uk/?gfe_rd=cr&ei=V ... 7HXqGPlqAI
Content-Length: 259
Date: Wed, 23 Aug 2017 16:16:20 GMT

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/?gfe_rd=cr&ei=V ... I">here</A>.
</BODY></HTML>
-- END --
Size 2:503
Next step: Closing
Closing TcpClient!
NOT Posted
Data prepared to send:55
I still need to check why I'm having a "NOT Posted" message at end as it WAS really posted, but I'll have a look at it later.
Cheers

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

Re: chipkit mx7ck calling "hello world" from webserver

Post by aabfm » Wed Aug 23, 2017 9:53 pm

It also worked on my Linux Mint machine.
I've also tried the UECIDE - much better! And easier to control! (I have used chipKit Pro MX7 board which apparently is the one succeeding the mx7ck)

I'm now trying to have the code checking the website every 5 seconds but no luck... Is there anything that should be done? the code is not too different from the one previously posted.
Any ideas?
Thx

Post Reply