diff --git a/.github/workflows/arduino-lint.yml b/.github/workflows/arduino-lint.yml index 258ac58..63b60dd 100644 --- a/.github/workflows/arduino-lint.yml +++ b/.github/workflows/arduino-lint.yml @@ -4,4 +4,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: arduino/arduino-lint-action@v1 \ No newline at end of file + - uses: arduino/arduino-lint-action@v1 + with: + library-manager: update \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d41fe2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +src/.* +.vscode/** \ No newline at end of file diff --git a/examples/08_distance_measuring/08_distance_measuring.ino b/examples/01_ultrasonic_distance_measuring/01_ultrasonic_distance_measuring.ino similarity index 80% rename from examples/08_distance_measuring/08_distance_measuring.ino rename to examples/01_ultrasonic_distance_measuring/01_ultrasonic_distance_measuring.ino index 31b76bb..9d7ebc8 100644 --- a/examples/08_distance_measuring/08_distance_measuring.ino +++ b/examples/01_ultrasonic_distance_measuring/01_ultrasonic_distance_measuring.ino @@ -1,23 +1,29 @@ -#include "blite.h" +#include + +//connect the ultrasonic sensor(HS-SR04) with 4 pin sensor //define sound velocity in cm/uS #define SOUND_VELOCITY 0.034 #define CM_TO_INCH 0.393701 +Blite myBot; +int trigPin = myBot.getIO("io3"); +int echoPin = myBot.getIO("io4"); + long duration; float distanceCm; float distanceInch; -#define echoPin D1 -#define trigPin D2 - void setup() { + myBot.setup(); + myBot.smartConnectWiFi(); Serial.begin(115200); // Starts the serial communication pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input } void loop() { + myBot.otaLoop(); // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); diff --git a/examples/01_wifi_controlled_bot/01_wifi_controlled_bot.ino b/examples/01_wifi_controlled_bot/01_wifi_controlled_bot.ino deleted file mode 100644 index 586a15c..0000000 --- a/examples/01_wifi_controlled_bot/01_wifi_controlled_bot.ino +++ /dev/null @@ -1,178 +0,0 @@ -#include -#include -#include -// Motor pins(2 X 2-pin connector) connected to DRR8833 -#define m1_pin1 0 -#define m1_pin2 2 -#define m2_pin1 14 -#define m2_pin2 15 - -// programmable I/O (2 X 3-pin connector) 5v bi-directional level shifted driving at BAT level -#define io1 12 -#define io2 13 - -// Analog input pin (1 X 3-pin connector) driving at 3.3V -#define analog_sensor A0 - -// I2C/HC-SR04 pin (1X 4-pin connector) driving at BAT level -#define i2c_tx 4 -#define i2c_rx 5 - - -String command; //String to store app command state. -int speedCar = 800; // 400 - 1023. -int speed_Coeff = 3; -int wither = 0; -const int IN_1 = m1_pin2; -const int IN_2 = m1_pin1; -const int IN_3 = m2_pin2; -const int IN_4 = m2_pin1; - -const char* ssid = "buidybee_rc_car"; -ESP8266WebServer server(80); - -void setup() { - pinMode(IN_1, OUTPUT); - pinMode(IN_2, OUTPUT); - pinMode(IN_3, OUTPUT); - pinMode(IN_4, OUTPUT); - - Serial.begin(115200); - // We set a Static IP address - 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); - WiFi.softAP(ssid); - - IPAddress myIP = WiFi.softAPIP(); - // should print the same ip set above - Serial.print("AP IP address: "); - Serial.println(myIP); - - // Starting WEB-server - server.on ( "move", HTTP_handleRoot ); - server.onNotFound ( HTTP_handleRoot ); - server.begin(); -} - -void goAhead(){ - - digitalWrite(IN_1, LOW); - analogWrite(IN_2, speedCar); - - digitalWrite(IN_3, LOW); - analogWrite(IN_4, speedCar); - - } - -void goBack(){ - - analogWrite(IN_1, speedCar); - digitalWrite(IN_2, LOW); - - analogWrite(IN_3, speedCar); - digitalWrite(IN_4, LOW); - } - -void goRight(){ - - analogWrite(IN_1, speedCar); - digitalWrite(IN_2, LOW); - - digitalWrite(IN_3, LOW); - analogWrite(IN_4, speedCar); - } - -void goLeft(){ - - digitalWrite(IN_1, LOW); - analogWrite(IN_2, speedCar); - - analogWrite(IN_3, speedCar); - digitalWrite(IN_4, LOW); - } - -void goAheadRight(){ - - digitalWrite(IN_1, LOW); - analogWrite(IN_2, speedCar/speed_Coeff); - - digitalWrite(IN_3, LOW); - analogWrite(IN_4, speedCar); - } - -void goAheadLeft(){ - - digitalWrite(IN_1, LOW); - analogWrite(IN_2, speedCar); - - digitalWrite(IN_3, LOW); - analogWrite(IN_4, speedCar/speed_Coeff); - } - -void goBackRight(){ - - analogWrite(IN_1, speedCar/speed_Coeff); - digitalWrite(IN_2, LOW); - - analogWrite(IN_3, speedCar); - digitalWrite(IN_4, LOW); - } - -void goBackLeft(){ - - analogWrite(IN_1, speedCar); - digitalWrite(IN_2, LOW); - - analogWrite(IN_3, speedCar/speed_Coeff); - digitalWrite(IN_4, LOW); - } - -void stopRobot(){ - - digitalWrite(IN_1, LOW); - digitalWrite(IN_2, LOW); - - digitalWrite(IN_3, LOW); - digitalWrite(IN_4, LOW); - } - -void loop() { - server.handleClient(); - - command = server.arg("dir"); - if (command == "F") goAhead(); - else if (command == "B") goBack(); - else if (command == "L") goLeft(); - else if (command == "R") goRight(); - else if (command == "I") goAheadRight(); - else if (command == "G") goAheadLeft(); - else if (command == "J") goBackRight(); - else if (command == "H") goBackLeft(); - else if (command == "0") speedCar = 400; - else if (command == "1") speedCar = 470; - else if (command == "2") speedCar = 540; - else if (command == "3") speedCar = 610; - else if (command == "4") speedCar = 680; - else if (command == "5") speedCar = 750; - else if (command == "6") speedCar = 820; - else if (command == "7") speedCar = 890; - else if (command == "8") speedCar = 960; - else if (command == "9") speedCar = 1023; - else if (command == "S") stopRobot(); - -} - -void HTTP_handleRoot(void) { - -if( server.hasArg("dir") ){ - Serial.println(server.arg("dir")); - server.send ( 200, "text/html", "" ); - wither = 0; - } - delay(1); -} diff --git a/examples/02_auto_obstruction_detection/readme.md b/examples/02_auto_obstruction_detection/readme.md deleted file mode 100644 index 994d622..0000000 --- a/examples/02_auto_obstruction_detection/readme.md +++ /dev/null @@ -1,18 +0,0 @@ -The bot will detect any obstruction and stop without even crashing into it -To enable the kit add the below code on top: - -``` -#define m1_pin1 2; -#define m1_pin2 10; -#define m2_pin1 0; -#define m2_pin2 14; - -#define io1 12; -#define io2 13; - -#define analog_sensor A0; - -#define i2c_tx 4; -#define i2c_rx 5; - -``` \ No newline at end of file diff --git a/examples/02_single_ir_line_follower/02_single_ir_line_follower.ino b/examples/02_single_ir_line_follower/02_single_ir_line_follower.ino new file mode 100644 index 0000000..325bfca --- /dev/null +++ b/examples/02_single_ir_line_follower/02_single_ir_line_follower.ino @@ -0,0 +1,44 @@ +//Connect a IR sensor or TCTR5000 module to IO1 and place the car on a black track with white background ground. +//press the tactile button to start the line follower mode and press gain to stop line following mode. + +#include +Blite myBot; +int ir = myBot.getIO("io1"); +bool lineFollowerMode = false; +int cs; + +void setup() +{ + // Debug console + Serial.begin(115200); + myBot.setup(); + myBot.reversePolarityM12(); + myBot.smartConnectWiFi(); + +} + +void loop() +{ + myBot.otaLoop(); + + if(myBot.buttonPressed()) { + lineFollowerMode = !lineFollowerMode; + } + if (lineFollowerMode){ + cs=digitalRead(ir); + + if(cs==HIGH) + { + myBot.moveForward(); + } + else if(cs==LOW) + { + myBot.turnRight(); + } + if (myBot.buttonPressed()){ + lineFollowerMode = false; + myBot.stopMotor(); + } + } + +} \ No newline at end of file diff --git a/examples/03_thermistor/03_thermistor.ino b/examples/03_thermistor/03_thermistor.ino deleted file mode 100644 index 010d0cd..0000000 --- a/examples/03_thermistor/03_thermistor.ino +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -#define ONE_WIRE_BUS 10 //SD3 pin of nodemcu - -OneWire oneWire(ONE_WIRE_BUS); - -DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. - -void setup(void) -{ - Serial.begin(115200); - sensors.begin(); -} - -void loop(void) -{ - sensors.requestTemperatures(); // Send the command to get temperatures - Serial.println("Temperature is: "); - Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire - delay(500); -} diff --git a/examples/04_IoT_neo_pixel/04_IoT_neo_pixel.ino b/examples/04_IoT_neo_pixel/04_IoT_neo_pixel.ino new file mode 100644 index 0000000..ec83519 --- /dev/null +++ b/examples/04_IoT_neo_pixel/04_IoT_neo_pixel.ino @@ -0,0 +1,70 @@ +#include +#include "lighting.h" +#include + +//install "Adafruit Neopixel" from arduino library manager +#include + +//number of rgb leds present in the device +#define NUM_LEDS 16 + +Blite myBot; +Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, myBot.getIO("io1"), NEO_RGB + NEO_KHZ800); +WebSocketsServer webSocket = WebSocketsServer(81); + +void setup(){ + myBot.setup(); + myBot.reversePolarityM12(); + Serial.begin(115200); + delay(1000); + if (myBot.buttonPressed()){ + myBot.APServer(); + } else { + myBot.smartConnectWiFi(); + } + webSocket.begin(); + webSocket.onEvent(webSocketEvent); + strip.setBrightness(150); + strip.begin(); +} +void loop(){ + String html = HTML_LIGHT; + myBot.smartRenderServer(html); + webSocket.loop(); +} + +void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) { + String command = String((char*)payload); + Serial.print("command: "); + Serial.println(command); + // setting the color to the strip + setNeoColor(command); +} + +void setNeoColor(String value){ + Serial.print("Setting Neopixel..."); + // converting Hex to Int + int number = (int) strtol( &value[1], NULL, 16); + // splitting into three parts + int r = number >> 16; + int g = number >> 8 & 0xFF; + int b = number & 0xFF; + + // DEBUG + Serial.print("RGB: "); + Serial.print(r, DEC); + Serial.print(" "); + Serial.print(g, DEC); + Serial.print(" "); + Serial.print(b, DEC); + Serial.println(" "); + + // setting whole strip to the given color + for(int i=0; i < NUM_LEDS; i++) { + strip.setPixelColor(i, strip.Color( g, r, b ) ); + } + // init + strip.show(); + + Serial.println("on."); +} diff --git a/examples/04_IoT_neo_pixel/lighting.h b/examples/04_IoT_neo_pixel/lighting.h new file mode 100644 index 0000000..a3a3765 --- /dev/null +++ b/examples/04_IoT_neo_pixel/lighting.h @@ -0,0 +1,75 @@ +const char *HTML_LIGHT = R"=====( + + + + wifi pixel + + + + + +

Buildybee

+

pick the color!

+

+   +

+

+ WebSocket : closed +
+

+ + +)====="; \ No newline at end of file diff --git a/examples/05_IoT_beebot_rc_car/05_IoT_beebot_rc_car.ino b/examples/05_IoT_beebot_rc_car/05_IoT_beebot_rc_car.ino new file mode 100644 index 0000000..9a147ab --- /dev/null +++ b/examples/05_IoT_beebot_rc_car/05_IoT_beebot_rc_car.ino @@ -0,0 +1,80 @@ +//connect the blite dev board with b-rover: https://buildybee.github.io/beebotFullKit.html +#include +#include "remote.h" +#include + +#define CMD_STOP 0 +#define CMD_FORWARD 1 +#define CMD_BACKWARD 2 +#define CMD_LEFT 4 +#define CMD_RIGHT 8 + +Blite myBot; +WebSocketsServer webSocket = WebSocketsServer(81); + +void setup(){ + myBot.setup(); + myBot.reversePolarityM12(); + Serial.begin(115200); + delay(1000); + if (myBot.buttonPressed()){ + myBot.APServer(); + } else { + myBot.smartConnectWiFi(); + } + webSocket.begin(); + webSocket.onEvent(webSocketEvent); + +} +void loop(){ + String html = REMOTE_HTML_CONTENT; + myBot.smartRenderServer(html); + webSocket.loop(); +} + +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.stopMotor(); + 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; + default: + Serial.println("Unknown command"); + } + + break; + } +} \ No newline at end of file diff --git a/examples/14_blite_rc_car/remote.h b/examples/05_IoT_beebot_rc_car/remote.h similarity index 53% rename from examples/14_blite_rc_car/remote.h rename to examples/05_IoT_beebot_rc_car/remote.h index 149a560..bccb1ff 100644 --- a/examples/14_blite_rc_car/remote.h +++ b/examples/05_IoT_beebot_rc_car/remote.h @@ -8,57 +8,110 @@ const char *REMOTE_HTML_CONTENT = R"=====( - - -

ESP8266 - RC Car via Web

-
-
-
-
-
-
-
-

-WebSocket : closed
-

- -
-
- - - -)====="; \ No newline at end of file diff --git a/examples/13_rgb_web_control/13_rgb_web_control.ino b/examples/13_rgb_web_control/13_rgb_web_control.ino deleted file mode 100644 index 94897cd..0000000 --- a/examples/13_rgb_web_control/13_rgb_web_control.ino +++ /dev/null @@ -1,170 +0,0 @@ -#include -#include -#include -#include -#include - -// Webserver Config -const char *ssid = "ZTE_2.4G_6eZrkS"; -const char *password = "ijF3kXh4"; -ESP8266WebServer server ( 80 ); - -// Neopixel Config -#define NeoPIN D4 -#define NUM_LEDS 16 -int brightness = 150; -Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800); - - -const int led = 13; - -void setup ( void ) { - - Serial.begin ( 115200 ); - - // ############## - // NeoPixel start - Serial.println(); - strip.setBrightness(brightness); - strip.begin(); - strip.show(); - delay(50); - Serial.println("NeoPixel started"); - - // ######### - // Webserver - pinMode ( led, OUTPUT ); - digitalWrite ( led, 0 ); - - WiFi.begin ( ssid, password ); - Serial.println ( "" ); - - // Wait for connection - while ( WiFi.status() != WL_CONNECTED ) { - delay ( 500 ); - Serial.print ( "." ); - } - - Serial.println ( "" ); - Serial.print ( "Connected to " ); - Serial.println ( ssid ); - Serial.print ( "IP address: " ); - Serial.println ( WiFi.localIP() ); - - if ( MDNS.begin ( "esp8266" ) ) { - Serial.println ( "MDNS responder started" ); - } - - // what to do with requests - server.on ( "/", handleRoot ); - server.onNotFound ( handleNotFound ); - server.begin(); - - - - Serial.println ( "HTTP server started" ); -} - -void loop ( void ) { - // waiting fo a client - server.handleClient(); -} - - -void handleRoot() { - Serial.println("Client connected"); - digitalWrite ( led, 1 ); - - // data from the colorpicker (e.g. #FF00FF) - String color = server.arg("c"); - Serial.println("Color: " + color); - // setting the color to the strip - setNeoColor(color); - - // building a website - char temp[5000]; - int sec = millis() / 1000; - int min = sec / 60; - int hr = min / 60; - char clr [7]; - color.toCharArray(clr, 7); - snprintf ( temp, 5000, - -"\n\n\ - \n\ - wifi pixel\n\ - \n\ - \n\ - \n\ - \n\ -

Buildybee

\n\ -

pick the color!

\n\ - \n\ -
\n\ - \n\ -   \n\ -
\n\ - \n\ - \ -", - - hr, min % 60, sec % 60, clr - ); - server.send ( 200, "text/html", temp ); - digitalWrite ( led, 0 ); -} - -void handleNotFound() { - digitalWrite ( led, 1 ); - String message = "File Not Found\n\n"; - message += "URI: "; - message += server.uri(); - message += "\nMethod: "; - message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; - message += "\nArguments: "; - message += server.args(); - message += "\n"; - - for ( uint8_t i = 0; i < server.args(); i++ ) { - message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; - } - - server.send ( 404, "text/plain", message ); - digitalWrite ( led, 0 ); -} - - - -void setNeoColor(String value){ - Serial.print("Setting Neopixel..."); - // converting Hex to Int - int number = (int) strtol( &value[1], NULL, 16); - - // splitting into three parts - int r = number >> 16; - int g = number >> 8 & 0xFF; - int b = number & 0xFF; - - // DEBUG - Serial.print("RGB: "); - Serial.print(r, DEC); - Serial.print(" "); - Serial.print(g, DEC); - Serial.print(" "); - Serial.print(b, DEC); - Serial.println(" "); - - // setting whole strip to the given color - for(int i=0; i < NUM_LEDS; i++) { - strip.setPixelColor(i, strip.Color( g, r, b ) ); - } - // init - strip.show(); - - Serial.println("on."); -} \ No newline at end of file diff --git a/examples/14_blite_rc_car/14_blite_rc_car.ino b/examples/14_blite_rc_car/14_blite_rc_car.ino deleted file mode 100644 index 8c70a84..0000000 --- a/examples/14_blite_rc_car/14_blite_rc_car.ino +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include "remote.h" - -Blite myBot; -ESP8266WebServer wifiRemoteControl(80); - -void setup(){ - myBot.setup(); - Serial.begin(115200); - myBot.smartConnectWiFi(); - wifiRemoteControl.on("/", HTTP_GET, []() { - Serial.println("Web Server: received a web page request"); - String html = REMOTE_HTML_CONTENT; - wifiRemoteControl.send(200, "text/html", html); - }); - wifiRemoteControl.begin(); -} -void loop(){ - wifiRemoteControl.handleClient(); - if (myBot.buttonPressed()){ - myBot.blinkLed(2); - } -} - diff --git a/extras/blite_schematics.pdf b/extras/blite_schematics.pdf new file mode 100644 index 0000000..29fa39d Binary files /dev/null and b/extras/blite_schematics.pdf differ diff --git a/extras/test.html b/extras/test.html deleted file mode 100644 index 498b247..0000000 --- a/extras/test.html +++ /dev/null @@ -1,134 +0,0 @@ - - - -Blite RC car control - - - - - -

Blite - RC car control

-
-
-
-
-
-
-
-
-
- - - \ No newline at end of file diff --git a/library.properties b/library.properties index ae611e8..d9bc934 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=blite -version=0.1.0 -author=Sayan Paul -maintainer=Sayan Paul +version=0.1.1 +author=Buildybee +maintainer=Sayan Paul Debasish Goswami sentence=Dev kit for buildybee blite breakout board paragraph=Develop easily with buildybee devikits url=https://github.com/buildybee/blite.git architectures=esp8266 -depends=WiFiManager \ No newline at end of file +depends=WiFiManager,WebSockets,ESP8266 and ESP32 OLED driver for SSD1306 displays,Adafruit NeoPixel \ No newline at end of file diff --git a/src/blite.cpp b/src/blite.cpp index 8a9d72c..e96f0cd 100644 --- a/src/blite.cpp +++ b/src/blite.cpp @@ -9,43 +9,73 @@ int Blite::getIO(const char * io){ return I2C_SCL; } else if (io == "sda"){ return I2C_SDA; + } else if (io == "io3") { + return I2C_SCL; + } else if (io == "io4"){ + return I2C_SDA; } return -1; } + void Blite::reversePolarityM12(){ this->defineM12(false); } void Blite::reversePolarityM34(){ this->defineM34(false); } + +void Blite::turnM12(bool direction){ + if (direction) { + analogWrite(this->m1,this->speed); + digitalWrite(this->m2,LOW); + } else { + analogWrite(this->m2,this->speed); + digitalWrite(this->m1,LOW); + } +} +void Blite::stopM12(){ + digitalWrite(this->m1,LOW); + digitalWrite(this->m2,LOW); +} +void Blite::turnM34(bool direction){ + if (direction) { + analogWrite(this->m3,this->speed); + digitalWrite(this->m4,LOW); + } else { + analogWrite(this->m4,this->speed); + digitalWrite(this->m3,LOW); + } +} +void Blite::stopM34(){ + digitalWrite(this->m3,LOW); + digitalWrite(this->m4,LOW); +} + void Blite::moveForward(){ - analogWrite(m1,speed); - digitalWrite(m2,LOW); - analogWrite(m4,speed); - digitalWrite(m3,LOW); + this->turnM12(true); + this->turnM34(false); } void Blite::moveBackward(){ - analogWrite(m2,speed); - digitalWrite(m1,LOW); - analogWrite(m3,speed); - digitalWrite(m4,LOW); + this->turnM12(false); + this->turnM34(true); } void Blite::turnRight(){ - digitalWrite(m1,LOW); - digitalWrite(m2,LOW); - analogWrite(m4,speed); - digitalWrite(m3,LOW); + this->turnM12(false); + this->turnM34(false); } void Blite::turnLeft(){ - analogWrite(m1,speed); - digitalWrite(m2,LOW); - digitalWrite(m3,LOW); - digitalWrite(m4,LOW); + this->turnM12(true); + this->turnM34(true); +} +void Blite::stopMotor(){ + this->stopM12(); + this->stopM34(); } void Blite::setSpeed(int s){ this->speed = s ; } + bool Blite::connectWiFi(const char *username, const char *password){ WiFi.disconnect(); WiFi.mode(WIFI_STA); @@ -59,6 +89,8 @@ bool Blite::connectWiFi(const char *username, const char *password){ } else { Serial.println("connected to wifi"); Serial.println(WiFi.localIP()); + this->printTxt("connected to wifi"); + this->printTxt(WiFi.localIP().toString().c_str()); return 1; } retry++; @@ -71,9 +103,13 @@ bool Blite::smartConnectWiFi(){ WiFiManager wm; bool res; res = wm.autoConnect("Buildybee-smart-config","buildybee"); // password protected ap + if(res){ + this->printTxt("Connected to wifi..!!"); + this->printTxt("local IP:"); + this->printTxt(WiFi.localIP().toString().c_str()); + } return res; } - bool Blite::APServer() { if (WiFi.isConnected()){ if (WiFi.disconnect()){ @@ -88,6 +124,9 @@ bool Blite::APServer() { // Connecting WiFi WiFi.softAPConfig(local_IP,gateway,subnet); WiFi.mode(WIFI_AP); + this->printTxt("Running local Wifi"); + this->printTxt("SSID: buidybee_rc_car"); + this->printTxt("IP: 192.168.4.1"); return WiFi.softAP(ssid); } bool Blite::buttonPressed() { @@ -96,26 +135,26 @@ bool Blite::buttonPressed() { void Blite::setup(){ pinMode(SW1,INPUT_PULLUP); - pinMode(M12_A,OUTPUT); - pinMode(M12_B,OUTPUT); - pinMode(M34_A,OUTPUT); - pinMode(M34_B,OUTPUT); - pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, HIGH); + pinMode(M1,OUTPUT); + pinMode(M2,OUTPUT); + pinMode(M3,OUTPUT); + pinMode(M4,OUTPUT); + this->stopMotor(); this->defineM12(true); this->defineM34(true); + this->speed = 100; + this->display.init(); + this->display.flipScreenVertically(); + this->display.clear(); + this->display.setFont(ArialMT_Plain_10); + this->display.setTextAlignment(TEXT_ALIGN_LEFT); + this->lineNo = 0; + this->printTxt("Welcome Buildybees..!!"); + String newHostname = "buildybee"; + WiFi.hostname(newHostname.c_str()); WiFi.disconnect(); WiFi.mode(WIFI_OFF); - - -} - -void Blite::glowLed(bool s){ - if (s){ - digitalWrite(LED_BUILTIN, LOW); - } else{ - digitalWrite(LED_BUILTIN, HIGH); - } + this->otaSetup(); } @@ -127,7 +166,68 @@ void Blite::blinkLed(int c){ delay(500); } } - int Blite::readADC(){ return analogRead(ADC1); +} + +void Blite::setupServer(String &html_content) { + this->webServer.on("/", HTTP_GET, [=]() { + this->webServer.send(200, "text/html", html_content); + }); + this->webServer.begin(); + this->serverSetupDone = true; +} +void Blite::renderServer() { + this->webServer.handleClient(); + this->otaLoop(); +} +void Blite::smartRenderServer(String &html_content){ + if (!this->serverSetupDone) { + this->setupServer(html_content); + } + this->renderServer(); +} +void Blite::otaSetup(){ + + ArduinoOTA.onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) { + type = "sketch"; + } else { // U_FS + type = "filesystem"; + } + + // NOTE: if updating FS this would be the place to unmount FS using FS.end() + Serial.println("Start updating " + type); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) { + Serial.println("Auth Failed"); + } else if (error == OTA_BEGIN_ERROR) { + Serial.println("Begin Failed"); + } else if (error == OTA_CONNECT_ERROR) { + Serial.println("Connect Failed"); + } else if (error == OTA_RECEIVE_ERROR) { + Serial.println("Receive Failed"); + } else if (error == OTA_END_ERROR) { + Serial.println("End Failed"); + } + }); + ArduinoOTA.begin(); +} +void Blite::otaLoop(){ + ArduinoOTA.handle(); +} + +void Blite::printTxt(const char *dispalytxt){ +this->display.drawString(0,this->lineNo*10+2,dispalytxt); +this->display.display(); +this->lineNo++; } \ No newline at end of file diff --git a/src/blite.h b/src/blite.h index c781a0d..2118167 100644 --- a/src/blite.h +++ b/src/blite.h @@ -20,6 +20,11 @@ #include #include #include +#include +#include +#include +#include +#include class Blite { public: @@ -31,19 +36,36 @@ void moveForward(); void moveBackward(); void turnRight(); void turnLeft(); +void stopMotor(); void setSpeed(int speed); void reversePolarityM12(); +void turnM12(bool direction); +void stopM12(); void reversePolarityM34(); +void turnM34(bool direction); +void stopM34(); int getIO(const char * io); bool buttonPressed(); -void glowLed(bool s); void blinkLed(int c); int readADC(); +void setupServer(String &html_content); +void renderServer(); +void smartRenderServer(String &html_content); + +void otaSetup(); +void otaLoop(); + +void printTxt(const char *dispalytxt); + private: -int m1,m2,m3,m4,speed; +int m1,m2,m3,m4,speed,lineNo; +ESP8266WebServer webServer = ESP8266WebServer(80); +SSD1306Wire display = SSD1306Wire(0x3c, I2C_SDA, I2C_SCL); +bool serverSetupDone = false; + void defineM12(bool polarity){ if (polarity){ this->m1 = M1;