... | ... |
@@ -13,6 +13,16 @@ int posTilt = A0; |
13 | 13 |
int posPan = A1; |
14 | 14 |
int motorSpeedStat = 0; |
15 | 15 |
|
16 |
+int speedStatus = 0; |
|
17 |
+int motorSpeed = 100; |
|
18 |
+int gotoTilt = 70; |
|
19 |
+ |
|
20 |
+ |
|
21 |
+int val; |
|
22 |
+int valOld; |
|
23 |
+char buf[12]; |
|
24 |
+char buf2[12]; |
|
25 |
+ |
|
16 | 26 |
// Update these with values suitable for your network. |
17 | 27 |
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; |
18 | 28 |
IPAddress ip(10, 13, 37, 165); |
... | ... |
@@ -48,7 +58,7 @@ void setup() |
48 | 58 |
pinMode(posTilt, INPUT); |
49 | 59 |
pinMode(posPan, INPUT); |
50 | 60 |
|
51 |
- Serial.begin(9600); |
|
61 |
+ Serial.begin(115200); |
|
52 | 62 |
Ethernet.begin(mac, ip); |
53 | 63 |
client.setBufferSize(255); |
54 | 64 |
|
... | ... |
@@ -85,8 +95,22 @@ void loop() |
85 | 95 |
reconnect(); |
86 | 96 |
} |
87 | 97 |
client.loop(); |
88 |
- |
|
89 |
- |
|
98 |
+val = analogRead(posTilt); |
|
99 |
+//Serial.println(val); |
|
100 |
+val = map(val, 0, 902, 0, 90); |
|
101 |
+if (((val - valOld) > 1) || ((valOld - val) > 1)) { |
|
102 |
+ if(speedStatus == 0) { |
|
103 |
+ motorSpeed = map(abs((val - gotoTilt)), 5, 20, 30, 100); |
|
104 |
+ if (motorSpeed < 30) { motorSpeed = 30; } |
|
105 |
+ if (motorSpeed > 100) { motorSpeed = 100; } |
|
106 |
+ client.publish("hamradio/rotator/motor/speed", itoa(motorSpeed, buf2, 10)); |
|
107 |
+ } |
|
108 |
+ valOld = val; |
|
109 |
+ client.publish("hamradio/rotator/pos/tilt", itoa(val, buf, 10)); |
|
110 |
+ delay(15); |
|
111 |
+ Serial.println(analogRead(posTilt)); |
|
112 |
+} |
|
113 |
+delay(15); |
|
90 | 114 |
|
91 | 115 |
|
92 | 116 |
} |
... | ... |
@@ -3,13 +3,34 @@ |
3 | 3 |
#include <EthernetENC.h> |
4 | 4 |
#include <PubSubClient.h> |
5 | 5 |
|
6 |
+ |
|
7 |
+int motorSpeedPin = 6; |
|
8 |
+int motorUpPin = 2; |
|
9 |
+int motorDownPin = 3; |
|
10 |
+int motorLeftPin = 4; |
|
11 |
+int motorRightPin = 5; |
|
12 |
+int posTilt = A0; |
|
13 |
+int posPan = A1; |
|
14 |
+int motorSpeedStat = 0; |
|
15 |
+ |
|
6 | 16 |
// Update these with values suitable for your network. |
7 | 17 |
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; |
8 | 18 |
IPAddress ip(10, 13, 37, 165); |
9 | 19 |
byte server[] = {10, 13, 37, 101}; |
10 | 20 |
|
11 | 21 |
void callback(char* topic, byte* payload, unsigned int length) { |
12 |
- // handle message arrived |
|
22 |
+ |
|
23 |
+ Serial.print("Message arrived ["); |
|
24 |
+ Serial.print(topic); |
|
25 |
+ Serial.print("] "); |
|
26 |
+ for (int i = 0; i < length; i++) { |
|
27 |
+ Serial.print((char)payload[i]); |
|
28 |
+ } |
|
29 |
+ Serial.println(""); |
|
30 |
+ |
|
31 |
+ |
|
32 |
+ |
|
33 |
+ |
|
13 | 34 |
} |
14 | 35 |
|
15 | 36 |
EthernetClient ethClient; |
... | ... |
@@ -17,6 +38,16 @@ PubSubClient client(server, 1883, callback, ethClient); |
17 | 38 |
|
18 | 39 |
void setup() |
19 | 40 |
{ |
41 |
+ |
|
42 |
+ pinMode(motorSpeedPin, OUTPUT); |
|
43 |
+ pinMode(motorUpPin, OUTPUT); |
|
44 |
+ pinMode(motorDownPin, OUTPUT); |
|
45 |
+ pinMode(motorLeftPin, OUTPUT); |
|
46 |
+ pinMode(motorRightPin, OUTPUT); |
|
47 |
+ |
|
48 |
+ pinMode(posTilt, INPUT); |
|
49 |
+ pinMode(posPan, INPUT); |
|
50 |
+ |
|
20 | 51 |
Serial.begin(9600); |
21 | 52 |
Ethernet.begin(mac, ip); |
22 | 53 |
client.setBufferSize(255); |
... | ... |
@@ -30,10 +61,14 @@ void reconnect() { |
30 | 61 |
// Attempt to connect |
31 | 62 |
if (client.connect("connectionName", "[[USER]]", "[[PASS]]")) { |
32 | 63 |
Serial.println("connected"); |
33 |
- // Once connected, publish an announcement... |
|
34 |
- client.publish("outTopic","hello world"); |
|
35 |
- // ... and resubscribe |
|
36 |
- client.subscribe("inTopic"); |
|
64 |
+ client.publish("hamradio/rotator/status","READY"); |
|
65 |
+ client.subscribe("hamradio/rotator/#"); |
|
66 |
+ |
|
67 |
+ client.publish("hamradio/rotator/motor/speed", "100"); |
|
68 |
+ client.publish("hamradio/rotator/pos/pan", "180"); |
|
69 |
+ client.publish("hamradio/rotator/pos/tilt", "60"); |
|
70 |
+ client.publish("hamradio/rotator/goto/pan", "180"); |
|
71 |
+ client.publish("hamradio/rotator/goto/tilt", "60"); |
|
37 | 72 |
} else { |
38 | 73 |
Serial.print("failed, rc="); |
39 | 74 |
Serial.print(client.state()); |
... | ... |
@@ -50,4 +85,8 @@ void loop() |
50 | 85 |
reconnect(); |
51 | 86 |
} |
52 | 87 |
client.loop(); |
88 |
+ |
|
89 |
+ |
|
90 |
+ |
|
91 |
+ |
|
53 | 92 |
} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,53 @@ |
1 |
+ |
|
2 |
+#include <SPI.h> |
|
3 |
+#include <EthernetENC.h> |
|
4 |
+#include <PubSubClient.h> |
|
5 |
+ |
|
6 |
+// Update these with values suitable for your network. |
|
7 |
+byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; |
|
8 |
+IPAddress ip(10, 13, 37, 165); |
|
9 |
+byte server[] = {10, 13, 37, 101}; |
|
10 |
+ |
|
11 |
+void callback(char* topic, byte* payload, unsigned int length) { |
|
12 |
+ // handle message arrived |
|
13 |
+} |
|
14 |
+ |
|
15 |
+EthernetClient ethClient; |
|
16 |
+PubSubClient client(server, 1883, callback, ethClient); |
|
17 |
+ |
|
18 |
+void setup() |
|
19 |
+{ |
|
20 |
+ Serial.begin(9600); |
|
21 |
+ Ethernet.begin(mac, ip); |
|
22 |
+ client.setBufferSize(255); |
|
23 |
+ |
|
24 |
+} |
|
25 |
+ |
|
26 |
+void reconnect() { |
|
27 |
+ // Loop until we're reconnected |
|
28 |
+ while (!client.connected()) { |
|
29 |
+ Serial.print("Attempting MQTT connection..."); |
|
30 |
+ // Attempt to connect |
|
31 |
+ if (client.connect("connectionName", "[[USER]]", "[[PASS]]")) { |
|
32 |
+ Serial.println("connected"); |
|
33 |
+ // Once connected, publish an announcement... |
|
34 |
+ client.publish("outTopic","hello world"); |
|
35 |
+ // ... and resubscribe |
|
36 |
+ client.subscribe("inTopic"); |
|
37 |
+ } else { |
|
38 |
+ Serial.print("failed, rc="); |
|
39 |
+ Serial.print(client.state()); |
|
40 |
+ Serial.println(" try again in 5 seconds"); |
|
41 |
+ // Wait 5 seconds before retrying |
|
42 |
+ delay(5000); |
|
43 |
+ } |
|
44 |
+ } |
|
45 |
+} |
|
46 |
+ |
|
47 |
+void loop() |
|
48 |
+{ |
|
49 |
+ if (!client.connected()) { |
|
50 |
+ reconnect(); |
|
51 |
+ } |
|
52 |
+ client.loop(); |
|
53 |
+} |