Advertisement
Guest User

Untested Switching Concept

a guest
Apr 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266HTTPClient.h>
  5.  
  6. // SSID and Password for AP mode
  7. const char *ssid = "node_middle1";
  8. const char *password = "a12345678";
  9.  
  10. // SSID and Password for other ESP network
  11. const char *nextSSID = "main_node";
  12. const char *nextPassword = "a12345678";
  13.  
  14. // Check if already get data from last node
  15. int connectionMadeWithLastNode = 0;
  16.  
  17. // Check if configClientMode() was already called
  18. int clientModeConfigured = 0;
  19.  
  20. // Fixed IP in network on AP mode
  21. IPAddress apIP(192, 168, 4, 1);
  22.  
  23. ESP8266WebServer server(80);
  24.  
  25. void response();
  26. void configAPMode();
  27. void configClientMode();
  28. void sendMessage();
  29.  
  30. void setup() {
  31. Serial.begin(115200);
  32. configAPMode();
  33. }
  34.  
  35. void loop() {
  36. if(connectionMadeWithLastNode == 0){
  37. server.handleClient();
  38. } else {
  39. if(clientModeConfigured == 0) {
  40. configClientMode();
  41. } else{
  42. sendMessage();
  43. }
  44. }
  45. }
  46.  
  47. void response() {
  48. Serial.print("MENSAGEM DO ULTIMO NÓ: ");
  49. Serial.println(server.arg("plain"));
  50. server.send(200);
  51. connectionMadeWithLastNode = 1;
  52. }
  53.  
  54. void configAPMode() {
  55. WiFi.mode(WIFI_AP_STA);
  56. WiFi.softAP(ssid, password);
  57.  
  58. server.on("/", response);
  59. server.begin();
  60. }
  61.  
  62. void configClientMode() {
  63. WiFi.softAPdisconnect();
  64. WiFi.begin(nextSSID, nextPassword);
  65. clientModeConfigured = 1;
  66. Serial.println("CLIENT MODE");
  67. }
  68.  
  69. void sendMessage(){
  70. if (WiFi.status() == WL_CONNECTED){
  71. HTTPClient http;
  72. http.begin("http://192.168.4.1");
  73. http.addHeader("Content-Type", "application/json");
  74. int httpCode = http.POST("{\"Value\": 200}");
  75. Serial.print("HTTP Code: ");
  76. Serial.println(httpCode);
  77.  
  78. http.end();
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement