par Pat11 » 25 Nov 2013 18:26
Bonsoir
je te mets se que j'avais fais avec un arduino en xpl , j’espère que sa sera claire car je suis pas un spécialiste, mais sa fonctionnais bien je l’utilise pour les températures de la piscine et son niveau
/* Inclus la librairie SPI pour le module Ethernet Shield */
#include <SPI.h>
#include <tools.h>
/* Inclus la librairie Ethernet */
#include <Client.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Server.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "xPL.h"
xPL xpl;
// Définition de la pin arduino utilisée pour le bus OneWire
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//Surface
DeviceAddress TempSurface = { 0x28, 0x72, 0xFF, 0x5C, 0x04, 0x00, 0x00, 0x75 };
//Fond
DeviceAddress TempFond = { 0x28, 0x5F, 0x9E, 0x5D, 0x04, 0x00, 0x00, 0xDA };
char xPLSchema[20];
char Tp_str[5];
int Adr;
char Adr_str[13];
float temperature;
char TempChar[10];
char* ValeurPlein ;
char* ValeurCom;
char* Total;
/* Variables de configuration ethernet */
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x73 };
IPAddress ip(192,168,1,72);
/* Variables xPL */
char* xPLType = "xpl-trig";
char* xPLSource = "arduino-lan.piscine";
char* xPLTarget = "*";
char packetBuffer[500];
/* Adresse de Broadcast */
byte BroadcastIP[] = {192, 168, 1, 70};
/* Port xPL */
unsigned int xplPort = 3865;
EthernetUDP Udp;
unsigned int TargetPort = 3639;
/* Fonction d'initialisation */
void setup()
{
xpl.Process();
Serial.begin(115200);
sensors.begin();
sensors.setResolution(TempSurface,10);
sensors.setResolution(TempFond,10);
// Initialise la connexion ethernet
Ethernet.begin(mac,ip);
Udp.begin(6600);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
Serial.println("Initialisation terminee.");
xpl.SendExternal = &SendUdPMessage; // pointer to the send callback
xpl.AfterParseAction = &AfterParseAction; // pointer to a post parsing action callback
xpl.SetSource_P(PSTR("xpl"), PSTR("arduino"), PSTR("test")); // parameters for hearbeat message
}
void loop()
{
//commande de récupération des températures
sensors.requestTemperatures();
//récup de la valeur temperature surface et mise en centi-degrés
int Etat1 = sensors.getTempC(TempSurface);
if (Etat1 != temperature) {
temperature = Etat1;
Adr = 1;
delay (1000);
sprintf( Adr_str, "%s 0x100%d", "th3", Adr);
ftoa(TempChar,temperature,1);
sprintf(xPLSchema, "%s", "sensor.basic");
xPLSendSensor(xPLSchema,Adr_str,"temp",TempChar);
}
//récup de la valeur fond et mise en centi-degrés
int Etat2 = sensors.getTempC(TempFond);
if (Etat2 != temperature){
temperature = Etat2;
Adr = 2;
delay(1000);
sprintf( Adr_str, "%s 0x100%d", "th3", Adr);
ftoa(TempChar,temperature,1);
sprintf(xPLSchema, "%s", "sensor.basic");
xPLSendSensor(xPLSchema, Adr_str,"temp",TempChar);
}
//calcul temperature moyenne
int Etat3 = (sensors.getTempC(TempSurface) + sensors.getTempC(TempFond))/2;
if (Etat3 != temperature){
temperature = Etat3;
Adr = 3;
delay (1000);
sprintf( Adr_str, "%s 0x100%d", "th3", Adr);
ftoa(TempChar,temperature,1);
sprintf(xPLSchema, "%s", "sensor.basic");
xPLSendSensor(xPLSchema, Adr_str,"temp",TempChar);
}
//Calcul niveau
int Etat4 = digitalRead (4);
int Etat5 = digitalRead (5);
int Etat6 = digitalRead (6);
int Etat7 = digitalRead (7);
//if (Etat != Total) {
// Total = Etat;
if (Etat4 == 1) {
ValeurPlein = "0";
}
if (Etat5 ==1) {
ValeurPlein = "25";
}
if (Etat6 == 1) {
ValeurPlein = "50";
}
if (Etat7 == 1) {
ValeurPlein = "100";
}
if (ValeurPlein != Total) {
Total = ValeurPlein;
Adr = 4;
ValeurCom = "dim";
//delay (1000);
sprintf( Adr_str, "%s%d" , "N", Adr);
sprintf(xPLSchema, "%s", "x10.basic");
xPLSendCommandDim(xPLSchema, Adr_str,ValeurCom,ValeurPlein);
}
int packetSize = Udp.parsePacket();
if(packetSize)
{
char xPLMessageBuff[XPL_MESSAGE_BUFFER_MAX];
// read the packet into packetBufffer
Udp.read(xPLMessageBuff, XPL_MESSAGE_BUFFER_MAX);
// parse message
xpl.ParseInputMessage(xPLMessageBuff);
}
}
//Message pour les capteurs xpl
void xPLSendSensor(char* xPLSchema, char* Capteur, char* Type, char* Valeur)
{
sprintf( packetBuffer, "");
strcat( packetBuffer, xPLType);
strcat( packetBuffer, "\n{\nhop=1\nsource=");
strcat( packetBuffer, xPLSource );
strcat( packetBuffer, "\ntarget=" );
strcat( packetBuffer, xPLTarget );
strcat( packetBuffer, "\n}\n" );
strcat( packetBuffer, xPLSchema );
strcat( packetBuffer, "\n{\n" );
sprintf( packetBuffer, "%sdevice=%s\n", packetBuffer, Capteur );
sprintf( packetBuffer, "%stype=%s\n", packetBuffer, Type );
sprintf( packetBuffer, "%scurrent=\%s\n", packetBuffer, Valeur );
sprintf( packetBuffer, "%s}\n", packetBuffer );
Serial.println(packetBuffer);
Udp.beginPacket(BroadcastIP, xplPort);
Udp.write(packetBuffer);
Udp.endPacket();
delay(10000);
}
//Message pour les actionneurs xpl
void xPLSendCommand(char* xPLSchema, char* Capteur, char* Valeur)
{
sprintf( packetBuffer, "");
strcat( packetBuffer, xPLType);
strcat( packetBuffer, "\n{\nhop=1\nsource=");
strcat( packetBuffer, xPLSource );
strcat( packetBuffer, "\ntarget=" );
strcat( packetBuffer, xPLTarget );
strcat( packetBuffer, "\n}\n" );
strcat( packetBuffer, xPLSchema );
strcat( packetBuffer, "\n{\n" );
sprintf( packetBuffer, "%sdevice=%s\n", packetBuffer, Capteur );
sprintf( packetBuffer, "%scommand=\%s\n", packetBuffer, Valeur );
sprintf( packetBuffer, "%s}\n", packetBuffer );
Serial.println(packetBuffer);
Udp.beginPacket(BroadcastIP, xplPort);
Udp.write(packetBuffer);
Udp.endPacket();
delay(10000);
}
void xPLSendCommandDim(char* xPLSchema, char* Capteur, char* Com, char* Valeur)
{
sprintf( packetBuffer, "");
strcat( packetBuffer, xPLType);
strcat( packetBuffer, "\n{\nhop=1\nsource=");
strcat( packetBuffer, xPLSource );
strcat( packetBuffer, "\ntarget=" );
strcat( packetBuffer, xPLTarget );
strcat( packetBuffer, "\n}\n" );
strcat( packetBuffer, xPLSchema );
strcat( packetBuffer, "\n{\n" );
sprintf( packetBuffer, "%sdevice=%s\n", packetBuffer, Capteur );
sprintf( packetBuffer, "%scommand=\%s\n", packetBuffer, Com );
sprintf( packetBuffer, "%slevel=\%s\n", packetBuffer, Valeur );
sprintf( packetBuffer, "%s}\n", packetBuffer );
Serial.println(packetBuffer);
Udp.beginPacket(BroadcastIP, xplPort);
Udp.write(packetBuffer);
Udp.endPacket();
delay(10000);
}
void SendUdPMessage(char *buffer)
{
Udp.beginPacket(BroadcastIP, xplPort);
Udp.write(buffer);
Udp.endPacket();
}
void AfterParseAction(xPL_Message * message)
{
if (xpl.TargetIsMe(message))
{
if (message->IsSchema_P(PSTR("lighting"), PSTR("basic")))
{
Serial.println(PSTR("is lighting.basic"));
}
}
// show message
Serial.println(message->toString());
}
bon courage si tu as des questions hésite pas mais je suis pas un expert