Introduction: How to Use WiFi Module in Arduino IDE on PcDuino
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.
Step 1: Part List
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
Step 2: Theory
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.
Step 3: Sample Code for Arduino IDE
* 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
int led_pin = 2;
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
led_pin = 2;
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));
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
// 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(sendBuff=='O') digitalWrite(led_pin, HIGH); // set the LED on
if(sendBuff=='F') digitalWrite(led_pin,LOW); // set the LED off
Step 4: Result
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:
We have a be nice policy.
Please be positive and constructive.