Advertisement
Krejzi_Dark

esp

Dec 15th, 2023
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. #include <FS.h> // this needs to be first, or it all crashes and burns...
  2. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  3. #include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
  4. #include <ESP8266HTTPClient.h>
  5.  
  6. #define BUTTON_PIN D1
  7.  
  8. #define led_r D6
  9. #define led_g D5
  10. #define led_b D2
  11.  
  12. #define led_stat D7
  13.  
  14. // WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  15. WiFiManager wm;
  16.  
  17. //define your default values here, if there are different values in config.json, they are overwritten.
  18. char serwer[60];
  19. char api_token[32];
  20.  
  21. String str;
  22. byte i = 0;
  23.  
  24. //default custom static IP
  25. char static_ip[16] = "10.0.1.56";
  26. char static_gw[16] = "10.0.1.1";
  27. char static_sn[16] = "255.255.255.0";
  28.  
  29. //flag for saving data
  30. bool shouldSaveConfig = false;
  31.  
  32. //callback notifying us of the need to save config
  33. void saveConfigCallback () {
  34. Serial.println("Should save config");
  35. shouldSaveConfig = true;
  36. }
  37.  
  38. void setupSpiffs() {
  39. //clean FS, for testing
  40. //SPIFFS.format();
  41.  
  42. //read configuration from FS json
  43. Serial.println("mounting FS...");
  44.  
  45. if (SPIFFS.begin()) {
  46. Serial.println("mounted file system");
  47. if (SPIFFS.exists("/config.json")) {
  48. //file exists, reading and loading
  49. Serial.println("reading config file");
  50. File configFile = SPIFFS.open("/config.json", "r");
  51. if (configFile) {
  52. Serial.println("opened config file");
  53. size_t size = configFile.size();
  54. // Allocate a buffer to store contents of the file.
  55. std::unique_ptr<char[]> buf(new char[size]);
  56.  
  57. configFile.readBytes(buf.get(), size);
  58. DynamicJsonBuffer jsonBuffer;
  59. JsonObject& json = jsonBuffer.parseObject(buf.get());
  60. json.printTo(Serial);
  61. if (json.success()) {
  62. Serial.println("\nparsed json");
  63.  
  64. strcpy(serwer, json["serwer"]);
  65. strcpy(api_token, json["api_token"]);
  66.  
  67. } else {
  68. Serial.println("failed to load json config");
  69. }
  70. }
  71. }
  72. } else {
  73. Serial.println("failed to mount FS");
  74. }
  75. //end read
  76. }
  77.  
  78. void checkButton() {
  79. // check for button press
  80. if ( digitalRead(BUTTON_PIN) == LOW ) {
  81. // poor mans debounce/press-hold, code not ideal for production
  82. delay(50);
  83. if ( digitalRead(BUTTON_PIN) == LOW ) {
  84. Serial.println("Button Pressed");
  85. analogWrite(led_g, 0);
  86. analogWrite(led_r, 10);
  87. analogWrite(led_b, 0);
  88.  
  89. wm.autoConnect("Ardu_Konsola", "password");
  90. // still holding button for 3000 ms, reset settings, code not ideaa for production
  91. delay(1000); // reset delay hold
  92. if ( digitalRead(BUTTON_PIN) == LOW ) {
  93. Serial.println("Button Held");
  94. Serial.println("Erasing Config, restarting");
  95. wm.resetSettings();
  96. //SPIFFS.format();
  97. delay(1000);
  98. ESP.restart();
  99. }
  100. }
  101. }
  102. else
  103. {
  104. analogWrite(led_g, 0);
  105. analogWrite(led_r, 0);
  106. analogWrite(led_b, 0);
  107. }
  108. }
  109.  
  110. void setup() {
  111. // put your setup code here, to run once:
  112. Serial.begin(115200);
  113. pinMode(led_g, OUTPUT);
  114. pinMode(led_r, OUTPUT);
  115. pinMode(led_b, OUTPUT);
  116.  
  117. analogWrite(led_g, 0);
  118. analogWrite(led_r, 0);
  119. analogWrite(led_b, 10);
  120.  
  121. pinMode(BUTTON_PIN, INPUT_PULLUP);
  122. pinMode(led_stat, INPUT_PULLUP);
  123.  
  124. Serial.println();
  125.  
  126. setupSpiffs();
  127.  
  128. //set config save notify callback
  129. wm.setSaveConfigCallback(saveConfigCallback);
  130.  
  131. std::vector<const char *> menu = {"wifi", "info", "param", "sep", "restart", "erase", "exit"};
  132. wm.setMenu(menu);
  133.  
  134. WiFiManagerParameter custom_serwer("server", "Server: (http://.../post-esp-data.php)", serwer, 60);
  135. WiFiManagerParameter custom_api_token("api", "api_key:", api_token, 32);
  136.  
  137. //add all your parameters here
  138. wm.addParameter(&custom_serwer);
  139. wm.addParameter(&custom_api_token);
  140.  
  141. wm.autoConnect("Ardu_AP", "password");
  142.  
  143. if (!wm.autoConnect()) {
  144. Serial.println("failed to connect and hit timeout");
  145. analogWrite(led_r, 10);
  146. delay(1000);
  147. analogWrite(led_r, 0);
  148. ESP.restart();
  149. }
  150.  
  151. if (wm.autoConnect()) {
  152. Serial.println("Connected!");
  153. analogWrite(led_b, 0);
  154. analogWrite(led_g, 10);
  155. delay(1000);
  156. analogWrite(led_g, 0);
  157. }
  158.  
  159. //read updated parameters
  160. strcpy(serwer, custom_serwer.getValue());
  161. strcpy(api_token, custom_api_token.getValue());
  162.  
  163. //save the custom parameters to FS
  164. if (shouldSaveConfig) {
  165. Serial.println("saving config");
  166. DynamicJsonBuffer jsonBuffer;
  167. JsonObject& json = jsonBuffer.createObject();
  168. json["serwer"] = serwer;
  169. json["api_token"] = api_token;
  170.  
  171. File configFile = SPIFFS.open("/config.json", "w");
  172. if (!configFile) {
  173. Serial.println("failed to open config file for writing");
  174. }
  175.  
  176. json.prettyPrintTo(Serial);
  177. json.printTo(configFile);
  178. configFile.close();
  179. //end save
  180. shouldSaveConfig = false;
  181. }
  182. i = 1;
  183. }
  184.  
  185. void loop() {
  186. checkButton();
  187.  
  188. if (WiFi.status() == WL_CONNECTED)
  189. {
  190. Serial.println("WiFi OK");
  191. analogWrite(led_r, 0);
  192. if (i == 1)
  193. {
  194. Serial.println("local ip");
  195. Serial.println(WiFi.localIP());
  196. Serial.println(WiFi.gatewayIP());
  197. Serial.println(WiFi.subnetMask());
  198. Serial.print("server name: ");
  199. Serial.println(serwer);
  200. Serial.print("api_key: ");
  201. Serial.println(api_token);
  202. i = 0;
  203. }
  204. if (digitalRead(led_stat) == HIGH)
  205. {
  206. Serial.println("LED OFF");
  207. analogWrite(led_g, 0);
  208. }
  209. if (digitalRead(led_stat) == LOW)
  210. {
  211. Serial.println("LED ON");
  212. analogWrite(led_g, 10);
  213. }
  214. delay(100);
  215. }
  216. else
  217. {
  218. Serial.println("WiFi ERROR");
  219. analogWrite(led_r, 30);
  220. analogWrite(led_g, 0);
  221. delay(5000);
  222. ESP.restart();
  223. }
  224.  
  225. if (Serial.available()) {
  226. str = Serial.readString();
  227.  
  228. analogWrite(led_g, 0);
  229. analogWrite(led_r, 0);
  230. analogWrite(led_b, 10);
  231.  
  232. HTTPClient http;
  233.  
  234. // Your Domain name with URL path or IP address with path
  235. http.begin(serwer);
  236.  
  237. // Specify content-type header
  238. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  239.  
  240. // Prepare your HTTP POST request data
  241. String api_token1 = String(api_token);
  242. String httpRequestData = "api_key=" + api_token1 + str;
  243.  
  244. Serial.print("httpRequestData: ");
  245. Serial.println(httpRequestData);
  246.  
  247. // Send HTTP POST request
  248. int httpResponseCode = http.POST(httpRequestData);
  249.  
  250. if (httpResponseCode > 0) {
  251. Serial.print("HTTP Response code: ");
  252. Serial.println(httpResponseCode);
  253. }
  254. else {
  255. Serial.print("Error code: ");
  256. Serial.println(httpResponseCode);
  257. }
  258. http.end();
  259. delay(100);
  260. }
  261. }
  262.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement