Testet akkurat nå, det fungerer hos meg, men kjører forsatt versjon .118 av pluginen.
Her er testkoden jeg kjørte, en NodeMCU med en DHT11 sensor, du må evt bytte
char* Version = "API1.0.0.118"; til din versjon og
const byte BoardAdd = 4; til ditt Board i HS
samt endre
SendToHS(1, t);
SendToHS(2, h);
til riktige nr for deg
/************************************************************
*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*****************
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ISIP 1
#include <EEPROM.h>
#if ISIP == 1
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#else
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#endif
#endif
const byte BoardAdd = 4;
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "DHT.h"
#define DHTPIN 5 //D1
#define DHTTYPE DHT11
#define SLEEP_DELAY_IN_SECONDS 60
#if ISIP == 1
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x04};
IPAddress ip(192,168,1,204); //IP entered In HS config.
const unsigned int localPort = 8903; //port entered In HS config.
IPAddress HomeseerIP(192,168,1,61); //Homeseer IP address
IPAddress ServerIP(EEPROM.read(2),EEPROM.read(3),EEPROM.read(4),EEPROM.read(5));
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
byte EEpromVersion = EEPROM.read(250);
//Nettwork details for ESP8266
char ssid[] = "DIN_SSID"; // your network SSID (name)
char pass[] = "DITT_PW"; // your network password
#endif
//************Do not change anything in Here*****************
int FromHS[10]; // *
boolean IsConnected = false; // *
#if ISIP == 1 // *
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // *
#ifdef ESP8266
WiFiUDP Udp;
#else
EthernetUDP Udp;
#endif // *
const unsigned int ServerPort = 8888; // *
#endif
// *
#ifdef ESP8266
void resetFunc() {
ESP.restart();
}
#else
void(* resetFunc) (void) = 0;
#endif
//***********************************************************
//int sensorPin = A0; // select the input pin for the potentiometer
//int ledPin = 13; // select the pin for the LED
//int sensorValue = 0; // variable to store the value coming from the sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
HSSetup();
//************************
//Add YOUR SETUP HERE;
//************************
dht.begin();
}
void loop() {
#if ISIP == 1
IsUDP();
#endif
//************************
//Add YOUR CODE HERE;
//************************
/* 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*/
//sensorValue = analogRead(sensorPin);
//digitalWrite(ledPin, HIGH);
//delay(sensorValue);
//digitalWrite(ledPin, LOW);
// delay(sensorValue);
//Serial.println(sensorValue, DEC);
//Temp
int t = dht.readTemperature();
int h = dht.readHumidity();
Serial.print("Temp");
Serial.print(t);
Serial.print(" *C ");
Serial.print("Fuktighet: ");
Serial.print(h);
Serial.print(" %Rh");
if (IsConnected == true) {
/*Execute ONLY when HomeSeer is connected*/
SendToHS(1, t);
SendToHS(2, h);
delay(5000);
// Serial.println("Entering deep sleep mode for 60 seconds...");
// ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
//ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
//delay(500); // wait for deep sleep to happen
}
}
char* Version = "API1.0.0.118";
byte Byte1,Byte2,Byte3;
int Byte4,Byte5;
void HSSetup() {
#ifdef ESP8266
EEPROM.begin(256);
#endif
EEpromVersion = EEPROM.read(250);
ServerIP = IPAddress(EEPROM.read(2), EEPROM.read(3), EEPROM.read(4), EEPROM.read(5));
#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
//EEPROM.commit();//kommenter vekk?
EEpromVersion = 22;
}
#ifdef ESP8266
EEPROM.commit();
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#else
Ethernet.begin(mac, ip);
#endif
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)
{
//LastRcvIP = Udp.remoteIP();
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 (LastRcvIP != ServerIP) {
if (Udp.remoteIP() != ServerIP) {
//ServerIP=LastRcvIP;
ServerIP = Udp.remoteIP();
EEPROM.write(2, ServerIP[0]);
EEPROM.write(3, ServerIP[1]);
EEPROM.write(4, ServerIP[2]);
EEPROM.write(5, ServerIP[3]);
#ifdef ESP8266
EEPROM.commit();
#endif
}
#endif
break;
case 'C':
#if ISIP == 1
//Udp.beginPacket(LastRcvIP, ServerPort);
Udp.beginPacket(HomeseerIP, ServerPort);
Udp.print("Version ");
Udp.print(BoardAdd);
Udp.print(" ");
Udp.print(Version);
Udp.println(" HS3");
Udp.endPacket();
//Udp.beginPacket(LastRcvIP, ServerPort);
Udp.beginPacket(HomeseerIP, 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(LastRcvIP, ServerPort);
Udp.beginPacket(HomeseerIP, 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]);
#ifdef ESP8266
EEPROM.commit();
#endif
}
#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(LastRcvIP, ServerPort);
Udp.beginPacket(HomeseerIP, 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(LastRcvIP, ServerPort);
Udp.beginPacket(HomeseerIP, 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
}
}