Electric Imp för att rapportera till temperatur.nu

I detta forum hör diskussioner gällande teknik i största allmänhet och hemautomation i synnerhet hemma.
Kategoriregler
Vill du visa bilder i ditt inlägg? Använd funktionen "Ladda upp bilaga" nedanför textrutan!
Användarvisningsbild
bruka
Tar hemautomation på allvar
Inlägg: 290
Blev medlem: 12 nov 2007, 13:48
Ort: Delsbo

Electric Imp för att rapportera till temperatur.nu

Inlägg av bruka »

Jag har med hjälp av kod hittad på nätet som jag modifierat en aning gjort en egen temperatur.nu reporter med en Electric Imp och en DS18B20. Finns på m.nu. På bilden används pin 1 och 2 för att kommunicera med tempsensorn men i koden är det pin 5 och 7(uart57 på flera ställen), ändra på valfritt ställe.
Fördelen med den här är lika som med m.nu´s temperatur reporter att du slipper ha en dator igång.
Ett plus är också att den kör wifi (enbart) så du slipper dra kabel. Ström måste den förstås ha.
Med koden nedan så går det bara skicka värdet från en sensor men den stödjer ett flertal sensorer.
Den klarar inte Parasite power.

Agent

Kod: Markera allt

device.on("senddata", function(data) {
  // Set URL to your web service, http://www.temperatur.nu  
  local url = "http://www.temperatur.nu/rapportera.php?hash=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&t=";  //din hash från temperatur.nu
  // Set Content-Type header to json
  local headers = { "Content-Type": "application/x-www-form-urlencoded" };
  local url = (url + data);
  // encode data and log
  local body = http.jsonencode(data);
  server.log(body);

// send data to your web service
  http.post   (url, headers, body).sendsync();

});
Device

Kod: Markera allt

// oneWire Test program
// imp.configure("oneWire", [], []);

function owReset()
{
    // configure UART for OneWire RESET timing
    hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
    hardware.uart57.write(0xF0);
    
    if ( hardware.uart57.read() == 0xF0 )
    {
        server.log("owDevice not present");
        return 0;
    } else {
        // Switch UART to OneWire data speed timing
        hardware.uart57.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS);
        return 1;
    }
}

function owWrite(byte)
{
  for (local b=0; b<8; b++, byte=byte>>1) owBit(byte&0x01);
} 

function owRead()
{
  local byte = 0;
  for (local b=0; b<8; b++) byte=(byte>>1)+0x80*owBit(1);
  return byte;
}

// Write/Read bit
function owBit(bit)
{
  bit=bit?0xFF:0x00;
  hardware.uart57.write(bit);
  return hardware.uart57.read()==0xFF?1:0;
} 

function owSearch(next)
{
     local Y = 0;   // last found fork point

// Reset the bus and exit if no device found
     if(owReset())
     {
     
// OW_SEARCH_ROM readies all 1-Wire devices to begin the search and
// simultaneously sending the first least significant bit on the next read
     owWrite(0xF0);
     
// Reading 64 bits romcode LSB: family code (8 bits), serialnummber (48 bits), MSB: CRC (8 bits)
     
        for (local i=64; i>0; i--) {
              local byte = (i-1)/8;
              local b = owBit(1);   // read bit from bus
              if(owBit(1))          // read inverse bit
              {
                if(b) { Y=0; break } // read 1+1, bus error, no response
              } else if(!b) {       // read 0+0, found 2 devices with opposite bits
                    if( next > i || ( next != i && (id[byte] & 1) ) ) // or take the same path as in the previous ID
                    {
                        b = 1;    // take the '1' direction on this point
                        Y = i;    // store last found fork point
                    }                
              }
// select only devices on the bus with the same bit valvue, other devices go offline and no longer participate in the search until next search
             owBit(b);
// shift out the previous path bits, add on the msb side the new choosen path bits
             id[byte] = (id[byte] >> 1) + 0x80 * b;
        }
     }
// return the last fork point for next search
     return Y;
}

// Wake up every 30 seconds and write to the server

function awake()
{
 id <- [0,0,0,0,0,0,0,0]; // OW device ID array
 next <- 65;

 local tempL = 0; 
 local tempH = 0; 
 local temp = 0; 
 // agent.send("temp", 0);
 imp.wakeup(60.0, awake);  // sekunder mellan läsningar
 
 server.log("Trigger all devices to do temperature conversion");
 
 owReset();
 owWrite(0xCC);  // SKIP RO to select all devices on the bus
 owWrite(0x44);  // CONVERT_T DS18B20 start conversion on all devices
 
 imp.sleep(0.75); // wait 750 msec for temperature conversion to finish

 local dev = 0;

 while(next)
 {
  dev++;
  next = owSearch(next);

  if (id[7] == 0x28)
  {
  owReset();
  // MATCH ROM to select device for reading temperature  
  owWrite(0x55);
  for (local i=7; i>=0; i--)  owWrite(id[i]);
  // READ_SCRATCHPAD DS18B20
  owWrite(0xBE);
 
  tempL = owRead();
  tempH = owRead()*256;
  owReset();  // stop reading the rest of the scratchpad

      local tempC = tempH & 0x8000; // test most significant bit. Den här + 2 rader till för att fungera i minusgrader.
  if (tempC) {tempH = (tempH ^ 0xffff) + 1;} // negative, 2's compliment
  if (tempC) {tempH *= -1;}     //  .... minusgrader



  temp = (tempL + tempH)/16.0; // calculate temperature from LSB and MSB
  
  server.log(format("#%02d: Family: %02x (DS18B20), Serial: %02x%02x%02x%02x%02x%02x, CRC: %02x, Temp: %3.2f", dev, id[7],id[1],id[2],id[3],id[4],id[5],id[6],id[0],temp));
  }
  
 if (id[7] == 0x12)
  {
  owReset();
  owWrite(0x55); // MATCH ROM to select device for reading temperature
  for (local i=7; i>=0; i--)  owWrite(id[i]);
    // CHANNEL_ACCESS followed by 2 channel control bytes
  owWrite(0xF5);
    // first channel control byte: no crc, set CHS0 = select PIO-A only, b6=IM=0=writing-reading
  owWrite(4); // 00000100
    // write dummy channel control byte
  owWrite(0xFF);
    // read channel info byte
  local pioA = owRead() & 5;        // PIO-A bits 00000101
  owBit(!pioA);                     // toggle output PIO-A
 
  server.log(format("#%02d: Family: %02x (DS-2406), Serial: %02x%02x%02x%02x%02x%02x, CRC: %02x, pioA: %02x", dev, id[7],id[1],id[2],id[3],id[4],id[5],id[6],id[0],pioA));
  }
 }
 local data = (temp);
 agent.send("senddata"  data ); 
}


// Start the loop
imp.wakeup(2.0, awake);
Dioden är en schottky och motståndet är på 2,2 kOhm.
Bilagor
DS18b20IMP.png
DS18b20IMP.png (9.46 KiB) Visad 2172 gånger
Användarvisningsbild
CirruZZ
Master Moderator
Inlägg: 1621
Blev medlem: 13 feb 2008, 16:46
Ort: Ystad

Re: Electric Imp för att rapportera till temperatur.nu

Inlägg av CirruZZ »

Häftigt!

Sent from my Pad
Skriv svar