├── README.md ├── LICENSE ├── esp8266_motion_server.ino └── esp8266_motion_client.ino /README.md: -------------------------------------------------------------------------------- 1 | # esp8266-wifi-direct-examples for Arduino IDE 2 | Server and client example to wifi control a Adafruit PCA9685 servo controller 3 | 4 | This will allow you to take advantage of the esp8266's WIFI direct feature, aka p2p feature. A client esp8266 connects to a server esp8266 with no access point inbetween, and establishes a TCP socket connection. Analog data is read from potentiometers wired to a 4051 multiplexer to the esp's single analog input. Data is sent as a casted array to the server. The speed is very fast, possibly up to HD video quality streaming. 5 | 6 | IMPORTANT NOTES: 7 | 8 | There is little information on the internet about how to p2p the esp8266. The majority of the issue is because the wifi mode on the client must be set to Wifi_STA and the server must be set to Wifi_AP. If Wifi_AP_STA or Wifi_STA is used for the server, the client will connect and send some little data, and then quickly disconnect. 9 | 10 | client.setNoDelay(1) must be used or transmission will be slow due to the Nagle algorithm. 11 | 12 | Any calls to server.available() will disconnect the current client. For multiple clients a array and queue can be used to keep previous connections alive (google examples). 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /esp8266_motion_server.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define LOWFREQ 110 6 | #define HIFREQ 460 7 | 8 | //for ledcWrite 9 | //RANGE OF JX 2060 IS 1800 TO 7800 10 | //RANGE OF MG90S IS 1750 TO 7550 11 | 12 | //for adafruit PWM 13 | //RANGE OF MG90S IS 100 TO 500 14 | //RANGE OF JX60 IS 110 TO 460 15 | 16 | const char* ssid = "espServer"; 17 | const char* password = "11111111"; 18 | uint8_t readin[16]; 19 | uint16_t pot_array[8]; 20 | 21 | Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40); 22 | uint8_t servonum = 0; // -1 correction 23 | 24 | int middle; 25 | 26 | WiFiServer server(5000); 27 | WiFiClient client; 28 | 29 | 30 | void setup() { 31 | Serial.begin(115200); 32 | Wire.begin(5,4); 33 | 34 | delay(10); 35 | 36 | WiFi.mode(WIFI_AP); 37 | WiFi.softAP(ssid, password); 38 | 39 | server.begin(); 40 | server.setNoDelay(1); 41 | IPAddress ServerIP = WiFi.softAPIP(); // Obtain the IP of the Server 42 | Serial.print("Server IP is: "); // Print the IP to the monitor window 43 | Serial.println(ServerIP); 44 | 45 | middle = LOWFREQ + ((HIFREQ - LOWFREQ) / 2); 46 | pwm.begin(); 47 | pwm.setPWMFreq(50); 48 | } 49 | 50 | void loop() { 51 | 52 | // Check if a client has connected 53 | 54 | if (!client.connected()) { 55 | //Serial.println("no client yet"); 56 | client = server.available(); 57 | return; 58 | } 59 | Serial.println("GOT CLIENT!!!!!!!"); 60 | client.setNoDelay(1); 61 | 62 | for (;;) 63 | { 64 | 65 | while (client.available()) 66 | { 67 | 68 | 69 | client.read((uint8_t*)pot_array, sizeof(pot_array)); 70 | 71 | for(int x = 0; x < 8; x++) 72 | { 73 | pot_array[x] = map(pot_array[x], 315, 1024, LOWFREQ, HIFREQ); //10bit mapping 74 | 75 | Serial.print(pot_array[x]); 76 | Serial.print(','); 77 | pwm.setPWM(x, 0, pot_array[x]); 78 | } 79 | Serial.println(" "); 80 | 81 | 82 | yield(); 83 | 84 | } 85 | 86 | 87 | if (!client.connected()) 88 | { 89 | Serial.println("client disconnected"); 90 | //close client and reconnect 91 | client.flush(); 92 | client.stop(); 93 | break; 94 | } 95 | } 96 | } 97 | 98 | 99 | -------------------------------------------------------------------------------- /esp8266_motion_client.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define Selector_0 16 5 | #define Selector_1 5 6 | #define Selector_2 4 7 | #define Enable_0 14 8 | #define Enable_1 12 9 | 10 | #define pot_limit 315 11 | #define timeout 10 12 | 13 | 14 | IPAddress serverIP(192, 168, 4, 1); 15 | 16 | 17 | //for ledcWrite 18 | //RANGE OF JX 2060 IS 1800 TO 7800 19 | //RANGE OF MG90S IS 1750 TO 7550 20 | 21 | //for adafruit PWM 22 | //RANGE OF MG90S IS 100 TO 500 23 | //RANGE OF JX60 IS 110 TO 460 24 | 25 | 26 | uint16_t pot_array[8]; 27 | 28 | void select_channel(int); 29 | WiFiClient client; 30 | 31 | 32 | void setup() { 33 | 34 | pinMode(Selector_0, OUTPUT); 35 | pinMode(Selector_1, OUTPUT); 36 | pinMode(Selector_2, OUTPUT); 37 | pinMode(Enable_0, OUTPUT); 38 | pinMode(Enable_1, OUTPUT); 39 | 40 | Serial.begin(115200); 41 | 42 | client.setNoDelay(1); 43 | 44 | const char *ssid = "espServer"; 45 | const char *password = "11111111"; 46 | 47 | WiFi.mode(WIFI_STA); 48 | WiFi.begin(ssid, password); 49 | 50 | Serial.print("\nConnecting to "); 51 | Serial.println(ssid); 52 | 53 | while ( WiFi.status() != WL_CONNECTED) { 54 | delay(500); 55 | Serial.println(WiFi.status()); 56 | } 57 | 58 | Serial.println("WIFI connected"); 59 | Serial.println(WiFi.localIP()); // get interface IP address mask 60 | 61 | Serial.println(client.connect(serverIP, 5000)); 62 | } 63 | 64 | 65 | void loop() { 66 | 67 | if (client.connected()) 68 | { 69 | Serial.println("Connection success!"); 70 | } 71 | else { 72 | Serial.println("oops"); 73 | client.connect(serverIP, 5000); 74 | delay(50); 75 | } 76 | 77 | for (;;) 78 | { 79 | for (int channel = 0; channel < 8; channel++) 80 | { 81 | select_channel(channel); 82 | pot_array[channel] = analogRead(0); 83 | 84 | if (pot_array[channel] < pot_limit) { 85 | pot_array[channel] = pot_limit; 86 | } 87 | 88 | yield(); 89 | 90 | } 91 | 92 | client.write_P((const char*)pot_array, sizeof(pot_array)); 93 | delay(timeout); 94 | 95 | 96 | if(!client.connected()) 97 | { 98 | Serial.println("connection reset"); 99 | client.connect(serverIP, 5000); 100 | break; 101 | } 102 | 103 | // yield(); 104 | } 105 | 106 | } 107 | void select_channel(int channel_num) 108 | { 109 | if (channel_num < 8) 110 | { 111 | digitalWrite(Enable_0, 0); 112 | digitalWrite(Enable_1, 1); 113 | } 114 | else { 115 | digitalWrite(Enable_0, 1); 116 | digitalWrite(Enable_1, 0); 117 | } 118 | 119 | switch (channel_num) { 120 | 121 | case 0: 122 | digitalWrite(Selector_0, 0); 123 | digitalWrite(Selector_1, 0); 124 | digitalWrite(Selector_2, 0); 125 | break; 126 | 127 | case 1: 128 | digitalWrite(Selector_0, 1); 129 | digitalWrite(Selector_1, 0); 130 | digitalWrite(Selector_2, 0); 131 | break; 132 | 133 | 134 | case 2: 135 | digitalWrite(Selector_0, 0); 136 | digitalWrite(Selector_1, 1); 137 | digitalWrite(Selector_2, 0); 138 | break; 139 | 140 | 141 | case 3: 142 | digitalWrite(Selector_0, 1); 143 | digitalWrite(Selector_1, 1); 144 | digitalWrite(Selector_2, 0); 145 | break; 146 | 147 | 148 | case 4: 149 | digitalWrite(Selector_0, 0); 150 | digitalWrite(Selector_1, 0); 151 | digitalWrite(Selector_2, 1); 152 | break; 153 | 154 | 155 | case 5: 156 | digitalWrite(Selector_0, 1); 157 | digitalWrite(Selector_1, 0); 158 | digitalWrite(Selector_2, 1); 159 | break; 160 | 161 | 162 | case 6: 163 | digitalWrite(Selector_0, 0); 164 | digitalWrite(Selector_1, 1); 165 | digitalWrite(Selector_2, 1); 166 | break; 167 | 168 | 169 | case 7: 170 | digitalWrite(Selector_0, 1); 171 | digitalWrite(Selector_1, 1); 172 | digitalWrite(Selector_2, 1); 173 | break; 174 | } 175 | } 176 | --------------------------------------------------------------------------------