Skip to content

Ethernet library assigns incorrect IP Address. #2168

Closed
@Alienwaren

Description

@Alienwaren

Hello, I found annoying bug in Ethernet Library. When I want to use static IP Address not from DHCP, it assigns incorrect IP Address. Everything in it is incorrect, not even one octet is correct. Getting IP from DHCP does not reproduce problem; assigned IP is correct.
I'm using Arduino Uno R3 with Ethernet Shield clone, Arduino 1.5.6-r2, using following code:

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <OneWire.h>

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ipXively(192, 168, 0, 20);
IPAddress gateway(192,168,0,1);
IPAddress netmask(255,255,255,0);
// Your Xively key to let you upload data
char xivelyKey[] = "xyz";

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = A0;

// Define the strings for our datastream IDs
char sensorId[] = "xyz";
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(458360579, datastreams, 1 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  analogReference(DEFAULT);
  Serial.println("Starting single datastream upload to Xively...");
  Serial.println();

  Ethernet.begin(mac, ipXively, gateway, netmask);
  Serial.println(Ethernet.localIP());
  /////////////////////////////////
}

void loop() {
  float sensorValue = analogRead(sensorPin);
  float voltage = sensorValue * (5.0 / 1023);
    datastreams[0].setFloat(voltage);
  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());
  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  Serial.print("xivelyclient.put returned ");
  Serial.println(ret);
  Serial.println();
  delay(5000);
  float temperature = getTemp(3);
  Serial.println(temperature);
}
float getTemp(int pin)
{
  OneWire ds(pin);  // on some pin (PULLUP!)
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return -5000;
  }else
  {
    ds.reset();
    ds.select(addr);
    ds.write(0x44, 1);        // start conversion, with parasite power on at the end\

    delay(1000);     // maybe 750ms is enough, maybe not
    // we might do a ds.depower() here, but the reset will take care of it.

    present = ds.reset();
    ds.select(addr);    
    ds.write(0xBE);         // Read Scratchpad
    for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
     int16_t raw = (data[1] << 8) | data[0];
    if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
    } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
      //// default is 12 bit resolution, 750 ms conversion time
    }
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    return celsius;
    }
  }
}

The output on serial monitor (there is IP Address): [ScreenShot]
https://www.dropbox.com/s/6do3m761ey5jzob/Zrzut%20ekranu%202014-07-06%2011.51.25.png

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions