Announcement

Collapse
No announcement yet.

PHP error: Wrong pin?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP error: Wrong pin?

    Hi. I'm very newbe with PHPoC Shield for Adruino and today i trying to make small project like here: https://forum.phpoc.com/articles/tut...age-with-image , but when i change pin to 8 - i become a message: " UIO / On-Board User LED

    PHP error: invalid argument in file lib/sd_340.php on line 65"


    What did I do wrong?

  • #2
    Hello Badener,
    You are using PHPoC Blue (P4S-342) or Black (P4S-341), NOT PHPoC Shield, right ?
    The code in the link you give is for PHPoC Blue/Black, NOT for PHPoC Shield.

    Could you please post your modified code here ?

    Comment


    • #3
      Hello there,

      The example above is for PHPoC board (PHPoC Black/Blue), and it is used to control a built-in LED in PHPoC board (pin 30).
      In your case, I/O pins are controlled by Arduino itself, not by PHPoC device like in the case of the example.
      So I am not surprised if it didn't work .

      The good news is you can still do the same thing on Arduino and PHPoC shield.
      Like I said, let Arduino do the I/O control stuff as it should, and PHPoC shield may help you to do the rest.
      The combination of Arduino and PHPoC shield is a Web/WebSocket server, so you can host a web script on PHPoC shield.
      The script should allow web client (yeah, a technical name for web browser ) to exchange data with the web server (Arduino with PHPoC shield), so that you can send control command to Arduino board and get the response status back to your web browser.

      Considering that you are new to PHPoC shield, I made a quick example so you can upload this script on PHPoC shield. Let's say you name it remote_led.php. Believe me, unless you want to fully customize the shield yourself, otherwise you should choose a different name with the existing built-in web apps in the shield (including index.php)

      Code:
      <html>
          <head>
              <title><?echo system("uname -i")?></title>
              <meta content="initial-scale=0.7, maximum-scale=1.0, minimum-scale=0.5, width=device-width, user-scalable=yes" name="viewport">
              <style>
                  body {
                      text-align: center;
                  }
      
                  h2 {
                      font-weight: bold;
                      font-size: 15pt;
                      padding-bottom: 5px;
                  }
      
                  button {
                      font-weight: bold;
                      font-size: 15pt;
                  }
      
              </style>
          </head>
          <body>
              <h2> UIO / Control LED<br>
                  <br>
                  <img id="remote" src="button_pop.png">
                  <br>
              </h2>
              <h2>WebSocket <span id="ws_state"><span style="color: gray">CLOSED</span></span></h2>
              <button id="bt_connect" type="button" onclick="connect_onclick();">Connect</button>
          </body>
          <script>
      
          var isON;
          var ws;
          var remote;
      
          function init()
          {
              remote = document.getElementById("remote");
              remote.addEventListener("click", toogleLed);
              isON = 0;
          }
      
          function connect_onclick()
          {
              if(ws == null)
              {
                  var ws_host_addr = "<?echo _SERVER("HTTP_HOST")?>";
                  if((navigator.platform.indexOf("Win") != -1) && (ws_host_addr.charAt(0) == "["))
                  {
                      // network resource identifier to UNC path name conversion
                      ws_host_addr = ws_host_addr.replace(/[\[\]]/g, '');
                      ws_host_addr = ws_host_addr.replace(/:/g, "-");
                      ws_host_addr += ".ipv6-literal.net";
                  }
                  ws = new WebSocket("ws://" + ws_host_addr + "/remote_led", "text.phpoc");
                  document.getElementById("ws_state").innerHTML = "CONNECTING";
                  ws.onopen = ws_onopen;
                  ws.onclose = ws_onclose;
                  ws.onmessage = ws_onmessage;
              }
              else
                  ws.close();
          }
      
          function ws_onopen()
          {
              document.getElementById("ws_state").innerHTML = "<span style='color: blue'>CONNECTED</span>";
              document.getElementById("bt_connect").innerHTML = "Disconnect";
              displayLEDStatus();
          }
      
          function ws_onclose()
          {
              document.getElementById("ws_state").innerHTML = "<span style='color: gray'>CLOSED</span>";
              document.getElementById("bt_connect").innerHTML = "Connect";
              ws.onopen = null;
              ws.onclose = null;
              ws.onmessage = null;
              ws = null;
              isON = 0;
              displayLEDStatus();
          }
      
          function ws_onmessage(e_msg)
          {
              e_msg = e_msg || window.event; // MessageEvent
              isON = parseInt(e_msg.data);
              displayLEDStatus();
          }
      
          function toogleLed()
          {
              if (isON)
                  ws.send("0");
              else
                  ws.send("1");
          }
      
          function displayLEDStatus()
          {
              remote.src = (isON) ? "button_push.png" : "button_pop.png";
          }
      
          window.onload = init;
          </script>
      </html>

      And here is the Arduino code

      Code:
      #include <Phpoc.h>
      
      PhpocServer server(80);
      int ledPin = 8;
      
      void setup() {
        Serial.begin(9600);
        while(!Serial)
          ;
      
        // initialize PHPoC [WiFi] Shield:
        Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
        //Phpoc.begin();
      
        // start WebSocket server
        server.beginWebSocket("remote_led");
      
        // print IP address of PHPoC [WiFi] Shield to serial monitor:
        Serial.print("WebSocket server address : ");
        Serial.println(Phpoc.localIP());
        pinMode(ledPin, OUTPUT);
      }
      
      void loop() {
        // wait for a new client:
        PhpocClient client = server.available();
      
        if (client) {
          if (client.available() > 0) {
            // read a byte incoming from the client:
            char thisChar = client.read();
      
            switch(thisChar)
            {
              case '0':
                Serial.println("Turn LED OFF");
                digitalWrite(ledPin, LOW);
                server.write('0');
                break;
              case '1':
                Serial.println("Turn LED ON");
                digitalWrite(ledPin, HIGH);
                server.write('1');
                break;
            }
          }
        }
      }

      Then try to access the web app at http://REPLACE_YOUR_SHIELD_IP_ADDR_HERE/remote_led.php
      Don't forget to upload the two images that were used in the original example (button_push.png, button_pop.png).
      P/S: In my example I use WebSocket to exchange data between web browser and Arduino, so you may need to click the CONNECT button to initialize a WebSocket connection.
      Cheers
      Last edited by Homer; 04-25-2019, 11:39 AM.

      Comment

      Working...
      X