PAJ
Published © LGPL

Pick to Light Project 2 WiFi

In the second project in my pick to light, I am using WIFI to pass the data between PC and Arduino.

EasyFull instructions provided2 hours937

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
LED (generic)
LED (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
×4
Breadboard (generic)
Breadboard (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1

Story

Read more

Schematics

Pick to light UDP Project 2

Connections for the UDP Pick to Light

Code

Arduino sketch

Arduino
The sketch to be loaded to the MKR1000
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>

const int OKbutton = 2; // set the pin for the okay button/switch
const int Bin1 = 8; // the pin that the LED for bin 1 is attached to
const int Bin2 = 9; // the pin that the LED for bin 2 is attached to
String mydata = " ";  // a variable to read incoming serial data into set to empty string
String myseq = " ";
int buttonPress = 0; // a variable to store state of the button/switch

int status = WL_IDLE_STATUS;
#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

unsigned int localPort = 2390;      // local port to listen on

char packetBuffer[255]; //buffer to hold incoming packet
char  ReplyBuffer[5] = "   ";       // a string to send back
String mystring;
WiFiUDP Udp;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);// all serial statements are not needed once set up
//while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}

  // 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 SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);

}

void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    String mystring(packetBuffer);
    mydata = mystring.substring(4);
    myseq = mystring.substring(0,4);
    Serial.println(mydata);
    Serial.println(myseq);
    
       while (mydata != " "){ //Test to see if mydata is still empty if it isn't check which bin should be lit up
      if (mydata == "Bin1"){ // start bin 1 routine
        digitalWrite(Bin1, HIGH); // light LED for bin 1 by setting pin high
        digitalWrite(Bin2, LOW);  // set LED for bin 2 off by setting pin low
            while (buttonPress != HIGH) { // wait for button press loop
              buttonPress = digitalRead(OKbutton); //keep checking button
              mydata = " "; //set mydata back to empty string
    }
        digitalWrite(Bin1, LOW);// turn led for bin 1 off
        Serial.println("Picked"); //send message to PC
        buttonPress = 0; //reset button low
        delay(1000);
      }
      if (mydata == "Bin2"){// start bin 2 routine
        digitalWrite(Bin2, HIGH);// light LED for bin 2 by setting pin high
        digitalWrite(Bin1, LOW); // set LED for bin 1 off by setting pin low
            while (buttonPress != HIGH) { // wait for button press loop
              buttonPress = digitalRead(OKbutton); //keep checking button
              mydata = " "; //set mydata back to emty string
    }
        digitalWrite(Bin2, LOW); // turn led for bin 1 off
        Serial.println("Picked"); //send message to PC
        buttonPress = 0; //reset button low
        delay(1000);
      }
 
    }

    // send a reply, to the IP address and port that sent us the packet we received
    myseq.toCharArray(ReplyBuffer, 5);
    //char ReplyBuffer[5] = "0001"; // for testing only
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
    Serial.println(ReplyBuffer);
  }
}


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

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

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

Python Script

Python
Python Script to be run.
## load the necessary libraries
import csv
import socket
import time
import sys

UDP_IP = "192.168.1.119" ## the ip of our Arduino
UDP_PORT = 2390 ## the port we wish to communicate on

print("UDP target IP:", UDP_IP) ## display ip to user
print("UDP target port:", UDP_PORT) ## display port to user

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # create a socket


time.sleep(5)
##open csv file and read it in one line at a time
with open('sequence1.txt') as csvDataFile: 
    csvReader = csv.reader(csvDataFile)
    for row in csvReader: ##for each line do the following
        myseq = row[0] ##read in the sequence number
        mystate = row[1] ##read in the bin number
        myrow = row[0] + row[1]
        print("Current sequence being picked ",myseq,"from ", mystate)
        sock.sendto(bytes(myrow, "utf-8"), (UDP_IP, UDP_PORT)) # send seq and bin number
        data = "" # set data to blank to enter while loop i=until data is received
        while data == "": # until data is received keep looping through
            (data, addr) = sock.recvfrom(1024) # set data to data received from socket
            mytest = data.decode( "utf-8") #set mytest to equal value received over socket
            print ("picked = ", mytest) # print the value received
            if mytest != myseq: # test what has ben received matches what was expected i.e. last seq sent
                print ("there is a sequence fault at sequence", mytest) # display message to indicate a fault
                sys.exit() #end program execution if fault exists

Sequence

Plain text
Sequence file for the python program
0001,Bin1
0002,Bin2
0003,Bin1
0004,Bin2
0005,Bin1
0006,Bin2
0007,Bin1
0008,Bin2
0009,Bin1
0010,Bin2
0011,Bin1
0012,Bin2
0013,Bin1

Credits

PAJ

PAJ

3 projects • 4 followers

Comments