Skip to content

Commit

Permalink
commited by Debasish Goswami
Browse files Browse the repository at this point in the history
  • Loading branch information
DebaDr0na committed Mar 14, 2024
1 parent 8b27afd commit a3c2913
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 13 deletions.
88 changes: 80 additions & 8 deletions examples/14_blite_rc_car/14_blite_rc_car.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,88 @@
#include <blite.h>
#include "blite.h"
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include "remote.h"

Blite myBot;
#define CMD_STOP 0
#define CMD_FORWARD 1
#define CMD_BACKWARD 2
#define CMD_LEFT 4
#define CMD_RIGHT 8
#define CMD_PUSH 5
#define CMD_SRVCLCK 6
#define CMD_SRVACLCK 9


Blite mybot;
ESP8266WebServer wifiRemoteControl(80);
WebSocketsServer webSocket = WebSocketsServer(81); // WebSocket server on port 81

void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
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\n", num, ip[0], ip[1], ip[2], ip[3]);
}
break;
case WStype_TEXT:
//Serial.printf("[%u] Received text: %s\n", num, payload);
String angle = String((char*)payload);
int command = angle.toInt();
Serial.print("command: ");
Serial.println(command);

switch (command) {
case CMD_STOP:
Serial.println("Stop");
// mybot.stop();
break;
case CMD_FORWARD:
Serial.println("Move Forward");
mybot.moveForward();
break;
case CMD_BACKWARD:
Serial.println("Move Backward");
mybot.moveBackward();
break;
case CMD_LEFT:
Serial.println("Turn Left");
mybot.turnLeft();
break;
case CMD_RIGHT:
Serial.println("Turn Right");
mybot.turnRight();
break;
case CMD_PUSH:
Serial.println("Push button pressed");
//mybot.push();
break;
case CMD_SRVCLCK:
Serial.println("Turn servo clockwise");
//mybot.turnServoClockwise();
break;
case CMD_SRVACLCK:
Serial.println("Turn servo anti-clockwise");
//mybot.turnServoAntiClockwise();
break;
default:
Serial.println("Unknown command");
}

break;
}
}


void setup(){
myBot.setup();
mybot.setup();
Serial.begin(115200);
myBot.smartConnectWiFi();
mybot.smartConnectWiFi();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
wifiRemoteControl.on("/", HTTP_GET, []() {
Serial.println("Web Server: received a web page request");
String html = REMOTE_HTML_CONTENT;
Expand All @@ -17,8 +92,5 @@ void setup(){
}
void loop(){
wifiRemoteControl.handleClient();
if (myBot.buttonPressed()){
myBot.blinkLed(2);
}
webSocket.loop();
}

133 changes: 133 additions & 0 deletions examples/14_blite_rc_car/blite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "blite.h"

int Blite::getIO(const char * io){
if (io == "io1") {
return IO1;
} else if (io =="io2") {
return IO2;
} else if (io == "scl") {
return I2C_SCL;
} else if (io == "sda"){
return I2C_SDA;
}
return -1;

}
void Blite::reversePolarityM12(){
this->defineM12(false);
}
void Blite::reversePolarityM34(){
this->defineM34(false);
}
void Blite::moveForward(){
analogWrite(m1,speed);
digitalWrite(m2,LOW);
analogWrite(m4,speed);
digitalWrite(m3,LOW);
}
void Blite::moveBackward(){
analogWrite(m2,speed);
digitalWrite(m1,LOW);
analogWrite(m3,speed);
digitalWrite(m4,LOW);
}
void Blite::turnRight(){
digitalWrite(m1,LOW);
digitalWrite(m2,LOW);
analogWrite(m4,speed);
digitalWrite(m3,LOW);
}
void Blite::turnLeft(){
analogWrite(m1,speed);
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,LOW);
}
void Blite::setSpeed(int s){
this->speed = s ;
}
bool Blite::connectWiFi(const char *username, const char *password){
WiFi.disconnect();
WiFi.mode(WIFI_STA);
int retry = 0;
while (retry <= 20) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Trying to connect to wifi");
WiFi.begin(username,password);
WiFi.waitForConnectResult();
delay(1000);
} else {
Serial.println("connected to wifi");
Serial.println(WiFi.localIP());
return 1;
}
retry++;
}
return 0;
}
bool Blite::smartConnectWiFi(){
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFiManager wm;
bool res;
res = wm.autoConnect("Buildybee-smart-config","buildybee"); // password protected ap
return res;
}

bool Blite::APServer() {
if (WiFi.isConnected()){
if (WiFi.disconnect()){
return 0;
}
}
const char* ssid = "buidybee_rc_car";
IPAddress local_IP(192, 168, 4, 1);
// We set a Gateway IP address
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
// Connecting WiFi
WiFi.softAPConfig(local_IP,gateway,subnet);
WiFi.mode(WIFI_AP);
return WiFi.softAP(ssid);
}
bool Blite::buttonPressed() {
return !digitalRead(SW1);
}

void Blite::setup(){
pinMode(SW1,INPUT_PULLUP);
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);
pinMode(M3,OUTPUT);
pinMode(M4,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
this->defineM12(true);
this->defineM34(true);
WiFi.disconnect();
WiFi.mode(WIFI_OFF);


}

void Blite::glowLed(bool s){
if (s){
digitalWrite(LED_BUILTIN, LOW);
} else{
digitalWrite(LED_BUILTIN, HIGH);
}

}

void Blite::blinkLed(int c){
for (int i=0;i<c;i++){
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(500);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(500);
}
}

int Blite::readADC(){
return analogRead(ADC1);
}
67 changes: 67 additions & 0 deletions examples/14_blite_rc_car/blite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef BLITE_H_
#define BLITE_H_

#if defined(ESP8266)

#define IO1 12
#define IO2 13
#define SW1 16

#define M1 0
#define M2 2
#define M3 14
#define M4 15

#define I2C_SCL 5
#define I2C_SDA 4

#define ADC1 A0

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

class Blite {
public:
void setup();
bool APServer();
bool connectWiFi(const char * username, const char * password);
bool smartConnectWiFi();
void moveForward();
void moveBackward();
void turnRight();
void turnLeft();
void setSpeed(int speed);

void reversePolarityM12();
void reversePolarityM34();

int getIO(const char * io);
bool buttonPressed();
void glowLed(bool s);
void blinkLed(int c);
int readADC();

private:
int m1,m2,m3,m4,speed;
void defineM12(bool polarity){
if (polarity){
this->m1 = M1;
this->m2 = M2;
} else {
this->m2 = M1;
this->m1 = M2;
}
};
void defineM34(bool polarity){
if (polarity){
this->m3 = M3;
this->m4 = M4;
} else {
this->m4 = M3;
this->m3 = M4;
}
};
};
#endif
#endif
Loading

0 comments on commit a3c2913

Please sign in to comment.