/* Linksprite */ #include <SoftwareSerial.h> #include <Ethernet.h> #include <SPI.h> #define ReadLen 0x30 byte mac[] = { 0xDE, 0xAC, 0xEA, 0xEF, 0xFF, 0xE2 }; // if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) char server[] = "192.168.9.6"; // name address for Google (using DNS) char HexTable[] = "0123456789ABCDEF"; // Set the static IP address to use if the DHCP fails to assign IPAddress ip(192,168,16,121); IPAddress gateway(192,168,1,1); IPAddress dns_server(60,191,134,196); IPAddress subnet(255,255,0,0); // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; byte incomingbyte; SoftwareSerial mySerial(4,5); //Configure pin 4 and 5 as soft serial port int a=0x0000,j=0,k=0,count=0; //Read Starting address uint8_t MH,ML; boolean EndFlag=0; int errCount=0; int led=8; void SendResetCmd(); void SendTakePhotoCmd(); void SendReadDataCmd(); void StopTakePhotoCmd(); void httpRequest(byte bytes[]); void setup() { Serial.begin(9600); mySerial.begin(38400); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); Ethernet.begin(mac, ip,dns_server,gateway,subnet); } setImgSize(); delay(1000); readEcho(false); Serial.println("Ready"); } void loop() { SendResetCmd(); delay(2000);//After reset, wait 2-3 second to send take picture command EndFlag=0; a=0x0000;j=0;k=0; count=0; MH=0;ML=0; SendTakePhotoCmd(); readEcho(false); byte a[ReadLen]; while(!EndFlag) { j=0; k=0; count=0; SendReadDataCmd(); delay(25); while(mySerial.available()>0) { incomingbyte=mySerial.read(); k++; if((k>5)&&(j<ReadLen)&&(!EndFlag)) { a[j]=incomingbyte; if((a[j-1]==0xFF)&&(a[j]==0xD9)){ //Check if the picture is over EndFlag=1; StopTakePhotoCmd(); readEcho(false); } j++; count++; } } // for(j=0;j<count;j++) // { if(a[j]<10) // Serial.print("00"); // if(a[j]>=10 && a[j]<100) // Serial.print("0"); // Serial.print(a[j]); // Serial.print(" "); // } //Send jpeg picture over the serial port // Serial.println(); while(!client.connected()){ byte aa[]={0,0,0}; httpRequest(aa,3); } httpRequest(a,count); // for(j=0;j<count;j++) // { if(a[j]<0x10) // Serial.print("0"); // Serial.print(a[j],HEX); // Serial.print(" "); // } //Send jpeg picture over the serial port // Serial.println(); } // delay(30000); } //==============byte to hex string=============== String toHex(byte bs[],int count){ String bundle=""; for(int i=0;i<count;i++){ bundle += (String)( HexTable[ bs[i] / 16]) + (String)( HexTable[bs[i] % 16]); } return bundle; } //==============End byte to hex string============= //================Http Request Code======================== void httpRequest(byte bytes[],int count){ // if you get a connection, report back via serial: if(!client.connected()){ Serial.println("disconnecting."); client.stop(); if (client.connect(server, 7896)) { Serial.println("connected"); }else{ // kf you didn't get a connection to the server: Serial.println("connection failed"); errCount++; if(errCount>=3){ digitalWrite(led,HIGH); delay(1000); } } } else{ String bundle=toHex(bytes,count); //Serial.println(bundle); client.println("GET /main.ashx?m=img&mil=" + String(millis()) + "&b="+ bundle +" HTTP/1.1"); client.println("Host:192.168.9.6"); //client.println("Connection: close"); client.println(); delay(10); String reply=""; // if there are incoming bytes available // from the server, read them and print them: while (client.available()) { char c = client.read(); reply+=c; } //Serial.println(reply); if(reply.indexOf(""Model":"OPEN"")>=0){ Serial.println("OPEN"); } if(reply.indexOf(""Model":"CLOSE"")>=0){ Serial.println("CLOSE"); } errCount=0; //wdt_reset(); } } //================End Http Request Code=============== void readEcho(bool show){ while(mySerial.available()>0){ char c=mySerial.read(); if(show){ Serial.print(c); } } } //160 * 120 void setImgSize(){ mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x31); mySerial.write(0x05); mySerial.write(0x04); mySerial.write(0x01); mySerial.write((byte)0x00); mySerial.write(0x19); mySerial.write(0x22); } //Send Reset command void SendResetCmd() { mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x26); mySerial.write((byte)0x00); } //Send take picture command void SendTakePhotoCmd() { mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x36); mySerial.write(0x01); mySerial.write((byte)0x00); } //Read data void SendReadDataCmd() { MH=a/0x100; ML=a%0x100; mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x32); mySerial.write(0x0c); mySerial.write((byte)0x00); mySerial.write(0x0a); mySerial.write((byte)0x00); mySerial.write((byte)0x00); mySerial.write(MH); mySerial.write(ML); mySerial.write((byte)0x00); mySerial.write((byte)0x00); mySerial.write((byte)0x00); mySerial.write(ReadLen); mySerial.write((byte)0x00); mySerial.write(0x0a); a+=ReadLen; //address increases 32,set according to buffer size } void StopTakePhotoCmd() { mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x36); mySerial.write(0x01); mySerial.write(0x03); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Concurrent; using System.Threading; namespace ColorRecognition.JPEGCamera { public class JPEGCameraRecord { private static long GRecId = 0; private static int C_MAXLen = 100; private static int C_ShrinkageLen = 60; private List<byte> Bytes { get; set; } //正在收集的图片(单张) private Queue<byte[]> Queue { get; set; } //已经收集的图片集合 private DateTime? BTime { get; set; } private DateTime? ETime { get; set; } public Queue<int> SpeedQueue = new Queue<int>(); public string IP { get; set; } public long? LastClientMillis { get; set; } public DateTime? LastEditTime { get; set; } public long RecId { get; set; } /// <summary> /// 上传一张要多少秒 /// </summary> public int? Speed { get; set; } public void ConcatImg(byte[] bytes) { lock (this) { #region if (bytes.Length >= 2) { if (bytes[0] == 0xFF && bytes[1] == 0xD8) { //开始 Bytes.Clear(); Bytes = new List<byte>(); Bytes.AddRange(bytes); BTime = DateTime.Now; } else if (bytes[bytes.Length - 2] == 0xFF && bytes[bytes.Length - 1] == 0xD9) { //结束 Bytes.AddRange(bytes); Queue.Enqueue(Bytes.ToArray()); Bytes.Clear(); Bytes = new List<byte>(); if (Queue.Count >= C_MAXLen) { byte[] r = null; while (Queue.Count > C_ShrinkageLen) { Queue.Dequeue(); } } ETime = DateTime.Now; var due =(int)((TimeSpan) (ETime - BTime)).TotalSeconds; SpeedQueue.Enqueue(due); if (SpeedQueue.Count >=5) { SpeedQueue.Dequeue(); } Speed=(int) SpeedQueue.Average(); } else { if (Bytes.Count > 0) { Bytes.AddRange(bytes); } if (Bytes.Count > 1024 * 1024 * 10) //丢弃超过10M的图片 { Bytes.Clear(); Bytes = new List<byte>(); } } } #endregion } } public List<byte[]> GetImages() { lock (this) { var list = new List<byte[]>(); foreach (var img in Queue) { list.Add(img); } return list; } } public JPEGCameraRecord() { Bytes = new List<byte>(); Queue = new Queue<byte[]>(); Interlocked.Increment(ref GRecId); RecId = GRecId; } public long? BufferLen { get { lock (this) { return Bytes.Count; } } } public long? ImageCount { get { lock (this) { return Queue.Count; } } } } public class JPEGCameraRecordMgr { private static int C_MAXLen = 200; private static int C_ShrinkageLen = 120; public readonly static ConcurrentQueue<JPEGCameraRecord> Queue = new ConcurrentQueue<JPEGCameraRecord>(); public static void Add(byte[] bytes, string ip, long? clientMillis) { var it = Queue.FirstOrDefault(ent => string.Compare(ent.IP , ip,true)==0); if (it == null) { var record = new JPEGCameraRecord(); record.IP = ip; Queue.Enqueue(record); if (Queue.Count > C_MAXLen) { JPEGCameraRecord r=null; while (Queue.Count >= C_ShrinkageLen) { Queue.TryDequeue(out r); } } it = record; } it.LastClientMillis = clientMillis; it.LastEditTime = DateTime.Now; it.ConcatImg(bytes); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace ColorRecognition.JPEGCamera { public class CameraHttpHandle:IHttpHandler { public HttpRequest Request { get; set; } public HttpResponse Response { get; set; } public void ProcessRequest(HttpContext context) { Request = context.Request; Response = context.Response; Response.ContentType = "text/plain"; Response.Expires = -1; var response = new ResponseBase<String>() { Code = 1 }; try { var method = Request["m"]; if (string.Compare(method, "img", true) == 0) { DoCollect(); } else { throw new Exception("未知道的方法"); } response.Code = 0; response.Msg = "OK"; } catch (Exception ex) { response.Code = -1; response.Msg = ex.Message; } Response.Write(this.ToJson(response)); } public virtual void DoCollect() { long? millis = this.TryParser<long?>(Request["mil"], 0); var imgSegment = Request["b"]; var ip = this.GetClientIP(); if (!string.IsNullOrWhiteSpace(imgSegment)) { var bytes=new byte[imgSegment.Length/2]; int j = 0; for(int i=0;i<imgSegment.Length;i+=2){ var v = imgSegment.Substring(i, 2); bytes[j] = byte.Parse(v,System.Globalization.NumberStyles.HexNumber); j++; } JPEGCameraRecordMgr.Add(bytes, ip, millis); } } public bool IsReusable { get { return false; } } } }
//调整后Arduino端
/* Linksprite */ #include <avr/io.h> #include <avr/wdt.h> #include <SoftwareSerial.h> #include <Ethernet.h> #include <SPI.h> #define ReadLen 0x30 byte mac[] = { 0xDE, 0xAC, 0xEA, 0xEF, 0xFF, 0xE2 }; // if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) char server[] = "192.168.9.6"; // name address for Google (using DNS) char HexTable[] = "0123456789ABCDEF"; // Set the static IP address to use if the DHCP fails to assign IPAddress ip(192,168,16,121); IPAddress gateway(192,168,1,1); IPAddress dns_server(60,191,134,196); IPAddress subnet(255,255,0,0); // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; byte incomingbyte; SoftwareSerial mySerial(4,5); //Configure pin 4 and 5 as soft serial port int a=0x0000,j=0,k=0,count=0; //Read Starting address uint8_t MH,ML; boolean EndFlag=0; int errCount=0; int led=8; int led2=9; long btime=0; boolean picGetOutTime=false; void SendResetCmd(); void SendTakePhotoCmd(); void SendReadDataCmd(); void StopTakePhotoCmd(); void httpRequest(byte bytes[]); void setup() { pinMode(led,OUTPUT); pinMode(led2,OUTPUT); digitalWrite(led2,LOW); digitalWrite(led,LOW); Serial.begin(9600); mySerial.begin(38400); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); Ethernet.begin(mac, ip,dns_server,gateway,subnet); } SendResetCmd(); delay(4000); readEcho(false); setImgSize(); delay(1000); readEcho(false); //wdt_enable(WDTO_8S); Serial.println("Ready"); } void loop() { Serial.println("loop!"); SendResetCmd(); delay(2000);//After reset, wait 2-3 second to send take picture command EndFlag=0; a=0x0000;j=0;k=0; count=0; MH=0;ML=0; SendTakePhotoCmd(); readEcho(false); byte a[ReadLen]; btime=millis(); while(!EndFlag) { j=0; k=0; count=0; SendReadDataCmd(); delay(25); if((millis() - btime) > (1000 * 30)){ digitalWrite(led2,HIGH); Serial.write("break1"); picGetOutTime=true; break; } while(mySerial.available()>0) { if((millis() - btime) > (1000 * 30)){ picGetOutTime=true; break; } incomingbyte=mySerial.read(); k++; if((k>5)&&(j<ReadLen)&&(!EndFlag)) { a[j]=incomingbyte; if((a[j-1]==0xFF)&&(a[j]==0xD9)){ //Check if the picture is over EndFlag=1; picGetOutTime=false; StopTakePhotoCmd(); readEcho(false); Serial.println("end pic"); digitalWrite(led2,LOW); } j++; count++; } } // for(j=0;j<count;j++) // { if(a[j]<10) // Serial.print("00"); // if(a[j]>=10 && a[j]<100) // Serial.print("0"); // Serial.print(a[j]); // Serial.print(" "); // } //Send jpeg picture over the serial port // Serial.println(); if(picGetOutTime){ digitalWrite(led2,HIGH); digitalWrite(led,HIGH); delay(1000); } // Serial.println(count); while(!client.connected()){ byte aa[]={0,0,0}; httpRequest(aa,3); } httpRequest(a,count); // for(j=0;j<count;j++) // { if(a[j]<0x10) // Serial.print("0"); // Serial.print(a[j],HEX); // Serial.print(" "); // } //Send jpeg picture over the serial port // Serial.println(); } // delay(30000); } //==============byte to hex string=============== String toHex(byte bs[],int count){ String bundle=""; for(int i=0;i<count;i++){ bundle += (String)( HexTable[ bs[i] / 16]) + (String)( HexTable[bs[i] % 16]); } return bundle; } //==============End byte to hex string============= //================Http Request Code======================== void httpRequest(byte bytes[],int count){ // if you get a connection, report back via serial: if(!client.connected()){ Serial.println("disconnecting."); client.stop(); if (client.connect(server, 7896)) { Serial.println("connected"); }else{ // kf you didn't get a connection to the server: Serial.println("connection failed"); errCount++; if(errCount>=3){ digitalWrite(led,HIGH); delay(1000); } } } else{ String bundle=toHex(bytes,count); //Serial.println(bundle); client.println("GET /main.ashx?m=img&mil=" + String(millis()) + "&b="+ bundle +" HTTP/1.1"); client.println("Host:192.168.9.6"); //client.println("Connection: close"); client.println(); delay(10); String reply=""; // if there are incoming bytes available // from the server, read them and print them: while (client.available()) { char c = client.read(); reply+=c; } //Serial.println(reply); if(reply.indexOf(""Model":"OPEN"")>=0){ Serial.println("OPEN"); } if(reply.indexOf(""Model":"CLOSE"")>=0){ Serial.println("CLOSE"); } errCount=0; //wdt_reset(); } } //================End Http Request Code=============== void readEcho(bool show){ while(mySerial.available()>0){ char c=mySerial.read(); if(show){ Serial.print(c); } } } //160 * 120 void setImgSize(){ mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x31); mySerial.write(0x05); mySerial.write(0x04); mySerial.write(0x01); mySerial.write((byte)0x00); mySerial.write(0x19); mySerial.write(0x22); } //Send Reset command void SendResetCmd() { mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x26); mySerial.write((byte)0x00); } //Send take picture command void SendTakePhotoCmd() { mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x36); mySerial.write(0x01); mySerial.write((byte)0x00); } //Read data void SendReadDataCmd() { MH=a/0x100; ML=a%0x100; mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x32); mySerial.write(0x0c); mySerial.write((byte)0x00); mySerial.write(0x0a); mySerial.write((byte)0x00); mySerial.write((byte)0x00); mySerial.write(MH); mySerial.write(ML); mySerial.write((byte)0x00); mySerial.write((byte)0x00); mySerial.write((byte)0x00); mySerial.write(ReadLen); mySerial.write((byte)0x00); mySerial.write(0x0a); a+=ReadLen; //address increases 32,set according to buffer size } void StopTakePhotoCmd() { mySerial.write(0x56); mySerial.write((byte)0x00); mySerial.write(0x36); mySerial.write(0x01); mySerial.write(0x03); }
12,13,设置pinMode(OUTPUT)时无效(连网络扩展板上)
9V 1A,外接电源,错误时,无法带动继电器