很多用户对TCP/IP sockets 编程能否用于Arduio-ish C环境抱有疑问,答案是肯定的,与Arduino相比,也不需要再用其他WiFi Shield 或者Ethernet shield。
下面介绍的是如何通过socket通信实现pcDuino 控制LED灯,10mm的绿色LED灯以及proto shield,接线图如下:
要在pcDuino上建立TCP/IP socket 服务器,从客服端接收信息执行连接。当收到字符"O"时,灯亮;当收到字符 "F"时,灯灭,收到其它指令时不作出反应。 程序代码: - /*
- * 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
- #include
- #include
- #include
- #include
- #include <sys/types.h>
- #include
- #include
- 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);
- }
复制代码 在c_enviroment/sample里修改Makefile加入新的源文件:
- 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
复制代码
编译,运行 ‘$make’
- /c_enviroment/output/test$ ./tcpip_led_test
复制代码 这样就可以通过TCP客服端发送字符指令来控制LED灯了
|