void ProgramStarted() { if (sdCard.IsCardMounted) { rootDirectory = sdCard.StorageDevice.RootDirectory; } else { Mainboard.SetDebugLED(true); } Byte[] buffer = new Byte[10]; TimeService.SystemTimeChanged += TimeService_SystemTimeChanged; TimeService.TimeSyncFailed += TimeService_TimeSyncFailed; ethernetJ11D.NetworkDown += ethernetJ11D_NetworkDown; ethernetJ11D.NetworkUp += ethernetJ11D_NetworkUp; button.ButtonPressed += button_ButtonPressed; sdCard.Unmounted += sdCard_Unmounted; sdCard.Mounted += sdCard_Mounted; _mqttclient.MqttMsgPublished += _mqttclient_MqttMsgPublished; Thread syncTimerThread, measureThread, systemTimerThread, publisheverythingThread; if (sdCard.IsCardMounted) { JSON.printConfiguration(sdCard, config); } //Debug.Print("Hello PC"); Bitmap my_bitmap = new Bitmap((Int32)displayTE35.Width, (Int32)displayTE35.Height); string mystring = "waiting for measure"; my_bitmap.DrawText(mystring, font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 10); displayTE35.SimpleGraphics.DisplayImage(my_bitmap, 0, 0); ethernetJ11D.NetworkInterface.Open(); ethernetJ11D.NetworkSettings.EnableDhcp(); ethernetJ11D.NetworkSettings.EnableDynamicDns(); //ethernetJ11D.UseDHCP(); //initializeNetwork(); if (!sdCard.IsCardMounted) { sdCard.Mount(); } try { if (sdCard.IsCardMounted) { myread.delete_wrong_file(sdCard); FileStream fs = new FileStream(rootDirectory + @"\setup.txt", FileMode.Open); int byteRead = fs.Read(buffer, 0, 0); if (byteRead != 0) { SetUp.Wrong = 0; SetUp.lastToSend = 0; SetUp.writeFile(sdCard); } Debug.Print(buffer[0].ToString()); fs.Close(); } } catch (System.IO.IOException e) { } mytempsensor.Setup(); myuvsensor.Setup(); //myread.readSetup(sdCard); //initializeNetworkTimerThread = new Thread(this.initializeNetworkThread); //initializeNetworkTimerThread.Start(); //Creation of threads starting from there //Timer systemTimeDisplayTimer = new Timer(new TimerCallback(systemTimeDisplay), null, 0, 1000); systemTimerThread = new Thread(this.systemTimeDisplay); systemTimerThread.Start(); syncTimerThread = new Thread(this.syncTimer); syncTimerThread.Start(); //Thread.Sleep(1000); measureThread = new Thread(this.Measure); measureThread.Start(); publisheverythingThread = new Thread(this.PublishEverything); publisheverythingThread.Start(); //publishthread = new thread(this.publishmqtt); //publishthread.start(); }
void Measure() { Thread.Sleep(20000); displayTE35.BacklightEnabled = true; //displayTE35.DebugPrintEnabled = true; Bitmap my_bitmap = new Bitmap((Int32)displayTE35.Width, (Int32)displayTE35.Height / 2); int counter = 0; Measurement temp = new Measurement(); temp.sensor = 1; temp.device_id = "FEZ27_1"; Measurement humid = new Measurement(); humid.sensor = 2; humid.device_id = "FEZ27_2"; Measurement UVindex = new Measurement(); UVindex.sensor = 3; UVindex.device_id = "FEZ27_3"; //string rootDirectory = sdCard.StorageDevice.RootDirectory; while (true) { if (!mytempsensor.Measure(temp, humid)) { Debug.Print("Mesure fails!"); string timestring = "Measurement Time:" + DateTime.Now.ToString(); my_bitmap.DrawText(timestring, font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 10); my_bitmap.DrawText("Measure fails!", font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 25); displayTE35.SimpleGraphics.DisplayImage(my_bitmap, 0, 0); } else { counter++; myuvsensor.Measure(UVindex); if (counter >= 10)//mandatory publish every 10 measurements { if (sdCard.IsCardMounted) { //there is internet or cardmounted ,dont need buffer counter = 0; //publishMqtt(temp); //publishMqtt(humid); //publishMqtt(UVindex); JSON.printString(sdCard, temp, isInternet); try { if (timeSyncfinished) { publishMqtt(temp); File.Delete(rootDirectory + @"\correct_toSend" + (JSON.getOnline() - 1) + ".JSON"); } } catch { } JSON.printString(sdCard, humid, isInternet); try { if (timeSyncfinished) { publishMqtt(humid); File.Delete(rootDirectory + @"\correct_toSend" + (JSON.getOnline() - 1) + ".JSON"); } } catch { } JSON.printString(sdCard, UVindex, isInternet); try { if (timeSyncfinished) { publishMqtt(UVindex); File.Delete(rootDirectory + @"\correct_toSend" + (JSON.getOnline() - 1) + ".JSON"); } } catch { } Debug.Print("I wrote on sd card(mandatory).\n"); lastMeasureTemperature = temp.DeepCopy(); lastMeasureHumidity = humid.DeepCopy(); lastMeasureUVindex = UVindex.DeepCopy(); SetUp.setWrong(JSON.getOnline()); SetUp.setLastToSend(JSON.getOffline()); SetUp.writeFile(sdCard); } else//sd card unmounted { if (ethernetJ11D.IsNetworkUp && timeSyncfinished) { publishMqtt(humid); publishMqtt(temp); publishMqtt(UVindex); } else { //save to buffer SaveToBuffer(humid); SaveToBuffer(temp); SaveToBuffer(UVindex); } } } else { //increse index anyway if (Measurement.isSimilar(lastMeasureHumidity, humid, humid.sensor)) //add the online case { if (!isInternet) { JSON.increaseOffline();//when should we increse?? } else { JSON.increseOnline(); } } else { if (sdCard.IsCardMounted) { //publishMqtt(humid); //Debug.Print(DateTime.UtcNow.ToString()); JSON.printString(sdCard, humid, isInternet); Debug.Print("I wrote on sd card.\n"); lastMeasureHumidity = humid.DeepCopy(); try { publishMqtt(humid); File.Delete(rootDirectory + @"\correct_toSend" + (JSON.getOnline() - 1) + ".JSON"); } catch { } SetUp.setWrong(JSON.getOnline()); SetUp.setLastToSend(JSON.getOffline()); SetUp.writeFile(sdCard); } else { if (timeSyncfinished) { if (ethernetJ11D.IsNetworkUp) { publishMqtt(humid); } else { SaveToBuffer(humid); } } } } if (Measurement.isSimilar(lastMeasureTemperature, temp, temp.sensor)) //add the online case { if (!isInternet) { JSON.increaseOffline();//when should we increse?? } else { JSON.increseOnline(); } } else { //publishMqtt(temp); if (sdCard.IsCardMounted) { JSON.printString(sdCard, temp, isInternet); Debug.Print("I wrote on sd card.\n"); lastMeasureTemperature = temp.DeepCopy(); try { publishMqtt(temp); File.Delete(rootDirectory + @"\correct_toSend" + (JSON.getOnline() - 1) + ".JSON"); } catch { } SetUp.setWrong(JSON.getOnline()); SetUp.setLastToSend(JSON.getOffline()); SetUp.writeFile(sdCard); } else { if (timeSyncfinished) { if (ethernetJ11D.IsNetworkUp) { publishMqtt(temp); } else { SaveToBuffer(temp); } } } } if (Measurement.isSimilar(lastMeasureUVindex, UVindex, UVindex.sensor)) //add the online case { if (!isInternet) { JSON.increaseOffline();//when should we increse?? } else { JSON.increseOnline(); } } else { if (sdCard.IsCardMounted) { //publishMqtt(UVindex); JSON.printString(sdCard, UVindex, isInternet); Debug.Print("I wrote on sd card.\n"); lastMeasureUVindex = UVindex.DeepCopy(); try { publishMqtt(UVindex); File.Delete(rootDirectory + @"\correct_toSend" + (JSON.getOnline() - 1) + ".JSON"); } catch { } SetUp.setWrong(JSON.getOnline()); SetUp.setLastToSend(JSON.getOffline()); SetUp.writeFile(sdCard); } else { if (timeSyncfinished) { if (ethernetJ11D.IsNetworkUp) { publishMqtt(UVindex); } else { SaveToBuffer(UVindex); } } } } //} } // measure_count++; my_bitmap.Clear(); string timestring1 = "Measurement Time:" + DateTime.Now.ToString(); string tempstring = "Temperature:" + temp.value.ToString("F") + "°C"; string humdstring = "Humidity:" + humid.value.ToString("F") + "%"; string internetstring = "Ineternet is" + (isInternet ? " " : " not ") + "available"; my_bitmap.DrawText("This is a scheduled measurement.", font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 10); my_bitmap.DrawText(timestring1, font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 25); my_bitmap.DrawText(tempstring, font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 40); my_bitmap.DrawText(humdstring, font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 55); my_bitmap.DrawText(internetstring, font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 85); my_bitmap.DrawText("UVINDEX:" + UVindex.value.ToString(), font, Microsoft.SPOT.Presentation.Media.Color.White, 5, 70); displayTE35.SimpleGraphics.DisplayImage(my_bitmap, 0, 0); Debug.Print("Current Real-time Clock " + DateTime.Now.ToString()); Debug.Print("Humidity:" + humid.value.ToString("F") + "%"); Debug.Print("Temperature:" + temp.value.ToString("F") + "°C"); //Debug.Print("back from json!"); } Thread.Sleep(10000); } }