Go Down

Topic: Analog pin locked to 1023 when using wifi shield (Read 94 times) previous topic - next topic

Colonel Panic

I'm building a shield for monitoring the environment in my living room, with temperature, humidity, light and atmospheric pressure but I've noticed a weird thing with my photocell. I read each sensor with a timer, and as long as I don't use the wifi shield to send a request, the photocell reads accurately (between 0-1023) but as soon as I add the wifi request, the values move to 1023 and stay there until I reset the system.

This is what it looks like with the serial monitor (column 1 = measured value, column 2 = filtered value)

Code: [Select]
    Connected to Wifi!
    723 -- 0
    729 -- 723
    723 -- 723
    724 -- 724
    721 -- 723
    720 -- 721
    720 -- 720
    723 -- 720
    721 -- 721
    Sending update...
    735 -- 723
    1023 -- 735
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    Sending update...
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    1023 -- 1023
    Sending update...


I've read the photocell voltage and confirm that it lands 0-5V and just now after moving the analog pin to 5V or GND directly, I get the expected value, so I have no idea why it spikes as soon as I send requests.

Here is also the full listing of the code. I'm using an Uno with the Arduino Wifi shield.

Code: [Select]
    #include <Event.h>
    #include <Timer.h>
    #include <WiFi.h>
    #include <SPI.h>
    #include <WiFiClient.h>
    #include <WiFiServer.h>
    #include <WiFiUdp.h>
    #include <medianFilter.h>

    /* WIFI STUFF */
    char ssid[] = "";      //  your network SSID (name)
    char pass[] = "";   // your network password
    int status = WL_IDLE_STATUS;
    WiFiClient client;

    // website values
    const String publicKey = "";
    const String privateKey = "";
    char server[] = "data.sparkfun.com";
    unsigned long lastConnectionTime = 0;

    // sensor inputs
    #define LIGHT_PIN  A0

    // sensor values
    const byte NUM_FIELDS = 5;
    const String fieldNames[NUM_FIELDS] = {"light", "humidity", "temperature", "pressure", "co2"};
    String fieldData[NUM_FIELDS];
    #define LIGHT_INDEX 0
    #define HUMIDITY_INDEX 1
    #define TEMP_INDEX 2
    #define PRESSURE_INDEX 3
    #define CO2_INDEX 4

    // timers
    Timer t;
    int sensorTimer, httpTimer;
    const int SENSOR_INTERVAL = 6;
    const int UPDATE_INTERVAL = 60;

    // filters
    medianFilter lightFilter;

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(115200);
     
      // check for the presence of the shield:
      if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue:
        while (true);
      }
      // connect to 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);

        delay(2000);
      }
     
      Serial.println("Connected to Wifi!");
      lightFilter.begin();
     
      sensorTimer = t.every(SENSOR_INTERVAL*1000.0, readSensors);
      //httpTimer = t.every(UPDATE_INTERVAL*1000.0, sendUpdate);
    }

    void loop() {
      t.update();
    }

    void readSensors() {
      //Serial.println("reading sensors...");
     
      // light
      int lightSensorReading = analogRead(LIGHT_PIN);
      int filteredLightReading = lightFilter.run(lightSensorReading);
      fieldData[LIGHT_INDEX] = String(filteredLightReading);
     
      Serial.print(lightSensorReading);
      Serial.print(" -- ");
      Serial.println(filteredLightReading);
    }

    void sendUpdate() {
      client.stop();
     

     
      Serial.println("Sending update...");
      //t.stop(sensorTimer);
       
      // if there's a successful connection:
      if (client.connect(server, 80)) {
        // send the HTTP PUT request:
        client.print("GET /input/");
        client.print(publicKey);
        client.print("?private_key=");
        client.print(privateKey);
        for (int i=0; i<NUM_FIELDS; i++)
        {
          client.print("&");
          client.print(fieldNames[i]);
          client.print("=");
          client.print(fieldData[i]);
        }
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(server);
        client.println("Connection: close");
        client.println();

        // note the time that the connection was made:
        lastConnectionTime = millis();
       
        //Serial.println("request complete");
      }
      else {
        // if you couldn't make a connection:
        Serial.println("connection failed");
      }
     
      //sensorTimer = t.every(SENSOR_INTERVAL*1000, readSensors);
    }


For some reason sending web requests is screwing with the analog measurement, and I have no idea where to look for answers.
Mechanical engineer
Robotics enthusiast
Working on YardBot --http://www.yardbot.ca

PaulS


Colonel Panic

Mechanical engineer
Robotics enthusiast
Working on YardBot --http://www.yardbot.ca

PaulS

The schematic indicates that it uses A5, though I can't tell for what. There have been some comments that A0 and A1 are used as the card detect and write detect pins, but the schematic doesn't show that. Does using A3 exhibit the same issue?

Go Up