Many users are wondering if TCP/IP sockets programming can be used in the Arduio-ish C-enviroment. The answer is positive. Compared to Arduino, there is NO need to additional WiFi Shield or Ethernet shield.
In this tutorial, we are going to show how to use pcDuino to control a LED through socket communication.
The LED used is 10mm Green LED module. The LED module is installed on Linker base shield, which is installed on CuteDigi’s pcDuino T-board, which maps pcDuino headers to standard Arduino headers. A picture of the hardware setup is shown below:
In this experiment, 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 off the LED. No actions if receive something else.
We need to get pcDuino API first from: http://www.github.com/pcduino/c_enviroment.
The source code is:
/* * 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); }
The source file tcpip_led_test can be downloaded into directory c_enviroment/sample.
We also need to modify the Makefile under c_enviroment/sample to include the new source file:
DIR=$(shell pwd)/../ INCS = -I.\ -I$(DIR) \ -I$(DIR)/hardware \ -I$(DIR)/hardware/arduino \ -I$(DIR)/hardware/arduino/cores \ -I$(DIR)/hardware/arduino/cores/arduino \ -I$(DIR)/hardware/arduino/variants \ -I$(DIR)/hardware/arduino/variants/sunxi \ -I$(DIR)/libraries \ -I$(DIR)/libraries/Serial \ -I$(DIR)/libraries/SPI \ -I$(DIR)/libraries/Wire \ -I$(DIR)/libraries/LiquidCrystal \ -I$(DIR)/libraries/PN532_SPI LIBS=../libarduino.a TARGET=../output/test OBJS = <strong>tcpip_led_test</strong> io_test adc_test pwm_test spi_test adxl345_test serial_te st liquidcrystal_i2c liquidcrystal_spi interrupt_test tone_test OBJS += linker_led_test linker_potentiometer_test linker_tilt_test linker_light_ sensor_test linker_button_test OBJS += linker_touch_sensor_test linker_magnetic_sensor_test linker_temperature_ sensor_test linker_joystick_test OBJS += linker_rtc_test linker_sound_sensor_test linker_buzzer_test linker_hall_ sensor_test linker_led_bar_test linker_relay_test OBJS += pn532_readAllMemoryBlocks pn532readMifareMemory pn532readMifareTargetID pn532writeMifareMemory all: @mkdir -p $(TARGET) for i in $(OBJS); do echo "$(CXX) $(INCS) $$i.c -o $(TARGET)/$$i $(LIBS) "; done @for i in $(OBJS); do $(CXX) $(INCS) $$i.c -o $(TARGET)/$$i $(LIBS); don e clean: @for i in $(OBJS); do rm -f $(TARGET)/$$i; done
To compile it, run ‘$make’ under c_enviroment/sample. To run it, do the following:
$ ./c_enviroment/output/test$ ./tcpip_led_test
We install a TCP/IP Client on another PC, and use it to send data to the server which is implemented 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.
Leave a Reply
You must be logged in to post a comment.