[vc_row][vc_column][vc_column_text]pcDuino v2 has a built-in WiFi module that provides Internet connection. Users are asking how to use the WiFi module in the Arduino style IDE on pcDuino? Is it same as WiFi shield for Arduino?
pcDuino v2 is a single board linux computer itself. The network function is also provided. We don’t need to go through the trouble as we did on Arduino Uno. In this tutorial, we show a project to remotely control a LED through TCP/IP using Arduino style code.[/vc_column_text][vc_tour][vc_tab title=”Parts List” tab_id=”1396166707-1-25″][vc_column_text]
- 1 x pcDuino v2 (or 1 x pcDuino v1 and 1 x WiFi USB Dongle)
- 1 x T board for pcDuino v1 (or T board for pcDuino v2 if pcDuino v2 is used)
- 1 x 10mm Green LED module
- 1 x Linker Base Shield
- 1 x Linker 20cm cable
[/vc_column_text][/vc_tab][vc_tab title=”Wire Diagram” tab_id=”1396166707-2-50″][vc_column_text]Connect the Linker LED module to [D2 D3 V G] on Linker base shield.
The whole setup is shown below:
[/vc_column_text][/vc_tab][vc_tab title=”Theory” tab_id=”1396167805529-2-0″][vc_column_text]In this project, on the pcDuino side, we will implement a TCP/IP socket server, which will listen and accept connection from client, when it receives character ’O’, it will turn on the LED, and when it receives ‘F”, it will turn on the LED. No actions if receive something else.
We install a TCP/IP Client on another PC, and use it to send data to the server which is implemented on pcDuino.[/vc_column_text][/vc_tab][vc_tab title=”Sample Code for Arduino IDE” tab_id=”1396167882040-3-5″][vc_column_text]
/* * LED test program * The LED will be controlled by TCP socket * This program serves an example of TCP socket server * *The TCP socket code is adpoted from : http://www.thegeekstuff.com/2011/12/c-socket-programming/ * * Please use linker kit LED module,and install it on D2 D3 V G postion * * Jingfeng Liu * * 5/4/2013 * */ #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <time.h> #include <core.h> int led_pin = 2; int listenfd = 0, connfd = 0; int n; struct sockaddr_in serv_addr; char sendBuff[1025]; time_t ticks; void setup() { led_pin = 2; pinMode(led_pin, OUTPUT); listenfd = socket(AF_INET, SOCK_STREAM, 0); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(5000); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(listenfd, 10); connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); } void loop() { // ticks = time(NULL); // snprintf(sendBuff, sizeof(sendBuff), "Jingfeng Liu%.24s\r\n", ctime(&ticks)); // write(connfd, sendBuff, strlen(sendBuff)); n = read(connfd, sendBuff, strlen(sendBuff) ); if(n>0) { if(sendBuff[0]=='O') digitalWrite(led_pin, HIGH); // set the LED on if(sendBuff[0]=='F') digitalWrite(led_pin,LOW); // set the LED off } // close(connfd); // sleep(1); }
[/vc_column_text][/vc_tab][vc_tab title=”Results” tab_id=”1396167947742-4-2″][vc_column_text]When we fun the code on pcDuino, on another PC, we open the TCP/IP tool to send data to the server which is implemented on pcDuino. The IP address of pcDuino can be found by typing “$ifconfig” on pcDuino.
A screen shot of TCP client is shown below:
Now we can use this TCP client to send characters to turn on/off the LED.[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.