-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_motoradfruitclient.ino
126 lines (97 loc) · 3.35 KB
/
sketch_motoradfruitclient.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <SPI.h>
#include <Ethernet.h>
Adafruit_MCP4725 dac;
//vars for ip/mac
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0x2A, 0xC7
};
//network variables
IPAddress ip(10, 7, 0, 180);
IPAddress myDns(10, 0, 0, 15);
IPAddress gateway(10, 7, 0, 20);
IPAddress subnet(255, 255, 255, 0);
//ints for pin numbers
int runpin = 3;
int groundpin = 2;
float delaytime;
String direction;
//varriables for reading
String readString;
int currentspeed = 0;
// server port
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously
void setup() {
//pin set up
dac.begin(0x62);
pinMode(runpin, OUTPUT);
digitalWrite(runpin, LOW);
// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
}
void loop() {
//server start
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clear out the input buffer:
client.flush();
client.println("connected");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
if (c == '\n') {
if (readString.equals("FORWARD")) {
client.println("DF");
direction = "CW";
} else if (readString.equals("REVERSE")) {
client.println("DR");
direction = "CCW";
} else if (readString.equals("STOP")) {
dac.setVoltage(2047.5, false);
digitalWrite(runpin, LOW);
client.println("DS");
} else if (readString.equals("EMERGENCY")) {
/*emergency pin*/
} else if (readString.equals("RESET")) {
/*Reset emergency*/
} else if (readString.charAt(0) == 'S') {
readString.remove(0, 1);
if(readString.toInt() <= 0){
dac.setVoltage(2047.5, false);
digitalWrite(runpin, LOW);
currentspeed = 0;
} else {
digitalWrite(runpin, HIGH);
if(direction.equals("CCW")){
dac.setVoltage(double(abs(double(double(readString.toInt() * 0.0416)-2.5)) * 819), false);
currentspeed = (double(abs(double(double(readString.toInt() * 0.0416)-2.5)) * 819));
} else if (direction.equals("CW")) {
dac.setVoltage(double(double(double(readString.toInt() * 0.0416) + 2.5) * 819), false);
currentspeed = double(double(double(readString.toInt() * 0.0416) + 2.5) * 819);
}
}
client.println(currentspeed);
}else if (readString.equals("1")) {
digitalWrite(runpin, LOW);
client.println("connected");
}
readString = "";
} else {
readString = readString + c;
}
}
}
}
}