Created
March 28, 2020 14:48
-
-
Save Hermann-SW/afeb8ba69ae290178b61f957e7048f87 to your computer and use it in GitHub Desktop.
Wifi Joystick ESP32 Arduino sketch (https://esp32.com/viewtopic.php?f=19&t=14803&p=57338#p57338)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include "SSD1306Wire.h" | |
SSD1306Wire display(0x3c, 5, 4); | |
#include "WiFi.h" | |
#include <WiFiMulti.h> | |
WiFiMulti WiFiMulti; | |
WiFiClient client; | |
const char * ssid = "AndroidAPhsw"; | |
const char * password = "verySecret"; | |
void draw(const String &s) { | |
display.clear(); | |
display.drawStringMaxWidth(0, 0, 128, s); | |
display.display(); | |
} | |
void setup() { | |
display.init(); | |
display.flipScreenVertically(); | |
display.setTextAlignment(TEXT_ALIGN_LEFT); | |
display.setFont(ArialMT_Plain_16); | |
delay(10); | |
WiFiMulti.addAP(ssid, password); | |
String line="Waiting for WiFi... "; | |
draw(line); | |
while(WiFiMulti.run() != WL_CONNECTED) { | |
line += "."; | |
draw(line); | |
delay(500); | |
} | |
draw(String(WiFi.localIP())); | |
delay(500); | |
const uint16_t port = 1337; | |
const char * host = "192.168.43.175"; | |
draw("Connecting to "+String(host)+":"+String(port)); | |
if (!client.connect(host, port)) { | |
draw("Connection failed."); | |
if (!client.connect(host, port+1)) { | |
draw("Connection failed."); | |
for(;;) {} | |
} | |
} | |
draw("connected"); | |
delay(1000); | |
while (digitalRead(16) == 0) delay(100); | |
char buf[1]; | |
client.write(buf[0]); | |
} | |
void loop() { | |
if (digitalRead(16) == 0) | |
{ | |
draw("emergency stop triggered"); | |
for(;;) {} | |
} | |
unsigned char buf[2] = { | |
map(analogRead(36),0,4095,0,255), | |
map(analogRead(39),0,4095,0,255) | |
}; | |
client.write(buf, 2); | |
draw(String(buf[0])+" : "+String(buf[1])); | |
delay(5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment