Skip to content

Instantly share code, notes, and snippets.

@mithi
Created December 27, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithi/4dc7dee49a5129489c516f3f675fba2e to your computer and use it in GitHub Desktop.
Save mithi/4dc7dee49a5129489c516f3f675fba2e to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// frequency for servo drivers
int FREQ = 150;
Adafruit_PWMServoDriver pwmTop = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwmBottom = Adafruit_PWMServoDriver(0x41);
// Load Wi-Fi library
#include <ESP8266WiFi.h>
// Replace with your network credentials
const char* ssid = "";
const char* password = "";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("PWM DRIVER HERE");
pwmTop.begin();
pwmBottom.begin();
pwmTop.setOscillatorFrequency(27000000);
pwmBottom.setOscillatorFrequency(27000000);
pwmTop.setPWMFreq(FREQ);
pwmBottom.setPWMFreq(FREQ);
// if you want to really speed stuff up, you can go into 'fast 400khz I2C' mode
// some i2c devices dont like this so much so if you're sharing the bus, watch
// out for this!
Wire.setClock(400000);
}
void tick(Adafruit_PWMServoDriver pwm, int servo_id, int value, int myDelay) {
pwm.setPWM(servo_id, 0, value);
// take a breather, required for ESP8266
#ifdef ESP8266
yield();
#endif
delay(myDelay);
}
void doCommand(int id, int pulse) {
if (id >= 16) {
tick(pwmTop, id - 16, pulse, 2000);
tick(pwmTop, id - 16, 0, 1000);
} else {
tick(pwmBottom, id, pulse, 2000);
tick(pwmBottom, id, 0, 1000);
}
}
void loop(){
// Listen for incoming clients
WiFiClient client = server.available();
// If a new client connects,
if (client) {
// print a message out in the serial port
Serial.println("New Client.");
// make a String to hold incoming data from the client
String currentLine = "";
currentTime = millis();
previousTime = currentTime;
// loop while the client's connected
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
// if there's bytes to read from the client,
if (client.available()) {
// read a byte, then
char c = client.read();
// print it out the serial monitor
Serial.write(c);
header += c;
// if the byte is a newline character
if (c == '\n') {
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json;charset=utf-8");
client.println("Server: Arduino");
client.println("Connnection: close");
client.println();
int servoId = -1;
int pulse = 0;
// http://192.168.254.116/x/09/0210
// {"status": {"servoId": 9, "pulse": 210 }}
int index = header.indexOf("GET /x/");
if (index >= 0) {
Serial.println("............");
Serial.print("servoId: ");
String servoIdstring = String(String(header[7]) + String(header[8]));
servoId = servoIdstring.toInt();
Serial.println(servoId);
Serial.print("pulse: ");
String pulseString = String(String(header[10]) + String(header[11]) + String(header[12]) + String(header[13]));
pulse = pulseString.toInt();
Serial.println(pulse);
Serial.println("............");
doCommand(servoId, pulse);
}
client.print("{\"status\": {\"servoId\": ");
client.print(servoId);
client.print(", \"pulse\": ");
client.print(pulse);
client.print(" }}");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else {
// if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') {
// if you got anything else but a carriage return character,
// add it to the end of the currentLine
currentLine += c;
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment