donglei2288 2014-10-10 05:12
浏览 30

Arduino Buffer POST PHP

I'm trying to build a application, that reads a BarCode via a PS/2 reader with Arduino, and sends it to a MySQL DB, this's the code that I'm using, but it seems like isn't working with the PHP part. What I'm doing wrong?? Thanks for any help and sorry for any english mistake.

    #include <PS2Keyboard.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // RESERVED MAC ADDRESS
EthernetClient client;


int SCAN_ENTER = 0x5a; int SCAN_BREAK = 0xf0;
int breakActive = 0;
int clockPin = 3; // Clock is only output. 
int dataPin = 2; // The data pin is bi-directional
                // But at the moment we are only interested in receiving   
int ledPin = 13;  // When a SCAN_ENTER scancode is received the LED blink
int clockValue = 0; byte dataValue;
byte scanCodes[10] = {0x45,0x16,0x1e,0x26,0x25,0x2e,0x36,0x3d,0x3e,0x46}; char characters[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int quantityCodes = 10;
char buffer[64] = {};   // This saves the characters (for now only numbers) 
int bufferPos = 0; 
int bufferLength = 64;

void setup() {
    pinMode(dataPin, INPUT);                                               
    pinMode(clockPin, INPUT);                                              
    pinMode(ledPin, OUTPUT);                                               
    Serial.begin(9600);    

    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP"); 

}
 }


void loop() {

     dataValue = dataRead();                                               
    // If there is a break code, skip the next byte                        
    if (dataValue == SCAN_BREAK) {                                         
        breakActive = 1;                                                     
    }                                                                      
    // Translate the scan codes to numbers                                 
    // If there is a match, store it to the buffer                         
    for (int i = 0; i < quantityCodes; i++) {                              
        byte temp = scanCodes[i];                                            
        if(temp == dataValue){                                               
            if(!breakActive == 1){                                             
                buffer[bufferPos] = characters[i];                               
                bufferPos++;                                                     
            }                                                                  
        }                                                                    
    }                                                                      
    //Serial.print('*'); // Output an asterix for every byte               
    // Print the buffer if SCAN_ENTER is pressed.                          
    if(dataValue == SCAN_ENTER){                                           
        Serial.print("");  //Texto antes do codigo                                        
        // Read the buffer                                                   
        int i=0;                                                                                                 
        if (buffer[i] != 0) {                                                
            while(buffer[i] != 0) {                                            
                Serial.print( buffer[i] );                                       
                buffer[i] = 0;                                                   
                i++;                                                             
            }                                                                  

        Serial.println("");                                          
        bufferPos = 0;                                                       
        // Blink the LED                                                       
        digitalWrite(ledPin, HIGH);                                          
        delay(300);                                                          
        digitalWrite(ledPin, LOW);                                           

     }
               if (client.connect("192, 168, 0, 66",80)) { // REPLACE WITH YOUR SERVER ADDRESS
        client.println("POST /add.php HTTP/1.1"); 
        client.println("Host: 192, 168, 0, 66"); // SERVER ADDRESS HERE TOO
        client.println("Content-Type: application/x-www-form-urlencoded"); 
        client.print("Content-Length: "); 
        client.println(buffer[i]); 
        client.println(); 
        client.print( buffer[i] ); 
    } 
        }
    if (client.connected()) { 
        client.stop();  // DISCONNECT FROM THE SERVER
    }
    if(dataValue != SCAN_BREAK){                                           
        breakActive = 0;                                                     
    }                                                                      
    dataValue = 0;                                                         
  }

int dataRead() {
    byte val = 0;                                                          
    // Skip start state and start bit                                      
    while (digitalRead(clockPin));  // Wait for LOW.                       
    // clock is high when idle                                             
    while (!digitalRead(clockPin)); // Wait for HIGH.                      
    while (digitalRead(clockPin));  // Wait for LOW.                       
    for (int offset = 0; offset < 8; offset++) {                           
        while (digitalRead(clockPin));         // Wait for LOW               
        val |= digitalRead(dataPin) << offset; // Add to byte                
        while (!digitalRead(clockPin));        // Wait for HIGH              
    }                                                                      
// Skipping parity and stop bits down here.                            
    while (digitalRead(clockPin));           // Wait for LOW.              
    while (!digitalRead(clockPin));          // Wait for HIGH.             
    while (digitalRead(clockPin));           // Wait for LOW.              
    while (!digitalRead(clockPin));          // Wait for HIGH.             
    return val;                                                            
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)