Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FS.h> // this needs to be first, or it all crashes and burns...
- #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
- #include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
- #include <ESP8266HTTPClient.h>
- #define BUTTON_PIN D1
- #define led_r D6
- #define led_g D5
- #define led_b D2
- #define led_stat D7
- // WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
- WiFiManager wm;
- //define your default values here, if there are different values in config.json, they are overwritten.
- char serwer[60];
- char api_token[32];
- String str;
- byte i = 0;
- //default custom static IP
- char static_ip[16] = "10.0.1.56";
- char static_gw[16] = "10.0.1.1";
- char static_sn[16] = "255.255.255.0";
- //flag for saving data
- bool shouldSaveConfig = false;
- //callback notifying us of the need to save config
- void saveConfigCallback () {
- Serial.println("Should save config");
- shouldSaveConfig = true;
- }
- void setupSpiffs() {
- //clean FS, for testing
- //SPIFFS.format();
- //read configuration from FS json
- Serial.println("mounting FS...");
- 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(serwer, json["serwer"]);
- strcpy(api_token, json["api_token"]);
- } else {
- Serial.println("failed to load json config");
- }
- }
- }
- } else {
- Serial.println("failed to mount FS");
- }
- //end read
- }
- void checkButton() {
- // check for button press
- if ( digitalRead(BUTTON_PIN) == LOW ) {
- // poor mans debounce/press-hold, code not ideal for production
- delay(50);
- if ( digitalRead(BUTTON_PIN) == LOW ) {
- Serial.println("Button Pressed");
- analogWrite(led_g, 0);
- analogWrite(led_r, 10);
- analogWrite(led_b, 0);
- wm.autoConnect("Ardu_Konsola", "password");
- // still holding button for 3000 ms, reset settings, code not ideaa for production
- delay(1000); // reset delay hold
- if ( digitalRead(BUTTON_PIN) == LOW ) {
- Serial.println("Button Held");
- Serial.println("Erasing Config, restarting");
- wm.resetSettings();
- //SPIFFS.format();
- delay(1000);
- ESP.restart();
- }
- }
- }
- else
- {
- analogWrite(led_g, 0);
- analogWrite(led_r, 0);
- analogWrite(led_b, 0);
- }
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- pinMode(led_g, OUTPUT);
- pinMode(led_r, OUTPUT);
- pinMode(led_b, OUTPUT);
- analogWrite(led_g, 0);
- analogWrite(led_r, 0);
- analogWrite(led_b, 10);
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- pinMode(led_stat, INPUT_PULLUP);
- Serial.println();
- setupSpiffs();
- //set config save notify callback
- wm.setSaveConfigCallback(saveConfigCallback);
- std::vector<const char *> menu = {"wifi", "info", "param", "sep", "restart", "erase", "exit"};
- wm.setMenu(menu);
- WiFiManagerParameter custom_serwer("server", "Server: (http://.../post-esp-data.php)", serwer, 60);
- WiFiManagerParameter custom_api_token("api", "api_key:", api_token, 32);
- //add all your parameters here
- wm.addParameter(&custom_serwer);
- wm.addParameter(&custom_api_token);
- wm.autoConnect("Ardu_AP", "password");
- if (!wm.autoConnect()) {
- Serial.println("failed to connect and hit timeout");
- analogWrite(led_r, 10);
- delay(1000);
- analogWrite(led_r, 0);
- ESP.restart();
- }
- if (wm.autoConnect()) {
- Serial.println("Connected!");
- analogWrite(led_b, 0);
- analogWrite(led_g, 10);
- delay(1000);
- analogWrite(led_g, 0);
- }
- //read updated parameters
- strcpy(serwer, custom_serwer.getValue());
- strcpy(api_token, custom_api_token.getValue());
- //save the custom parameters to FS
- if (shouldSaveConfig) {
- Serial.println("saving config");
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- json["serwer"] = serwer;
- json["api_token"] = api_token;
- File configFile = SPIFFS.open("/config.json", "w");
- if (!configFile) {
- Serial.println("failed to open config file for writing");
- }
- json.prettyPrintTo(Serial);
- json.printTo(configFile);
- configFile.close();
- //end save
- shouldSaveConfig = false;
- }
- i = 1;
- }
- void loop() {
- checkButton();
- if (WiFi.status() == WL_CONNECTED)
- {
- Serial.println("WiFi OK");
- analogWrite(led_r, 0);
- if (i == 1)
- {
- Serial.println("local ip");
- Serial.println(WiFi.localIP());
- Serial.println(WiFi.gatewayIP());
- Serial.println(WiFi.subnetMask());
- Serial.print("server name: ");
- Serial.println(serwer);
- Serial.print("api_key: ");
- Serial.println(api_token);
- i = 0;
- }
- if (digitalRead(led_stat) == HIGH)
- {
- Serial.println("LED OFF");
- analogWrite(led_g, 0);
- }
- if (digitalRead(led_stat) == LOW)
- {
- Serial.println("LED ON");
- analogWrite(led_g, 10);
- }
- delay(100);
- }
- else
- {
- Serial.println("WiFi ERROR");
- analogWrite(led_r, 30);
- analogWrite(led_g, 0);
- delay(5000);
- ESP.restart();
- }
- if (Serial.available()) {
- str = Serial.readString();
- analogWrite(led_g, 0);
- analogWrite(led_r, 0);
- analogWrite(led_b, 10);
- HTTPClient http;
- // Your Domain name with URL path or IP address with path
- http.begin(serwer);
- // Specify content-type header
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- // Prepare your HTTP POST request data
- String api_token1 = String(api_token);
- String httpRequestData = "api_key=" + api_token1 + str;
- Serial.print("httpRequestData: ");
- Serial.println(httpRequestData);
- // Send HTTP POST request
- int httpResponseCode = http.POST(httpRequestData);
- if (httpResponseCode > 0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- }
- else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- http.end();
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement