public int livetemperature()
        {
            SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
            res = convertToInt(readBuffer);
            int mVolt = res * (2000 / 1023);

            res = mVolt / 10;
            return(res);
        }
        public void DisplayTextBoxContents()
        {
            SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
            res = convertToInt(readBuffer);
            //voltage = ADC_value / 4096 * 5.0

            //TMP36 is 0.5V at 0 C and 10 mV per degree

            //so...

            //Temp_in_C = (voltage - 0.5) / 0.01

            var voltage = ((double)res / 4096) * 5.0;

            tempInC = (voltage - 0.5) / 0.01;


            textPlaceHolder.Text = String.Format("{0} °C", tempInC.ToString("##.#"));
            Debug.WriteLine(textPlaceHolder.Text);
        }
Beispiel #3
0
 public void DisplayTextBoxContents()
 {
     SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
     res = convertToInt(readBuffer);
     textPlaceHolder.Text = res.ToString();
 }
Beispiel #4
0
 public void DisplayTextBoxContents()
 {
     SpiDisplay.TransferFullDuplex(ReadBuffer, WriteBuffer);
     resolution           = ConvertToInt(ReadBuffer);
     TextPlaceHolder.Text = resolution.ToString();
 }
        public void DisplayTextBoxContents()
        {
            SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
            stevec1 = stevec1 + convertToDouble(readBuffer);
            _counter++;
            // preberemo nekaj vrednost.
            // 317 branj = 5 sekund
            // Za optimizirati
            if (_counter == (225 * (int)stOsvezevanja / 5))
            {
                temperatura = (stevec1 / _counter); // prikazana temperatura. Povprecje prebranih vrednosti. ce ne prevec skace temperatura.

                // ce ni napake na vezju.
                if (error == false)
                {
                    // pomozno za povprecno temperaturo
                    zacasniResultat = zacasniResultat + temperatura;
                    counter2++;

                    // povprecnje zadnjih 10 temperatur
                    if (counter2 == 10)
                    {
                        povprecnaTemperatura = (zacasniResultat / counter2);
                        counter2             = 0;
                        zacasniResultat      = 0;
                    }
                }

                // ce je ze izracunalo povprecno temperaturo in ce je prebrana temperatura prevec oddaljena od povprecne je napaka na vezju.
                if ((povprecnaTemperatura != 0.0) && ((temperatura <= povprecnaTemperatura - 10) || (temperatura >= povprecnaTemperatura + 10)))
                {
                    error            = true; // napaka na vezju
                    textBlock10.Text = "Napaka na vezju! Senzor odstranjen?";
                }
                else
                {
                    error            = false; // ok
                    textBlock10.Text = "";
                }

                if (unit == "Celsius")
                {
                    // ce je prebrana temperatura manjsa od nastavljenega nivoja alarma, poslje alarm
                    if (temperatura < nivoAlarmaMin)
                    {
                        if (error == true)
                        {
                            opozorilo = "Napaka na vezju! Senzor odstranjen? " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Pregledati temperaturni senzor.";
                        }
                        else
                        {
                            opozorilo = "Pozor, nizka temperatura. " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Nastaviti klimatsko napravo na " + ((nivoAlarmaMin + nivoAlarmaMax) / 2) + " \u00b0" + unit;
                        }
                    }
                    // ce je prebrana temperatura vecja od nastavljenega nivoja alarma, poslje alarm
                    else if (temperatura > nivoAlarmaMax)
                    {
                        if (error == true)
                        {
                            opozorilo = "Napaka na vezju! Senzor odstranjen? " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Pregledati temperaturni senzor.";
                        }
                        else
                        {
                            opozorilo = "Pozor, visoka temperatura. " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Nastaviti klimatsko napravo na " + ((nivoAlarmaMin + nivoAlarmaMax) / 2) + " \u00b0" + unit;
                        }
                    }
                    // ni alarma
                    else
                    {
                        opozorilo = "";
                        sporocilo = "";
                    }

                    textPlaceHolder.Text  = temperatura.ToString("#.#") + " \u00b0" + unit;
                    textPlaceHolder2.Text = povprecnaTemperatura.ToString("#.#") + " \u00b0" + unit;

                    //posljemo podatke na spletno stran in restartamo stevce in temperaturo
                    Poslji(null, null);
                    //if (temperatura < -20)
                    //SendIFTTTEvent(EVENT_NAME, "Pozor odstranjen senzor!");

                    _counter    = 0;
                    temperatura = 0.0;
                    stevec1     = 0.0;
                }

                //Fahrenheit
                else
                {
                    temperatura = toFahrenheit(temperatura);
                    // ce je prebrana temperatura manjsa od nastavljenega nivoja alarma, poslje alarm
                    if (temperatura < nivoAlarmaMinF)
                    {
                        if (error == true)
                        {
                            opozorilo = "Napaka na vezju! Senzor odstranjen? " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Pregledati temperaturni senzor.";
                        }
                        else
                        {
                            opozorilo = "Pozor, nizka temperatura. " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Nastaviti klimatsko napravo na " + (int)((nivoAlarmaMinF + nivoAlarmaMaxF) / 2) + " \u00b0" + unit;
                        }
                    }
                    // ce je prebrana temperatura vecja od nastavljenega nivoja alarma, poslje alarm
                    else if (temperatura > nivoAlarmaMaxF)
                    {
                        if (error == true)
                        {
                            opozorilo = "Napaka na vezju! Senzor odstranjen? " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Pregledati temperaturni senzor.";
                        }
                        else
                        {
                            opozorilo = "Pozor, visoka temperatura. " + temperatura.ToString("#.#") + " \u00b0" + unit + " !!";
                            sporocilo = "Nastaviti klimatsko napravo na " + (int)((nivoAlarmaMaxF + nivoAlarmaMinF) / 2) + " \u00b0" + unit;
                        }
                    }
                    // ni alarma
                    else
                    {
                        opozorilo = "";
                        sporocilo = "";
                    }

                    textPlaceHolder.Text = temperatura.ToString("#.#") + " \u00b0" + unit;
                    if (povprecnaTemperatura != 0.0)
                    {
                        textPlaceHolder2.Text = toFahrenheit(povprecnaTemperatura).ToString("#.#") + " \u00b0" + unit;
                    }

                    //posljemo podatke na spletno stran in restartamo stevce in temperaturo
                    Poslji(null, null);
                    _counter    = 0;
                    temperatura = 0.0;
                    stevec1     = 0.0;
                }
            }
        }
        public MainPage()
        {
            this.InitializeComponent();
            this.InitializeComponent();
            _azureIoTHubService = new AzureIoTHubService();
            timer          = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(3000);
            timer.Tick    += async(s, e) =>
            {
                SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
                res = convertToInt(readBuffer);
                var forceSensorValue = res.ToString();

                textPlaceHolder.Text = forceSensorValue;
                System.Diagnostics.Debug.WriteLine("FORCE SENSOR VALUE: " + forceSensorValue);

                if (res > 0)
                {
                    await _azureIoTHubService.SendDataToAzure(forceSensorValue);
                }
            };
            timer.Start();

            whichADCChip = ADCChip.mcp3008;
            switch (whichADCChip)
            {
            case ADCChip.mcp3002:
            {
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 1 leading "0" bit

                // Write 0SGO MNxx xxxx xxxx
                // Read  ???? ?N98 7654 3210
                // S = start bit
                // G = Single / Differential
                // O = Chanel data
                // M = Most significant bit mode
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // 9-0 = 10 data bits

                // 0110 1000 = 1 0 pad bit, start bit, Single ended, odd (channel 0), MSFB only, 2 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[2] {
                    0x00, 0x00
                };
                writeBuffer = new byte[2] {
                    0x68, 0x00
                };
            }
            break;

            case ADCChip.mcp3008:
            {
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 7 leading "0" bits

                // Write 0000 000S GDDD xxxx xxxx xxxx
                // Read  ???? ???? ???? ?N98 7654 3210
                // S = start bit
                // G = Single / Differential
                // D = Chanel data
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // 9-0 = 10 data bits

                // 0000 01 = 7 pad bits, start bit
                // 1000 0000 = single ended, channel bit 2, channel bit 1, channel bit 0, 4 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[3] {
                    0x00, 0x00, 0x00
                };
                writeBuffer = new byte[3] {
                    0x01, 0x80, 0x00
                };
            }
            break;

            case ADCChip.mcp3208:
            {
                /* mcp3208 is 12 bits output */
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 5 leading "0" bits

                // Write 0000 0SGD DDxx xxxx xxxx xxxx
                // Read  ???? ???? ???N BA98 7654 3210
                // S = start bit
                // G = Single / Differential
                // D = Chanel data
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // B-0 = 12 data bits


                // 0000 0110 = 5 pad bits, start bit, single ended, channel bit 2
                // 0000 0000 = channel bit 1, channel bit 0, 6 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[3] {
                    0x00, 0x00, 0x00
                };
                writeBuffer = new byte[3] {
                    0x06, 0x00, 0x00
                };
            }
            break;
            }

            InitSPI();
        }