Arduino - Problema para leer lector de codigo de barras

 
Vista:
sin imagen de perfil
Val: 1
Ha aumentado su posición en 28 puestos en Arduino (en relación al último mes)
Gráfica de Arduino

Problema para leer lector de codigo de barras

Publicado por jose ramon (1 intervención) el 28/08/2019 21:09:53
Quisiera ver si alguien me puede ayudar con este codigo, estoy usando la conexion USB de arduino para habilitar un lector de barras y tambien un modulo wifi para enviar los datos que recompile con el lector de codigo de barras, pero al momento de unir los dos programas del lector USB y el modulo wifi que envia los datos mi lector de codigo de barras parpadea un foco rojo y se traba por algunos segundos, creo que es por el usb.task() pero no estoy seguro, si alguien pudiera ayudarme, lo que quiero es que el lector de codigo de barra funcione naturalmente al momento de escanear y enviar los datos a travez de wifi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <hidboot.h>
#include <usbhub.h>
 
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
//////////////////////////PARTE WIFI//////////////////////////////////////////////////////////////
String apiKey = "XXXXXXXXXXXXXXXX";
//MAC=5e:cf:7f:b6:17:ef
//////////////////////////////////////////////////////////////////////////////////////////////////
String chain;
String getStr;
String cmd;
int count = 0;
int timer = 0;
 
class KbdRptParser : public KeyboardReportParser
{
    void PrintKey(uint8_t mod, uint8_t key);
 
  protected:
    void OnControlKeysChanged(uint8_t before, uint8_t after);
    void OnKeyDown  (uint8_t mod, uint8_t key);
    void OnKeyUp  (uint8_t mod, uint8_t key);
    void OnKeyPressed(uint8_t key);
};
 
void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
  MODIFIERKEYS mod;
  *((uint8_t*)&mod) = m;
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  PrintKey(mod, key);
  uint8_t c = OemToAscii(mod, key);
 
  if (c)
    OnKeyPressed(c);
}
 
void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
 
  MODIFIERKEYS beforeMod;
  *((uint8_t*)&beforeMod) = before;
 
  MODIFIERKEYS afterMod;
  *((uint8_t*)&afterMod) = after;
 
}
 
void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
  PrintKey(mod, key);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void KbdRptParser::OnKeyPressed(uint8_t key)
{
  //Detect enter
  if (key == 19) {
    Serial.println(chain);
    //////PARTE WIFI////////////////////////////////////////////////////////////
    // TCP connection
    cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += "172.18.1.105"; // api.thingspeak.com
    cmd += "\",80";
    Serial1.println(cmd);//sends AT+START
    //Serial.println(cmd);
    delay(500);
    // prepare GET string
    getStr = "GET /ARDUINOINS.php?numero=";
    getStr += chain;
    getStr += " HTTP/1.1";
    getStr += "\r\n";
    getStr += "Host: 172.18.1.105";
    getStr += "\r\n\r\n";
    //Serial.println(getStr);
 
    // send data length
    cmd = "AT+CIPSEND=";
    cmd += String(getStr.length());
    Serial1.println(cmd);
    //Serial.println(cmd);
 
    if (Serial1.find(">")) {
      Serial1.print(getStr);
 
      if (Serial1.find("OK")) {
        Serial.println("OK");
        chain = "";
        return;
      }
      //Serial.println(ser.readString());
 
    }
    else {
      Serial1.println("AT+CIPCLOSE");
      Serial.println("NO ENVIADO");
      chain = "";
    }
 
    // thingspeak needs 15 sec delay between updates
    delay(1000);
    /////////////////////////////////////////////////////////////////////////////////
  }
 
  else {
    chain += String((char)key);
 
  }
 
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
USB     Usb;
//USBHub     Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);
 
KbdRptParser Prs;
 
void setup()
{
  //////////////////////////PARTE WIFI//////////////////////////////////////////////////////////////
  Serial1.begin(115200);
  Serial.begin(115200);
  unsigned char check_connection = 0;
  unsigned char times_check = 0;
 
  Serial.println("CONECTANDO A RED WIFI");
  while (check_connection == 0)
  {
    Serial.print("..");
    Serial1.print("AT+CWJAP=\"HMO-PLANT1\",\"T3cat3Beer\"\r\n");
    Serial1.setTimeout(8000);
    if (Serial1.find("WIFI CONNECTED\r\n") == 1 )
    {
      Serial.println("CONEXION ESTABLECIDA");
      break;
    }
    times_check++;
    if (times_check > 3) {
      times_check = 0;
      Serial.println("RECONECTANDO");
    }
  }
  delay(5000);
  //////////////////////////////////////////////////////////////////////////////////////////////////
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect
#endif
  Serial.println("START");
 
  if (Usb.Init() == -1)
    Serial.println("DID'N START.");
 
  delay( 200 );
 
  HidKeyboard.SetReportParser(0, &Prs);
 
}
 
void loop()
{
  Usb.Task();
  //delay(50);
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder