Neuling braucht Hilfe



  • Hallo liebe Forengemeinde,
    ich habe angefangen mich mit C++ zu beschäftigen und habe mit einem Project begonnen das ein Temperatur und Luftfeuchtigkeit an eine vorhandene Wetterstation gesendet werden soll. Folgendes Script habe ich dafür erstellt.

    // Example testing sketch for various DHT humidity/temperature sensors
    // Written by ladyada, public domain
    
    #include "DHT.h"
    
    #define DHTPIN  3     // PIN # for DHT22 Temperature Sensor
    #define TXPIN   1    // PIN # for 433Mhz Transmitter
    #define LEDPIN  0    // PIN # for LED Indicator
    
    #define DHTTYPE DHT22   // DHT 21 (AM2301)
    
    DHT dht(DHTPIN, DHTTYPE);
    
    unsigned long msg_timer = 15000; // Transmits a signal every 1 minute
    unsigned long msg_gap = 32; //gap between packets
    
    char buf[11]; //message to be built
    
    void setup() {
    //  Serial.begin(9600);
      //Serial.println("Wireless Temp");
      pinMode(LEDPIN,OUTPUT);
      pinMode(TXPIN, OUTPUT);
      dht.begin();
    }
    
    // Changed int to float
    // num is in Celsius. Ex) 26.30
    
    void make_message(float num) {
    
      // make up a TC6U message from the integer num (-500 to 499)
    
      // parity table  0 1 2 3 4 5 6 7 8 9
      char   ptable[]={
        0,1,1,2,1,2,2,3,1,2  }; //number of bits set
      int p;  //parity bit
      char h,t,d;  //hundreds,tens,digits
    
      p=0; //clear parity
    
      // preamble
      buf[0] = 0;
      buf[1] = 0xa;  //message length and start bits
      buf[2] = 0xe;    //type = temperature
      buf[3] = 0xf;  //device ID
      buf[4] = 0x0;  //bottom bit is parity
    
        num = num * 10;
    
      // encode the integer
      num = num+0;
      h = num/100;
    
      buf[5] = h;  //save hundreds
      p += ptable[h];  //update parity calculation
      num = num - h*100;
      t = num/10;
      buf[6] = t; //save tens
      p += ptable[t];  //update parity
      d = num - t*10;
      buf[7] = d; //save digits
      p += ptable[d];
      buf[8] = h; //second copy of hundreds digit
      buf[9] = t; //second copy of tens digit
    
      //if value parity odd, set message parity bit
      if ( (p&1) == 1) buf[4] |= 1;
      //calculate and store checksum
      buf[10] = (buf[1]+buf[3]+buf[4]+buf[5]+buf[6]+buf[7]+buf[8]+buf[9])&0x0F;
    }
    
    void send_one(char s) { //s=1 for short TXPIN off. 4th bit of quartet
      digitalWrite(TXPIN,HIGH);
      delayMicroseconds(588);
      digitalWrite(TXPIN,LOW);
      if (s) delayMicroseconds(1020);
      else delayMicroseconds(1088);
    }
    
    void send_zero(char s) { //s=1 for short TX off, 4th bit of quartet
      digitalWrite(TXPIN,HIGH);
      delayMicroseconds(1400);
      digitalWrite(TXPIN,LOW);
      if (s) delayMicroseconds(1020);
      else delayMicroseconds(1088);
    }
    
    void send_burst(char *msg) {
      char i,c;
    
      for(i=0; i<12; i++) {
    
        c = *msg++;  //values in lower 4 bits
    
        //unroll inner loop, send a quartet
    
        if(c&8) send_one(0);
        else send_zero(0);
        if(c&4) send_one(0);
        else send_zero(0);
        if(c&2) send_one(0);
        else send_zero(0);
        if(c&1) send_one(1); //"short tx off" (1020 us) for last bit
        else send_zero(1);
      }
    }
    
    // Changed int to float
    
    void send_message(float num) {
      digitalWrite(LEDPIN,HIGH);
      make_message(num);
      send_burst(buf);
      delay(msg_gap);
      send_burst(buf);
      digitalWrite(LEDPIN,LOW);
    }
    
    void loop() {
      // Wait a few seconds between measurements.
      delay(2000);
    
    //   Reading temperature or humidity takes about 250 milliseconds!
    //   Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float t = dht.readTemperature();
      float h = dht.readHumidity();
      // Check if any reads failed and exit early (to try again).
      if (isnan(t)) {
    //    Serial.println("Failed to read from DHT sensor!");
        return;
      }
    
    //  Serial.print(t);
      //Serial.println(" *C\t");
      send_message(h);
      send_message(t); // t is in Celisius. Ex) 26.30
      delay(msg_timer);
    }
    

    Wenn ich nun Temp. und Luftfeuchte einzeln send funktioniert das sehr gut nur komme ich nicht drauf wie das ganze aussehen muss wenn ich beides senden möchte.

    Brauche dringend Hilfe und danke schonmal im Vorraus


  • Mod

    So wird das nichts, sorry. Du brauchst Hilfe? Reduziere deine Frage auf etwas direktes. Wir machen hier nicht deine Arbeit.



  • Sorryy ich wollte die Arbeit schon selbst machen ich brauchte nur einen Denkanstoss. 😞



  • Wenn du beides senden möchtest, dann sende zuerst das eine und danach eben das andere 😕
    Oder möchtest du beides auf einmal senden? Da wir nichts vom verwendeten Protokoll wissen, musst du selber in die Spezifikationen schauen.

    Ausßerdem: Der Code ist kein C++, sondern grauenhaft. Globale Variablen, #define und Makros, char-Buffer statt container wie std::string, nichtssagende Variablen, fehlendes const.



  • ich würde roflo zustimmen,

    iwie scheint das für Arduino oder ähnlichen Mikrocontroller zu sein, welche allerdings i C programmiert werden, nicht in C++



  • Hallo und danke für die Antworten.
    Ja das ist für Arduino allerdings läuft das auf einem Attiny 85


Anmelden zum Antworten