Bonsoir à tous.
Encore une fois je sollicite vos lumières pour le truc le plus con qu'il soit : enregistrer des variations de températures sur Cosm via une DS18B20
J'ai combiné un script de base de DS18B20 et le script que donne Cosm pour alimenter un feed, mais:
- la température n'est pas lue par l'Arduino (donc affiche -1000);
- la température est envoyée sur Cosm mais je n'ai pas de suivi dans le temps. -1000 est bien affiché mais pas de graphe;
- sur la console "Reading data from Cosm" reste sans réponse.
Je vous colle le code:
/**
* 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.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <OneWire.h>
#define API_KEY "--" // your Cosm API key
#define FEED_ID 124576 // 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)
int sensorPin = 12;
OneWire ds(sensorPin);
unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 5000; // delay between connecting to Cosm in milliseconds
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "Température";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
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() {
float temperature = getTemp();
Serial.println(temperature);
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int sensorValue = temperature;
// send it to Cosm
sendData(sensorValue);
// read the datastream back from Cosm
getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int sensorValue) {
datastreams[0].setFloat(sensorValue);
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());
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("Sensor value is: ");
Serial.println(feed[0].getFloat());
}
Serial.println();
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
Serial.println("problem");
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
Arduino Uno R3 + Ethernet Shiel Arduino + Sonde DS18B20
Thank you !