3. Wifi

This is the third challenge - time to control your robot wirelessly!

Language
C++
Est. Time
30—60min
Difficulty
Intermediate

Requires a PC and a Hedgehog Bot

If you've not built a Hedgehog Bot, complete the Bot challenge. You'll also need a microUSB cable.

This challenge is for Hedgehog Bot v2 only

Hedgehog Bot Kit V1 was distributed in 2015 and contains an Arduino Nano and breadboard. No further workshops are being developed for older versions of the kit, although a compatible version of this challenge is available.

  1. If statements

    For this Challenge, you need to learn about if statements. An if statement is saying “if this condition, then do this”. For example:

    if (x = 1) {
      Draw.smile(); // if x is 1 then draw a smile face on the screen
    } 
    

    You can also do else if statements. It’s like saying “else if this condition, then do this.” You write them following on from if statements. Here’s an example:

    if (x = 1) {
      Draw.smile(); // if x is 1 then draw a smile face on the screen
    }
    
    else if (x = 2) {
      Draw.wink(); // else if x is 2 then draw a wink face on the screen
    }
    
    else if (x = 3) {
      Draw.frown(); // else if x is 4 then draw a frown face on the screen
    }
    
  2. Creating a Wi-Fi remote

    You’ll be using your phone or computer to remote control your robot over wifi!

    You’ll be clicking different buttons to tell the robot what to do.

    In the code, you’l need to create else if statements to tell the robot what to do for the different buttons you press.

    Here’s an example for turning the motor right:

    else if (contains(webPage, "right")) {
      Motor.right();
    } 
    
  3. The code

    Below is the code for wifi enabling hedgehog bot. Copy and paste it into Arduino IDE.

    #include "drawFace.h"
    #include "espDualStepper.h"
    #include "ESP8266WiFi.h"
    #include "WebServer.h"
    #include "StringUtils.h"
    
    char wifiname[] = "wifiname"; // ***Type in the WiFi name in the quotes***
    char password[] = "password"; // ***Type in the Wifi password in the quotes***
    char ipString[24];
    
    WiFiServer Server(80);
    
    void setup() {
      Motor.start(D8, D7, D6, D5, D4, D3, D0, RX); // set up motors
      Draw.start(); // set up screen
      Draw.print("Connecting to ");
      Draw.println(wifiname);
      WiFi.begin(wifiname, password); // begin connecting to wifi
          
      while(WiFi.status() != WL_CONNECTED) { // != means not equal to.
        delay(500); // so this means while the wifi is not connected
        Draw.print("."); // print a "." to the screen
      }
          
      Server.begin(); // make the bot into a server so it can be controlled
      IPAddress myIp = WiFi.localIP();
      sprintf(ipString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
      Draw.println("IP:");
      Draw.print(ipString);
    }
    
    void loop() {
      WiFiClient client = Server.available();
      if(!client) {
        return;
      }
      while(!client.available()) {
        delay(1);
      }
      String webPage = client.readStringUntil('\r');
      client.flush();
          
      if (contains(webPage, "smile")) {
        Draw.smile(); // if you click the smile button, draw a smile on screen
      }
          
      // ***insert your else if statements here!***
          
      client.print(getWebPageHtml());
      client.flush();
      delay(1);
    } 
    

    Once again, If you’re using the WeMos D1 WiFi, you’ll need to replace Motor.start(D5, D6, D7, D8, D4, D3, D0, RX); with Motor.start(D7, D8, D9, D12, D0, D2, D5, D6);.

  4. Testing the Wi-Fi

    1. Change the Wi-Fi name and password near the top of the code in the quotes to the one given to you by a helper.
    2. Press the upload button . You’ll see the code compiling and uploading in the black box at the bottom. When it’s done you should see ‘Done Uploading’ just above the black box.

    3. You should see the screen saying Connecting to Wi-Fi! Wait for it to connect.

      If it doesn’t connect, check you’ve got the Wi-Fi name and password right and ask a helper. If you’re at home, you’ll need the SSID and passphrase for your home network; this is often found on a sticker on your Wi-Fi router.

  5. Remote control

    Once the robot is connected to the wifi it will say something like “IP:192.168.1.123”

    1. Connect a Wi-Fi enabled device—such as a smartphone or laptop—to the same Wi-Fi network that you connected Hedgehog Bot to.
    2. Open a web browser on that device, such as Firefox or Chrome.
    3. Type in the IP address on your Hedgehog Bot’s screen (such as 192.168.1.123) into the address box (which is normally at the top), and press Enter.

    If all has gone well, a remote control will pop up.

    Only the smile button should work. If you click it, a smile should show on the screen of your bot!

  6. Fixing the remote

    Now it’s time to program the other remote control buttons! Back to the code. In the loop, create else if statements for all these commands:

    Motor.forward();
    Motor.reverse();
    Motor.left();
    Motor.right();
    Motor.stop();
    
    Draw.smile();
    Draw.frown();
    Draw.wink();
    

    Here’s an example for drawing a frown on the screen:

    else if (contains(webPage, "frown")) {
      Draw.frown();
    } 
    

    Upload the code and test out the remote. Have you managed to get all the buttons to work? Don’t forget the stop button, otherwise your motors will move forever!

  1. Challenge Complete!

    You should now know a bit about if statements and networking. When you're ready, continue to the next challenge.

Next

Clock