Browse Source

move UDP to ticker style

* UDP in and out only if ticker flag set true
* better connection management at boot
Etienne Landon 9 years ago
parent
commit
aedd97dfa9
4 changed files with 107 additions and 60 deletions
  1. 9 44
      ESP_UKI.ino
  2. 2 2
      includes.h
  3. 55 0
      udp.h
  4. 41 14
      wifimgr.h

+ 9 - 44
ESP_UKI.ino

@@ -13,38 +13,23 @@
 #include "includes.h"  //headers and variables declaration
 
 
-/* UDP CONFIGURATION */
-int UKI_UDP_In_Port = 9000;  //udp port input for ESP
-String UKI_UDP_IP_test = "192.168.1.1";
-IPAddress UKI_UDP_Master_IP(192, 168, 10, 100);  //default udp address to send to. Will automatically change to the ip sending something to udp in
-Ticker tkUKI;  // periodic send ADC to UDP
-int GSR_sensor;
+
 
 
 void setup ( void ) {
   
+  
   Serial.begin(115200);
   Serial.println("Starting ESP8266");
 
   setupLeds();
-   
-  setupWifi();  
   
-  //setupOTA();
+  setupWifi(); 
+
+  setupUDP() ;
   
-  delay(200);
-  Serial.println("Ready");
-  Serial.print("IP address: ");
-  Serial.println(WiFi.localIP());
-
-  //UKI sensor setup
-  UKI_UDP.begin(UKI_UDP_In_Port); 
-//  delay(1000);
-//  digitalWrite(Red_Led, HIGH); //red led off
-//  digitalWrite(Blue_Led, HIGH);
-//  delay(1000);
-//  ledBlink(Red_Led, 3, 100); //3 quick blink on red led as we start 
-//  delay (1000);
+  //setupOTA();
+ 
   blueLedState(-1, 500);
   
   
@@ -54,31 +39,11 @@ void setup ( void ) {
 void loop ( void ) {
   //Serial.println(WiFi.status());
   StartConfigAP();
-  
+  UDP_send_receive();
 
-  /*  UKI part	*/
-  GSR_sensor = analogRead(A0);
-  //UKI_UDP.beginPacketMulticast((224, 1, 2, 3), 8000, WiFi.localIP());//
-  UKI_UDP.beginPacket(UKI_UDP_IP_test, 8000);
-  UKI_UDP.print("UKI");
-  UKI_UDP.print(" ");
-  UKI_UDP.print(GSR_sensor);
-  UKI_UDP.endPacket();
-  yield();
-  
-  delay(50);
-  
-  //Check udp in
-  int packetSize = UKI_UDP.parsePacket();
   
-  if(packetSize) {
-    UKI_UDP_Master_IP = UKI_UDP.remoteIP();
-    UKI_UDP.beginPacket(UKI_UDP_Master_IP, 8000);
-    UKI_UDP.print("new master ip");
-    UKI_UDP.endPacket();
-  }
+}
 
 
 
-}
 

+ 2 - 2
includes.h

@@ -26,12 +26,12 @@ WiFiUDP UKI_UDP;
 
 
 #include "leds.h"     //config and functions relative to leds
-//#include "helpers.h"  //some helpers functions
+
 #include "ota.h"      //config and functions relative to ota firmware updates
 #include "wifimgr.h"   //config and functions relative to wifi and access point configuration and configuration permanent saving
 
 
-
+#include "udp.h"  //some helpers functions
 
 
 

+ 55 - 0
udp.h

@@ -0,0 +1,55 @@
+/* UDP CONFIGURATION */
+int UKI_UDP_In_Port = 9000;  //udp port input for ESP
+
+//default address and port to send to (IP read from config)
+IPAddress UKI_UDP_Out_IP ;
+int UKI_UDP_Out_Port = 8000 ;
+
+Ticker tkUKI;  // periodic send ADC to UDP
+int GSR_sensor;
+bool flag_UDP;
+int  UDP_tick = 50 ; //delay between to udp send
+
+
+void UDP_flag(){
+  flag_UDP = true;
+}
+
+
+void UDP_send_receive() {
+
+  if (flag_UDP) {
+    flag_UDP = false;
+      
+       /*  UKI part  */
+    GSR_sensor = analogRead(A0);
+    //UKI_UDP.beginPacketMulticast((224, 1, 2, 3), 8000, WiFi.localIP());//
+    UKI_UDP.beginPacket(UKI_UDP_Out_IP, UKI_UDP_Out_Port);
+    UKI_UDP.print("UKI");
+    UKI_UDP.print(" ");
+    UKI_UDP.print(GSR_sensor);
+    UKI_UDP.endPacket();
+      
+    //Check udp in
+    int packetSize = UKI_UDP.parsePacket();
+    
+    if(packetSize) {
+      UKI_UDP_Out_IP = UKI_UDP.remoteIP();
+      UKI_UDP.beginPacket(UKI_UDP_Out_IP, 8000);
+      UKI_UDP.print("new master ip");
+      UKI_UDP.endPacket();
+    }
+  }
+  
+
+ 
+}
+
+
+void setupUDP(){
+  UKI_UDP_Out_IP.fromString(UKI_UDP_IP);
+  Serial.print ("sending UDP to ");
+  Serial.println (UKI_UDP_Out_IP);
+  UKI_UDP.begin(UKI_UDP_In_Port); 
+  tkUKI.attach_ms(UDP_tick, UDP_flag); //raises flag 
+}

+ 41 - 14
wifimgr.h

@@ -7,7 +7,7 @@ bool flag_ConfigPortal = false;
 
 char UKI_NAME[40];
 char UKI_UDP_PORT[6] = "9000";
-char UKI_UDP_IP[34] = "192.168.10.100";
+char UKI_UDP_IP[16] = "192.168.1.100";
 
 
 
@@ -52,6 +52,7 @@ void ReadConfig() {
           strcpy(UKI_NAME, json["UKI_NAME"]);
           strcpy(UKI_UDP_PORT, json["UKI_UDP_PORT"]);
           strcpy(UKI_UDP_IP, json["UKI_UDP_IP"]);
+          
 
         } else {
           Serial.println("failed to load json config");
@@ -85,6 +86,7 @@ void WriteConfig() {
       json.printTo(Serial);
       json.printTo(configFile);
       configFile.close();
+      Serial.println();
       //end save
     }
 }
@@ -132,21 +134,30 @@ void StartConfigAP(){
     //here  "AutoConnectAP"
     //and goes into a blocking loop awaiting configuration
 
-    redLedState (0, 500);
-    blueLedState (-1, 100);
+    
     delay(1000);
-    if (!wifiManager.startConfigPortal("ESP_UKI_AP")) {
-      Serial.println("failed to connect, restarting");
-      //reset and try again
-      redLedState (-1, 100);
+
+    //Modify below to restart config portal 
+   // bool flag_connected =false ;
+    //while (!flag_connected) {
+      redLedState (0, 500);
       blueLedState (-1, 100);
-      ESP.reset();
-      delay(3000);
+    
+      if (!wifiManager.startConfigPortal("ESP_UKI_AP")) {
+        Serial.println("failed to connect, restarting config portal");
+        //reset and try again
+        redLedState (-1, 100);
+        blueLedState (-1, 100);
+        ESP.reset();
+        delay(3000);
       }
+     // else {flag_connected = true;}
+    //}
+
 
     //if you get here you have connected to the WiFi
     Serial.println("connected to UKI wifi");
-    blueLedState(1,500);
+    blueLedState(0,500);
 
     //read updated parameters
     strcpy(UKI_NAME, custom_UKI_NAME.getValue());
@@ -154,17 +165,33 @@ void StartConfigAP(){
     strcpy(UKI_UDP_IP, custom_UKI_UDP_IP.getValue());
 
     WriteConfig();
-    
-    // attach() tickers back now that it's working
-    
+    Serial.println("Restarting");
+    delay(500);
+    ESP.reset();
+    delay(3000);
   }
 }
 
 
 void setupWifi() {
   pinMode(TRIGGER_PIN, INPUT);
-  tkConfig.attach(5, CheckTriggerPin); // check TRIGGER_PIN state periodically
+  blueLedState (-1, 100);
+  Serial.print("Connecting");
   WiFi.begin();
+  while(WiFi.status()!=3){
+    Serial.print(".");
+    delay(100);
+  }
+  Serial.println();
+  Serial.println("connected");  //add wifi ssid 
+  blueLedState(1,500);
+  //ajout attente connection + leds
+  Serial.print("IP address: ");
+  Serial.println(WiFi.localIP());// ou vérifier si IP = 0.0.0.0, lancer config portal
+  
+  //delay(10000);
+  tkConfig.attach(5, CheckTriggerPin); // check TRIGGER_PIN state periodically
+  ReadConfig();
 //  while(WiFi.status()<3) {
 //    Serial.print
 //clean FS, for testing