291 lines
7.4 KiB
C++
Executable File
291 lines
7.4 KiB
C++
Executable File
/*
|
|
|
|
*/
|
|
|
|
#include <RCSwitch.h>
|
|
#include <Wire.h>
|
|
|
|
char last_message[200] = "";
|
|
char previous[100] = "";
|
|
|
|
float temp = 0.0;
|
|
float bar = 0.0;
|
|
float hum = 0.0;
|
|
float lum = 0.0;
|
|
boolean yaduboulot = false;
|
|
long id = 0;
|
|
float tmp = 0;
|
|
int val = 0;
|
|
int boucle = 0;
|
|
int boucle2 = 0;
|
|
|
|
#define ENABLE_Sniffer true
|
|
|
|
// I2C
|
|
#define SLAVE_ADDRESS 0x12
|
|
int dataReceived = 0;
|
|
|
|
RCSwitch mySwitch = RCSwitch(); // Create an instance of RCSwitch
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
// initialize digital pin 13 as an output.
|
|
pinMode(13, OUTPUT);
|
|
|
|
// ********* IMPORTANT ***********
|
|
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
|
|
|
|
// I2C
|
|
Wire.begin(SLAVE_ADDRESS);
|
|
Wire.onReceive(receiveData);
|
|
Wire.onRequest(sendData);
|
|
|
|
blink();
|
|
}
|
|
|
|
void loop() {
|
|
|
|
if (mySwitch.available()) {
|
|
if (strlen(last_message) > 180) {
|
|
strcpy(last_message,"");
|
|
}
|
|
// printf("", last_message);
|
|
|
|
//clear;
|
|
long value = mySwitch.getReceivedValue();
|
|
|
|
if (value <= 0) {
|
|
Serial.print("Unknown encoding");
|
|
}
|
|
else {
|
|
|
|
if (ENABLE_Sniffer) {
|
|
Serial.print("Received ");
|
|
Serial.print( value );
|
|
// Serial.print(" / ");
|
|
// Serial.print( mySwitch.getReceivedBitlength() );
|
|
// Serial.print("bit ");
|
|
// Serial.print("Protocol: ");
|
|
// Serial.println( mySwitch.getReceivedProtocol() );
|
|
|
|
}
|
|
|
|
switch (value) {
|
|
case 111269:
|
|
Serial.println(printf("Debut de message %i \n", value));
|
|
id = 0;
|
|
tmp = 0;
|
|
strcpy(last_message,"");
|
|
val =0;
|
|
break;
|
|
case 1969:
|
|
case 2069:
|
|
case 2169:
|
|
case 2269:
|
|
case 2369:
|
|
id = value;
|
|
tmp = 0;
|
|
break;
|
|
case 962111:
|
|
id = 0;
|
|
tmp = 0;
|
|
|
|
break;
|
|
default :
|
|
tmp = value;
|
|
Serial.println(printf("valeur recue = %i id=%i\n", tmp, id));
|
|
break;
|
|
}
|
|
Serial.println(printf("valeur recue = %i id=%i\n", tmp, id));
|
|
|
|
if (ENABLE_Sniffer) {
|
|
Serial.print(" id=");
|
|
Serial.print(id);
|
|
|
|
Serial.print(" value=");
|
|
Serial.print(tmp);
|
|
|
|
Serial.println("");
|
|
}
|
|
|
|
char buf[7];
|
|
//char nom[200] = "curl \"http://localhost:8080/json.htm?type=command¶m=udevice&"; //idx=107&svalue=";
|
|
char nom[100] = "";
|
|
|
|
boolean envoi = false;
|
|
if (tmp != 0) {
|
|
if (id == 1969 && tmp < 10000 ) {
|
|
|
|
// sprintf(buf,"%.2f", tmp / 100.0);
|
|
toString(&buf[0], tmp / 100.0);
|
|
strcat(nom, "idx=122&svalue=");
|
|
temp = tmp / 100.0;
|
|
envoi = true;
|
|
} else if (id == 2069 && tmp < 12000 && tmp > 7000) {
|
|
// sprintf(buf,"%.2f", tmp / 10.0);
|
|
toString(&buf[0], tmp / 10.0);
|
|
strcat(nom, "idx=121&svalue=");
|
|
bar = tmp / 10.0;
|
|
envoi = true;
|
|
} else if (id == 2169 && tmp < 12000 && tmp > 7000) {
|
|
// sprintf(buf,"%.2f", tmp / 10.0);
|
|
toString(&buf[0], tmp / 10.0) ;
|
|
strcat(nom, "idx=123&svalue=");
|
|
envoi = true;
|
|
} else if (id == 2269 && tmp < 1024) {
|
|
// sprintf(buf,"%.2f", tmp);
|
|
toString(&buf[0], tmp);
|
|
strcat(nom, "idx=158&svalue=");
|
|
lum = tmp;
|
|
envoi = true;
|
|
} else if (id == 2369 && tmp < 100 && tmp > 20) {
|
|
// sprintf(buf,"%.2f", tmp);
|
|
toString(&buf[0], tmp);
|
|
//strcat(nom, "idx=158&svalue=");
|
|
hum = tmp;
|
|
} else if (id == 331969 && tmp > 0) {
|
|
Serial.println(sprintf(buf,"%.2f;%.2f", tmp, tmp));
|
|
strcat(nom, "idx=166&svalue=");
|
|
strcat(nom,buf);
|
|
strcat(nom,"\"");
|
|
printf("%s\n",nom);
|
|
// FIXME std::system(nom);
|
|
envoi=false;
|
|
}
|
|
if (envoi) {
|
|
strcat(nom,buf);
|
|
//strcat(nom,"\"");
|
|
Serial.println(printf("%s\n",nom));
|
|
|
|
if (ENABLE_Sniffer) {
|
|
Serial.print("previous=");
|
|
Serial.print(previous);
|
|
Serial.print(" nom=");
|
|
Serial.println(nom);
|
|
}
|
|
|
|
if (strcmp(previous, nom) != 0) {
|
|
//char last_message[200] = "value=";
|
|
strcat(last_message, nom);
|
|
strcat(last_message, "|");
|
|
if (ENABLE_Sniffer) {
|
|
Serial.println(last_message);
|
|
}
|
|
strcpy(previous, nom);
|
|
|
|
blink();
|
|
}
|
|
// FIXME std::system(nom);
|
|
}
|
|
}
|
|
}
|
|
char buf[8];
|
|
|
|
if (/*lum > 0 &&*/ bar > 0 && temp > 0 && hum > 0) {
|
|
// /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
|
|
//char nom[200] = "curl \"http://localhost:8080/json.htm?type=command¶m=udevice&idx=160&svalue=";
|
|
char nom[200] = "idx=160&svalue=";
|
|
// Température
|
|
// sprintf(buf,"%.2f;", temp);
|
|
toString(&buf[0], temp);
|
|
|
|
strcat(nom, buf); strcat(nom, ";");
|
|
// Humidité
|
|
//sprintf(buf,"%.2f;", hum);
|
|
toString(&buf[0], hum);
|
|
|
|
strcat(nom, buf); strcat(nom, ";");
|
|
// Hum stat
|
|
//sprintf(buf,"%.2f;", hum);
|
|
toString(&buf[0], hum);
|
|
|
|
strcat(nom, buf); strcat(nom, ";");
|
|
// Bar
|
|
// sprintf(buf,"%.2f;", bar);
|
|
toString(&buf[0], bar);
|
|
strcat(nom, buf); strcat(nom, ";");
|
|
// Bar Forecast
|
|
// sprintf(buf,"%.2f", bar);
|
|
toString(&buf[0], bar);
|
|
strcat(nom, buf); strcat(nom, ";");
|
|
// Fin de chaine
|
|
//strcat(nom,"\"");
|
|
//Commande
|
|
Serial.println(printf("%s\n",nom));
|
|
// Exec
|
|
//FIXME std::system(nom);
|
|
// Serial.println(nom);
|
|
lum = 0;
|
|
hum = 0;
|
|
bar = 0;
|
|
temp = 0;
|
|
strcat(last_message, nom);
|
|
strcat(last_message, "|");
|
|
if (ENABLE_Sniffer) {
|
|
Serial.println(last_message);
|
|
}
|
|
blink();
|
|
}
|
|
|
|
// Prepare for more input
|
|
mySwitch.resetAvailable();
|
|
}
|
|
|
|
delay(100);
|
|
boucle++;
|
|
boucle2++;
|
|
if (boucle > 10) {
|
|
Serial.print(".");
|
|
boucle = 0;
|
|
}
|
|
if (boucle2 > 800) {
|
|
Serial.println();
|
|
boucle2 = 0;
|
|
}
|
|
}
|
|
|
|
|
|
void toString(char *p, float f) {
|
|
|
|
Serial.print("To String");
|
|
int i = ((int) f);
|
|
p += sprintf(p, "%i", i);
|
|
p += sprintf(p, ".%i", ((int) ((f - i) * 100)));
|
|
Serial.println(p);
|
|
// etc
|
|
}
|
|
|
|
|
|
// I2C
|
|
void receiveData(int byteCount){
|
|
while(Wire.available()) {
|
|
dataReceived = Wire.read();
|
|
Serial.print("Donnee recue : ");
|
|
Serial.println(dataReceived);
|
|
if (dataReceived == 3) {
|
|
val = 0;
|
|
}
|
|
blink();
|
|
}
|
|
}
|
|
|
|
void sendData(){
|
|
// Wire.beginTransmission(SLAVE_ADDRESS); // transmit to device #44 (0x2c)
|
|
// device address is specified in datasheet
|
|
Wire.write(last_message[val]);
|
|
//Wire.endTransmission();
|
|
|
|
val++;
|
|
if (val > strlen(last_message)) {
|
|
val = 0;
|
|
}
|
|
|
|
}
|
|
void blink() {
|
|
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
|
|
delay(50); // wait for a second
|
|
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
|
|
delay(50);
|
|
}
|
|
|