Pages: [1]   Go Down
Author Topic: WiFi Connection problems - Digitalfoosball App  (Read 660 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am actually a newby on adruino. But want to make this stuff working. I read an article about building a table foosball with light barriers for transfering the goals to a website. Now I have the hardware but actually some problems with the code for the adruino one with the sparkfun wifi shield. The barriers are on Pin 2 and 4.

- I tested the uno with the wifi shield examples: working without any problems
- I tested the script I have without using wifi but I actually cant manage to use it with the wifi. I think there are some problems with the new version of the wifly libary?

This ist the code I am using - maybe you can help me on finding the errors?

Code:
/***********************
 ***  CONFIGURATION  ***
 ***********************/
#define TABLE_ID "main"

// - INTERNET_WIFLY (Sparkfun WiFly shield), or
// - INTERNET_MOCKUP (simulate connection)
#define INTERNET_WIFLY

// WIFI Access
char WIFLY_SSID[] = "gastknallt";
char WIFLY_PASSPHRASE[] = "berlin2012";

// Server
char SERVER_NAME[] = "82.165.145.84";
byte SERVER_IP[] = {82, 165, 165, 84};

#include "WiFly.h"

// Define to enable output messages on Serial
#define DEBUG_APP

/**  CONSTANTS AND GLOBALS  **/

// Pin constants
const int GOAL_A_PIN = 2;
const int GOAL_B_PIN = 4;
const int RESET_A_PIN = 3;
const int RESET_B_PIN = 5;
const int LED_PIN = 8; // Standard LED pin 13 used by Spi

// Uniqueness token, initialized by random

#if defined(INTERNET_WIFLY)
// The client for communication with goal server
Client client(SERVER_IP, 80);
#endif

// Whether we think we have associated/connected
boolean associated = false;
boolean connected = false;
int failures = 0;

// Debugging
#ifdef DEBUG_APP
#define LOG(message) Serial.print(message)
#else
#define LOG(message) (((0)))
#endif

/**************************
 ***  HELPER FUNCTIONS  ***
 **************************/

void disconnect()
{
#if defined(INTERNET_WIFLY)
  client.stop();
#endif

  connected = false;
}

boolean ensureConnection(boolean checkWiFlyStatus)
{
#if defined(INTERNET_WIFLY)
WiFlyDevice::Status status = checkWiFlyStatus ? wiFly.getStatus(false) : WiFlyDevice::StatusConnected;
  if (status == WiFlyDevice::StatusError || status == WiFlyDevice::StatusNotAssociated
    || status == WiFlyDevice::StatusNoIp)
  {
    if (associated)
    {
      if (status == WiFlyDevice::StatusNotAssociated)
        LOG("ERROR: Association LOST, resetting...\n");
      else if (status == WiFlyDevice::StatusNoIp)
        LOG("ERROR: No WiFi IP, resetting...\n");
      else
        LOG("ERROR: WiFi problem, resetting...\n");

      reset();
    }

    LOG("Joining network...\n");
    if (!wiFly.join(WIFLY_SSID, WIFLY_PASSPHRASE))
    {
      LOG("ERROR: Joining network failed, trying again later.\n");
      flashError(1);

      if (failures++ >= 3)
      {
        LOG("ERROR: Three failures, resetting.\n");
        reset();
      }

      return false;
    }

    LOG("Network joined.\n");
    associated = true;
    connected = false;
    failures = 0;
  }

  status = checkWiFlyStatus ? wiFly.getStatus(false) : WiFlyDevice::StatusConnected;
  if (!client.isConnected() || status != WiFlyDevice::StatusConnected)
  {
    if (connected)
      LOG("Connection LOST, reconnecting...\n");
    else
      LOG("Preconnecting to server...\n");

    if (!client.connect(false) || !client.isConnected())
    {
      LOG("Connection FAILED, trying again later. (003) \n");
      flashError(2);

      if (failures++ >= 3)
      {
        LOG("ERROR: Three failures, resetting.\n");
        reset();
      }

      return false;
    }

    LOG("Connected.\n");
    connected = true;
    failures = 0;
  }

  delay(250);
#endif

  return true;
}

void flashError(int errorNo)
{
  int i;
  for (int i=0; i<8; i++)
  {
    digitalWrite(LED_PIN, HIGH);
    delay(100);
    digitalWrite(LED_PIN, LOW);
    delay(100);
  }

  delay(500);

  for (int i=0; i<errorNo; i++)
  {
    digitalWrite(LED_PIN, HIGH);
    delay(500);
    digitalWrite(LED_PIN, LOW);
    delay(500);
  }

  delay(500);
}

void reset()
{
  associated = false;
  connected = false;
  
#if defined(INTERNET_WIFLY)
  wiFly.begin();
#endif
  failures = 0;
}


/*****************************
 ***  Setup and main loop  ***
 *****************************/

void setup()
{
  Serial.begin(9600);
  LOG("Initializing...\n");

  pinMode(GOAL_A_PIN, INPUT);
  pinMode(GOAL_B_PIN, INPUT);
  pinMode(RESET_A_PIN, OUTPUT);
  pinMode(RESET_B_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);

  digitalWrite(GOAL_A_PIN, LOW);
  digitalWrite(GOAL_B_PIN, LOW);
  digitalWrite(RESET_A_PIN, LOW);
  digitalWrite(RESET_B_PIN, LOW);

  for (int i=0; i<10; i++)
  {
    digitalWrite(LED_PIN, LOW);
    delay(50);
    digitalWrite(LED_PIN, HIGH);
    delay(50);
  }

  randomSeed(analogRead(0));
  token = random(65535);

#ifdef INTERNET_WIFLY
  wiFly.begin();

  while (!ensureConnection(true))
    delay(1000);
#endif

  digitalWrite(RESET_A_PIN, HIGH);
  digitalWrite(RESET_B_PIN, HIGH);
  delay(10);
  digitalWrite(RESET_A_PIN, LOW);
  digitalWrite(RESET_B_PIN, LOW);
  delay(10);

  digitalWrite(LED_PIN, LOW);
  LOG("Initialization done.\n");
}

void loop()
{
  char string[512];

  // Analyze inputs until we find a goal (HIGH is true)
  // Also check that we are still connected to the server and access point

  int playerPin = GOAL_A_PIN;
  long checkCount = 0;
  while (true)
  {
    playerPin = playerPin == GOAL_A_PIN ? GOAL_B_PIN : GOAL_A_PIN;
    if (digitalRead(playerPin) == HIGH)
      break;

    if ((checkCount % 200) == 0)
      while (!ensureConnection(checkCount == 0))
        delay(5000);

    delay(10);
    checkCount = (checkCount + 1) % 2000;
  }

  digitalWrite(LED_PIN, HIGH);
#ifdef DEBUG_APP
  sprintf(string, "Goal for %s team, ID %lu\n", playerPin == GOAL_A_PIN ? "home" : "visitors", token);
  LOG(string);
#endif

  // Retry at most 3 times

  boolean success = false;
  while (!success && failures < 3)
  {
    while (!ensureConnection(false))
      delay(1000);

    // Send a POST to the goal server

    char content[128];
    sprintf(content, "token=%lu&table=%s", token, TABLE_ID);
    sprintf(string, "POST %s/events.php?type=goal&team=%s HTTP/1.1\r\n"
      "Host: %s\r\n"
      "User-Agent: Arduino/KnalltsKicker\r\n"
      "Content-Type: application/x-www-form-urlencoded\r\n"
      "Content-Length: %d\r\n\r\n%s",
    playerPin == GOAL_A_PIN ? "home" : "visitors", SERVER_NAME,
    strlen(content), content);

    LOG("Sending request...\n");
    LOG(string);
    LOG("\n");
#if defined(INTERNET_ETHERNET) || defined(INTERNET_WIFLY)
    client.print(string);
#endif

    LOG("Request done, checking response...\n");
#if defined(INTERNET_ETHERNET)
    success = findInEthernetResponse("200 OK", 5000);
#elif defined(INTERNET_WIFLY)
    success = wiFly.findInResponse("200 OK", 5000);
#else
    delay(500);
    success = true;
#endif

    if (success)
    {
      LOG("Request successful.\n");
      failures = 0;
    }
    else
    {
      LOG("Request FAILED.\n");
      failures++;

      disconnect();
    }
  }

  token++;

  if (!success)
  {
    LOG("Giving up and resetting...\n");

    reset();
    ensureConnection(true);
  }

  // Reset goal flip-flop and wait for input to be false (LOW) again
  do
  {
    digitalWrite(playerPin == GOAL_A_PIN ? RESET_A_PIN : RESET_B_PIN, HIGH);
    delay(10);
    digitalWrite(playerPin == GOAL_A_PIN ? RESET_A_PIN : RESET_B_PIN, LOW);
    delay(10);
  }
  while (digitalRead(playerPin) != LOW);

  digitalWrite(LED_PIN, LOW);

  // Disconnect and preconnect again

  if (success)
  {
    disconnect();
    ensureConnection(false);
  }

  LOG("Ready for next goal.\n");
}


ERRORs

Code:
sketch_sep21b.cpp: In function 'boolean ensureConnection(boolean)':
sketch_sep21b:78: error: 'Status' is not a member of 'WiFlyDevice'
sketch_sep21b:78: error: expected `;' before 'status'
sketch_sep21b:79: error: 'status' was not declared in this scope
sketch_sep21b:79: error: 'StatusError' is not a member of 'WiFlyDevice'
sketch_sep21b:79: error: 'StatusNotAssociated' is not a member of 'WiFlyDevice'
sketch_sep21b:80: error: 'StatusNoIp' is not a member of 'WiFlyDevice'
sketch_sep21b:84: error: 'StatusNotAssociated' is not a member of 'WiFlyDevice'
sketch_sep21b:86: error: 'StatusNoIp' is not a member of 'WiFlyDevice'
sketch_sep21b:95: error: 'wiFly' was not declared in this scope
sketch_sep21b:115: error: 'status' was not declared in this scope
sketch_sep21b:115: error: 'wiFly' was not declared in this scope
sketch_sep21b:115: error: 'StatusConnected' is not a member of 'WiFlyDevice'
sketch_sep21b:116: error: 'class Client' has no member named 'isConnected'
sketch_sep21b:116: error: 'StatusConnected' is not a member of 'WiFlyDevice'

Thank you so much!!!
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

sketch_sep21b:78: error: 'Status' is not a member of 'WiFlyDevice'

Why do you believe WiFlyDevice has a member called Status?  It doesn't, hence the compile error when you try to access a member that doesn't exist.

Ditto for pretty much all the errors (although I didn't confirm each and every one).
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To be honest I have no clue what this part of the code does. I thought it's defined in the wifly libary :o ...

Maybe you can help me to correct the program? What I know is that the problem is the wifi connection check... But I don't know how to correct it.

Thank you!!
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't have a WiFly shield, and have no experience using one.  I did notice that the library does come with several examples.  Have you looked through those to see if they do what you are trying to do?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I think I found the solution. There was a private libary which I changed to public.
I still have one problem, within the serial monitor the board says:

Match, skipping rest of line.
Waiting for response '8'
Wait timed out.

Do you know what the 8 means. Is there an error with the pin?

Thank you!
Logged

Pages: [1]   Go Up
Jump to: