So hier mal ein stückchen Code von mir um eine Teleskopmontierung mit dem Arduino und Redfly shield zu steuern.
Benutzen tue ich dafür im Moment HTML Formulare.
/*
Web Server
This sketch acts as a server
using an Arduino RedFly-Shield.
*/
#include <RedFly.h>
#include <WProgram.h>
#include <soft_merlin_mount.h>
#define LED_Merlin_Connected 12
#define LED_Merlin_Move 13
#define LED_WLAN_Connected 11
byte ip[] = {
192,168,001,230}; //ip from shield (server)
//byte ip[] = {192,168,1,101};
byte netmask[] = {
255,255,255,0 }; //netmask
uint8_t http=0xFF; //socket handle
uint16_t http_len=0; //receive len
char http_buf[64]; //receive buffer
char substring[32];
byte LED_on = 0;
byte TimerCounter = 0;
long pos_pitch = 0;
long pos_yaw = 0;
long startpoint_pitch = 0;
long startpoint_yaw = 0;
long endpoint_pitch = 0;
long endpoint_yaw = 0;
String readString = "";
uint8_t sock = 0, buf[32];
char* Button_Start = "<form method=get><input type=submit name=MERLIN value=";
char* Ende = "></form>";
char* Textbox_Start = "<form method=get><input type=text name=value size=25><input type=submit name=MERLIN value=";
void debugout(char *s) {
RedFly.disable();
Serial.print(s);
RedFly.enable();
}
void debugoutln(char *s){
RedFly.disable();
Serial.println(s);
RedFly.enable();
}
void setup()
{
uint8_t ret;
pinMode(LED_Merlin_Connected, OUTPUT);
pinMode(LED_Merlin_Move, OUTPUT);
pinMode(LED_WLAN_Connected, OUTPUT);
// Connect Merlin
Merlin_Connect();
//init the WiFi module on the shield
//ret = RedFly.init(HIGH_POWER); //LOW_POWER MED_POWER HIGH_POWER
ret = RedFly.init(115200, HIGH_POWER);
if(ret)
{
//debugoutln("INIT ERR");
}
else
{
//scan for wireless networks (must be run before join command)
RedFly.scan();
//join network
ret = RedFly.join("ABC", "***", INFRASTRUCTURE);
if(ret)
{
//debugoutln("JOIN ERR");
}
else
{
ret = RedFly.begin(ip, 0, netmask);
if(ret)
{
//debugoutln("BEGIN ERR");
RedFly.disconnect();
}
else
{
http = RedFly.socketListen(PROTO_TCP, 80); //start listening on port 80
if(http == 0xFF)
{
//debugoutln("SOCKET ERR");
RedFly.disconnect();
}
else
{
//WLAN connected
//debugoutln("connected");
digitalWrite(LED_WLAN_Connected, HIGH);
}
}
}
}
delay(500);
digitalWrite(LED_WLAN_Connected, LOW);
delay(500);
digitalWrite(LED_WLAN_Connected, HIGH);
}
void loop()
{
Webserver();
}
void Merlin_Connect()
{
merlin.init();
pos_pitch = merlin.readAxisPosition(AXIS_PITCH);
pos_yaw = merlin.readAxisPosition(AXIS_YAW);
if(pos_pitch > 0 && pos_yaw > 0) digitalWrite(LED_Merlin_Connected, HIGH);
}
void Merlin_Move(long degree, int move_direction)
{
if(move_direction==0)
{
pos_yaw = pos_yaw + merlin.fromAngle(degree);
}
else
{
pos_pitch = pos_pitch + merlin.fromAngle(degree);
}
Merlin_Goto(pos_pitch, pos_yaw);
}
void Merlin_Goto(long pitch, long yaw)
{
digitalWrite(LED_Merlin_Move, HIGH);
merlin.driveToPositionBothAxis(yaw, pitch, SYNC_SYNCHRONY);
digitalWrite(LED_Merlin_Move, LOW);
pos_pitch = pitch;
pos_yaw = yaw;
}
unsigned long ParseValueFromString(String string)
{
unsigned long value = 0;
int index = string.indexOf("value=");
if(index >= 0)
{
for(int i = 0; i < 3; i++)
{
substring[i] = http_buf[index + 6 + i];
}
value = atol(substring);
return value;
}
else
{
return 0;
}
}
void Webserver()
{
uint16_t rd, len;
if(http == 0xFF) //no socket open
{
return;
}
sock = 0xff; //return data from all open sockets
rd = RedFly.socketRead(&sock, &len, 0, 0, buf, sizeof(buf));
if(sock == http)
{
if((rd != 0) && (rd != 0xffff))
{
if((http_len+rd) > sizeof(http_buf))
{
rd = sizeof(http_buf)-http_len;
}
memcpy(&http_buf[http_len], buf, rd);
http_len += rd;
}
if((rd == 0xffff) || (len == 0)) //connection closed or all data received
{
//debug request message from iPod
//debugoutln("Request: ");
//debugoutln(http_buf);
readString = http_buf;
if(readString.indexOf("GET / HTTP") >= 0)
{
}
else if(readString.indexOf("UP") >= 0)
{
Merlin_Move(ParseValueFromString(readString),1);
}
else if(readString.indexOf("DOWN") >=0)
{
Merlin_Move(-1 * ParseValueFromString(readString),1);
}
else if(readString.indexOf("LEFT") >=0)
{
Merlin_Move(-1 * ParseValueFromString(readString),0);
}
else if(readString.indexOf("RIGHT") >=0)
{
Merlin_Move(ParseValueFromString(readString),0);
}
else if(readString.indexOf("Set_Start") >=0)
{
startpoint_yaw = pos_yaw;
startpoint_pitch = pos_pitch;
}
else if(readString.indexOf("Set_End") >=0)
{
endpoint_yaw = pos_yaw;
endpoint_pitch = pos_pitch;
}
else if(readString.indexOf("Goto_Start") >=0)
{
Merlin_Goto(startpoint_pitch, startpoint_yaw);
}
else if(readString.indexOf("Goto_End") >=0)
{
Merlin_Goto(endpoint_pitch, endpoint_yaw);
}
else
{
}
Send_Main();
//reset input buffer
http_buf[0] = 0;
http_len = 0;
readString = "";
//close connection
RedFly.socketClose(sock);
//restart listening
http = RedFly.socketListen(PROTO_TCP, 80);
}
}
}
void Send_Main()
{
//HTML header
RedFly.socketSend(sock, PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"), 46); //Content-Length: xx\r\n
// Write Merlin position
sprintf((char*)buf, "
yaw: %lu ", pos_yaw);
RedFly.socketSend(sock, (char*)buf);
sprintf((char*)buf, "
pitch: %lu ", pos_pitch);
RedFly.socketSend(sock, (char*)buf);
sprintf((char*)buf, "
start_yaw: %lu ", startpoint_yaw);
RedFly.socketSend(sock, (char*)buf);
sprintf((char*)buf, "
start_pitch: %lu ", startpoint_pitch);
RedFly.socketSend(sock, (char*)buf);
sprintf((char*)buf, "
end_yaw: %lu ", endpoint_yaw);
RedFly.socketSend(sock, (char*)buf);
sprintf((char*)buf, "
end_pitch: %lu ", endpoint_pitch);
RedFly.socketSend(sock, (char*)buf);
RedFly.socketSend(sock, "
");
Send_Textbox("DOWN");
Send_Textbox("UP");
Send_Textbox("RIGHT");
Send_Textbox("LEFT");
Send_Button("Set_Start");
Send_Button("Set_End");
Send_Button("Goto_Start");
Send_Button("Goto_End");
}
void Send_Textbox(char* Buttonname)
{
RedFly.socketSend(sock, Textbox_Start);
RedFly.socketSend(sock, Buttonname);
RedFly.socketSend(sock, Ende);
}
void Send_Button(char* Buttonname)
{
RedFly.socketSend(sock, Button_Start);
RedFly.socketSend(sock, Buttonname);
RedFly.socketSend(sock, Ende);
}