-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreativeIoTSubmarine.ino
259 lines (209 loc) · 6.29 KB
/
CreativeIoTSubmarine.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <Bonezegei_DHT11.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiNINA.h>
#include <Servo.h>
#include "wifi_secrets.h"
#include "cloud_secret.h"
#include "netutils.h"
#include "servoutils.h"
#include "mdriverutils.h"
#include "usonicSensUtils.h"
// #include "led.h"
#include "phsensor.h"
#include "cloud.h"
// Motor driver pins
#define MDRIVER_OUT1 2
#define MDRIVER_OUT2 3
#define MDRIVER_OUT3 13
#define MDRIVER_OUT4 14
#define MDRIVER_ENA 5
#define MDRIVER_ENB 4
// Rudder Servo
#define RUDDER_SERVO 0
// Ultrasonic sensor pins
#define US_SENS_TRIG 6
#define US_SENS_ECHO 7
// Orientation bulb
#define ORIENTATION_BULB 1
// Buzzer
#define BUZZER1_PIN 8
// Temp sensor
#define T_SENS_PIN 9
// pH sensor
#define PH_SENS_PIN A6 // connect to Po
OneWire wire(T_SENS_PIN);
DallasTemperature tempsens(&wire);
// Motors
Motor thruster_main_out34enB(MDRIVER_OUT3, MDRIVER_OUT4, MDRIVER_ENB);
char ssid[] = WIFI_SSID;
char password[] = WIFI_PASSWORD;
bool orientation_ok;
long int duration;
int distance, prevDistance;
int rudder_pos, thruster_speed; // rudder_pos: middle: 0, left: 1, right: 2
float temp, ph, prevTemp = -9999;
bool huskylens_is_obj_detected;
Servo _rudder_servo;
HakimiServo rudder_servo(&_rudder_servo, RUDDER_SERVO);
void setup() {
int status = WL_IDLE_STATUS;
pinMode(MDRIVER_OUT1, OUTPUT);
pinMode(MDRIVER_OUT2, OUTPUT);
pinMode(MDRIVER_OUT3, OUTPUT);
pinMode(MDRIVER_OUT4, OUTPUT);
pinMode(MDRIVER_ENA, OUTPUT);
pinMode(MDRIVER_ENB, OUTPUT);
pinMode(US_SENS_ECHO, INPUT);
pinMode(US_SENS_TRIG, OUTPUT);
pinMode(PH_SENS_PIN, INPUT);
pinMode(ORIENTATION_BULB, INPUT);
pinMode(T_SENS_PIN, INPUT);
Serial.begin(9600);
delay(1500);
tempsens.begin();
// led
// White: Connecting to network
// Blue: Connected
// Yellow: Obstacle
// Red: Error
// set_color(255, 255, 255);
// // WiFi connections
// if (WiFi.status() == WL_NO_MODULE)
// Serial.println("Cannot detect wifi module!");
// else
// Serial.println("WiFi module detected");
// Serial.print("WiFiNINA FW version: ");
// Serial.println(WiFi.firmwareVersion());
// while (status != WL_CONNECTED) {
// Serial.print("Trying to connect to wifi network: ");
// Serial.println(ssid);
// status = WiFi.begin(ssid, password);
// tone(BUZZER1_PIN, 2000, 1000);
// delay(3000);
// switch (status) {
// case WL_CONNECT_FAILED:
// Serial.println("Failed to connect to network!");
// break;
// case WL_CONNECTED:
// Serial.println("Connected successfully");
// set_color(0, 0, 255);
// break;
// }
// }
// Arduino Cloud
ArduinoCloud.setThingId(THING_ID); // from cloud_secret.h
ArduinoCloud.addProperty(ph, READ, 2 * SECONDS, NULL);
ArduinoCloud.addProperty(temp, READWRITE, 2 * SECONDS, NULL);
ArduinoCloud.addProperty(distance, READ, 2 * SECONDS, NULL);
ArduinoCloud.addProperty(orientation_ok, READWRITE, 2 * SECONDS, NULL);
ArduinoCloud.addProperty(huskylens_is_obj_detected, READWRITE, 1 * SECONDS, NULL);
ArduinoCloud.addProperty(rudder_pos, READWRITE, ON_CHANGE, on_rudder_pos_change);
ArduinoCloud.addProperty(thruster_speed, READWRITE, ON_CHANGE, on_thruster_change);
ArduinoCloud.begin(iotPreferredCon);
// printCurrentNet();
// printWifiData();
thruster_speed = 70;
thruster_main_out34enB.start_motor(thruster_speed);
}
void on_thruster_change()
{
Serial.println("Thruster value changed");
bool should_reverse = false;
if (!(-255<=thruster_speed<=255))
return;
if (thruster_speed < 0)
should_reverse = true;
thruster_main_out34enB.start_motor(abs(thruster_speed), should_reverse);
}
void on_rudder_pos_change()
{
Serial.println("Rudder position changed");
if (!(0<=rudder_pos<=2))
return;
switch (rudder_pos) {
case 0:
rudder_servo.servo_resetpos();
Serial.println("Resetting servo position");
break;
case 1:
rudder_servo.servo_steerLeft();
Serial.println("Moving servo to the left");
break;
case 2:
rudder_servo.servo_steerRight();
Serial.println("Moving servo to the right");
}
}
float get_stable_temp(float temp)
{
int calibrationTemp;
if (prevTemp == -9999) {
tempsens.requestTemperatures();
calibrationTemp = tempsens.getTempCByIndex(0);
while (calibrationTemp == -127) {
Serial.println("Waiting for valid reading to calibrate");
tempsens.requestTemperatures();
calibrationTemp = tempsens.getTempCByIndex(0);
}
prevTemp = calibrationTemp;
}
if (abs(temp-prevTemp) > 10) {
// Re-use previous temp instead
Serial.println("Warning: Temperature probe disconnected, check connection, using old values for now...");
return prevTemp;
}
prevTemp = temp;
return temp;
}
void loop() {
// if (WiFi.status() == WL_DISCONNECTED || WL_FAILURE)
// set_color(255, 0, 0);
// else
// set_color(0, 0, 255);
// srand(time(NULL));
// if ((rand() % 2) == 0)
// huskylens_is_obj_detected = true;
// else
// huskylens_is_obj_detected = false;
ArduinoCloud.update();
send_sonic_burst(US_SENS_TRIG);
duration = pulseIn(US_SENS_ECHO, HIGH);
distance = calc_usonic_sensor_distance(duration);
orientation_ok = !digitalRead(ORIENTATION_BULB);
Serial.print("Orientation bulb value: ");
Serial.println(orientation_ok);
if (distance == 0) {
Serial.println("Warning: Ultrasonic sensor disconnected");
distance = prevDistance;
}
else
prevDistance = distance;
// if (!orientation_ok) {
// if (thruster_main_out34enB.is_motor_running())
// thruster_main_out34enB.halt_motor();
// for (int i=0; i<5; i++) {
// tone(BUZZER1_PIN, 2000, 50);
// delay(100);
// }
// }
tempsens.requestTemperatures();
temp = get_stable_temp(tempsens.getTempCByIndex(0));
ph = calculate_ph(PH_SENS_PIN);
Serial.print("distance=");
Serial.print(distance);
Serial.print("cm");
Serial.print(" pH=");
Serial.print(ph);
Serial.print(" temp=");
Serial.print(temp);
Serial.print("∘C\n");
if (temp > 40)
Serial.println("Warning: Water temperature is too high, please re-position the submarine!");
else if (temp > 35)
Serial.println("Warning: Water temp is quite high...");
// if (distance < 30)
// thruster_main_out34enB.halt_motor();
// else
// thruster_main_out34enB.start_motor(200);
}