/* * Wifi Camera Bot ESP8266 * */ #include #include #include #include #include #include #include Servo servo1; Servo servo2; //Ajust to find value to stop rotation of Continuous Servo int servo1_zero=78; int servo2_zero=78; //Ajust for speed of Continuous Servo on clockwise rotation, decrease for speed up int servo1_clock=68; int servo2_clock=68; //Ajust for speed of Continuous Servo on counter clockwise rotation, increase for speed up int servo1_counterclock=88; int servo2_counterclock=88; byte incomingbyte; int a=0x0000, //Read Starting address j=0, k=0, count=0, sendcount=0; uint8_t MH,ML; boolean EndFlag=0; int sizeout; //Change for your Wifi Network const char *ssid = "SBH_Zero_Network"; const char *password = "9713397133"; MDNSResponder mdns; ESP8266WebServer server ( 80 ); //const int led = 13; //************************************************SETUP**********************************************// void setup ( void ) { WiFi.begin ( ssid, password ); //Change for your Wifi Network //static ip, gateway, netmask WiFi.config(IPAddress(10,0,1,70), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); Serial.begin ( 115200 ); SendResetCmd(); delay(1000); //ChangeSizeSmall(); ChangeSizeMedium(); ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); // Wait for connection while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); } if ( mdns.begin ( "esp8266", WiFi.localIP() ) ) { //Serial.println ( "MDNS responder started" ); } server.on ( "/", handleRoot ); server.on ( "/picture.jpg", picture ); server.on ( "/picturepage.htm", picturepage ); server.on ( "/picture_ld.htm", picture_ld ); server.on ( "/picture_md.htm", picture_md ); server.on ( "/picture_hd.htm", picture_hd ); server.on ( "/command.htm", command ); server.on ( "/go.htm", go ); server.on ( "/back.htm", back ); server.on ( "/left.htm", left ); server.on ( "/right.htm", right ); server.on ( "/stop.htm", Stop ); server.on ( "/inline", []() { server.send ( 200, "text/plain", "this works as well" ); } ); server.onNotFound ( handleRoot ); server.begin(); //Serial.println ( "HTTP server started" ); delay(500); servo_turnon(); servo1.write(servo1_zero); servo2.write(servo2_zero); delay(500); //servo_turnoff(); } //************************************************LOOP**********************************************// void loop ( void ) { mdns.update(); server.handleClient(); } //*****************************************WEBSERVER HANDLE*****************************************// void handleRoot() { //digitalWrite ( led, 1 ); char temp[800]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; snprintf ( temp, 800, "\ \ ESP8266 Camera Bot\ \ \
\

IP Camera Robot with an ESP8266 and serial JPEG camera!

\

Uptime: %02d:%02d:%02d

\ \

Select Resolution:

\ \ \ \

Commands:

