【问题标题】:Trying to turn on 4 leds in arduino with yii but it only turns led #1试图用 yii 在 arduino 中打开 4 个 LED,但它只变成了 LED #1
【发布时间】:2019-05-13 20:56:40
【问题描述】:

我正在编写一个代码,我将能够通过 wifi 打开 4 个 arduino LED,我遇到的问题是,当我尝试打开 LED #2 时,它只打开 LED #1,我'已经检查了代码,它的编写方式应该是正确的,但我找不到它为什么不起作用的原因

我尝试更改 ifs 上的条件以查看它是否检测到它们,假设我更改了代码,当它应该是引脚 4 时它应该找到引脚 5,如果我尝试用它打开引脚 #4条件下,它仍然会打开 pin #1

注意: if 和 else if 我改变了它们的编写方式,但结果仍然相同,led #1 是唯一会打开的

public function actionEncender($id,$pin)
{

   *if*($pin = 1)
   {
    $equipo = Equipos::find()->where(['id'=>$id])->one();
    $equipo->estado = '1';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDa=ON');

    return $this->redirect(['view', 'id' => $id]);
}
elseif ($pin = 2)
{
    $equipo = Equipos::find()->where(['id'=>$id])->two();
    $equipo->estado = '1';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDb=ON');

    return $this->redirect(['view', 'id' => $id]);
}

elseif ($pin = '3')
{
    $equipo = Equipos::find()->where(['id'=>$id])->three();
    $equipo->estado = '1';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDc=ON');

    return $this->redirect(['view', 'id' => $id]);
}
elseif ($pin = '4')
{
    $equipo = Equipos::find()->where(['id'=>$id])->four();
    $equipo->estado = '1';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDd=ON');

    return $this->redirect(['view', 'id' => $id]);
}
}

public function actionApagar($id, $pin)
{
if ($pin = '1')
    {
    $equipo = Equipos::find()->where(['id'=>$id])->one();
    $equipo->estado = '0';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDa=OFF');

    return $this->redirect(['view', 'id' => $id]);
}

if ($pin = '2')
{
    $equipo = Equipos::find()->where(['id'=>$id])->two();
    $equipo->estado = '0';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDb=OFF');

    return $this->redirect(['view', 'id' => $id]);
}

if ($pin = '3')
{
    $equipo = Equipos::find()->where(['id'=>$id])->three();
    $equipo->estado = '0';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDc=OFF');

    return $this->redirect(['view', 'id' => $id]);
}

if ($pin = '4')
{
    $equipo = Equipos::find()->where(['id'=>$id])->four();
    $equipo->estado = '0';
    $equipo->save ();

    $curl = new curl\Curl();

    $response = $curl->get($equipo->ip.'/gpio/LEDd=OFF');

    return $this->redirect(['view', 'id' => $id]);
}
}

【问题讨论】:

  • 使用==进行比较; = 将右边的值赋给左边的变量。
  • 进行了更改,但仍然无效:/
  • 实际上当我进行更改时弹出一个错误:调用未知方法:yii\db\ActiveQuery::two(),抱歉,如果它很复杂,不是真的进入编程
  • 正如@ChrisWhite 所说,您必须将所有投注转换为==。因为,例如$pin = 1,引脚在所有条件下都等于 1(其他条件未检查)。然后当您更改为== 时,您的ActiveQuery 类 (two ()) 中出现了另一个问题
  • 感谢您的帮助,确实是 ==,只是我能够修复的功能有另一个问题,抱歉回复晚了,但我感谢所有帮助

标签: php arduino yii2 arduino-esp8266


【解决方案1】:

我重构了你的代码并纠正了逻辑错误。

private function loadModel($id)
{
    $equipo = Equipos::findOne((int)$id);
    if ($equipo === null) {
        throw new \yii\web\HttpException('Pin #' . $id . ' not found');
    }

    return $equipo;
}

public function actionEncender ($id)
{
    $equipo = $this->loadModel($id);
    $equipo->estado = 1;
    if (!$equipo->save(false, ['estado'])) {
        throw new \yii\web\HttpException('Error during save estado state');
    }

    $curl = new curl\Curl();
    $response = $curl->get($equipo->ip.'/gpio/LEDa=ON');

    return $this->redirect(['view', 'id' => $id]);
}

public function actionApagar($id)
{
    $equipo = $this->loadModel($id);
    $equipo->estado = 0;
    if (!$equipo->save(false, ['estado'])) {
        throw new \yii\web\HttpException('Error during save estado state');
    }

    $curl = new curl\Curl();
    $response = $curl->get($equipo->ip.'/gpio/LEDa=OFF');


    return $this->redirect(['view', 'id' => $id]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多