sankmv
Published © GPL3+

Wireless Product Tracking (Arduino MKR & Barcode Scanner)

Let's consider creating a system for tracking goods in a warehouse using a barcode scanner. We use the Arduino MKR 1000 as the controller.

IntermediateFull instructions provided10 hours22,679
Wireless Product Tracking (Arduino MKR & Barcode Scanner)

Things used in this project

Story

Read more

Code

Scanner sketch for Arduino MKR1000

Arduino
// 
// QR
// Restore Factory Settings (   )
// UART Output     (  UART)
// Continuous Mode ( ,   )
// Non-Scanning Interval (2000 )
// Lighting - No light ( )

#include <SPI.h>
#include <WiFi101.h>

//#include "arduino_secrets.h" 

#include <Arduino.h>                              
#include <wiring_private.h>

char ssid[] = "Kassa1";        // your network SSID (name)
char pass[] = "12345678";    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status

// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX       (1ul)                // Pin description number for PIO_SERCOM on D1
#define PIN_SERIAL2_TX       (0ul)                // Pin description number for PIO_SERCOM on D0
#define PAD_SERIAL2_TX       (UART_TX_PAD_0)      // SERCOM pad 0 TX
#define PAD_SERIAL2_RX       (SERCOM_RX_PAD_1)    // SERCOM pad 1 RX

// Serial3 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
//#define PIN_SERIAL3_RX       (5ul)                // Pin description number for PIO_SERCOM on D5
//#define PIN_SERIAL3_TX       (4ul)                // Pin description number for PIO_SERCOM on D4
//#define PAD_SERIAL3_TX       (UART_TX_PAD_2)      // SERCOM pad 2 TX
//#define PAD_SERIAL3_RX       (SERCOM_RX_PAD_3)    // SERCOM pad 3 RX

Uart Serial2(&sercom3, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);
//Uart Serial3(&sercom4, PIN_SERIAL3_RX, PIN_SERIAL3_TX, PAD_SERIAL3_RX, PAD_SERIAL3_TX);
WiFiClient client;
// ,    
String inputString1 = "";        
String inputString2 = "";        
//  
boolean stringComplete1 = false;  
int countstr1=0;
boolean stringComplete2 = false;  
int countstr2=0;
//   
unsigned long millisendstr1=0;
unsigned long millisendstr2=0;

String message;
String barcode="";
// name address 
char server[] = "www.freeparkovka.ru";    
char response[60];
int counter=0;

void SERCOM3_Handler()    // Interrupt handler for SERCOM3
{
Serial2.IrqHandler();
}
//void SERCOM4_Handler()    // Interrupt handler for SERCOM4
//{
//  Serial3.IrqHandler();
//}

void setup() {
  // 
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serials start");
  pinPeripheral(0, PIO_SERCOM);   // Assign pins 0 & 1 SERCOM functionality
  pinPeripheral(1, PIO_SERCOM);
  Serial2.begin(9600);               // Begin Serial2
  //pinPeripheral(4, PIO_SERCOM_ALT);   // Assign pins 4 & 5 SERCOM functionality
  //pinPeripheral(5, PIO_SERCOM_ALT);
  //Serial3.begin(9600);               // Begin Serial3
  Serial1.begin(9600);
  
  //  50 bytes  inputString:
  inputString1.reserve(50);
  inputString2.reserve(50);  
  // Nextion
  Serial2.print("t0.txt=\"wait ...\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);    
  delay(10);  
  Serial2.print("n0.val=");
  Serial2.print(counter);    
  Serial2.print("");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);    
  delay(10);  

  Serial.println("Serials ok");
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(5000);
  }

  // once you are connected :
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWiFiData();
}

void loop() {
  // 
  serialScanerEvent1(); 
  if (stringComplete1) {
    Serial.println(inputString1);
    Serial2.print("t0.txt=\"");
    Serial2.print(inputString1);
    Serial2.print("\"");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);  
    barcode=inputString1;      
    //  
    inputString1 = "";
    stringComplete1 = false;
    countstr1=0;
   }
  serialNextionEvent2(); 
  if (stringComplete2) {
    //Serial.println(inputString2); 
    parse_message(inputString2);
    //  
    inputString2 = "";
    stringComplete2 = false;
    countstr2=0;
   }  
  
}

void serialScanerEvent1() {
  //
  if (Serial1.available()>0) {
    // get the new byte:
    char inChar = (char)Serial1.read();
    // add it to the inputString:
    inputString1 += inChar;
    countstr1++;
    millisendstr1=millis();
  }
  else {
    if(millis()-millisendstr1>1000 && countstr1>0) {
       stringComplete1=true;
    }
  }
}

