bonjour
j'ai une connection i2c entre un arduino uno rev 3 (slave) et un rapsberry (master)
après plusieurs lectures, j 'ai compilé un code mais cela ne fonctionne pas car j'ai compris qu'il me fallait passer les valeurs en float, j'ai trouvé comment faire la conversion mais je bloque pour le passage de mes valeurs vers un script python.
J'ai trouvé ArduPi-I2C-DataType-CodeArduino — MCHobby - Wiki
mais c'est trop technique pour moi. je comprends le principe mais je n'arrive pas à l'appliquer à mon
cas .
j'aimerais faire 3 envois séparés selon les valeurs température, humidité et c02 afin de les récupérer et les transmettre dans une base mysql.
Dans le script mchobby, ils envoient un exemple 5.67. j'aurais aimé passer mes 3 valeurs en mettant une variable à la place de la constante valeurDouble = 5.67; mais je n'y arrive pas.
si vous pouviez m'éclairer...
merci
#include "DHT.h"
#include <Wire.h>
#define DHTPIN 2 // what pin DTH11 is connected to 2
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
/*
VEETECH CO2 Monitor and Data Logger
SKETCH 2 Operation
Code by Martin Liddament
Used to determine:
- CO2 Concentration;
IMPORTANT
- For demonstration only;
- Not verified or guaranteed free of errors;
- Not suitable for critical applications;
- Use of this code is entirely at user's risk;
- Accurate calibration of the CO2 module is essential.
This code is in the public domain.
Copyright VEETECH Ltd 2013 www.veetech.co.uk.
*/
int co2 = 9999; // co2 is the co2 concentration. Preset value for code checking
int analogPin = A0; // voltage input from sensor
// Setting Sensor Calibration Constants
float v400ppm = 4.535; //MUST BE SET ACCORDING TO CALIBRATION
float v40000ppm = 3.206; //MUST BE SET ACCORDING TO CALIBRATION````````````````````````
float deltavs = v400ppm - v40000ppm;
float A = deltavs/(log10(400) - log10(40000));
float B = log10(400);
void setup() {
Wire.begin(4); // join i2c bus (address optional for master)
pinMode(analogPin, INPUT); //voltage MG811 From A0
dht.begin();
Serial.begin(9600);
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong! end transmission
if (isnan(t) || isnan(h)) {
Wire.endTransmission(); // stop transmitting
} else {
// Read co2 data from sensor MG811
int data = analogRead(analogPin); //digitise output from c02 sensor
float voltage = data/204.6; //convert output to voltage
// Calculate co2 from log10 formula (see sensor MG811 datasheet)
float power = ((voltage - v400ppm)/A) + B;
float co2ppm = pow(10,power);
co2 = co2ppm;
// array of values of sensors on solar room
float Solar[3];
float valeurSolar;
int z=0;
Solar[0]=h;
Solar[1]=t;
Solar[2]=co2;
for (z=0;z<3;z++) {
valeurSolar=Solar[z];
Serial.print ("valeur");
Serial.print (valeurSolar);
//call function wire_senddouble
Wire_SendDouble((double*)&valeurSolar);
delay(500);
}
// Wire.endTransmission(); // stop transmitting
}
}
// Function for build a double vaiable in array of Bytes
// and send data on bus I2C
//
// Based on code :
//http://arduino103.blogspot.fr/2013/08/transmettre-un-floatdouble-par-i2c.html
//http://stackoverflow.com/questions/12664826/sending-float-type-data-from-arduino-to-python
void Wire_SendDouble( double* d){
// cut two types of diffeent data on the same stack of memory
union Sharedblock
{
byte part[4]; // use char parts[4] for serial port
double data;
} block;
block.data = *d;
/*
example for
block.data = 5.67
the array part[x] is
block.part[0] = 164;
block.part[1] = 112;
part[2] = 181;
part[3] = 64;
This values must be the same on the other side of BUS I2C
*/
Wire.write( block.part, 4 );
}
][code]
[/code]