尝试使用Arduino和wifi模块将图像发送到服务器

时间:2017-09-22 13:01:34

标签: php arduino arduino-ide arduino-esp8266 arduino-due

byte incomingbyte; 
String full;

int a=0x0000,j=0,k=0,count=0;
uint8_t MH,ML;
boolean EndFlag=0;
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
String ssid ="Mudangawe";
char *filename = "ted.jpg";
String password="Tepza4@xo";
String data;
boolean success = 0;
String server = "ndivhuwoprojects.000webhostapp.com"; // www.example.com
String uri = "/tester/Serial38266.php";// our example is /Serial3post.php
int inde =0;

void setup() { 
  Serial.begin(19200);
  Serial2.begin(38400);
  Serial3.begin(9600);    
  reset();
  connectWifi();
}

void loop()  {    
  char val = 'x'; 
  val = httppost("1","/app/cleaner.php");
  val = httppost("1","/app/clear.php");
  SendResetCmd();
  delay(4000);                               
  SendTakePhotoCmd();
  Serial.println("Let see this");
  Serial3.println("HTTP/1.1 200 OK");
  if (strstr(filename, ".htm") != 0) {
    Serial3.println("Content-Type: text/html");
    Serial.println("AN Html page");
  } else if (strstr(filename, ".css") != 0) {
    Serial3.println("Content-Type: text/css");
  } else if (strstr(filename, ".png") != 0) {
    Serial3.println("Content-Type: image/png");
  } else if (strstr(filename, ".jpg") != 0) {
    Serial.println("Is an Image");
    Serial3.println("Content-Type: image/jpeg");
  } else if (strstr(filename, ".gif") != 0) {
    Serial3.println("Content-Type: image/gif");
  } else if (strstr(filename, ".3gp") != 0) {
    Serial3.println("Content-Type: video/mpeg");
  } else if (strstr(filename, ".pdf") != 0) {
    Serial3.println("Content-Type: application/pdf");
  } else if (strstr(filename, ".js") != 0) {
    Serial3.println("Content-Type: application/x-javascript");
  } else if (strstr(filename, ".xml") != 0) {
    Serial3.println("Content-Type: application/xml");
  } else {
    Serial.println("Content-Type: text");
  }
  Serial3.println();
  byte a[32];
  while(!EndFlag) {
    j=0;
    k=0;
    count=0;
    SendReadDataCmd();
    delay(25);
    while(Serial2.available()>0) {
      incomingbyte=Serial2.read();
      Serial3.print((char) incomingbyte);
      k++;
      if((k>5)&&(j<32)&&(!EndFlag)) {
        a[j]=incomingbyte;
        if((a[j-1]==0xFF)&&(a[j]==0xD9))
          //Check if the picture is over
          EndFlag=1;                           
        j++;
        count++;
      }
    }
    for(j=0;j<count;j++) {
      if(a[j]<0x10) Serial.print("0");
      Serial.print(a[j],HEX);
      Serial.print(" ");
      full = full + String(a[j],HEX);
      inde++; 
    }
    //Send jpeg picture over the serial port
    if(!(inde<700)) { 
      success = 0;
      while(success==0) {
        Serial3.println("Content-Type: image/jpeg");
        val = httppost(full,"/app/image.php");
      }
      inde = 0;
      full = " ";
    }
    Serial.println();
  }
  Serial.println(inde);
  success = 0;
  while(success==0) {
    Serial3.println("Content-Type: image/jpeg");
    val = httppost(full,"/app/image.php");
  }
  delay(1);
  while(1);
}

//Send Reset command
void SendResetCmd() {
  Serial2.write(0x56);
  Serial2.write((byte)0);
  Serial2.write(0x26);
  Serial2.write((byte)0);
}

//Send take picture command
void SendTakePhotoCmd() {
  Serial2.write(0x56);
  Serial2.write((byte)0);
  Serial2.write(0x36);
  Serial2.write(0x01);
  Serial2.write((byte)0);  
}

