www.homeseer.com    
 

Go Back   HomeSeer Message Board > Analog & Digital IO Device Plug-ins > Analog & Digital IO Discussion > Arduino Plugin (3P)

Arduino Plugin (3P) Discussions related to the Arduion plugin for HS3 by enigmatheatre

Reply
 
Thread Tools Display Modes
  #1  
Old November 25th, 2017, 09:56 AM
Beerpal Beerpal is offline
Viewer
 
Join Date: Nov 2017
Location: NORWAY
Posts: 4
Noob API startup help

Hi, I have been playing around with the basic plugin functions. And it is great.

But I want to read a value from a water meter like this one: http://forum.arduino.cc/index.php?topic=8548.0
and
K type temperature sensor:
https://github.com/adafruit/Adafruit...ermocouple.ino
Also adding
8 onewire temperature sensors
and several inputs and outputs for relays and signal

And for that, I need to go use API mode?

I hope someone can share a code sketch with me so I can play with.
a simple code with onewire and some output would be great.

Best regards
Pål
Reply With Quote
  #2  
Old November 25th, 2017, 03:22 PM
logbuilder logbuilder is offline
Seer Master
 
Join Date: Nov 2016
Location: Pacific North West
Posts: 676
The API route is a good move if:

1. You have devices that are not supported by the standard plugin sketch
2. You are willing to learn how to interact and control devices with code.

If those are true, then your best route is to select a device to start with and then find example sketches to work with that device. They are all over. Now the challenge is integrating that example code into the API sketch. Actually very easy. Several examples in this forum. But.... you gotta understand how to deal with devices/sensors at the code level first.
Reply With Quote
  #3  
Old November 26th, 2017, 12:58 PM
Beerpal Beerpal is offline
Viewer
 
Join Date: Nov 2017
Location: NORWAY
Posts: 4
Quote:
Originally Posted by logbuilder View Post
If those are true, then your best route is to select a device to start with and then find example sketches to work with that device. They are all over. Now the challenge is integrating that example code into the API sketch. Actually very easy. Several examples in this forum. But.... you gotta understand how to deal with devices/sensors at the code level first.
I have not found any sketch send several onewire probes that send to homeseer.

I don't know how I can address two or more different sensors "SendToHS(1,celsius)"
See the code.
Thanks for all your help so far.

Code:
/************************************************************
 *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
 * V1.0.0.118                                                 *
 *                                                           *
 *******Change the values below only*************************
 */

//************Do not change anything in Here*****************
 
#define ISIP 0
#if ISIP == 1
#include <EEPROM.h>
#include <SPI.h>       
#endif


const byte BoardAdd = 1;

#include <Ethernet.h>
#include <EthernetUdp.h> 

#if ISIP == 1
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01};
IPAddress ip(192,168,0,110);     //IP entered In HS config.
const unsigned int localPort = 8900;      //port entered In HS config.
IPAddress HomeseerIP(192,168,0,109); //Homeseer IP address
IPAddress ServerIP(EEPROM.read(2),EEPROM.read(3),EEPROM.read(4),EEPROM.read(5));
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
byte EEpromVersion = EEPROM.read(250);
#endif

int FromHS[10];                                          // *
boolean IsConnected = false;                             // *
#if ISIP == 1                                            // *
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];               // *
EthernetUDP Udp;                                         // *
const unsigned int ServerPort = 8888;                    // *
#endif                                                   // *
void(* resetFunc) (void) = 0;                            // *
//***********************************************************
#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(2);  // on pin 2 (a 4.7K resistor is necessary)
int ledPin = 5;
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval pause (milliseconds)



void setup() {
  HSSetup();
  //************************
  //Add YOUR SETUP HERE;
  //************************

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);


}




void loop() {
#if ISIP == 1
  IsUDP();
#endif

    //************************
    //Add YOUR CODE HERE;
    //************************
     unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;
  digitalWrite(ledPin, HIGH);
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    //delay(250);
    return;
  }
digitalWrite(ledPin, LOW);
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44);        // start conversion, use ds.write(0x44,1) with parasite power on at the end

 // delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.println(" Celsius, ");
  SendToHS(1,celsius);
  }
    /* To Send Data to Homeseer use SendToHS(Device,Value)
     Eg.. SendToHS(1,200); where 1 is the API device in homeseer and 200 is the value to send
     To Recieve data from Homeseer look up the FromHS array that is updated when the device value changes.
     Eg.. FromHS[5] would be the data from API Output device 5
     All code that is located just below this block will execute regardless of connection status!
     You can include SendToHS() calls, however when there isn't an active connection, it will just return and continue.
     If you only want code to execute when HomeSeer is connected, put it inside the if statement below.
     */

