Skip to content

Instantly share code, notes, and snippets.

@pipoblak
Created February 15, 2017 16:19
Show Gist options
  • Save pipoblak/a57a77fcdde09381113cebbd38dda154 to your computer and use it in GitHub Desktop.
Save pipoblak/a57a77fcdde09381113cebbd38dda154 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WebSocketsServer.h>
#include <Ultrasonic.h>
#define PIN_RODARIGHT D0
#define PIN_RODALEFT D1
//Define os pinos para o trigger e echo
#define pino_trigger D5
#define pino_echo D6
//Inicializa o sensor nos pinos definidos acima
Ultrasonic ultrasonic(pino_trigger, pino_echo);
//WEBSOCKET SERVER
WebSocketsServer webSocket = WebSocketsServer(81);
//Wifi Settings
const char* ssid = "SUAREDEWIFI";
const char* password = "SUASENHAWIFI";
//------------------------------------- WEBSOCKET EVENT ----------------------------- x x x x x ----------------------
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED: {
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// send message to client
webSocket.sendTXT(num, "Connected");
}
break;
case WStype_ERROR:{
break;
}
case WStype_TEXT: {
int Red = 0, Green = 0, Blue = 0;
int indexR, indexG, indexB, indexS, indexSize, indexD = 0;
String command = "", commandBalance = "";
int ID = 0;
//DETECT EVENT
if (payload[0] == '$') {
webSocket.sendTXT(num,"Carrinho");
}
command = (char*)payload;
commandBalance = "";
char char_array[lenght];
command.toCharArray(char_array, lenght + 1);
float cmMsec, inMsec;
//long microsec = ultrasonic.timing();
cmMsec=100;
//cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
String rightValue= command.substring(command.indexOf('R')+1,command.indexOf('L'));
String leftValue= command.substring(command.indexOf('L')+1);
Serial.println("RIGHT: " +rightValue + " LEFT: " + leftValue);
analogWrite(PIN_RODARIGHT,rightValue.toInt());
analogWrite(PIN_RODALEFT,leftValue.toInt());
if(cmMsec>15){
}
}
}
}
void setup() {
// put your setup code here, to run once:
pinMode(PIN_RODARIGHT,OUTPUT);
pinMode(PIN_RODALEFT,OUTPUT);
Serial.begin(115200);
//INITIALIZING WIFI
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
MDNS.addService("ws", "tcp", 81);
// start webSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment