Bastiaan Slee
Created September 16, 2018 © GPL3+

Arduino WiFi Robot Controller

Control any WiFi robot or WiFi sensor with an Arduino WiFi controller, with single protocol to rule them all!

ExpertWork in progress20 hours897
Arduino WiFi Robot Controller

Things used in this project

Hardware components

Arduino Esplora
Arduino Esplora
×1
ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
Adafruit PowerBoost 1000C
×1
Slide Switch
Slide Switch
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
×1
MeArm
×1
Adafruit 16-Channel 12-bit PWM/Servo Driver
×1
Monochrome 1.3" 128x64 OLED graphic display
×1

Software apps and online services

Arduino Web Editor
Arduino Web Editor

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Arduino Esplora + ESP8266 + PowerBoost

Arduino MKR WiFi 1010 + PWM/Servo board + MeArm + OLED display

Code

Arduino Esplora controller code

Arduino
This code is far from complete, it's just a start!!!
/*
  This code is far from complete, it's just a start!!!
  Code is partly based on Esplora Test from Mike Barela http://21stdigitalhome.blogspot.com/
*/

#include <Esplora.h>         // Arduino Esplora specific library
#include <TFT.h>             // Hardware-specific library
#include <SPI.h>             // SPI communications library


// Emulate Serial on pins 3/11 if not present
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,3); // RX, TX

// WiFiEsp-2.2.2 - Version: Latest 
#include <WiFiEsp.h>
//#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>

char ssid[] = "Arduino_MeArm";            // your network SSID (name)
char pass[] = "mrAeM_oniudrA";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//IPAddress remoteServer = 192.1.1.100;
unsigned int remotePort = 10002;

char packetBufferIn[255];          // buffer to hold incoming packet

const int UDP_TIMEOUT = 2000;    // timeout in miliseconds to wait for an UDP packet to arrive

byte packetBufferOut[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
WiFiEspUDP Udp;


/*
 Color definitions
*/
#define	COLOR_BLACK   0x0000 //0x0000
#define	COLOR_BLUE    0xF800 //0x001F
#define	COLOR_RED     0x001F //0xF800
#define	COLOR_GREEN   0x07E0 //0x07E0
#define COLOR_CYAN    0xFFE0 //0x07FF
#define COLOR_MAGENTA 0xF81F //0xF81F
#define COLOR_YELLOW  0x07FF //0xFFE0
#define COLOR_WHITE   0xFFFF //0xFFFF

/*
  This array holds the last sensed state of each of the buttons read.
  Later in the code, you'll read the button states, and compare them
  to the previous states that are stored in this array. If the two
  states are different, it means that the button was either pressed or released.
 */
boolean buttonStates[8];

/*
  This array holds the names of the buttons being read.
  Later in the sketch, you'll use these names with
  the method Esplora.readButton(x), where x is one of these buttons.
 */
const byte buttons[] = {
  JOYSTICK_DOWN,
  JOYSTICK_LEFT,
  JOYSTICK_UP,
  JOYSTICK_RIGHT,
  SWITCH_RIGHT, 
  SWITCH_LEFT, 
  SWITCH_UP, 
  SWITCH_DOWN, 
};




void setup() {
  Serial.begin(9600); // serial port used for debugging

  // Start the TFT Screen
  EsploraTFT.begin();  
  EsploraTFT.background(0,0,0);  // clear the screen with black
  EsploraTFT.setTextSize(2);
  displayString(0, 0,"Welcome to", COLOR_GREEN);
  displayString(0,16," Arduino", COLOR_GREEN); 
  displayString(0,32,"  Esplora", COLOR_GREEN);
  EsploraTFT.setTextSize(1);


  // initialize serial for ESP module
  mySerial.begin(115200);                         // default ESP's baud rate for AT commands
  mySerial.println("AT+UART_CUR=9600,8,1,0,0");   // update the baud rate to 9600
  mySerial.end();                                 // end the serial
  mySerial.begin(9600);                           // use the new ESP's baud rate
  WiFi.init(&mySerial);                           // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
  } else {
    // attempt to connect to WiFi network
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
    if ( status == WL_CONNECTED) {
      // you're connected now, so print out the data
      Serial.println("You're connected to the network");
      Udp.begin(localPort);
    }
  }

  EsploraTFT.background(0,0,0);  // clear the screen with black

}

 
void loop() {
  int x_axis, y_axis, z_axis, g_axis;
  x_axis=0, y_axis=0, z_axis=0, g_axis=0;

  int S1, S2, S3, S4;   // holds values of switches 1 to 3
  S1=HIGH; S2=HIGH; S3=HIGH; S4=HIGH;
  S1 = Esplora.readButton(SWITCH_1);
  S2 = Esplora.readButton(SWITCH_2);  
  S3 = Esplora.readButton(SWITCH_3);
  S4 = Esplora.readButton(SWITCH_4);

  if(S1==LOW) { // Gripper changes
    if(Esplora.readJoystickY() > 20)   // if the joystick is moved significantly down
      g_axis = -3;
    if(Esplora.readJoystickY() < -20)  // if the joystick is moved significantly up
      g_axis = 1;
  }
  else if(S2==LOW) {  // ???
  }
  else if(S3==LOW) { // Use Accelerometer
    if(Esplora.readAccelerometer(X_AXIS) > 75)   // if the joystick is moved significantly left
      x_axis = -10;  // Map to 0-10 range
    else if(Esplora.readAccelerometer(X_AXIS) > 20)   // if the joystick is moved significantly left
      x_axis = map(Esplora.readAccelerometer(X_AXIS), 0, 75, 0, -10);  // Map to 0-10 range
    if(Esplora.readAccelerometer(X_AXIS) < -75)   // if the joystick is moved significantly right
      x_axis = 10;  // Map to 0-10 range
    else if(Esplora.readAccelerometer(X_AXIS) < -20)   // if the joystick is moved significantly right
      x_axis = map(Esplora.readAccelerometer(X_AXIS), 0, -75, 0, 10);  // Map to 0-10 range

    if(Esplora.readAccelerometer(Y_AXIS) > 150)   // if the joystick is moved significantly down
      y_axis = -10;  // Map to 0-10 range
    else if(Esplora.readAccelerometer(Y_AXIS) > 70)   // if the joystick is moved significantly down
      y_axis = map(Esplora.readAccelerometer(Y_AXIS), 25, 150, 0, -10);  // Map to 0-10 range
    if(Esplora.readAccelerometer(Y_AXIS) < -60)   // if the joystick is moved significantly down
      y_axis = 10;  // Map to 0-10 range
    else if(Esplora.readAccelerometer(Y_AXIS) < -5)   // if the joystick is moved significantly down
      y_axis = map(Esplora.readAccelerometer(Y_AXIS), 25, -60, 0, 10);  // Map to 0-10 range

    z_axis = map(Esplora.readAccelerometer(Z_AXIS), 50, 150, -5, 5);  // Map to 0-10 range
  }
  else if(S4==LOW) { // Z-axis changes
    if(Esplora.readJoystickY() > 20)   // if the joystick is moved significantly down
      z_axis = map(Esplora.readJoystickY(), 0, 512, 0, -10);  // Map to 0-10 range
    if(Esplora.readJoystickY() < -20)  // if the joystick is moved significantly up
      z_axis = map(Esplora.readJoystickY(), 0, -512, 0, 10);  // Map to 0-10 range
  }
  else { // No button pressed
    if(Esplora.readJoystickY() > 20)   // if the joystick is moved significantly down
      y_axis = map(Esplora.readJoystickY(), 0, 512, 0, -10);  // Map to 0-10 range
    if(Esplora.readJoystickY() < -20)  // if the joystick is moved significantly up
      y_axis = map(Esplora.readJoystickY(), 0, -512, 0, 10);  // Map to 0-10 range
    if(Esplora.readJoystickX() > 20)   // if the joystick is moved significantly left
      x_axis = map(Esplora.readJoystickX(), 0, 512, 0, -10);  // Map to 0-10 range
    if(Esplora.readJoystickX() < -20)  // if the joystick is moved significantly right
      x_axis = map(Esplora.readJoystickX(), 0, -512, 0, 10);  // Map to 0-10 range
  }  

  displayString(10, 10,"x_axis", COLOR_WHITE);
  displayString(10, 20,"y_axis", COLOR_WHITE);
  displayString(10, 30,"z_axis", COLOR_WHITE);
  displayString(10, 40,"g_axis", COLOR_WHITE);

  displayInt2(x_axis, 70, 10, COLOR_GREEN, COLOR_BLACK);
  displayInt2(y_axis, 70, 20, COLOR_GREEN, COLOR_BLACK);
  displayInt2(z_axis, 70, 30, COLOR_GREEN, COLOR_BLACK);
  displayInt2(g_axis, 70, 40, COLOR_GREEN, COLOR_BLACK);

  // UDP SEND LOGIC GOES HERE
  delay(500);

}



void displayString(byte x, byte y, char *text, uint16_t color) { // write string to LCD
  EsploraTFT.stroke(color);
  EsploraTFT.text(text,x,y);
}

void displayChar(byte x, byte y, char text, uint16_t color) {  // write character to LCD
  EsploraTFT.stroke(color);
  EsploraTFT.text(text,x,y);
}

// display an unsigned character on LCD (if you have a signed value, use displayInt2)
void displayInt(unsigned int num, byte nx, byte ny, unsigned int color, unsigned int backcolor) {
  displayInt2(num, nx, ny, color, backcolor);
}

void displayInt2(int num, byte nx, byte ny, unsigned int color, unsigned int backcolor) {
  char text[5];
  String str;
  str=String(num);
  str.toCharArray(text,5);

  EsploraTFT.noStroke(); // don't draw a line around the next rectangle
  EsploraTFT.fill(backcolor); // set the fill color to green
  EsploraTFT.rect(nx, ny, 29, 7); //draw a rectangle across the screen
  EsploraTFT.stroke(color);
  EsploraTFT.text(text,nx,ny);  // print handles signs, leading space, etc. in this version
}

Arduino MKR WiFi 1010 robot code

Arduino
This code is far from complete, it's just a start!!!
/*
  This code is far from complete, it's just a start!!!
*/


// Adafruit PWM Servo Driver Library - Version: 1.0.2
#include <Adafruit_PWMServoDriver.h>

#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>

int led =  LED_BUILTIN;
int status = WL_IDLE_STATUS;

char ssid[] = "Arduino_MeArm";            // your network SSID (name)
char pass[] = "mrAeM_oniudrA";        // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)

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

char packetBuffer[255]; //buffer to hold incoming packet
char replyBuffer[2];       // a string to send back

WiFiUDP Udp;




/* meArm IK joysticks - York Hackspace May 2014
 * Using inverse kinematics with joysticks
 */
#include "meArm.h"
#include <Servo.h>

int basePin = 6;
int shoulderPin = 4;
int elbowPin = 5;
int gripperPin = 7;

meArm arm;






void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  arm.begin(basePin, shoulderPin, elbowPin, gripperPin);


  Serial.println("Access Point Web Server");

  pinMode(led, OUTPUT);      // set the LED pin mode

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv != "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // by default the local IP address of will be 192.168.4.1
  // you can override it with the following:
  // WiFi.config(IPAddress(10, 0, 0, 1));

  // print the network name (SSID);
  Serial.print("Creating access point named: ");
  Serial.println(ssid);

  // Create open network. Change this line if you want to create an WEP network:
  status = WiFi.beginAP(ssid, pass);
  if (status != WL_AP_LISTENING) {
    Serial.println("Creating access point failed");
    // don't continue
    while (true);
  }

  // wait 10 seconds for connection:
  delay(10000);

  // start the udp server on port 80
  Udp.begin(localPort);

  // you're connected now, so print out the status
  printWiFiStatus();
  
}


void loop() {

  // compare the previous status to the current status
  if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }


  // 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;
    } else {
      packetID = packetBuffer[0];
      packetCMD = packetBuffer[1];
      packetVAL = packetBuffer[2];
      
      blinkLED();
      if (packetCMD == "GC"){
        arm.closeGripper();
      }
      if (packetCMD == "GO"){
        arm.openGripper();  
      }
    }
    Serial.println("Contents:");
    Serial.println(packetBuffer);


    if (len > 0) {
      packetBuffer[len] = 0;
    } else {
      packetID = packetBuffer[0];
      packetCMD = packetBuffer[1];
      packetVAL = packetBuffer[2];
      packetVALX = packetBuffer[3];
      packetVALY = packetBuffer[4];
      packetVALZ = packetBuffer[5];
    }
    

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    replyBuffer[0] = packetID;
    replyBuffer[1] = "ACK";
    Udp.write(replyBuffer);
    Udp.endPacket();


    if (len <= 0) {
      blinkLED();

      if (packetCMD == "GRC"){
        arm.openGripper();
      }
      if (packetCMD == "GRO"){
        if (( arm.getG()-(packetVAL/10) ) > 0) {
          arm.closeGripper( arm.getG()-(packetVAL/10) );  
        } else {
          arm.closeGripper(0);  
        }
      }

      if (packetCMD == "ARM"){
        //if (arm.isReachableCylinder(arm.getX() + packetVALX, arm.getY() + packetVALY, arm.getZ() + packetVALZ) {
          arm.gotoPointCylinder(arm.getX() + packetVALX, arm.getY() + packetVALY, arm.getZ() + packetVALZ);
      //}
      }
    }

  }
}

void blinkLED() {
  digitalWrite(led, HIGH); // Turn the LED on
  delay(50);
  digitalWrite(led, LOW);  // Turn the LED off

}

Credits

Bastiaan Slee

Bastiaan Slee

5 projects • 34 followers
Tinkerer in the field of Home Automation, with the main goal: fun! Using Raspberry Pi, Arduino (+clones), LoRaWAN, NodeRed, 3D Printing

Comments