Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <esp_wifi.h>
  4.  
  5. #include <WebSocketsServer.h>
  6.  
  7. const char* ssid = "Baitboat";//AP ssid
  8. const char* password = "Password";//AP password
  9.  
  10. WebSocketsServer webSocket = WebSocketsServer(81);
  11.  
  12. const byte numCharsPS3 = 40;
  13. char receivedCharsPS3[numCharsPS3];   // Lijst om ontvangen Seriële data in op te slaan
  14.  
  15. boolean verbinding;
  16. boolean newDataPS3 = false;
  17.  
  18. TaskHandle_t Task1;
  19. uint8_t espClient;
  20.  
  21. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  22.     espClient = num;
  23.     switch(type) {
  24.         case WStype_DISCONNECTED:
  25.             verbinding = false;
  26.             Serial.printf("[%u] Verbinding verbroken!\n", num);
  27.             digitalWrite(2, LOW);
  28.             break;
  29.         case WStype_CONNECTED:
  30.             {
  31.                 verbinding = true;
  32.                 IPAddress ip = webSocket.remoteIP(num);
  33.                 Serial.printf("Connectie van %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
  34.                 webSocket.sendTXT(num, "Geconnecteerd!");
  35.                 digitalWrite(2, HIGH);                            
  36.             }
  37.             break;
  38.         case WStype_TEXT:
  39.             Serial.printf("[%u] Ontvangen tekst: %s\n", num, payload);
  40.             break;
  41.     }
  42. }
  43.  
  44. void setup() {
  45.   Serial.begin(115200);
  46.   Serial.println("Hoofd-serial started!");
  47.   Serial2.begin(115200, SERIAL_8N1, 16, 17);
  48.   Serial.println("Serial1 started!");
  49.   pinMode(2, OUTPUT);
  50.  
  51.   Serial.println("Ontwikkelen van kern 0 ...");
  52.   xTaskCreatePinnedToCore(
  53.                     Task1code,   /* Task function. */
  54.                     "Task1",     /* name of task. */
  55.                     10000,       /* Stack size of task */
  56.                     NULL,        /* parameter of the task */
  57.                     1,           /* priority of the task */
  58.                     &Task1,      /* Task handle to keep track of created task */
  59.                     0);    
  60.   Serial.println("Kern 0 succesvol ontwikkeld!");
  61. }
  62.  
  63. //Code in kern 0
  64. void Task1code( void * pvParameters ){
  65.   Serial.println("Opstarten van WebSocket-server ...");
  66.   WiFi.mode( WIFI_AP );
  67.   delay(200);
  68.   esp_wifi_set_protocol( WIFI_IF_AP, WIFI_PROTOCOL_LR );
  69.   delay(200);
  70.   WiFi.softAP(ssid, password);
  71.   delay(200);
  72.   Serial.println( WiFi.softAPIP() );
  73.   delay(500);
  74.  
  75.   webSocket.begin();
  76.   webSocket.onEvent(webSocketEvent);
  77.   Serial.println("WebSocket-server succesvol gestart!");//for debug
  78.   for(;;){
  79.     webSocket.loop();
  80.     vTaskDelay(1000 / 10);
  81.   }
  82. }
  83.  
  84. void loop() {
  85.     serialDataPS3();
  86.     shownewDataPS3();
  87. }
  88.  
  89. //Functies voor zo snel/efficient mogelijke Seriële communicatie. Bron: arduino.cc
  90. void serialDataPS3() {
  91.     static byte ndx = 0;
  92.     char endMarker = '\n';
  93.     char rc;    
  94.     while (Serial2.available() > 0 && newDataPS3 == false) {
  95.         rc = Serial2.read();
  96.         if (rc != endMarker) {
  97.             receivedCharsPS3[ndx] = rc;
  98.             ndx++;
  99.             if (ndx >= numCharsPS3) {
  100.                 ndx = numCharsPS3 - 1;
  101.             }
  102.         }
  103.         else {
  104.             receivedCharsPS3[ndx] = '\0'; // terminate the string
  105.             ndx = 0;
  106.             newDataPS3 = true;
  107.         }
  108.     }
  109. }
  110.  
  111. void shownewDataPS3() {
  112.     if (newDataPS3 == true) {
  113.         PS3Data();
  114.         newDataPS3 = false;
  115.     }
  116. }
  117.  
  118. void PS3Data(){
  119.   String text = receivedCharsPS3;
  120.   if (text.substring(0,4) == "PS3>") {
  121.     text.remove(0,4);
  122.     /*if (text.substring(0,7) == "MotorL>"){
  123.       text.remove(0,7);
  124.       Serial.println(text);  
  125.       webSocket.sendTXT(espClient, text);
  126.     }
  127.     else if (text.substring(0,7) == "MotorR>"){
  128.       text.remove(0,7);
  129.       Serial.println(text);  
  130.     }
  131.     else {
  132.       Serial.println(text);
  133.     }*/
  134.     webSocket.sendTXT(espClient, text);
  135.   }  
  136.   else {
  137.     Serial.println("Geen match!");
  138.     Serial.println(text.substring(0,4));
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement