-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi-init.ino
151 lines (119 loc) · 3.78 KB
/
wifi-init.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
#include <FS.h>
#include <EEPROM.h> //Store counter in EEPROM memory to avoid sudden reset
#include <Wire.h>// I2C is used to communicate with sensors
// #include <VL53L0X.h>
#include <Ticker.h>
#include <ESP8266WiFi.h>// WiFi Library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define DAQ_INTERVAL 60 //5 minutes
Ticker DAQTimer;
volatile bool measurementFlag= true;
char sensorID[34] = "";
bool shouldSaveConfig = false;
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, HIGH);
Wire.begin();
wifiInit();
DAQTimer.attach(DAQ_INTERVAL, setMeasurementFlag);
}
void loop() {
// runDAQ();
yield();
}
void wifiInit() {
Serial.println("mounting FS...");
// SPIFFS.format();
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(sensorID, json["sensorID"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
}
WiFiManagerParameter custom_sensorID("sensorID", "Location", sensorID, 32);
WiFiManager wifiManager;
wifiManager.resetSettings();
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_sensorID);
wifiManager.setTimeout(120);
wifiManager.setConnectTimeout(30);
if (!wifiManager.autoConnect(String(ESP.getChipId()).c_str())) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//save the custom parameters to FS
if (shouldSaveConfig) {
strcpy(sensorID, custom_sensorID.getValue());
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["sensorID"] = sensorID;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
}
void postData(){
digitalWrite(LED_BUILTIN, LOW);
HTTPClient http;
http.setReuse(true);
static char msg[50];
static int port = 443;
String fingerprint = "d0ef874071f9f864abf5c1dc6376b591ee989866";
String host = "konttiserver.ddns.net";
String urlAPI = "/api/v1/hamk/tung";
// snprintf(msg, 75, "{\"countIn\": %lu, \"countOut\": %lu,\"id\": \"%s\"}", countIn, countOut,sensorID);
Serial.print("Sensor location: ");
Serial.println(sensorID);
// http.begin(host, port, urlAPI, fingerprint);
// http.addHeader("Content-Type", "application/json");
// int postcode = http.POST(msg);
String payload = http.getString();
// http.end();
Serial.println("Sent");
digitalWrite(LED_BUILTIN, HIGH);
measurementFlag = false;
}
void setMeasurementFlag() {
measurementFlag = true;
}