mirror of
https://github.com/anklimov/lighthub
synced 2025-12-12 06:39:51 +03:00
Retained MQTT topics logic developed, unused sources (SD & extra 1-wire) moved to spare folders to save static RAM
This commit is contained in:
@@ -17,13 +17,13 @@ bool shouldSaveConfig = false;
|
||||
|
||||
//callback notifying us of the need to save config
|
||||
void saveConfigCallback () {
|
||||
Serial.println("Should save config");
|
||||
Serial.println(F("Should save config"));
|
||||
shouldSaveConfig = true;
|
||||
}
|
||||
|
||||
|
||||
void espSetup () {
|
||||
Serial.println("Setting up Wifi");
|
||||
Serial.println(F("Setting up Wifi"));
|
||||
shouldSaveConfig = true;
|
||||
//WiFiManager
|
||||
|
||||
@@ -37,7 +37,7 @@ void espSetup () {
|
||||
wifiManager.setMinimumSignalQuality();
|
||||
|
||||
if (!wifiManager.autoConnect()) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
Serial.println(F("failed to connect and hit timeout"));
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
@@ -45,7 +45,7 @@ if (!wifiManager.autoConnect()) {
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
Serial.println(F("connected...yeey :)"));
|
||||
|
||||
//read updated parameters
|
||||
strcpy(mqtt_password, custom_mqtt_password.getValue());
|
||||
|
||||
@@ -71,7 +71,7 @@ void Input::Parse()
|
||||
|
||||
|
||||
s = aJson.getObjectItem(inputObj,"S");
|
||||
if (!s) { Serial.print("In: ");Serial.print(pin);Serial.print("/");Serial.println(inType);
|
||||
if (!s) { Serial.print(F("In: "));Serial.print(pin);Serial.print(F("/"));Serial.println(inType);
|
||||
aJson.addNumberToObject(inputObj,"S", 0);
|
||||
s = aJson.getObjectItem(inputObj,"S");
|
||||
}
|
||||
@@ -110,7 +110,7 @@ int Input::Poll()
|
||||
|
||||
void Input::Changed (int val)
|
||||
{
|
||||
Serial.print(pin);Serial.print("=");Serial.println(val);
|
||||
Serial.print(pin);Serial.print(F("="));Serial.println(val);
|
||||
aJsonObject * item = aJson.getObjectItem(inputObj,"item");
|
||||
aJsonObject * scmd = aJson.getObjectItem(inputObj,"scmd");
|
||||
aJsonObject * rcmd = aJson.getObjectItem(inputObj,"rcmd");
|
||||
@@ -121,11 +121,11 @@ void Input::Changed (int val)
|
||||
|
||||
if (val)
|
||||
{ //send set command
|
||||
if (!scmd) mqttClient.publish(emit->valuestring,"ON"); else if (strlen(scmd->valuestring)) mqttClient.publish(emit->valuestring,scmd->valuestring);
|
||||
if (!scmd) mqttClient.publish(emit->valuestring,"ON",true); else if (strlen(scmd->valuestring)) mqttClient.publish(emit->valuestring,scmd->valuestring,true);
|
||||
}
|
||||
else
|
||||
{ //send reset command
|
||||
if (!rcmd) mqttClient.publish(emit->valuestring,"OFF"); else if (strlen(rcmd->valuestring)) mqttClient.publish(emit->valuestring,rcmd->valuestring);
|
||||
if (!rcmd) mqttClient.publish(emit->valuestring,"OFF",true); else if (strlen(rcmd->valuestring)) mqttClient.publish(emit->valuestring,rcmd->valuestring,true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ GIT: https://github.com/anklimov/lighthub
|
||||
e-mail anklimov@gmail.com
|
||||
|
||||
*/
|
||||
|
||||
#include "options.h"
|
||||
#include "item.h"
|
||||
#include "aJSON.h"
|
||||
|
||||
@@ -41,8 +41,8 @@ extern aJsonObject *modbusitem;
|
||||
int modbusSet(int addr, uint16_t _reg, int _mask, uint16_t value);
|
||||
|
||||
extern PubSubClient mqttClient;
|
||||
//extern const char* outprefix;
|
||||
const char outprefix[] PROGMEM = "/myhome/s_out/";
|
||||
//extern char outprefix[];
|
||||
const char outprefix[] PROGMEM = OUTTOPIC;
|
||||
|
||||
static unsigned long lastctrl = 0;
|
||||
static aJsonObject *lastobj = NULL;
|
||||
@@ -252,7 +252,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
case CH_VC:
|
||||
case CH_DIMMER:
|
||||
case CH_MODBUS:
|
||||
SendCmd(0, 1, Par); // Send back parameter for channel above this line
|
||||
if (send) SendCmd(0, 1, Par); // Send back parameter for channel above this line
|
||||
case CH_THERMO:
|
||||
case CH_VCTEMP:
|
||||
setVal(Par[0]); // Store value
|
||||
@@ -279,7 +279,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
Par[1] = st.s;
|
||||
Par[2] = st.v;
|
||||
params = 3;
|
||||
SendCmd(0, params, Par); // Send restored triplet
|
||||
SendCmd(0, params, Par); // Send restored triplet. In any cases
|
||||
break;
|
||||
|
||||
|
||||
@@ -291,15 +291,15 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
|
||||
Par[0] = st.aslong;
|
||||
params = 1;
|
||||
SendCmd(0, params, Par); // Send restored parameter
|
||||
SendCmd(0, params, Par); // Send restored parameter, even if send=false - no problem, loop will be supressed at next hop
|
||||
break;
|
||||
case CH_THERMO:
|
||||
Par[0] = st.aslong;
|
||||
params = 0;
|
||||
SendCmd(CMD_ON); // Just ON (switch)
|
||||
if (send) SendCmd(CMD_ON); // Just ON (switch)
|
||||
break;
|
||||
default:
|
||||
SendCmd(cmd); // Just send ON
|
||||
if (send) SendCmd(cmd); // Just send ON
|
||||
}//itemtype
|
||||
else {// Default settings
|
||||
Serial.print(st.aslong);
|
||||
@@ -325,7 +325,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
}
|
||||
|
||||
|
||||
setCmd(cmd);
|
||||
if (send) setCmd(cmd);
|
||||
} else { //Double ON - apply special preset - clean white full power
|
||||
switch (itemType) {
|
||||
case CH_RGBW:
|
||||
@@ -341,7 +341,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
setVal(st.aslong);
|
||||
|
||||
//Send to OH
|
||||
SendCmd(0, 3, Par);
|
||||
if (send) SendCmd(0, 3, Par);
|
||||
break;
|
||||
} //itemtype
|
||||
} //else
|
||||
@@ -359,7 +359,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
Par[1] = 0;
|
||||
Par[2] = 0;
|
||||
setCmd(cmd);
|
||||
SendCmd(cmd);
|
||||
if (send) SendCmd(cmd); ///?
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -370,7 +370,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
Par[1] = 0;
|
||||
Par[2] = 0;
|
||||
setCmd(cmd);
|
||||
SendCmd(CMD_OFF);
|
||||
SendCmd(CMD_OFF); //HALT to OFF mapping - send in any cases
|
||||
Serial.println(F(" Halted"));
|
||||
}
|
||||
|
||||
@@ -770,7 +770,7 @@ int Item::SendCmd(short cmd, short n, int *Par) {
|
||||
Serial.print(addrstr);
|
||||
Serial.print(F("->"));
|
||||
Serial.println(valstr);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
mqttClient.publish(addrstr, valstr,true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ class Item
|
||||
Item(char * name);
|
||||
Item(aJsonObject * obj);
|
||||
boolean isValid ();
|
||||
virtual int Ctrl(short cmd, short n=0, int * Par=NULL, boolean send=false);
|
||||
virtual int Ctrl(short cmd, short n=0, int * Par=NULL, boolean send=true);
|
||||
int getArg(short n=0);
|
||||
boolean getEnableCMD(int delta);
|
||||
//int getVal(short n); //From VAL array. Negative if no array
|
||||
|
||||
@@ -149,11 +149,6 @@ EthernetClient ethClient;
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef PIO_SRC_REV
|
||||
#define PIO_SRC_REV v0.98
|
||||
#endif
|
||||
|
||||
|
||||
#include "item.h"
|
||||
#include "inputs.h"
|
||||
|
||||
@@ -173,13 +168,10 @@ extern Artnet *artnet;
|
||||
#define GIST 2
|
||||
//#define serverip "192.168.88.2"
|
||||
//IPAddress server(192, 168, 88, 2); //TODO - configure it
|
||||
//char* inprefix=("/myhome/in/");
|
||||
//char* outprefix=("/myhome/s_out/");
|
||||
//char* subprefix=("/myhome/in/#");
|
||||
|
||||
#define inprefix "/myhome/in/"
|
||||
const char outprefix[] PROGMEM = "/myhome/s_out/";
|
||||
#define subprefix "/myhome/in/#"
|
||||
//const char inprefix[] PROGMEM = "/myhome/in/"
|
||||
const char outprefix[] PROGMEM = OUTTOPIC;
|
||||
const char inprefix[] PROGMEM = INTOPIC;
|
||||
|
||||
aJsonObject *root = NULL;
|
||||
aJsonObject *items = NULL;
|
||||
@@ -213,30 +205,43 @@ PubSubClient mqttClient(ethClient);
|
||||
void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE
|
||||
|
||||
// MQTT Callback routine
|
||||
#define sublen 20
|
||||
#define topiclen 20
|
||||
void callback(char *topic, byte *payload, unsigned int length) {
|
||||
payload[length] = 0;
|
||||
Serial.print(F("\n["));
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.print(F("] "));
|
||||
|
||||
int fr = freeRam();
|
||||
if (fr < 250) {
|
||||
Serial.println(F("OOM!"));
|
||||
return;
|
||||
}
|
||||
|
||||
#define sublen 20
|
||||
char subtopic[sublen] = "";
|
||||
int cmd = 0;
|
||||
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
Serial.print((char) payload[i]);
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// short intopic = strncmp(topic,F(inprefix),strlen(inprefix));
|
||||
// short outtopic = strncmp(topic,F(outprefix),strlen(outprefix));
|
||||
boolean retaining = (lanStatus == 4); //Todo - named constant
|
||||
//Check if topic = Command topic
|
||||
short intopic=0;
|
||||
{
|
||||
char buf[topiclen+1];
|
||||
strncpy_P(buf,inprefix,sizeof(buf));
|
||||
intopic = strncmp(topic,buf,strlen(inprefix));
|
||||
}
|
||||
|
||||
// in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant.
|
||||
if (retaining && !intopic) {
|
||||
Serial.println(F("Skipping.."));
|
||||
return;
|
||||
}
|
||||
|
||||
char subtopic[sublen] = "";
|
||||
int cmd = 0;
|
||||
|
||||
cmd = txt2cmd((char *) payload);
|
||||
char *t;
|
||||
if (t = strrchr(topic, '/')) strncpy(subtopic, t + 1, sublen - 1);
|
||||
@@ -270,7 +275,7 @@ void callback(char *topic, byte *payload, unsigned int length) {
|
||||
while (payload && i < 3)
|
||||
Par[i++] = getInt((char **) &payload);
|
||||
|
||||
item.Ctrl(0, i, Par);
|
||||
item.Ctrl(0, i, Par, !retaining);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -280,13 +285,13 @@ void callback(char *topic, byte *payload, unsigned int length) {
|
||||
|
||||
case CMD_ON:
|
||||
|
||||
if (item.getEnableCMD(500))
|
||||
item.Ctrl(cmd); //Accept ON command not earlier then 500 ms after set settings (Homekit hack)
|
||||
if (item.getEnableCMD(500) || lanStatus == 4)
|
||||
item.Ctrl(cmd, 0, NULL, !retaining); //Accept ON command not earlier then 500 ms after set settings (Homekit hack)
|
||||
else Serial.println("on Skipped");
|
||||
|
||||
break;
|
||||
default: //some known command
|
||||
item.Ctrl(cmd);
|
||||
item.Ctrl(cmd, 0, NULL, !retaining);
|
||||
|
||||
} //ctrl
|
||||
} //valid json
|
||||
@@ -320,7 +325,7 @@ void printMACAddress() {
|
||||
|
||||
void restoreState() {
|
||||
// Once connected, publish an announcement... // Once connected, publish an announcement...
|
||||
mqttClient.publish("/myhome/out/RestoreState", "ON");
|
||||
//mqttClient.publish("/myhome/out/RestoreState", "ON");
|
||||
};
|
||||
|
||||
|
||||
@@ -397,13 +402,23 @@ if((wifiMulti.run() == WL_CONNECTED)) lanStatus=1;
|
||||
Serial.println(client_id);
|
||||
|
||||
|
||||
// ... and resubscribe
|
||||
mqttClient.subscribe(subprefix);
|
||||
|
||||
|
||||
restoreState();
|
||||
// ... Temporary subscribe to status topic
|
||||
char buf[topiclen];
|
||||
|
||||
strncpy_P(buf,outprefix,sizeof(buf));
|
||||
strncat(buf,"#",sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
//Subscribing for command topics
|
||||
strncpy_P(buf,inprefix,sizeof(buf));
|
||||
strncat(buf,"#",sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
//restoreState();
|
||||
// if (_once) {DMXput(); _once=0;}
|
||||
lanStatus = 3;
|
||||
lanStatus = 4;
|
||||
lanCheck = millis() + 5000;
|
||||
Serial.println(F("Awaiting for retained topics"));
|
||||
} else {
|
||||
Serial.print(F("failed, rc="));
|
||||
Serial.print(mqttClient.state());
|
||||
@@ -414,6 +429,21 @@ if((wifiMulti.run() == WL_CONNECTED)) lanStatus=1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 4: //retaining ... Collecting
|
||||
if (millis() > lanCheck) {
|
||||
char buf[topiclen];
|
||||
|
||||
//Unsubscribe from status topics..
|
||||
strncpy_P(buf,outprefix,sizeof(buf));
|
||||
strncat(buf,"#",sizeof(buf));
|
||||
mqttClient.unsubscribe(buf);
|
||||
|
||||
lanStatus = 3;
|
||||
Serial.println(F("Accepting commands..."));
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: //operation
|
||||
if (!mqttClient.connected()) lanStatus = 2;
|
||||
break;
|
||||
@@ -505,7 +535,7 @@ void Changed(int i, DeviceAddress addr, int val) {
|
||||
if (owEmit) {
|
||||
strncpy(addrbuf, owEmit, sizeof(addrbuf));
|
||||
Serial.print(owEmit);
|
||||
Serial.print("=");
|
||||
Serial.print(F("="));
|
||||
Serial.println(val);
|
||||
}
|
||||
owItem = aJson.getObjectItem(owObj, "item")->valuestring;
|
||||
@@ -866,7 +896,7 @@ int getConfig(int arg_cnt, char **args)
|
||||
response = htclient.responseBody();
|
||||
htclient.stop();
|
||||
wdt_res();
|
||||
Serial.print("HTTP Status code: ");
|
||||
Serial.print(F("HTTP Status code: "));
|
||||
Serial.println(responseStatusCode);
|
||||
//Serial.print("GET Response: ");
|
||||
|
||||
@@ -1043,7 +1073,7 @@ void owIdle(void) {
|
||||
|
||||
wdt_res();
|
||||
return;///
|
||||
Serial.print("o");
|
||||
Serial.print(F("o"));
|
||||
|
||||
if (lanLoop() == 1) mqttClient.loop();
|
||||
//if (owReady) owLoop();
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
// Configuration of drivers enabled
|
||||
#ifndef PIO_SRC_REV
|
||||
#define PIO_SRC_REV v0.99
|
||||
#endif
|
||||
|
||||
#ifndef OUTTOPIC
|
||||
#define OUTTOPIC "/myhome/s_out/"
|
||||
#endif
|
||||
|
||||
#ifndef INTOPIC
|
||||
#define INTOPIC "/myhome/in/"
|
||||
#endif
|
||||
|
||||
#ifndef DMX_DISABLE
|
||||
#define _dmxin
|
||||
#define _dmxout
|
||||
@@ -12,7 +24,9 @@
|
||||
#define _modbus
|
||||
#endif
|
||||
|
||||
#ifdef ARTNET_ENABLE
|
||||
#define _artnet
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266)
|
||||
#define __ESP__
|
||||
@@ -43,4 +57,4 @@
|
||||
#endif
|
||||
|
||||
#define Q(x) #x
|
||||
#define QUOTE(x) Q(x)
|
||||
#define QUOTE(x) Q(x)
|
||||
|
||||
@@ -1,457 +0,0 @@
|
||||
/* Copyright © 2017-2018 Andrey Klimov. (anklimov@gmail.com) All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Homepage: http://lazyhome.ru
|
||||
GIT: https://github.com/anklimov/lighthub
|
||||
|
||||
*/
|
||||
|
||||
#include "owSwitch.h"
|
||||
#include "owTerm.h"
|
||||
#include <Arduino.h>
|
||||
#include "utils.h"
|
||||
|
||||
int owRead2408(uint8_t* addr) {
|
||||
uint8_t buf[13];
|
||||
// PrintBytes(buf, 13, true);
|
||||
if (!net) return -1;
|
||||
net->reset();
|
||||
net->select(addr);
|
||||
|
||||
// uint8_t buf[13]; // Put everything in the buffer so we can compute CRC easily.
|
||||
buf[0] = 0xF0; // Read PIO Registers
|
||||
buf[1] = 0x88; // LSB address
|
||||
buf[2] = 0x00; // MSB address
|
||||
net->write_bytes(buf, 3,1);
|
||||
net->read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16
|
||||
net->reset();
|
||||
|
||||
if (!OneWire::check_crc16(buf, 11, &buf[11])) {
|
||||
Serial.print(F("CRC failure in DS2408 at "));
|
||||
PrintBytes(addr, 8, true);
|
||||
PrintBytes(buf+3,10);
|
||||
return -1;
|
||||
}
|
||||
return (buf[3]);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
int read1W(int i)
|
||||
{
|
||||
|
||||
Serial.print("1W requested: ");
|
||||
Serial.println (i);
|
||||
|
||||
int t=-1;
|
||||
switch (term[i][0]){
|
||||
case 0x29: // DS2408
|
||||
t=owRead2408(term[i]);
|
||||
break;
|
||||
case 0x28: // Thermomerer
|
||||
t=sensors.getTempC(term[i]);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
int ow2408out(DeviceAddress addr,uint8_t cur)
|
||||
{
|
||||
if (!net) return -1;
|
||||
uint8_t buf[5];
|
||||
net->reset();
|
||||
net->select(addr);
|
||||
buf[0] = 0x5A; // Write PIO Registers
|
||||
buf[1]=cur;
|
||||
buf[2] = ~buf[1];
|
||||
net->write_bytes(buf, 3);
|
||||
net->read_bytes(buf+3, 2);
|
||||
//net.reset();
|
||||
PrintBytes(buf, 5);
|
||||
Serial.print(F(" Out: "));Serial.print(buf[1],BIN);
|
||||
Serial.print(F(" In: "));Serial.println(buf[4],BIN);
|
||||
if (buf[3] != 0xAA) {
|
||||
Serial.print(F("Write failure in DS2408 at "));
|
||||
PrintBytes(addr, 8, true);
|
||||
return -2;
|
||||
}
|
||||
return buf[4];
|
||||
}
|
||||
int cntrl2408(uint8_t* addr, int subchan, int val) {
|
||||
if (!net) return -1;
|
||||
|
||||
uint8_t buf;
|
||||
int mask,devnum;
|
||||
|
||||
if ((devnum=owFind(addr))<0) return -1;
|
||||
buf=regs[devnum];
|
||||
Serial.print(F("Current: "));Serial.println(buf,BIN);
|
||||
mask=0;
|
||||
int r,f;
|
||||
switch (subchan) {
|
||||
case 0:
|
||||
if ((buf & SW_STAT0) != ((val)?SW_STAT0:0))
|
||||
{
|
||||
if (wstat[devnum] & (SW_PULSE0|SW_PULSE_P0))
|
||||
{
|
||||
wstat[devnum]|=SW_CHANGED_P0;
|
||||
Serial.println(F("Rollback 0"));
|
||||
}
|
||||
else {
|
||||
wstat[devnum]|=SW_PULSE0;
|
||||
regs[devnum] = (ow2408out(addr,(buf | SW_MASK) & ~SW_OUT0) & SW_INMASK) ^ SW_STAT0; ///?
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
case 1:
|
||||
if ((buf & SW_STAT1) != ((val)?SW_STAT1:0))
|
||||
{
|
||||
if (wstat[devnum] & (SW_PULSE1|SW_PULSE_P1))
|
||||
{
|
||||
wstat[devnum]|=SW_CHANGED_P1;
|
||||
Serial.println(F("Rollback 1"));
|
||||
}
|
||||
else {
|
||||
wstat[devnum]|=SW_PULSE1;
|
||||
regs[devnum] =(ow2408out(addr,(buf | SW_MASK) & ~SW_OUT1) & SW_INMASK) ^ SW_STAT1; /// -?
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
/* Assume AUX 0&1 it is INPUTS - no write
|
||||
case 2:
|
||||
mask=SW_AUX0;
|
||||
break;
|
||||
case 3:
|
||||
mask=SW_AUX1; */
|
||||
}
|
||||
|
||||
/* Assume AUX 0&1 it is INPUTS - no write
|
||||
switch (val) {
|
||||
case 0: buf=(buf | SW_MASK | SW_OUT0 | SW_OUT1) | mask;
|
||||
break;
|
||||
default: buf= (buf | SW_MASK | SW_OUT0 | SW_OUT1) & ~mask;
|
||||
}
|
||||
|
||||
|
||||
regs[devnum] = ow2408out(addr,buf); */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int cntrl2890(uint8_t* addr, int val) {
|
||||
uint8_t buf[13];
|
||||
if (!net) return -1;
|
||||
// case 0x2C: //Dimmer
|
||||
Serial.print(F("Update dimmer "));PrintBytes(addr, 8, true);Serial.print(F(" = "));
|
||||
Serial.println(val);
|
||||
|
||||
net->reset();
|
||||
net->select(addr);
|
||||
|
||||
buf[0] = 0x55;
|
||||
buf[1] = 0x4c;
|
||||
net->write_bytes(buf, 2);
|
||||
net->read_bytes(buf+2, 1); // check if buf[2] == val = ok
|
||||
buf[3]=0x96;
|
||||
net->write_bytes(buf+3, 1);
|
||||
net->read_bytes(buf+4, 1); // 0 = updated ok
|
||||
PrintBytes(buf, 5, true);
|
||||
|
||||
net->select(addr);
|
||||
|
||||
|
||||
if (val==-1)
|
||||
{
|
||||
buf[0] = 0xF0;
|
||||
net->write_bytes(buf, 1);
|
||||
net->read_bytes(buf+1, 2); // check if buf[2] == val = ok
|
||||
net->reset();
|
||||
return buf[2];
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[0] = 0x0F;
|
||||
buf[1] = val;
|
||||
net->write_bytes(buf, 2);
|
||||
net->read_bytes(buf+2, 1); // check if buf[2] == val = ok
|
||||
buf[3]=0x96;
|
||||
net->write_bytes(buf+3, 1);
|
||||
net->read_bytes(buf+4, 1); // 0 = updated ok
|
||||
net->reset();
|
||||
PrintBytes(buf, 5, true);
|
||||
return buf[2];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#define DS2413_FAMILY_ID 0x3A
|
||||
#define DS2413_ACCESS_READ 0xF5
|
||||
#define DS2413_ACCESS_WRITE 0x5A
|
||||
#define DS2413_ACK_SUCCESS 0xAA
|
||||
#define DS2413_ACK_ERROR 0xFF
|
||||
|
||||
#define DS2413_IN_PinA 1
|
||||
#define DS2413_IN_LatchA 2
|
||||
#define DS2413_IN_PinB 4
|
||||
#define DS2413_IN_LatchB 8
|
||||
|
||||
#define DS2413_OUT_PinA 1
|
||||
#define DS2413_OUT_PinB 2
|
||||
|
||||
|
||||
/*
|
||||
byte read(void)
|
||||
{
|
||||
bool ok = false;
|
||||
uint8_t results;
|
||||
|
||||
oneWire.reset();
|
||||
oneWire.select(address);
|
||||
oneWire.write(DS2413_ACCESS_READ);
|
||||
|
||||
results = oneWire.read(); / Get the register results /
|
||||
ok = (!results & 0x0F) == (results >> 4); / Compare nibbles /
|
||||
results &= 0x0F; / Clear inverted values /
|
||||
|
||||
oneWire.reset();
|
||||
|
||||
// return ok ? results : -1;
|
||||
return results;
|
||||
}
|
||||
|
||||
bool write(uint8_t state)
|
||||
{
|
||||
uint8_t ack = 0;
|
||||
|
||||
/ Top six bits must '1' /
|
||||
state |= 0xFC;
|
||||
|
||||
oneWire.reset();
|
||||
oneWire.select(address);
|
||||
oneWire.write(DS2413_ACCESS_WRITE);
|
||||
oneWire.write(state);
|
||||
oneWire.write(~state); / Invert data and resend /
|
||||
ack = oneWire.read(); / 0xAA=success, 0xFF=failure /
|
||||
if (ack == DS2413_ACK_SUCCESS)
|
||||
{
|
||||
oneWire.read(); / Read the status byte /
|
||||
}
|
||||
oneWire.reset();
|
||||
|
||||
return (ack == DS2413_ACK_SUCCESS ? true : false);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
int cntrl2413(uint8_t* addr, int subchan, int val) {
|
||||
|
||||
|
||||
bool ok = false;
|
||||
uint8_t results;
|
||||
uint8_t cmd;
|
||||
uint8_t set=0;
|
||||
uint8_t count =10;
|
||||
if (!net) return -1;
|
||||
// case 0x85: //Switch
|
||||
Serial.print(F("Update switch "));PrintBytes(addr, 8, false); Serial.print(F("/"));Serial.print(subchan);Serial.print(F(" = "));Serial.println(val);
|
||||
while (count--)
|
||||
{
|
||||
net->reset();
|
||||
net->select(addr);
|
||||
net->setStrongPullup();
|
||||
|
||||
cmd = DS2413_ACCESS_READ;
|
||||
net->write(cmd);
|
||||
|
||||
results = net->read();
|
||||
Serial.print(F("Got: ")); Serial.println(results,BIN);
|
||||
//Serial.println((~results & 0x0F),BIN); Serial.println ((results >> 4),BIN);
|
||||
|
||||
ok = (~results & 0x0F) == (results >> 4); // Compare nibbles
|
||||
results &= 0x0F; // Clear inverted values
|
||||
|
||||
if (ok) {Serial.println(F("Read ok"));break;} else {Serial.println(F("read Error"));delay(1);}
|
||||
} //while
|
||||
|
||||
if (ok && (val>=0))
|
||||
{
|
||||
count=10;
|
||||
while (count--)
|
||||
{
|
||||
net->reset();
|
||||
net->select(addr);
|
||||
|
||||
if (results & DS2413_IN_LatchA) set|=DS2413_OUT_PinA;
|
||||
if (results & DS2413_IN_LatchB) set|=DS2413_OUT_PinB;
|
||||
|
||||
switch (subchan) {
|
||||
case 0:
|
||||
if (!val) set|=DS2413_OUT_PinA; else set &= ~DS2413_OUT_PinA;
|
||||
break;
|
||||
case 1:
|
||||
if (!val) set|=DS2413_OUT_PinB; else set &= ~DS2413_OUT_PinB;
|
||||
};
|
||||
set |= 0xFC;
|
||||
Serial.print(F("New: "));Serial.println(set,BIN);
|
||||
cmd = DS2413_ACCESS_WRITE;
|
||||
net->write(cmd);
|
||||
|
||||
net->write(set);
|
||||
net->write(~set);
|
||||
|
||||
uint8_t ack = net->read(); // 0xAA=success, 0xFF=failure
|
||||
|
||||
if (ack == DS2413_ACK_SUCCESS)
|
||||
{
|
||||
results=net->read();
|
||||
Serial.print(F("Updated ok: ")); Serial.println(results,BIN);
|
||||
ok = (~results & 0x0F) == (results >> 4); // Compare nibbles
|
||||
{
|
||||
if (ok)
|
||||
{Serial.println(F("Readback ok"));
|
||||
break;}
|
||||
else {Serial.println(F("readback Error"));delay(1);}
|
||||
}
|
||||
results &= 0x0F; // Clear inverted values
|
||||
}
|
||||
else Serial.println (F("Write failed"));;
|
||||
|
||||
} //while
|
||||
} //if
|
||||
return ok ? results : -1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int sensors_ext(void)
|
||||
{
|
||||
|
||||
int t;
|
||||
switch (term[si][0]){
|
||||
case 0x29: // DS2408
|
||||
//Serial.println(wstat[si],BIN);
|
||||
|
||||
if (wstat[si] & SW_PULSE0) {
|
||||
wstat[si]&=~SW_PULSE0;
|
||||
wstat[si]|=SW_PULSE_P0;
|
||||
Serial.println(F("Pulse0 in progress"));
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
||||
if (wstat[si] & SW_PULSE0_R) {
|
||||
wstat[si]&=~SW_PULSE0_R;
|
||||
wstat[si]|=SW_PULSE_P0;
|
||||
regs[si] =(ow2408out(term[si],(regs[si] | SW_MASK) & ~SW_OUT0) & SW_INMASK) ^ SW_STAT0;
|
||||
Serial.println(F("Pulse0 in activated"));
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
||||
if (wstat[si] & SW_PULSE1) {
|
||||
wstat[si]&=~SW_PULSE1;
|
||||
wstat[si]|=SW_PULSE_P1;
|
||||
Serial.println(F("Pulse1 in progress"));
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
||||
if (wstat[si] & SW_PULSE1_R) {
|
||||
wstat[si]&=~SW_PULSE1_R;
|
||||
wstat[si]|=SW_PULSE_P1;
|
||||
regs[si] =(ow2408out(term[si],(regs[si] | SW_MASK) & ~SW_OUT1) & SW_INMASK) ^ SW_STAT1;
|
||||
Serial.println(F("Pulse0 in activated"));
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
||||
if (wstat[si] & SW_PULSE_P0) {
|
||||
wstat[si]&=~SW_PULSE_P0;
|
||||
Serial.println(F("Pulse0 clearing"));
|
||||
ow2408out(term[si],regs[si] | SW_MASK | SW_OUT0);
|
||||
|
||||
if (wstat[si] & SW_CHANGED_P0) {
|
||||
wstat[si]&=~SW_CHANGED_P0;
|
||||
wstat[si]|=SW_PULSE0_R;
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
|
||||
if (wstat[si] & SW_PULSE_P1) {
|
||||
wstat[si]&=~SW_PULSE_P1;
|
||||
Serial.println(F("Pulse1 clearing"));
|
||||
ow2408out(term[si],regs[si] | SW_MASK | SW_OUT1);
|
||||
|
||||
if (wstat[si] & SW_CHANGED_P1) {
|
||||
wstat[si]&=~SW_CHANGED_P1;
|
||||
wstat[si]|=SW_PULSE1_R;
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
|
||||
if (wstat[si])
|
||||
{
|
||||
t=owRead2408(term[si]) & SW_INMASK;
|
||||
|
||||
|
||||
|
||||
if (t!=regs[si]) {
|
||||
|
||||
Serial.print(F("DS2408 data = "));
|
||||
Serial.println(t, BIN);
|
||||
|
||||
if (!(wstat[si] & SW_DOUBLECHECK))
|
||||
{
|
||||
wstat[si]|=SW_DOUBLECHECK; //suspected
|
||||
Serial.println(F("DOUBLECHECK"));
|
||||
return recheck_interval;
|
||||
}
|
||||
|
||||
|
||||
Serial.println(F("Really Changed"));
|
||||
if (owChanged) owChanged(si,term[si],t);
|
||||
regs[si]=t;
|
||||
|
||||
|
||||
}
|
||||
wstat[si]&=~SW_DOUBLECHECK;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 0x01:
|
||||
case 0x81:
|
||||
t=wstat[si];
|
||||
if (t!=regs[si])
|
||||
{ Serial.println(F("Changed"));
|
||||
if (owChanged) owChanged(si,term[si],t);
|
||||
regs[si]=t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
si++;
|
||||
return check_circle;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/* Copyright © 2017-2018 Andrey Klimov. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Homepage: http://lazyhome.ru
|
||||
GIT: https://github.com/anklimov/lighthub
|
||||
e-mail anklimov@gmail.com
|
||||
|
||||
*/
|
||||
|
||||
//define APU_OFF
|
||||
#include <DS2482_OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
|
||||
|
||||
int owRead2408(uint8_t* addr);
|
||||
int ow2408out(DeviceAddress addr,uint8_t cur);
|
||||
//int read1W(int i);
|
||||
int cntrl2408(uint8_t* addr, int subchan, int val=-1);
|
||||
int cntrl2413(uint8_t* addr, int subchan, int val=-1);
|
||||
int cntrl2890(uint8_t* addr, int val);
|
||||
@@ -1,212 +0,0 @@
|
||||
//
|
||||
// Created by livello on 14.10.17.
|
||||
//
|
||||
|
||||
/*
|
||||
SD card test
|
||||
|
||||
This example shows how use the utility libraries on which the'
|
||||
SD library is based in order to get info about your SD card.
|
||||
Very useful for testing a card when you're not sure whether its working or not.
|
||||
|
||||
The circuit:
|
||||
* SD card attached to SPI bus as follows:
|
||||
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
|
||||
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
|
||||
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
|
||||
** CS - depends on your SD card shield or module.
|
||||
Pin 4 used here for consistency with other Arduino examples
|
||||
|
||||
|
||||
created 28 Mar 2011
|
||||
by Limor Fried
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
*/
|
||||
|
||||
//template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
|
||||
|
||||
|
||||
#include "sd_card_w5100.h"
|
||||
#include <SD.h>
|
||||
#include <SPI.h>
|
||||
//#include <iostream>
|
||||
|
||||
// set up variables using the SD utility library functions:
|
||||
Sd2Card card;
|
||||
SdVolume volume;
|
||||
SdFile sdFile;
|
||||
File myFile;
|
||||
|
||||
|
||||
char *entireFileContents;
|
||||
|
||||
// Arduino Ethernet shield: pin 4
|
||||
const int chipSelect = 4;
|
||||
|
||||
void bench() {
|
||||
/* uint8_t buf[BUF_SIZE];
|
||||
long maxLatency,minLatency,totalLatency,temp_DHT22;
|
||||
|
||||
|
||||
myFile = SD.open("test.txt", FILE_WRITE);
|
||||
|
||||
// if the file opened okay, write to it:
|
||||
if (myFile) {
|
||||
Serial.print("Writing to test.txt...");
|
||||
myFile.println("testing 1, 2, 3.");
|
||||
// close the file:
|
||||
|
||||
} else {
|
||||
// if the file didn'temp_DHT22 open, print an error:
|
||||
Serial.println("error opening test.txt");
|
||||
}
|
||||
|
||||
|
||||
for (uint16_t i = 0; i < (BUF_SIZE - 2); i++) {
|
||||
buf[i] = 'A' + (i % 26);
|
||||
}
|
||||
buf[BUF_SIZE - 2] = '\r';
|
||||
buf[BUF_SIZE - 1] = '\n';
|
||||
|
||||
Serial.println("File size MB: ");
|
||||
Serial.print(FILE_SIZE_MB);
|
||||
Serial.println("Buffer size ");
|
||||
Serial.print(BUF_SIZE);
|
||||
Serial.println("Starting write test, please wait.");
|
||||
|
||||
// do write test
|
||||
uint32_t n = FILE_SIZE / sizeof(buf);
|
||||
Serial.println("write speed and latency");
|
||||
Serial.print(" speed,max,min,avg");
|
||||
Serial.print(" KB/Sec,usec,usec,usec");
|
||||
for (uint8_t nTest = 0; nTest < WRITE_PASS_COUNT; nTest++) {
|
||||
// myFile.truncate(0);
|
||||
maxLatency = 0;
|
||||
minLatency = 9999999;
|
||||
totalLatency = 0;
|
||||
temp_DHT22 = millis();
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
uint32_t m = micros();
|
||||
if (myFile.write(buf, sizeof(buf)) != sizeof(buf)) {
|
||||
Serial.println("write failed");
|
||||
}
|
||||
m = micros() - m;
|
||||
if (maxLatency < m) {
|
||||
maxLatency = m;
|
||||
}
|
||||
if (minLatency > m) {
|
||||
minLatency = m;
|
||||
}
|
||||
totalLatency += m;
|
||||
}
|
||||
myFile.flush();
|
||||
temp_DHT22 = millis() - temp_DHT22;
|
||||
int s = myFile.size();
|
||||
Serial.println(s / temp_DHT22);
|
||||
Serial.print(',');
|
||||
Serial.print(maxLatency);
|
||||
Serial.print(',');
|
||||
Serial.print(minLatency);
|
||||
Serial.print(',');
|
||||
Serial.print(totalLatency / n);
|
||||
}*/
|
||||
myFile.close();
|
||||
Serial.println("done.");
|
||||
}
|
||||
|
||||
char* sdW5100_readEntireFile(const char *filename) {
|
||||
SdFile requestedFile;
|
||||
if(requestedFile.open(sdFile,filename)) {
|
||||
Serial.println("Success open INDEX.HTM:");
|
||||
long time_started = millis();
|
||||
entireFileContents = new char[requestedFile.fileSize()];
|
||||
requestedFile.read(entireFileContents,requestedFile.fileSize());
|
||||
Serial.print(millis()-time_started);
|
||||
Serial.println(" milliseconds takes to read.");
|
||||
return entireFileContents;
|
||||
}
|
||||
else {
|
||||
Serial.print("Failed sdFile.open ");
|
||||
Serial.println(filename);
|
||||
|
||||
}
|
||||
return NULL;
|
||||
|
||||
}
|
||||
uint32_t sdW5100_getFileSize(const char *filename){
|
||||
SdFile requestedFile;
|
||||
if(requestedFile.open(sdFile,filename))
|
||||
return requestedFile.fileSize();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sd_card_w5100_setup() {
|
||||
Serial.print("\nInitializing SD card...");
|
||||
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
|
||||
// Note that even if it's not used as the CS pin, the hardware SS pin
|
||||
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
|
||||
// or the SD library functions will not work.
|
||||
pinMode(chipSelect, OUTPUT);
|
||||
|
||||
if (!card.init(SPI_FULL_SPEED, chipSelect)) {
|
||||
Serial.println("initialization failed. Things to check:");
|
||||
Serial.println("* is a card is inserted?");
|
||||
Serial.println("* Is your wiring correct?");
|
||||
Serial.println("* did you change the chipSelect pin to match your shield or module?");
|
||||
return;
|
||||
} else {
|
||||
Serial.println("Wiring is correct and a card is present.");
|
||||
}
|
||||
|
||||
// print the type of card
|
||||
Serial.print("\nCard type: ");
|
||||
switch (card.type()) {
|
||||
case SD_CARD_TYPE_SD1:
|
||||
Serial.println("SD1");
|
||||
break;
|
||||
case SD_CARD_TYPE_SD2:
|
||||
Serial.println("SD2");
|
||||
break;
|
||||
case SD_CARD_TYPE_SDHC:
|
||||
Serial.println("SDHC");
|
||||
break;
|
||||
default:
|
||||
Serial.println("Unknown");
|
||||
}
|
||||
|
||||
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
|
||||
if (!volume.init(card)) {
|
||||
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// print the type and size of the first FAT-type volume
|
||||
uint32_t volumesize;
|
||||
Serial.print("\nVolume type is FAT");
|
||||
Serial.println(volume.fatType(), DEC);
|
||||
Serial.println();
|
||||
|
||||
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
|
||||
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
|
||||
volumesize *= 512; // SD card blocks are always 512 bytes
|
||||
Serial.print("Volume size (bytes): ");
|
||||
Serial.println(volumesize);
|
||||
Serial.print("Volume size (Kbytes): ");
|
||||
volumesize /= 1024;
|
||||
Serial.println(volumesize);
|
||||
Serial.print("Volume size (Mbytes): ");
|
||||
volumesize /= 1024;
|
||||
Serial.println(volumesize);
|
||||
|
||||
|
||||
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
|
||||
sdFile.openRoot(volume);
|
||||
// list all files in the card with date and size
|
||||
sdFile.ls(LS_R | LS_DATE | LS_SIZE);
|
||||
Serial.println(sdW5100_readEntireFile("INDEX.HTM"));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
//
|
||||
// Created by livello on 14.10.17.
|
||||
//
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef NODEMCU_STOPWATCH_SD_CARD_W5100_H
|
||||
#define NODEMCU_STOPWATCH_SD_CARD_W5100_H
|
||||
|
||||
#endif //NODEMCU_STOPWATCH_SD_CARD_W5100_H
|
||||
void sd_card_w5100_setup();
|
||||
void cidDmp();
|
||||
void bench();
|
||||
char* sdW5100_readEntireFile(const char *filename);
|
||||
uint32_t sdW5100_getFileSize(const char *filename);
|
||||
Reference in New Issue
Block a user