/// <summary>
        /// Get Data from DB(last two minutes)
        /// </summary>
        /// <param name="sqldb">Database</param>
        /// <returns>Return packet to send</returns>
        public Hashtable retriveData(SQL_Wrapper sqldb)
        {
            Hashtable packet = new Hashtable();
            ResultSet data   = sqldb.get_last2mindata(numberP);

            if (data.Data.Count != 0)
            {
                objson o = new objson("FEZ_46");
                foreach (ArrayList dato in data.Data)
                {
                    Misure m = new Misure();
                    m.sensor = (string)dato[0];
                    string id = dato[1].ToString();
                    m.sensor_id     = Int32.Parse(id);
                    m.iso_timestamp = (string)dato[2];
                    m.value         = (float)(double)dato[3];
                    m.status        = (string)dato[4];
                    o.addMisure(m);
                }
                string json = JsonSerializer.SerializeObject(o.hastTable);
                packet.Add(numberP, json);
                numberP++;
            }
            else
            {
                packet = null;
            }
            //inviare questa stringa..
            return(packet);
        }
示例#2
0
        // This method is run when the mainboard is powered up or reset.
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            #region ForEthernet
            //ethernetJ11D.UseThisNetworkInterface();
            //ethernetJ11D.UseStaticIP("192.168.137.1","255.255.255.0","192.168.137.46");

            //ethernetJ11D.NetworkUp += ethernetJ11D_NetworkUp;
            //ethernetJ11D.NetworkDown += ethernetJ11D_NetworkDown;
            #endregion

            /*Inizialized Database for Misure*/
            sqldb = new SQL_Wrapper(sdCard);
            sqldb.setup_database();

            /*Create Configuration*/
            //Configuration conf = new Configuration();
            StreamReader d = new StreamReader(sdCard.StorageDevice.RootDirectory + "\\configuration.json");
            configuration = d.ReadToEnd();

            /*Inizialized DHT11 sensor*/
            init = new InizializeDHT();

            /*Start Timer for read sensors*/
            GT.Timer t6sec = new GT.Timer(6000);
            t6sec.Tick += read_sensors;
            t6sec.Start();

            /*Start Timer for Backup DataBase*/
            //GT.Timer tBackup = new GT.Timer(1000 * 60 * 7); //7 minuti
            //tBackup.Tick += BackupDB;
            //tBackup.Start();

            /*Activate WiFi interface*/
            WiFiRS9110.NetworkParameters mynet = new WiFiRS9110.NetworkParameters();
            if (!wifiRS21.NetworkInterface.Opened)
            {
                wifiRS21.NetworkInterface.Open();
            }
            wifiRS21.UseThisNetworkInterface();

            #region AdHoc
            //mynet.Channel= 5;
            //mynet.Ssid="prova";
            //mynet.Key=" ";
            //mynet.SecurityMode = WiFiRS9110.SecurityMode.Open;
            //mynet.NetworkType = WiFiRS9110.NetworkType.AdHoc;
            ////wifiRS21.NetworkInterface.EnableStaticIP("192.168.1.100", "255.255.0.0", "192.168.1.1");
            //wifiRS21.NetworkInterface.StartAdHocNetwork(mynet);



            //Thread.Sleep(2000);
            //HttpListener l = new HttpListener("http");

            //l.Start();
            //var contex = l.GetContext();
            //var respons = contex.Response;

            //const string r = "<html><body>Hello world</body></html>";
            //var buffer = System.Text.Encoding.UTF8.GetBytes(r);
            //respons.ContentLength64 = buffer.Length;
            //var output = respons.OutputStream;
            //output.Write(buffer, 0, buffer.Length);
            //output.Close();
            #endregion

            /*Connect to wifi*/
            wifiC = new WiFiConnection();
            wifiC.WifiConnected += wifiC_WifiConnected;
            wifiC.connectToWiFI(wifiRS21, SSID, PASSWORD);

            //setup();
        }
        /// <summary>
        /// Read all sensors and validate readings, with specifices
        /// </summary>
        /// <param name="sqldb">Database to insert</param>
        /// <param name="moisture">A moinsture sensor</param>
        public void Read_and_check_values(SQL_Wrapper sqldb, Gadgeteer.Modules.GHIElectronics.Moisture moisture, Gadgeteer.Modules.GHIElectronics.LightSense lightSense)
        {
            /*Read and Print data*/
            Utilità.ReadDHT(dhtSensor);

            if (dhtSensor.Temperature != _last_tm.value || counterTm == 15)
            {
                _last_tm.value = dhtSensor.Temperature;
                TemperatureMisure tm = new TemperatureMisure(dhtSensor.Temperature, "OK");
                //obj_send.addMisure(tm);
                sqldb.insert(tm, 0);
                if (counterTm == 15)
                {
                    counterTm = 0;
                }
            }
            else
            {
                counterTm++;
            }

            if ((dhtSensor.Humidity != _last_hm.value && dhtSensor.Humidity != 0) || counterHm == 15)
            {
                _last_hm.value = dhtSensor.Humidity;
                HumidityMisure hm = new HumidityMisure(dhtSensor.Humidity, "OK");
                //obj_send.addMisure(hm);
                sqldb.insert(hm, 0);
                if (counterHm == 15)
                {
                    counterHm = 0;
                }
            }
            else
            {
                counterHm++;
            }

            float moinstureData = moisture.ReadMoisture();

            Debug.Print("Moinsture      = " + moinstureData);
            if ((moinstureData != _last_mm.value && moinstureData != 0) || counterMm == 15)
            {
                _last_mm.value = moinstureData;
                MoistureMisure mm = new MoistureMisure(moinstureData, "OK");
                //obj_send.addMisure(mm);
                sqldb.insert(mm, 0);
                if (counterMm == 15)
                {
                    counterMm = 0;
                }
            }
            else
            {
                counterMm++;
            }

            Debug.Print(String.Empty);

            double lightDataIll  = lightSense.GetIlluminance();
            double lightDataPro  = lightSense.ReadProportion();
            double lightDataVolt = lightSense.ReadVoltage();

            Debug.Print("LightSenseIll  = " + lightDataIll);
            Debug.Print("LightSensePro  = " + lightDataPro);
            Debug.Print("LightSenseVolt = " + lightDataVolt);
            if (lightDataIll < (_last_ls.value * 0.85) || lightDataIll > (_last_ls.value + _last_ls.value * 0.15) || counterLs == 15)
            {
                _last_ls.value = (float)lightDataIll;
                LightMisure ls = new LightMisure((float)lightDataIll, "OK");
                //obj_send.addMisure(ls);
                sqldb.insert(ls, 0);
                if (counterLs == 15)
                {
                    counterLs = 0;
                }
            }
            else
            {
                counterLs++;
            }
        }
 public M2QTT_Wrapper(string ip, SQL_Wrapper sqldb)
 {
     this.sqldb = sqldb;
     this.ip    = ip;
 }