-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumpmon.ino
211 lines (175 loc) · 5.12 KB
/
sumpmon.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
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_WINC1500.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "config.h"
#include "dtostrf.h"
#ifdef DISPLAY_SSD1306
#ifdef DISPLAY_I2C
Adafruit_SSD1306 display = Adafruit_SSD1306();
#elif DISPLAY_HWSPI
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
#else
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#endif
#endif
long last_update=0;
float interpolation_slope;
float interpolation_offset;
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
int status = WL_IDLE_STATUS;
Adafruit_WINC1500Client client;
void printWifiData() {
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0], HEX);
#ifdef DISPLAY_SSD1306
display.print("SSID: ");
display.println(WiFi.SSID());
display.print("IP: ");
display.println(ip);
display.print("Signal: ");
display.print(WiFi.RSSI());
display.print("dB");
display.display();
#endif
}
void setup_wifi()
{
#ifdef WINC_EN
pinMode(WINC_EN, OUTPUT);
digitalWrite(WINC_EN, HIGH);
#endif
Serial.println("Setting up WiFi");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
#ifdef DISPLAY_1306
display.println("Connecting to WiFi");
#endif
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printWifiData();
}
void setup() {
pinMode(SENSOR_PIN,INPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Got serial");
interpolation_slope = (float)(HIGH_LEVEL-LOW_LEVEL) / (float)(ADC_HIGH - ADC_LOW);
interpolation_offset = (float)LOW_LEVEL - ((float)ADC_LOW * interpolation_slope);
Serial.print("interpolation slope: ");
Serial.println(interpolation_slope);
Serial.print("interpolation offset: ");
Serial.println(interpolation_offset);
#ifdef DISPLAY_SSD1306
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
// init done
// Clear the buffer.
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello, World!");
display.display();
#endif
setup_wifi();
delay(1000);
}
void loop() {
float reading=analogRead(SENSOR_PIN);
float depth = reading * interpolation_slope + interpolation_offset;
#ifdef DISPLAY_SSD1306
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Reading: ");
display.println((int)reading);
display.print("Depth: ");
display.print(depth);
display.println(" in.");
display.display();
#endif
delay(250);
if (last_update == 0 || (millis() > last_update+UPDATE_MS)) {
char post_data[256];
char content_length[256];
Serial.println("sending reading");
if (client.connected()) {
client.stop();
}
if (client.connect(LOG_SERVER,LOG_PORT)) {
Serial.println("connected to server");
client.print("POST ");
client.print(LOG_PATH);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(LOG_SERVER);
client.println("Content-Type: application/x-www-form-urlencoded");
char depth_str[20];
dtostrf(depth,0,1,depth_str);
sprintf(post_data,"%s value=%s",LOG_PARAM,depth_str);
sprintf(content_length,"Content-Length: %d",strlen(post_data));
client.println(content_length);
Serial.println(content_length);
client.println();
client.print(post_data);
Serial.println(post_data);
//client.println();
}
else {
Serial.println("Unable to connect");
}
Serial.print("Analog reading: ");
Serial.println(reading);
Serial.print("Depth interpolation: ");
Serial.println(depth);
/* reading = (1023 / reading) - 1;
reading = SERIES_RESISTOR / reading;
Serial.print("Sensor resistance ");
Serial.println(reading);
Serial.println(); */
last_update = millis();
}
while (client.available()) {
char c = client.read();
Serial.write(c);
}
}