\ \
\ ", hr, min % 60, sec % 60 ); server.send ( 200, "text/html", temp ); } void picturepage() { //digitalWrite ( led, 1 ); char temp[500]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; String out = ""; out =+ "\ \ Camera ESP8266 Cam\ \ \ \ \ "; server.send ( 200, "text/html", out ); } //*****************************************CAMERA SNAPSHOOT**************************************// void picture() { WiFiClient client = server.client(); client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: image/jpeg")); client.println(F("")); String out = ""; SendTakePhotoCmd(); delay(100); while(Serial.available()>0) { incomingbyte=Serial.read(); } byte b[32]; while(!EndFlag) { j=0; k=0; count=0; sendcount=0; SendReadDataCmd(); delay(35); //try going up while(Serial.available()>0) { incomingbyte=Serial.read(); k++; if((k>5)&&(j<32)&&(!EndFlag)) { b[j]=incomingbyte; out += (char)incomingbyte; if((b[j-1]==0xFF)&&(b[j]==0xD9)) EndFlag=1; j++; count++; } } sizeout = out.length(); if (sizeout >= 2048) { client.print(out); out = ""; } } sizeout = out.length(); if (sizeout > 0) { client.print(out); out = ""; } delay(3000); StopTakePhotoCmd(); //stop this picture so another one can be taken EndFlag = 0; //reset flag to allow another picture to be read } void picture_ld() { SendResetCmd(); delay(1000); ChangeSizeSmall(); //ChangeSizeMedium(); //ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); handleRoot(); } void picture_md() { SendResetCmd(); delay(1000); //ChangeSizeSmall(); ChangeSizeMedium(); //ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); handleRoot(); } void picture_hd() { SendResetCmd(); delay(1000); //ChangeSizeSmall(); //ChangeSizeMedium(); ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); handleRoot(); } //*****************************************CAMERA COMMAND**************************************// //Send Reset command void SendResetCmd() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x26); Serial.write((byte)0x00); } //Send take picture command void SendTakePhotoCmd() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x36); Serial.write((byte)0x01); Serial.write((byte)0x00); a = 0x0000; //reset so that another picture can taken } void FrameSize() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x34); Serial.write((byte)0x01); Serial.write((byte)0x00); } //Read data void SendReadDataCmd() { MH=a/0x100; ML=a%0x100; Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x32); Serial.write((byte)0x0c); Serial.write((byte)0x00); Serial.write((byte)0x0a); Serial.write((byte)0x00); Serial.write((byte)0x00); Serial.write((byte)MH); Serial.write((byte)ML); Serial.write((byte)0x00); Serial.write((byte)0x00); Serial.write((byte)0x00); Serial.write((byte)0x20); Serial.write((byte)0x00); Serial.write((byte)0x0a); a+=0x20; } void StopTakePhotoCmd() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x36); Serial.write((byte)0x01); Serial.write((byte)0x03); } void ChangeSizeSmall() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x31); Serial.write((byte)0x05); Serial.write((byte)0x04); Serial.write((byte)0x01); Serial.write((byte)0x00); Serial.write((byte)0x19); Serial.write((byte)0x22); } void ChangeSizeMedium() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x31); Serial.write((byte)0x05); Serial.write((byte)0x04); Serial.write((byte)0x01); Serial.write((byte)0x00); Serial.write((byte)0x19); Serial.write((byte)0x11); } void ChangeSizeBig() { Serial.write((byte)0x56); Serial.write((byte)0x00); Serial.write((byte)0x31); Serial.write((byte)0x05); Serial.write((byte)0x04); Serial.write((byte)0x01); Serial.write((byte)0x00); Serial.write((byte)0x19); Serial.write((byte)0x00); } //*****************************************SERVO COMMAND**************************************// void go() { servo_go(); command(); } void back() { servo_back(); command(); } void left() { servo_left(); command(); } void right() { servo_right(); command(); } void Stop() { servo_stop(); command(); } void servo_go() { //servo_turnon(); servo1.write(servo1_counterclock); servo2.write(servo1_clock); } void servo_back() { //servo_turnon(); servo1.write(servo1_clock); servo2.write(servo1_counterclock); } void servo_stop() { servo1.write(servo1_zero); servo2.write(servo2_zero); //servo_turnoff(); } void servo_left() { //servo_turnon(); servo1.write(servo1_clock); servo2.write(servo1_clock); delay(300); servo1.write(servo1_zero); servo2.write(servo2_zero); //servo_turnoff(); } void servo_right() { //servo_turnon(); servo1.write(servo1_counterclock); servo2.write(servo1_counterclock); delay(300); servo1.write(servo1_zero); servo2.write(servo2_zero); //servo_turnoff(); } void servo_turnon() { servo1.attach(0); servo2.attach(2); } void servo_turnoff() { servo1.detach(); servo2.detach(); } //*****************************************HANDLE COMMAND**************************************// void command() { String out = ""; out =+ "\ \ ESP8266 Command\ \ \ \ \ \ \ \
\ \ "; server.send ( 200, "text/html", out ); }