This is an experimental implementation of HTTP/1.1 client for OMRON's NX/NJ controllers. This HTTP client lacks functionality and is not of sufficient quality. However, it shows that it is possible to implement a practical HTTP client with a user program using Sysmac Studio and the NX/NJ controller. The Sysmac project contains several test programs that actually query Beeceptor's HTTP echo server. For details, please check this article (Japanese).
The HTTP client has the following restrictions.
- Can't use proxy
- Invalid responses can cause tasks to time out
Inquiries using HTTP(S) can be written as follows.
// POST /chat/api/v1 ContentType: application/json
20:
IF NewHttpClientTask(
Context:=iHttpContext,
ClientTask:=iHttpClientTask)
THEN
HttpPost(Context:=iHttpContext,
Url:='https://echo.free.beeceptor.com/chat/api/v1',
KeepAlive:=FALSE,
ClientTask:=iHttpClientTask);
SetHttpHeader(Context:=iHttpContext,
Key:='X-Api-Key',
Value:='xxxxxx');
SetContentStr(
Context:=iHttpContext,
ContentType:='application/json',
Content:='{"channel":"hell","name":"taker","msg":"Have you played Awaria?"}');
iPostTick := 0;
InvokeHttpClientTask(Context:=iHttpContext,
ClientTask:=iHttpClientTask);
Inc(iState);
END_IF;
21:
CASE HttpClientTaskState(Context:=iHttpContext,
ClientTask:=iHttpClientTask) OF
HTTP_CLIENT_TASK_STATE_CLOSED:
iState := iTransState;
HTTP_CLIENT_TASK_STATE_RESPOND:
CASE GetStatusCode(Context:=iHttpContext) OF
200: // OK
iPostRspContentType
:= GetContentType(Context:=iHttpContext);
GetContent(Context:=iHttpContext,
Out:=iRspBinBody,
Head:=0,
Size=>iRspBinBodySize);
iPostRspContent
:= AryToString(In:=iRspBinBody[0],
Size:=iRspBinBodySize);
iState := iTransState;
204: // No Content
iState := iTransState;
ELSE
Inc(iState);
END_CASE;
HTTP_CLIENT_TASK_STATE_REQUESTING:
Inc(iPostTick);
HTTP_CLIENT_TASK_STATE_ERROR:
GetHttpClientError(Context:=iHttpContext,
Error=>iError,
ErrorID=>iErrorID,
ErrorIDEx=>iErrorIDEx);
iState := iState + 7;
END_CASE;
The following environment is required to use this project
Controller | NX1 or NX5 |
Sysmac Studio | I always recommend the latest version. |
Network | Must be able to connect to the Internet. |
This project was built in the following environment.
Controller | NX102-9000 Ver 1.64 |
Sysmac Studio | Ver.1.61 |
The Sysmac project includes a test program (Test_HttpEcho) that queries Beeceptor's HTTP echo server. There may be some restrictions when accessing from a network that is not under your control. If possible, use a test network to ensure there are no constraints.
Set the controller model of this project to match the controller model to be used, and to enable Internet connection and name resolution. At least the gateway and DNS server settings are required.
Set the controller to program mode to prevent unintended operation and to configure secure sockets.
Keep the controller in program mode to prevent the program from running.
Register a TLS session with zero ID and no client certificate. Do the following while online to the controller.
Paste the following into Watch window.
LIB Test_HttpEcho.iGetRspContent
LIB Test_HttpEcho.iPostRspContent
LIB Test_HttpEcho.iPostFormRspContent
LIB Test_HttpEcho.iPutRspContent
LIB Test_HttpEcho.iPatchRspContent
LIB Test_HttpEcho.iDeleteRspContent
The moment you switch to driving mode, it starts making requests to the server.
If there is a response from the server, the HTTP response will be displayed as shown below.
If an error occurs, there are the following possibilities:
- Incorrect secure socket settings
Check if the ID is zero. - HTTP echo server is down
Ping the HTTP echo server from your device and check the response. - Not connected to the Internet or unable to resolve name
Check the route to the HTTP echo server with tracert.