ESP_UKI.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ESP_UKI
  3. TODO : better function organizing
  4. add firmware number in webserver
  5. rework led system with tickers
  6. send ADC to default IP via udp, allow configuration
  7. build onDemand config mode
  8. load/save parameters (fixed ip, uki_name, udp_port udp_ip
  9. */
  10. #include <FS.h> //this needs to be first, or it all crashes and burns...
  11. char mqtt_server[40];
  12. char mqtt_port[6] = "8080";
  13. char blynk_token[34] = "YOUR_BLYNK_TOKEN";
  14. bool shouldSaveConfig = false;
  15. #include "includes.h" //headers and variables declaration
  16. /* UDP CONFIGURATION */
  17. int UKI_UDP_In_Port = 9000; //udp port input for ESP
  18. IPAddress UKI_UDP_Master_IP(192, 168, 0, 41); //default udp address to send to. Will automatically change to the ip sending something to udp in
  19. Ticker tkUKI; // periodic send ADC to UDP
  20. int GSR_sensor;
  21. void setup ( void ) {
  22. //EEPROM.begin(512);
  23. Serial.begin(115200);
  24. Serial.println("Starting ESP8266");
  25. //clean FS, for testing
  26. //SPIFFS.format();
  27. //read configuration from FS json
  28. Serial.println("mounting FS...");
  29. if (SPIFFS.begin()) {
  30. Serial.println("mounted file system");
  31. if (SPIFFS.exists("/config.json")) {
  32. //file exists, reading and loading
  33. Serial.println("reading config file");
  34. File configFile = SPIFFS.open("/config.json", "r");
  35. if (configFile) {
  36. Serial.println("opened config file");
  37. size_t size = configFile.size();
  38. // Allocate a buffer to store contents of the file.
  39. std::unique_ptr<char[]> buf(new char[size]);
  40. configFile.readBytes(buf.get(), size);
  41. DynamicJsonBuffer jsonBuffer;
  42. JsonObject& json = jsonBuffer.parseObject(buf.get());
  43. json.printTo(Serial);
  44. if (json.success()) {
  45. Serial.println("\nparsed json");
  46. strcpy(mqtt_server, json["mqtt_server"]);
  47. strcpy(mqtt_port, json["mqtt_port"]);
  48. strcpy(blynk_token, json["blynk_token"]);
  49. } else {
  50. Serial.println("failed to load json config");
  51. }
  52. }
  53. }
  54. } else {
  55. Serial.println("failed to mount FS");
  56. }
  57. //end read
  58. setupWifi();
  59. setupLeds();
  60. setupOTA();
  61. delay(200);
  62. Serial.println("Ready");
  63. Serial.print("IP address: ");
  64. //Serial.println(WiFi.localIP());
  65. //UKI sensor setup
  66. UKI_UDP.begin(UKI_UDP_In_Port);
  67. // delay(1000);
  68. // digitalWrite(Red_Led, HIGH); //red led off
  69. // digitalWrite(Blue_Led, HIGH);
  70. // delay(1000);
  71. // ledBlink(Red_Led, 3, 100); //3 quick blink on red led as we start
  72. // delay (1000);
  73. blueLedState(-1, 500);
  74. }
  75. void loop ( void ) {
  76. StartConfigAP();
  77. //save the custom parameters to FS
  78. if (shouldSaveConfig) {
  79. Serial.println("saving config");
  80. DynamicJsonBuffer jsonBuffer;
  81. JsonObject& json = jsonBuffer.createObject();
  82. json["mqtt_server"] = mqtt_server;
  83. json["mqtt_port"] = mqtt_port;
  84. json["blynk_token"] = blynk_token;
  85. File configFile = SPIFFS.open("/config.json", "w");
  86. if (!configFile) {
  87. Serial.println("failed to open config file for writing");
  88. }
  89. json.printTo(Serial);
  90. json.printTo(configFile);
  91. configFile.close();
  92. //end save
  93. }
  94. /* UKI part */
  95. GSR_sensor = analogRead(A0);
  96. //UKI_UDP.beginPacketMulticast((224, 1, 2, 3), 8000, WiFi.localIP());//
  97. UKI_UDP.beginPacket(UKI_UDP_Master_IP, 8000);
  98. UKI_UDP.print(config.DeviceName);
  99. UKI_UDP.print(" ");
  100. UKI_UDP.print(GSR_sensor);
  101. UKI_UDP.endPacket();
  102. yield();
  103. delay(20);
  104. //Check udp in
  105. int packetSize = UKI_UDP.parsePacket();
  106. if(packetSize) {
  107. UKI_UDP_Master_IP = UKI_UDP.remoteIP();
  108. UKI_UDP.beginPacket(UKI_UDP_Master_IP, 8000);
  109. UKI_UDP.print("new master ip");
  110. UKI_UDP.endPacket();
  111. }
  112. }