Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

How to control Arduino nano from a VIrtual Device button?


Guest samuelboerhoop
 Share

Question

Guest samuelboerhoop

How do I make a Virtual device with a button what can switch the following command to an arduino nano:

 

Please login or register to see this link.

 

or 

 

Please login or register to see this link.

 

 

I programmend the arduino like:

 

/* 
 -----------------------
 
 VCC -   3.3V
 GND -    GND
 SCK - Pin 13
 SO  - Pin 12
 SI  - Pin 11
 CS  - Pin 8 
  ------------------
 */
 
#include <EtherCard.h>  
 
// MAC Address 
static byte mymac[] = { 
  0xFA,0x5A,0x51,0x53,0x5A,0x5A };
 
// ip
static byte myip[] = { 
  10,0,1,190 };
 
byte Ethernet::buffer[900];
BufferFiller bfill;
 
int LedPins[] = {
  2,3,4,5};
 
boolean PinStatus[] = {
  1,2,3,4};
 
//-------------
 
const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";
 
const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";
 
const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";
 
//------------
 
void homePage()
{
  bfill.emit_p(PSTR("$F"
    "<title>ArduinoPIN Webserver</title>" 
    "Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><br />"
    "Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><br />"  
    "Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><br />"
    "Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a>"),   
 
  http_OK,
  PinStatus[1]?PSTR("off"):PSTR("on"),
  PinStatus[1]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  PinStatus[2]?PSTR("off"):PSTR("on"),
  PinStatus[2]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  PinStatus[3]?PSTR("off"):PSTR("on"),
  PinStatus[3]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  PinStatus[4]?PSTR("off"):PSTR("on"),
  PinStatus[4]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"));
}
 
//------------------------
 
void setup()
{
  Serial.begin(9600);
 
  // "ethercard" (CS-pin) =  8
  // if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
  // "ethercard" (CS-pin) = 10
  // if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0).
 
  if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0);
 
  if (!ether.dhcpSetup()); 
 
  ether.printIp("My Router IP: ", ether.myip);
 
    //ether.staticSetup(myip);
 
  ether.printIp("My SET IP: ", ether.myip);
  //-----
 
  for(int i = 0; i <= 4; i++)
  {
    pinMode(LedPins,OUTPUT); 
    digitalWrite (LedPins,HIGH);
    PinStatus=false;
  }  
}
 
// --------------------------------------
 
void loop()
{
 
  delay(1);
 
  word len = ether.packetReceive();   // check for ethernet packet
  word pos = ether.packetLoop(len);   // check for tcp packet
 
  if (pos) {
    bfill = ether.tcpOffset();
    char *data = (char *) Ethernet::buffer + pos;
    if (strncmp("GET /", data, 5) != 0) {
      bfill.emit_p(http_Unauthorized);
    }
   
    else {
 
      data += 5;
      if (data[0] == ' ') {       
        homePage(); // Return home page
        for (int i = 0; i <= 3; i++)digitalWrite(LedPins,!PinStatus[i+1]);
      }
 
      // "16" = "?ArduinoPIN1=on ".
      else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) {
        PinStatus[1] = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
        PinStatus[2] = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) {
        PinStatus[3] = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) {
        PinStatus[4] = true;        
        bfill.emit_p(http_Found);
      
      }
 
      //------------------------------------------------------  
 
      else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) {
        PinStatus[1] = false;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
        PinStatus[2] = false;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) {
        PinStatus[3] = false;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) {
        PinStatus[4] = false;        
        bfill.emit_p(http_Found);
      }
      
      //---------------------------
 
      else {
        // Page not found
        bfill.emit_p(http_Unauthorized);
      }
    }
    ether.httpServerReply(bfill.position());    // send http response
  }
}
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0
Guest samuelboerhoop
  • Inquirer
  • Update: The code below does work, but http page needs to refresh, then you see the response on the page.
                  How to add an "enter" in the GET command, so that the page will refresh ??

     

    I made a button in a VD with the following code:

    fibaro:log("Connecting...");
    local selfId = fibaro:getSelfId();   
    --local icon = fibaro:get(selfId, "deviceIcon");
    --fibaro:call(selfId, "setProperty", "currentIcon", icon)
    local ip = fibaro:get(selfId, 'IPAddress');
    local port = fibaro:get(selfId, 'TCPPort');
    NANO = Net.FHttp(ip, port)
    response, status, errorCode = NANO:GET('/?ArduinoPIN1=on')
    if ( errorCode == 0 )
    then
      fibaro:log("Transfer OK");
    else
      fibaro:log("Transfer failed");
    end
     
    But still not able to connect to the arduino. What I am doing wrong in the VD code or even Arduino code ??
    In the debug from the VD button I am not getting any errors in return.
    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

     Share

    ×
    ×
    • Create New...