// GET: TurnHeat?temperature=33 public ActionResult Index() { SerialPort arduinoPort = PortController.GetArduinoPort(); if (arduinoPort.IsOpen) { string temperatureString = Request.QueryString["temperature"]; int temperature = Int32.Parse(temperatureString); arduinoPort.Write("SET_THERMOSTAT#"); arduinoPort.Write(temperature.ToString(CultureInfo.InvariantCulture)); string response = arduinoPort.ReadLine(); } else { throw new InvalidOperationException("Arduino port not found"); } return(View()); }
// GET: TurnLamp public ActionResult Index() { SerialPort arduinoPort = PortController.GetArduinoPort(); if (arduinoPort.IsOpen) { string turn = Request.QueryString["turn"]; String command = (turn == "ON") ? "LAMP_ON" : "LAMP_OFF"; bool lampOn = (turn == "ON"); arduinoPort.Write(command); string temperatureString = arduinoPort.ReadLine(); db.Lamps.Add(new Lamp(lampOn, DateTime.Now)); db.SaveChanges(); } else { throw new InvalidOperationException("Arduino port not found"); } return(View()); }
public static void UpdateTemperatureToDb() { SerialPort arduinoPort = PortController.GetArduinoPort(); if (arduinoPort.IsOpen) { arduinoPort.Write("TEMPERATURE_INDOOR"); string temperatureString = arduinoPort.ReadLine(); Double temperature = Double.Parse(temperatureString); arduinoPort.Write("TEMPERATURE_OUTDOOR"); string outTemperatureString = arduinoPort.ReadLine(); Double outTemperature = Double.Parse(outTemperatureString); dbTemp.InOutTemperatures.Add(new InOutTemperature(temperature, outTemperature, DateTime.Now)); dbTemp.SaveChanges(); arduinoPort.Write("WINDOW_STATUS"); string windowStatusString = arduinoPort.ReadLine(); string cleanWindowStatus = windowStatusString.Trim(); bool windowOpen = (cleanWindowStatus == "WINDOW_OPEN"); dbWind.Windows.Add(new Window(windowOpen, DateTime.Now)); dbWind.SaveChanges(); arduinoPort.Write("HEATER_STATUS"); string heatStatusString = arduinoPort.ReadLine(); string eanHeaterStatus = heatStatusString.Trim(); bool heatOn = (eanHeaterStatus == "HEATER_ON"); dbHeat.Heats.Add(new Heat(heatOn, DateTime.Now)); dbHeat.SaveChanges(); } else { throw new InvalidOperationException("Arduino port not found"); } }