//Read data
void SendReadDataCmd() {
  MH=a/0x100;
  ML=a%0x100;
  Serial2.write(0x56);
  Serial2.write((byte)0);
  Serial2.write(0x32);
  Serial2.write(0x0c);
  Serial2.write((byte)0); 
  Serial2.write(0x0a);
  Serial2.write((byte)0);
  Serial2.write((byte)0);
  Serial2.write(MH);
  Serial2.write(ML);   
  Serial2.write((byte)0);
  Serial2.write((byte)0);
  Serial2.write((byte)0);
  Serial2.write(0x20);
  Serial2.write((byte)0);  
  Serial2.write(0x0a);
  a+=0x20; //address increases 32£¬set according 
}

void StopTakePhotoCmd() {
  Serial2.write(0x56);
  Serial2.write((byte)0);
  Serial2.write(0x36);
  Serial2.write(0x01);
  Serial2.write(0x03);        
}

char httppost(String value, String postRequest) {
  Serial3.println(F("AT+CIPSTART=\"TCP\", \"ndivhuwoprojects.000webhostapp.com\",80"));
  //start a TCP connection.
  delay(500);
  if(Serial3.find("OK")) {
    delay(500);
  }
  value = "id=" + value;
  char url[postRequest.length() + 1]; 
  postRequest.toCharArray(url, postRequest.length() + 1);
  postRequest =
    "POST " + (String)url + " HTTP/1.0\r\n" +
    "Host: ndivhuwoprojects.000webhostapp.com\r\n" +
    "Accept: *" + "/" + "*\r\n" +
    "Content-Length: " + value.length() + "\r\n" +
    "Content-Type: application/x-www-form-urlencoded\r\n" +
    "\r\n" + value;
  Serial3.print(F("AT+CIPSEND="));  
  Serial3.println(postRequest.length());
  delay(500);
  if(Serial3.find(">")) {
    Serial3.print(postRequest);
    delay(500);
    if(Serial3.find("SEND OK")) {
      success =1;
      Serial.println(F("Packet sent"));
      while (Serial3.available()) {
        postRequest = Serial3.readStringUntil('|');  
        postRequest = postRequest.substring(postRequest.length() - 2);
        postRequest.trim();
        if(postRequest.length() > 0){
          // close the connection
          Serial3.println(F("AT+CIPCLOSE"));
          delay(500);
          return postRequest[0];
        }
      }
    }
  }
  return 'x';
}

void reset() {
  Serial3.println("AT+RST");
  delay(1000);
  if(Serial3.find("OK")) Serial.println("Module Reset");
}
void connectWifi() {
  String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
  Serial3.println(cmd);
  delay(4000);
  if(Serial3.find("OK")) {
    Serial.println("Connected!");
  } else {
    Serial.println("Cannot connect to wifi");
    connectWifi();
  }
}

此代码用于捕获字节并将其传输到服务器。现在这是接受字节的PHP代码。

<?php
  if(isset($_POST['id'])) {
    $data = $_POST['id'];
    file_put_contents('cololl.txt',$data, FILE_APPEND);
    $im = imagecreatefromstring($data);
    if ($im) {
      header('Content-Type: image/jpeg');
      imagejpeg($im,'../arduino/images/curop.jpg');
      imagedestroy($im);
      file_put_contents('colo.txt',"Successfuly");
    } else {
      file_put_contents('colo.txt','Error.');
    }
  }

此外,我还有一个文本存储PHP页面中的所有字节。似乎这些字节无法生成图像。所以现在我被卡住了。我不知道该怎么做。任何人都可以帮我解决这个问题吗?

我还尝试下载文本文件并尝试使用PHP在我的电脑上进行转换,但是我收到了警告:imagecreatefromstring(): Data is not in a recognized format.

我忘了告诉我正在使用Arduino Due。

0 个答案:

没有答案