Go Down

Topic: Arduino Mega 2560+Arduino Ethernet Shield +LCD (Read 638 times) previous topic - next topic

fckw

Jan 23, 2013, 08:43 pm Last Edit: Jan 24, 2013, 05:15 pm by fckw Reason: 1
Hallo,
ich versuche gerade mit einem Arduino Mega 2560 + Arduino Ethernet Shield,  Messwerte zu Cosm (ehemals pachube) zu laden, was auch schonmal wunderbar klappt.
Den Beispielcode den man sich dort generieren lassen kann habe ich um zwei DS18B20 Temperatursensoren und ein LCD erweitert um damit ein wenig zu "spielen".
Jetzt ist mir aufgefallen, dass der Arduino beim ausführen von "void sendData" und "void getData" ewig braucht, was man z.B daran erkennt, dass in dieser Zeit das LCD nicht aktualisiert wird.

Code: [Select]
/**
* Cosm Arduino sensor client example.
*
* This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),
* using the new Arduino library to send and receive data.
*
* Requirements
*   * Arduino with Ethernet shield or Arduino Ethernet (board must use the
*     Wiznet Ethernet chipset)
*   * Arduino software with version >= 1.0
*   * An account at Cosm (https://cosm.com)
*
* Optional
*   * An analog sensor connected to pin 2 (note we can still read a value from
*     the pin without this)
*
* Created 8th January, 2013 using code written by Adrian McEwen with
* modifications by Sam Mulube
*
* Full tutorial available here: https://cosm.com/docs/quickstart/arduino.html
*
* This code is in the public domain.
*
* The circuit:
* LCD RS pin to digital pin 9
* LCD Enable pin to digital pin 8
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

OneWire ds(9);                                 // 1Wire Instance on Arduino digital pin 7
DallasTemperature sensors(&ds);                // Pass our oneWire reference to Dallas Temperature.

int resolution = 12;                              // Auflösung der Sensoren in bit

int numberOfDevices;                           // Number of temperature devices found
DeviceAddress tempDeviceAddress;               // We'll use this variable to store a found device address


#define API_KEY "XXX" // your Cosm API key
#define FEED_ID XXX // your Cosm feed ID

// MAC address for your Ethernet shield
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)

float sensorValue = 0;
float sensorValue2 = 0;

long previousMillis = 0;                             // will store last time
long previousMillis2 = 0;
long previousMillis3 = 0;
long interval = 400;
long interval2 = 250;
long interval3 = 15000;
unsigned long currentMillis;
int  delayInMillis = 0;

// Initialize the Cosm library

// Define the string for our datastream ID
char sensorId[] = "Temperatur_Aquarium";
char sensorId2[] = "Temperatur_Raum";

CosmDatastream datastreams[] = {
  CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
  CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
};

// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 2 /* number of datastreams */);

EthernetClient client;
CosmClient cosmclient(client);

LiquidCrystal lcd(8, 7, 6, 5, 3, 2);        // initialize the library with the numbers of the interface pins

//-----------------------------------------------------------------------------------------------------------------------

void setup() {
  // put your setup code here, to run once:

  sensors.begin();
  sensors.setResolution(resolution);                  // set the resolution to TEMPERATURE_PRECISION bit

  numberOfDevices = sensors.getDeviceCount();

  Serial.begin(115200);

  lcd.begin(20, 4);                                              // set up the LCD's number of columns and rows:
  Serial.println();
  Serial.print(numberOfDevices);
  Serial.println(" <-- DS18B20 erkannt");
  Serial.println();
  Serial.println("Initializing network");
  while (Ethernet.begin(mac) != 1) {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(15000);
  }

  Serial.println("Network initialized");
  Serial.println();
}

//-----------------------------------------------------------------------------------------------------------------------

void loop() {                                          // main program loop
  currentMillis = millis();
  // Cosm aktualisieren
  aktCosm();
  // LCD aktualisieren
  aktLCD();
  // Sensoren aktualisieren
  aktSensoren();
}

//LCD mit Messwerten füllen

void aktLCD () {
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;                      // save the last time

    lcd.begin(20, 4);
    lcd.setCursor(5,0);
    lcd.print("T1: ");
    lcd.print(sensorValue);
    lcd.setCursor(5,1);
    lcd.print("T2: ");
    lcd.print(sensorValue2);
  }
}

//Sensoren einlesen

void aktSensoren (){
  if(currentMillis - previousMillis2 > interval2) {
    previousMillis2 = currentMillis;
    Serial.println("Requesting temperatures...");
    sensors.requestTemperatures();                     // Send the command to get temperatures
    sensorValue = sensors.getTempCByIndex(0);
    sensorValue2 = sensors.getTempCByIndex(1);

  }
}

// Senden, lesen Csom
void aktCosm (){
  if (currentMillis - previousMillis3 > interval3) {
    previousMillis3 = currentMillis;
    // send it to Cosm
    sendData();
    // read the datastream back from Cosm
    getData();
  }
}

// send the supplied value to Cosm, printing some debug information as we go
void sendData() {
  Serial.println( );
  datastreams[0].setFloat(sensorValue);
  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());

  datastreams[1].setFloat(sensorValue2);
  Serial.print("Read sensor value2 ");
  Serial.println(datastreams[1].getFloat());
  Serial.println();

  Serial.println("Uploading to Cosm");
  int ret = cosmclient.put(feed, API_KEY);
  Serial.print("PUT return code: ");
  Serial.println(ret);

  Serial.println();
}

// get the value of the datastream from Cosm, printing out the value we received
void getData() {
  Serial.println("Reading data from Cosm");

  int ret = cosmclient.get(feed, API_KEY);
  Serial.print("GET return code: ");
  Serial.println(ret);

  if (ret > 0) {
    Serial.print("Datastream is: ");
    Serial.println(feed[0]);

    Serial.print("Datastream2 is: ");
    Serial.println(feed[1]);

    Serial.print("Sensor value is: ");
    Serial.println(feed[0].getFloat());

    Serial.print("Sensor value2 is: ");
    Serial.println(feed[1].getFloat());
  }

  Serial.println();
}

Der code dient mir nur zum lernen und experimentieren.

Da ich von Software nicht soviel Plan habe ist mir nicht klar woran dass liegt. Ist das Prinzipbedingt oder einfach nur ein schlechter Ansatz von mir?

Was mir noch aufgefallen ist, dass ich mit diesem Code, das LCD u.a. auch mit den Pins 10-13 belgen kann, ich dachte die wären für das Ethernet Shield reserviert, geht aber hier auch.
Verstehe ich auch nicht.

Hat jemand Rat?

Go Up