Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 1000000UL
- #include <avr/io.h>
- #include<string.h>
- #include "DHT.h"
- #include "protocolo.h"
- #define DHTPIN 2
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- #define PACKET_LENGTH 64
- #define PACKET_SEND 13
- pupload proto ={'1','1','0',{0},{'0'},0,'1','0',{'0'},0,0,1};
- int i, pulse[PACKET_LENGTH];
- String info, datos ="11110000001010011110100101100100001000";
- byte textBuf[ 12 ], *text;
- float t;
- unsigned long duration;
- boolean voltage = true; //voltage=true --> 1 | voltage=false --> 0
- char buf[9]={"11110000"};
- // the setup routine runs once when you press reset:
- void setup() {
- // initialize serial communication at 9600 bits per second:
- dht.begin();
- Serial.begin(9600);
- pinMode(7,OUTPUT);
- // Generem interrupcions
- attachInterrupt(0, change, CHANGE);
- }
- // the loop routine runs over and over again forever:
- void loop() {
- //char *buf = readInfo();
- //sendPacket(buf);
- readPacket(datos, &proto);
- }
- // Interrupció que investiga el OOK
- void change(){
- if(voltage){
- //guardem la duració amb la funcio pulseIn
- duration = pulseIn(2, HIGH);
- info = info + "1";
- voltage=false;
- } else {
- duration = pulseIn(2, LOW);
- info = info + "0";
- voltage=true;
- }
- pulse[i] = duration;
- i++;
- }
- // Funció a revisar. Llegeix els valors obtinguts del pin A0 i defineix el paquet rebut
- void readVoltage(int pulse[], String info){
- for(int j=0; j < PACKET_LENGTH; j++){
- //la duració de bit es de 50ms aprox. Comprovem la duració per veure si hi ha un bit o dos
- if((pulse[j] <= 120) && (pulse[j] >= 80)) datos = datos + info[j] + info[j];
- else if((pulse[j] > 120) && (pulse[j] < 30)) return; //duració per sota del temps d'un bit o per sobre del temsp de 2 bits --> error
- else datos = datos + info[j];
- }
- }
- //funcio que comprova la informacio rebuda
- void readPacket(String datos, pupload *proto){
- char packet_received[PACKET_LENGTH - 4],crc_received[4], info_received[8], check;
- //Comprovacio del PREAMBLE 111-->OK
- if(datos.substring(0,3) == "111"){
- //Extreu CRC i INFO
- datos.substring(datos.length() - 3).toCharArray(crc_received, sizeof(crc_received));
- datos.substring(11,datos.length() - sizeof(crc_received) + 1).toCharArray(packet_received, sizeof(packet_received));
- //Comprovem el CRC
- Serial.println(packet_received);
- check = crearCRC(packet_received);
- for(int j=0; j < 3; j++) {
- if(check != crc_received[j]){
- Serial.println("ERROR->CRC");
- return; }
- }
- datos.substring(5, datos.length() - sizeof(crc_received) - sizeof(packet_received) + 2).toCharArray(info_received, sizeof(info_received));
- //EL DUENDE ME DIJO QUE QUEMARA COSAS
- //proto->ack_recibido == datos[3];
- //if ((proto->fin_recibido = datos[4]) == '1');
- //Si el CRC es correcte, transformem binari --> ASCII
- BinarytoASCII(packet_received);
- } else proto->ack_envio = '0', Serial.println("ERROR->Preamble"); //Error PREAMBLE
- }
- // Transformació de la informació de codi binari a codi ASCII
- void BinarytoASCII(char binaryText[]){
- char *bT = binaryText;
- int counter = 0;
- text = textBuf;
- *text = 0;
- while (*bT){ // Mentre bT no sigui nul
- if (counter < 8){ // Contador per treure bytes. Com que no podem espais (*bT != " ") doncs toca aguantar-se.
- (*text) *= 2;
- if (*bT == '1') (*text)++;
- counter++;
- } else { // Quan ja hem recorregut el byte toca reiniciar el buffer. Tirem un valor cap enrere de bT per que salta aquí amb count = 9. Si no ho fem ens mengem 1 bit cada cicle.
- text++;
- *text = 0;
- counter=0;
- bT--;
- }
- bT++;
- }
- Serial.print((char *) textBuf );
- Serial.println("");
- }
- // Llegeix informació del sensor de temperatura i humitat
- char * readInfo(){
- String str;
- int a,x;
- t = dht.readTemperature();
- a = (int)t;
- str = String(a, BIN);
- x = str.length();
- char buf[x];
- for(i = 0; i <= x; i++){
- buf[i] = str[i];
- }
- return buf;
- }
- // Prepara i envia paquet
- void sendPacket(char *buf){
- char paquet_to_send[PACKET_SEND];
- save_to_send(buf, &proto);
- p_envio(&proto, paquet_to_send);
- for(i=0;i<PACKET_SEND;i++){
- if((paquet_to_send[i-3] == '1') || (i<3)) digitalWrite(7, HIGH);
- else digitalWrite(7, LOW);
- }
- eliminar_trama(&proto);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement