/* * Wifi Camera ESP8266 * */ #include #include #include #include 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"; const char* server = ""; WiFiClient client; WiFiClient dclient; char outBuf[128]; char outCount; int sizeout; //************************************************SETUP**********************************************// void setup ( void ) { 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); // Wait for connection while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); } } //************************************************LOOP**********************************************// void loop ( void ) { if (client.connect(server,21)) { Serial.println(F("Command connected")); eRcv(); client.println("USER "); //FTP User eRcv(); client.println("PASS "); //FTP Password eRcv(); client.println("OPTS UTF8 ON"); eRcv(); client.println("CWD www"); //Change Directory eRcv(); client.println("CWD WEBCAM"); //Change Directory eRcv(); client.println("SYST"); eRcv(); client.println("Type I"); eRcv(); client.println("PASV"); eRcv(); char *tStr = strtok(outBuf,"(,"); int array_pasv[6]; for ( int i = 0; i < 6; i++) { tStr = strtok(NULL,"(,"); array_pasv[i] = atoi(tStr); if(tStr == NULL) { Serial.println(F("Bad PASV Answer")); } } unsigned int hiPort,loPort; hiPort = array_pasv[4] << 8; loPort = array_pasv[5] & 255; Serial.print(F("Data port: ")); hiPort = hiPort | loPort; Serial.println(hiPort); if (dclient.connect(server,hiPort)) { Serial.println(F("Data connected")); } else { Serial.println(F("Data connection failed")); client.stop(); } client.print("STOR "); client.println(F("webcam_esp8266_1.jpg")); if(!eRcv()) { dclient.stop(); } Serial.println(F("Writing")); byte clientBuf[64]; int clientCount = 0; String out = ""; picture_hd(); 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) { dclient.print(out); out = ""; } } sizeout = out.length(); if (sizeout > 0) { dclient.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 dclient.stop(); Serial.println(F("Data disconnected")); eRcv(); client.println("QUIT"); eRcv(); client.stop(); Serial.println(F("Command disconnected")); } else { Serial.println(F("Command connection failed")); } delay(60000); } void picture_ld() { SendResetCmd(); delay(1000); ChangeSizeSmall(); //ChangeSizeMedium(); //ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); } void picture_md() { SendResetCmd(); delay(1000); //ChangeSizeSmall(); ChangeSizeMedium(); //ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); } void picture_hd() { SendResetCmd(); delay(1000); //ChangeSizeSmall(); //ChangeSizeMedium(); ChangeSizeBig(); delay(1000); SendResetCmd(); delay(1000); } //*****************************************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); } byte eRcv() { byte respCode; byte thisByte; while(!client.available()) delay(0); respCode = client.peek(); outCount = 0; while(client.available()) { thisByte = client.read(); Serial.write(thisByte); if(outCount < 127) { outBuf[outCount] = thisByte; outCount++; outBuf[outCount] = 0; } } if(respCode >= '4') { efail(); return 0; } return 1; } void efail() { byte thisByte = 0; client.println(F("QUIT")); while(!client.available()) delay(1); while(client.available()) { thisByte = client.read(); Serial.write(thisByte); } client.stop(); Serial.println(F("Command disconnected")); }