void serialNextionEvent2() {
  //
  if (Serial2.available()>0) {
    // get the new byte:
    char inChar = (char)Serial2.read();
    // add it to the inputString:
    inputString2 += String(inChar,HEX);
    countstr2++;
    inputString2 += " ";
    countstr2++;
    millisendstr2=millis();
  }
  else {
    if(millis()-millisendstr2>200 && countstr2>0) {
       stringComplete2=true;
    }
  }
}

void printWiFiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  printMacAddress(mac);
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  printMacAddress(bssid);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}

void parse_message(String msg)
  {  
  //Serial.println(msg); //...print it out
  // ******** экран 0 (logo)
  if (msg == "65 0 b 0 ff ff ff ") 
    {  
    counter=0;
    Serial2.print("n0.val=");
    Serial2.print(counter);    
    Serial2.print("");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);      
    delay(10);  
    }
  // send
  else if (msg == "65 0 c 0 ff ff ff ") 
    {
    Serial2.print("t0.txt=\"send ...\"");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);    
    delay(10);  
    // **** send data to server
    send_data_to_server();
    Serial2.flush();
    //
    Serial2.print("t0.txt=\"wait ...\"");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);    
    delay(10);  
    counter=0;
    Serial2.print("n0.val=");
    Serial2.print(counter);    
    Serial2.print("");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);    
    delay(10);  
    }
  // 0
  else if (msg == "65 0 1 0 ff ff ff ") 
    {
    add_count(0);  
    delay(10);  
    }
  // 1
  else if (msg == "65 0 2 0 ff ff ff ") 
    {
    add_count(1);  
    delay(10);  
    }
  // 2
  else if (msg == "65 0 3 0 ff ff ff ") 
    {
    add_count(2);  
    delay(10);  
    }
  // 3
  else if (msg == "65 0 4 0 ff ff ff ") 
    {
    add_count(3);  
    delay(10);  
    }
  // 4
  else if (msg == "65 0 5 0 ff ff ff ") 
    {
    add_count(4);  
    delay(10);  
    }
  // 5
  else if (msg == "65 0 6 0 ff ff ff ") 
    {
    add_count(5);  
    delay(10);  
    }
  // 6
  else if (msg == "65 0 7 0 ff ff ff ") 
    {
    add_count(6);  
    delay(10);  
    }
  // 7
  else if (msg == "65 0 8 0 ff ff ff ") 
    {
    add_count(7);  
    delay(10);  
    }
  // 8
  else if (msg == "65 0 9 0 ff ff ff ") 
    {
    add_count(8);  
    delay(10);  
    }
  // 9
  else if (msg == "65 0 a 0 ff ff ff ") 
    {
    add_count(9);  
    delay(10);  
    }
  else ;  
  }
  
void add_count(int n) {
  if(counter<100) {
     counter=counter*10+n;
     Serial2.print("n0.val=");
     Serial2.print(counter);    
     Serial2.print("");
     Serial2.write(0xff);
     Serial2.write(0xff);
     Serial2.write(0xff);    
     delay(10);  
  }
}

void send_data_to_server() {
   int ret=3;
   unsigned long previos;
   int x=0;int f=0;
   char s;
   client.stop();
   if (client.connect(server, 80)) {
      //// sending data to the server
      // forming a string
      // uid per line
      String str="/barcode/get_barcode.php?barcode=";
      str+=barcode;
      str+="&count="+String(counter);
      Serial.print("str=");Serial.println(str);
     client.println("GET "+str+" HTTP/1.1");
     client.println("Host: freeparkovka.ru");
     client.println("User-Agent: ArduinoWiFi/1.1");
     client.println("Connection: close");
     client.println();
     // получение ответа
     previos=millis();
     for(int i=0;i<40;i++)
        response[i]=0; 
     
     do{
        if(client.available() > 0) { 
           // получать данные с сервера
           s = client.read();
           if(s=='#') 
              f=1;
           if(f==1) {
              response[x]=s;
              //Serial.print(response[x]);
              x++;
           }
           Serial.write(s);
        }
     }
     while((millis() - previos) < 5000);
     Serial.println(response);
      delay(10); 
      }
   else {
      // no connection
      Serial.println("connection failed");
   }
}

php script on the server to get data sent by the scanner

PHP
<?php

//Параметры MySQL
$location="localhost";
$user="bhx20666_parking";
$pass="parking12345678";
$db_name="bhx20666_barcode";

// connect db
if(! $db=mysqli_connect($location,$user,$pass,$db_name))
 {echo "connect error";}
else
 {;}
 

$query1=" INSERT INTO count SET 
          barcode='".$_GET['barcode']."',
          count='".$_GET['count']."',
          date='".date('Y-m-d H:i:s')."' ";  
if(mysqli_query($db,$query1)){
   echo "#yes";
}
else {
   echo "#no";
}


    
 
?>

Credits

sankmv

sankmv

5 projects • 8 followers

Comments