Hirdetés

Új hozzászólás Aktív témák

  • junhum

    tag

    Sziasztok!

    Volna egy olyan problémám, hogy ez a kód:
    #include <OneWire.h>
    #include <SPI.h>
    #include <Ethernet.h>

    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = {0xAD, 0xBD, 0xCD, 0xED, 0xFD, 0x0D};
    IPAddress ip(192, 168, 0, 100);

    // ThingSpeak Settings
    char thingSpeakAddress[] = "api.thingspeak.com";
    String writeAPIKey = "xxxxxxxxxxxxxxxxxxx";
    const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

    // Variable Setup
    long lastConnectionTime = 0;
    boolean lastConnected = false;
    int failedCounter = 0;

    // Initialize Arduino Ethernet Client
    EthernetClient client;

    // Initialize the Ethernet server library
    // with the IP address and port you want to use
    // (port 80 is default for HTTP):
    EthernetServer server(80);


    OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)

    void setup() {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    // wait for serial port to connect. Needed for Leonardo only
    // Start Ethernet on Arduino
    startEthernet();
    }


    // start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    }

    void loop() {
    byte i;
    byte present = 0;
    byte type_s;
    byte data[12];
    byte addr[8];
    float celsius, fahrenheit, celsiuss;

    if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(850);
    return;
    }

    if (OneWire::crc8(addr, 7) != addr[7]) {
    Serial.println("CRC is not valid!");
    return;
    }
    Serial.println();

    ds.reset();
    ds.select(addr);
    ds.write(0x44); // start conversion, use ds.write(0x44,1) 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

    Serial.print(" Data = ");
    Serial.print(present, HEX);
    Serial.print(" ");
    for ( i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
    }
    Serial.print(" CRC=");
    Serial.print(OneWire::crc8(data, 8), HEX);
    Serial.println();

    // Convert the data to actual temperature
    // because the result is a 16 bit signed integer, it should
    // be stored to an "int16_t" type, which is always 16 bits
    // even when compiled on a 32 bit processor.
    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;
    Serial.print(" Temperature = ");
    Serial.print(celsius);
    Serial.print(" Celsius, ");
    Serial.print(fahrenheit);
    Serial.println(" Fahrenheit");

    celsiuss = round(celsius);
    String analogValue0 = String(celsiuss, DEC);
    Serial.println(celsius);
    // Print Update Response to Serial Monitor
    if (client.available())
    {
    char c = client.read();
    Serial.print(c);
    }
    // Disconnect from ThingSpeak
    if (!client.connected() && lastConnected)
    {
    Serial.println("...disconnected");
    Serial.println();

    client.stop();
    }

    // Update ThingSpeak
    // if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
    // {
    updateThingSpeak("field1="+analogValue0);
    // }
    // Check if Arduino Ethernet needs to be restarted
    if (failedCounter > 3 ) {startEthernet();}

    lastConnected = client.connected();

    }


    void updateThingSpeak(String tsData)
    {
    if (client.connect(thingSpeakAddress, 80))
    {
    //String tsData;
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");

    client.print(tsData);

    lastConnectionTime = millis();

    if (client.connected())
    {
    Serial.println("Connecting to ThingSpeak...");
    Serial.println();

    failedCounter = 0;
    }
    else
    {
    failedCounter++;

    Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
    Serial.println();
    }

    }
    else
    {
    failedCounter++;

    Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
    Serial.println();

    lastConnectionTime = millis();
    }
    }

    void startEthernet()
    {

    client.stop();

    Serial.println("Connecting Arduino to network...");
    Serial.println();

    delay(1000);

    // Connect to network amd obtain an IP address using DHCP
    if (Ethernet.begin(mac) == 0)
    {
    Serial.println("DHCP Failed, reset Arduino to try again");
    Serial.println();
    }
    else
    {
    Serial.println("Arduino connected to network using DHCP");
    Serial.println();
    }

    delay(1000);
    }

    No more adresses dob serialon és nem is megy tovább a program, legalábbis Thingspeaken nem látnám hogy akarmi menne (api kód kitöltve), ha csak egy sima példa programot futtatok akkor ott küld adatot.
    Ha csak onewire példát futtatok akkor működik.
    Egybe a kettőt nem tudom.
    Uno van w5100 shielddel

    szerk.:
    A shieldet elég egyébként csak spi-n összekötni? pl ha nanoval akarom majd összehozni?

Új hozzászólás Aktív témák