/*Execute regardless of connection status*/


 if (IsConnected == true) {
   /*Execute ONLY when HomeSeer is connected*/

  }
}


























































char* Version = "API1.0.0.118";

byte Byte1,Byte2,Byte3;
int Byte4,Byte5;


void HSSetup() {

#if ISIP == 1
    if (EEpromVersion!=22) {
    ServerIP=HomeseerIP;
    EEPROM.write(2,ServerIP[0]);
    EEPROM.write(3,ServerIP[1]);
    EEPROM.write(4,ServerIP[2]);
    EEPROM.write(5,ServerIP[3]);
    EEPROM.write(250,22); //Store the version where the eeprom data layout was last changed
    EEpromVersion=22;
  }
Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Udp.setTimeout(0);
  delay(1000);
SendConnect();
#else
  Serial.begin(115200);
  Serial.flush();
  Serial.setTimeout(0);
  delay(1000);
  Serial.print("Connect ");
  Serial.println(BoardAdd);
#endif

  IsConnected = false;

}

void SendConnect()
{
#if ISIP == 0
  Serial.print("Connect ");
  Serial.println(BoardAdd);
#else
    Udp.beginPacket(ServerIP,ServerPort);  //First send a connect packet to the dynamic IP stored in eeprom
    Udp.print("Connect ");
    Udp.print(BoardAdd);
    Udp.endPacket();
    if (ServerIP!=HomeseerIP) {
      Udp.beginPacket(HomeseerIP,ServerPort);  //Then if the stored value doesn't match the pre-specified one, send a connect packet there also
      Udp.print("Connect ");
      Udp.print(BoardAdd);
      Udp.endPacket();
    }
 
#endif
}


#if ISIP == 1
void IsUDP(){
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    IPAddress remote = Udp.remoteIP();
    Byte1 =Udp.parseInt();
    Udp.read(); 
    Byte2 =Udp.read(); 
    Byte3 =Udp.parseInt();
    Byte4 =Udp.parseInt();
    Byte5 =Udp.parseInt();
    DataEvent();
  }
}

#else
void serialEvent() {
  while (Serial.available() > 0) {
    delay(17);


    Byte1 = Serial.parseInt();
    Serial.read();  
    Byte2 = Serial.read(); 
    Byte3 = Serial.parseInt();
    Byte4 = Serial.parseInt();
    Byte5 = Serial.parseInt();
    DataEvent();
  }
}
#endif


/*

Used Data Input Cases
D Disconnect
r reset
K Keepalive
O PinMode Output Set
d Input debounce time set
C Connect request
c Connection established - report current status
*/
void DataEvent() {



  if (Byte1 == BoardAdd) {
    switch (Byte2) {

    case 'c':
      IsConnected = true;
#if ISIP == 1
      if (Udp.remoteIP() != ServerIP) {
        ServerIP=Udp.remoteIP();
        EEPROM.write(2,ServerIP[0]);
        EEPROM.write(3,ServerIP[1]);
        EEPROM.write(4,ServerIP[2]);
        EEPROM.write(5,ServerIP[3]);
      }     
#endif

      break;

    case 'C':   
#if ISIP == 1
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
      Udp.print("Version ");
      Udp.print(BoardAdd);
      Udp.print(" ");
      Udp.print(Version);
      Udp.println(" HS3");
      Udp.endPacket();

      Udp.beginPacket(Udp.remoteIP(), ServerPort);
      delay(100);
      Udp.print("Connected ");
      Udp.println(BoardAdd);
      Udp.endPacket();
#else
      Serial.print("Version ");
      Serial.print(BoardAdd);
      Serial.print(" ");
      Serial.print(Version);
      Serial.println(" HS3"); 
      delay(100);
      Serial.print("Connected ");
      Serial.println(BoardAdd);
#endif
      delay(100);
      IsConnected = false;
      break;

    case 'K':
      delay(200);
#if ISIP == 1
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
      Udp.print("Alive ");
      Udp.println(BoardAdd);
      Udp.endPacket();
      if (Udp.remoteIP() != ServerIP) {
        ServerIP=Udp.remoteIP();
        EEPROM.write(2,ServerIP[0]);
        EEPROM.write(3,ServerIP[1]);
        EEPROM.write(4,ServerIP[2]);
        EEPROM.write(5,ServerIP[3]);
      }     
#else     
      Serial.print("Alive ");
      Serial.println(BoardAdd);
#endif
      break; 
      
      case 'r':
      delay(200);
      resetFunc();  //call reset
      break; 

    case 'O':
      FromHS[Byte3] = Byte4;
      break; 

    case 'D':
      IsConnected = false;
      break;   
    }
  }
}

void SendToHS(byte Device, long Data){
if (IsConnected == true) {
#if ISIP == 1
  Udp.beginPacket(Udp.remoteIP(), ServerPort);
  Udp.print(BoardAdd);
  Udp.print(" API ");
  Udp.print(Device);
  Udp.print(" ");
  Udp.print(Data);
  Udp.endPacket();
#else
  Serial.print(BoardAdd);
  Serial.print(" API ");
  Serial.print(Device);
  Serial.print(" ");
  Serial.println(Data);
#endif
}
}

void SendToHS(byte Device, int Data){
if (IsConnected == true) {
#if ISIP == 1
  Udp.beginPacket(Udp.remoteIP(), ServerPort);
  Udp.print(BoardAdd);
  Udp.print(" API ");
  Udp.print(Device);
  Udp.print(" ");
  Udp.print(Data);
  Udp.endPacket();
#else
  Serial.print(BoardAdd);
  Serial.print(" API ");
  Serial.print(Device);
  Serial.print(" ");
  Serial.println(Data);
#endif
}
}

void SendToHS(byte Device, float Data){
if (IsConnected == true) {
#if ISIP == 1
  Udp.beginPacket(Udp.remoteIP(), ServerPort);
  Udp.print(BoardAdd);
  Udp.print(" API ");
  Udp.print(Device);
  Udp.print(" ");
  Udp.print(Data);
  Udp.endPacket();
#else
  Serial.print(BoardAdd);
  Serial.print(" API ");
  Serial.print(Device);
  Serial.print(" ");
  Serial.println(Data);
#endif
}
}

Last edited by Beerpal; November 26th, 2017 at 03:37 PM.
Reply With Quote
  #4  
Old November 26th, 2017, 04:35 PM
Beerpal Beerpal is offline
Viewer
 
Join Date: Nov 2017
Location: NORWAY
Posts: 4
Must I use this (https://www.hacktronics.com/Tutorial...ss-finder.html) to address all probes and make individual API input to
each?
Reply With Quote
  #5  
Old November 26th, 2017, 06:57 PM
Beerpal Beerpal is offline
Viewer
 
Join Date: Nov 2017
Location: NORWAY
Posts: 4
I have solved this after using my weekend to try to find out how this works.
I needed to update to beta version since the current version doesn't support comma as a decimal and therefore I got no value if I used float,
Reply With Quote
  #6  
Old November 27th, 2017, 02:30 AM
logbuilder logbuilder is offline
Seer Master
 
Join Date: Nov 2016
Location: Pacific North West
Posts: 676
I'm glad you solved your problem.

The point I was trying to make is that if you have a new sensor you want to add to an API sketch, the best way is to take a vanilla example sketch for that sensor, understand it, get it working, and then incorporate it onto the API sketch. You only have to deal with 'includes', globals, HSSetup, and HSloop. Call SendToHs and FromHS at the right times and you are good to go.
Reply With Quote
  #7  
Old February 5th, 2018, 06:22 PM
sweet sweet is offline
Seer
 
Join Date: May 2015
Location: Cleveland Ohio usa
Posts: 28
Hi can you share your code how you got multiple one wire addressed in api sketch.
Reply With Quote
  #8  
Old February 19th, 2018, 10:11 PM
Bugman1400 Bugman1400 is offline
Seer
 
Join Date: May 2016
Location: NC
Posts: 38
Where can I get a copy of the API and the documentation that shows how to use it?
Reply With Quote
  #9  
Old February 20th, 2018, 04:01 AM
ScottRennie ScottRennie is offline
Seer Deluxe
 
Join Date: Apr 2012
Location: Aberdeen
Posts: 457
Quote:
Originally Posted by sweet View Post
Hi can you share your code how you got multiple one wire addressed in api sketch.
Yes, please share what you found
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Startup Error: Startup.vb: could not find library 'Scheduler.dll' - Solution centerisland HS3 / HS3PRO Discussion 12 January 2nd, 2017 09:57 AM
noob HELP!! numgis HS3-Pi Discussion 9 December 18th, 2016 02:02 PM
Noob question mkailley Elk M1 Plug-In 3 December 22nd, 2006 11:43 AM
Super Noob - Have I Got This Right? bcard74 MCS Audrey 1 January 21st, 2004 01:08 AM


All times are GMT -4. The time now is 08:23 AM.


Copyright HomeSeer Technologies, LLC