Skip to content

Control Arduino Through Serial

swt edited this page Aug 22, 2017 · 15 revisions

Control Arduino Through Serial

Here you can use C & python for program in WisProduct, and control Arduino through Uart2

Note: uart0 used as debug, uart1 has been used as tunnel which transfer data between uart1 and tcp

If you want to use python for program, you need config it, but it will take up a lot of memory

In the WisCore-SDK:

$ cd wiswrt/15.05-rak-rc2

$ make menuconfig

* Select the options as below:

	Languages  --->
		Python  --->
			<*> python
			<*> python-pyserial
			<*> python-pip

* Save and exit

$ cd ../../

$ make

Step 1 Enable Uart2 Of WisProduct

By default Uart2 is disabled, you need enable it to control arduino - How to enable Uart2

Step 2 Install Arduino IDE

2.1 Get IDE - download arduino IDE, then install it

2.2 Connect arduino and WisProduct

Connect Table:

Arduino WisProduct Uart2 Silkprint
RX UART_TXD2 PWM2
TX UART_RXD2 PWM3
GND \ GND

Connect Picture:

2.3 Power on with usb, and you will find the COM in "Device Manager"

If not, pls install drivers

2.4 Open the arduino IDE and choose "Tools":

Board:"Arduino Leonardo ETH"

Port:"COMn (Arduino Leonardo ETH)"

Step 3 Applications

Here I will control LED(D13) of arduino through WisProduct with python.

Software through serial between Arduino and WisProduct

In this way, you should program both in Arduino & WisProduct.

Software for Arduino

Note: Here are Serial&Serial1, Serial is "USB COM", Serial1 is "Uart"

void setup() {
	// put your setup code here, to run once:
	
	Serial1.begin(115200); //init Uart 

	pinMode(13,OUTPUT); //init D13
}


void loop() {
	//put your main code here, to run repeatedly:

	int c = Serial1.read(); //read uart data

	if(c != -1){
		switch(c){
			case '0':
				digitalWrite(13,0); //set D13 low level
				break;

			case '1':
				digitalWrite(13,1); //set D13 high level
				break;
		}
	}
}

then Compile & Upload

In the Arduino IDE, there are some examples for you to reference

"File" ---> "Examples" --->

Software for WisProduct

vi Arduino_led_serial_test.py

import serial
import time

s = None;

def setup():
	global s;
	s = serial.Serial("/dev/ttyS2",115200);

def loop():
	s.write("1");
	time.sleep(1);
	s.write("0");
	time.sleep(1);

setup();

while True:
	loop();

Then save the file & execute

python Arduino_led_serial_test.py	

Finally you will find the LED in Arduino blinks