/* * Wifi Camera ESP8266 * */ #include #include #include #include #include #include #define min(a,b) ((a)<(b)?(a):(b)) byte incomingbyte; int a=0x0000, //Read Starting address j=0, k=0, count=0, sendcount=0; uint8_t MH,ML; boolean EndFlag=0; const char *ssid = "SBH_Zero_Network"; const char *password = "9713397133"; MDNSResponder mdns; ESP8266WebServer server ( 80 ); //const int led = 13; //************************************************SETUP**********************************************// void setup ( void ) { //pinMode ( led, OUTPUT ); //digitalWrite ( led, 0 ); WiFi.begin ( ssid, password ); //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(3000); //ChangeSizeSmall(); //ChangeSizeMedium(); ChangeSizeBig(); // 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 ( "/inline", []() { server.send ( 200, "text/plain", "this works as well" ); } ); server.onNotFound ( handleNotFound ); server.begin(); //Serial.println ( "HTTP server started" ); } //************************************************LOOP**********************************************// void loop ( void ) { mdns.update(); server.handleClient(); } //*****************************************WEBSERVER HANDLE*****************************************// void handleRoot() { //digitalWrite ( led, 1 ); char temp[400]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; snprintf ( temp, 400, "\ \ \ ESP8266 Demo\ \ \ \

IP Camera with an ESP8266 and serial JPEG camera!

\

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

\ \ \ ", hr, min % 60, sec % 60 ); 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 ); } //*****************************************CAMERA SNAPSHOOT**************************************// void picture() { //server.setContentLength(CONTENT_LENGTH_UNKNOWN); //server.send(200, "text/jpeg", ""); 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++; } } 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 //server.send ( 200, "image/jpeg", out); } //*****************************************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); }