-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwifi-test.ino
220 lines (190 loc) · 5.25 KB
/
wifi-test.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
//wifi libs
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
//ota
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//display libs
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//display consts
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//wifi station info
const char* ssid = "ssid";
const char* password = "pass";
//static ip info
IPAddress local_IP(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
//webserver
ESP8266WebServer server(80);
//ultra sonic sensors pins
const int trigPinT1 = 12;
const int echoPinT1 = 14;
const int trigPinT2 = 15;
const int echoPinT2 = 13;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701
//duration of ultra sonic wave travel
long duration;
float distanceCmT1;
int levelT1 = 0;
float distanceCmT2;
int levelT2 = 0;
char lcdText[30];
void setup() {
Serial.begin(9600);
delay(10);
startWifi();
setupOta();
startWebServer();
setupUltraSonicSensors();
setupDisplay();
}
void loop(void) {
ArduinoOTA.handle();
getUltraSonicSensorData(1);
getUltraSonicSensorData(2);
server.handleClient();
delay(1000);
}
void setupOta() {
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}
void setupDisplay() {
//Address 0x3D for 128x64
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
} else Serial.println("display init ok");
delay(2000);
writeToDisplay("Hello there!");
}
void startWifi() {
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
WiFi.begin(ssid, password);
//Serial.print("Connecting to ");
//Serial.print(ssid);
//Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
//Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
}
void startWebServer() {
if (MDNS.begin("esp8266")) {
Serial.println("mDNS responder started");
} else {
Serial.println("Error setting up MDNS responder!");
}
server.on("/", sendUltraSonicSensorData);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void setupUltraSonicSensors() {
pinMode(trigPinT1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinT1, INPUT); // Sets the echoPin as an Input
pinMode(trigPinT2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinT2, INPUT); // Sets the echoPin as an Input
}
//if tanker = 1 -> tanker 1 , if tanker = 2 -> tanker 2, duh!
void getUltraSonicSensorData(int tanker) {
int trigPin;
int echoPin;
float distance = 0;
int level = 0;
if (tanker == 1) {
trigPin = trigPinT1;
echoPin = echoPinT1;
}
else {
trigPin = trigPinT2;
echoPin = echoPinT2;
}
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * SOUND_VELOCITY / 2;
level = map(distance, 0, 170, 100, 0);
if (tanker == 1) {
levelT1 = level;
distanceCmT1 = distance;
}
else {
levelT2 = level;
distanceCmT2 = distance;
}
sprintf(lcdText, "T1 %d%% %d T2 %d%% %d", levelT1, (int) distanceCmT1, levelT2, (int) distanceCmT2);
writeToDisplay(lcdText);
}
void sendUltraSonicSensorData() {
server.send(200, "text/plain", lcdText);
}
void writeToDisplay(char* text) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println(text);
display.display();
}
void handleNotFound() {
server.send(404, "text/plain", "404: Not found");
}