Hi! I'm a newbie Arduino programmer, I am building an RC car using Wifi shield cc3000, I'm good in programming C# applications but I don't know how to program Arduino, the only thing I know is uploading the builtin example sketches.
So far I've done is compiled and upload the AdaFruit Wfi examples and they are working fine. Connection is being established, it is showing connection details.
I have a similar C# code and a wiShield sketch. can someone please convert that code for Wifi shield for me.
Thanks.
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
namespace CarTest
{
public partial class Form2 : Form
{
TcpClient tcpClient=new TcpClient();
NetworkStream networkStream ;
int KeyPressed;
private bool Arduino_Connect(string IP, int Port)
{
tcpClient.Connect(IP, Port);
networkStream = tcpClient.GetStream();
if ((!networkStream.CanWrite
|| !networkStream.CanRead))
{
tcpClient.Close();
networkStream = null;
return false;
}
return true;
}
private void Arduino_Write(string Output)
{
if (!(networkStream == null))
{
Byte[] sendBytes = Encoding.ASCII.GetBytes(Output);
Byte[] endByte = { 0xfe };
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Write(endByte, 0, 1);
}
else
{
MessageBox.Show ("ERROR");
}
}
private void Arduino_Disconnect()
{
if (!(networkStream == null))
{
tcpClient.Close();
networkStream = null;
}
}
private void Car_ForwardDrive(byte Speed)
{
Arduino_Write(("DF" + ((char)(Speed))));
}
private void Car_BackwardDrive(byte Speed)
{
Arduino_Write(("DB" + ((char)(Speed))));
}
private void Car_Stop()
{
Arduino_Write(("DF" + '\0'));
}
private void Car_TurnRight()
{
Arduino_Write("TR");
}
private void Car_TurnLeft()
{
Arduino_Write("TL");
}
private void Car_TurnNone()
{
Arduino_Write("TN");
}
private void Form1_Load(object sender, System.EventArgs e)
{
Arduino_Connect("192.168.0.25", 1000);
}
private void DriveForward_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void DriveForward_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Car_Stop();
}
private void DriveBackward_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void DriveBackward_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Car_Stop();
}
private void TurnLeft_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Car_TurnLeft();
}
private void TurnLeft_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Car_TurnNone();
}
private void TurnRight_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Car_TurnRight();
}
private void TurnRight_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Car_TurnNone();
}
private void Control_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
}
private void Control_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
}
public Form2()
{
InitializeComponent();
}
}
}
And Arduino Code:
/*
* Socket App
*
* A simple socket application example using the WiShield 1.0
*/
#include <WiShield.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,0,100}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,0,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"ROUTER_SSID"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"WPA_PASS"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
char buffer[20];
//---------------------------------------------------------------------------
void setup()
{
Car_Setup();
WiFi.init();
Serial.begin(9600);
}
void loop()
{
WiFi.run();
if (buffer[0] == 'A') { // Control specific pin
//PSOCK_SEND_STR(&s->p, "LED Command: ");
if (buffer[1] >= 3 && buffer[1] <= 12) {
//PSOCK_SEND_STR(&s->p, "Correct, LED");
//PSOCK_SEND(&s->p, &s->inputbuffer[1], 1);
//PSOCK_SEND(&s->p, "=", 1);
if (buffer[2] == 0) {
pinMode(buffer[1], OUTPUT); // define as output
digitalWrite(buffer[1], LOW);
//PSOCK_SEND_STR(&s->p, "OFF");
} else {
pinMode(buffer[1], OUTPUT); // define as output
digitalWrite(buffer[1], HIGH);
//PSOCK_SEND_STR(&s->p, "ON");
}
} else {
//PSOCK_SEND_STR(&s->p, "Wrong!");
}
}
if (buffer[0] == 'D') { // Control specific pin
Serial.print("Drive ");
if (buffer[1] == 'F') {
Serial.print("forward: ");
if (buffer[2] > 127) {
Car_Drive(127);
Serial.println(127, DEC);
} else {
Car_Drive(buffer[2]);
Serial.println(buffer[2], DEC);
}
} else if (buffer[1] == 'B') {
Serial.println("backward: ");
if (buffer[2] > 127) {
Car_Drive(-127);
Serial.println(-127, DEC);
} else {
Car_Drive(-1 * buffer[2]);
Serial.println(-1 * buffer[2], DEC);
}
}
}
if (buffer[0] == 'T') { // Control specific pin
Serial.print("Turn ");
if (buffer[1] == 'R') { // Right
Serial.println("right");
Car_Turn(1);
} else if (buffer[1] == 'L') { // Left
Serial.println("left");
Car_Turn(-1);
} else if (buffer[1] == 'N') { // None
Serial.println("none");
Car_Turn(0);
}
}
memset(buffer, 0x00, sizeof(buffer)); // Empty buffer
}
void Car_Setup()
{
pinMode(3, OUTPUT); // Turn Right Pin
digitalWrite(3, LOW);
pinMode(4, OUTPUT); // Turn Left Pin
digitalWrite(4, LOW);
analogWrite(5, 0); // Go Forward PWM
analogWrite(6, 0); // Go Backward PWM
}
void Car_Drive(signed char Direction) // Forward/Backward speed
{
if (Direction > 0) {
analogWrite(5, 2 * Direction); // Go Forward PWM
analogWrite(6, 0); // Go Backward PWM
} else if (Direction < 0) {
analogWrite(5, 0); // Go Backward PWM
analogWrite(6, -2 * Direction); // Go Forward PWM
} else {
analogWrite(5, 0); // Go Forward PWM
analogWrite(6, 0); // Go Backward PWM
}
}
void Car_Turn(signed char Direction) // Positive=Right, Negative=Left
{
if (Direction > 0) {
digitalWrite(3, HIGH); // Turn Right Pin
digitalWrite(4, LOW); // Turn Left Pin
} else if (Direction < 0) {
digitalWrite(3, LOW); // Turn Right Pin
digitalWrite(4, HIGH); // Turn Left Pin
} else {
digitalWrite(3, LOW); // Turn Right Pin
digitalWrite(4, LOW); // Turn Left Pin
}
}