Hi all,
I am having a bit of difficulty with the development of a project for my home automation ideas.
I also am not sure if i should start out by asking here. As far as the Sketch goes id imagine adding
the libraries for the SPI, DHT, Ethernet, Modbus would be all needed. As giving the raw digital
value from the DHT likely wont be useful to node red. The idea is to have for now humidity and temp
readings in the main living space, 2 temp probes with one in the kitchen and another in the main
bed room. Reason why I want modbus is to have it read by Node Red and then to make arguments
based on that information. As well as being able to log those values to either a database or to some
excel Doc. Once I have this I would like to add the ability for Node Red to send back control commands
for extractor fans to turn on providing some airflow and might expand upon the sensory at that same
time.
I have looked around multiple places and done alot of googling trying to get to some answers. some
have helped with the idea on how to implement it but I am still stuck. Between using this sketch I
found here ( MODBUS protocol Library - Networking, Protocols, and Devices - Arduino Forum )
But this does not include any DHT sensors it seems .As well as thermo resistors being used which also
wont help . The components I have are an Arduino mega 2560, Arduino Ethernet R3, DHT22 and 2
DS18B20's. I have a load of other components too as i want to experiment a bit.
Please Any help would really be appreciated .Thank you.
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;
void setup()
{
//Setup networking
uint8_t mac[] = { 0x90, 0xA2, 0xCA, 0x00, 0x54, 0x16 };
IPAddress ip(192, 168, 14, 30);
IPAddress gateway = ( 192, 168, 14, 69 );
IPAddress subnet = ( 255, 255, 255, 0 );
Ethernet.begin(mac, ip, gateway, subnet);
Mb.ConnectionTimeout = 100;
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
// Read the current state of the pins before calling Run()
// This allows you to read forced pin state changes
// Any writes after connecting to the master are written after
Mb.C[0] = digitalRead(0);
Mb.C[1] = digitalRead(1);
//Mb.C[2] = digitalRead(2);
//Mb.C[3] = digitalRead(3);
Mb.C[4] = false;
//Mb.C[5] = digitalRead(5);
//Mb.C[6] = digitalRead(6);
Mb.C[7] = digitalRead(7);
Mb.C[8] = digitalRead(8);
Mb.C[9] = digitalRead(9);
Mb.C[10] = false;
Mb.C[11] = digitalRead(11);
Mb.C[12] = digitalRead(12);
Mb.C[13] = digitalRead(13);
Mb.C[14] = digitalRead(14);
Mb.C[15] = digitalRead(15);
Mb.C[16] = digitalRead(16);
Mb.C[17] = digitalRead(17);
Mb.C[18] = digitalRead(18);
Mb.C[19] = digitalRead(19);
Mb.C[20] = digitalRead(20);
Mb.C[21] = digitalRead(21);
Mb.C[22] = digitalRead(22);
Mb.C[23] = digitalRead(23);
Mb.C[24] = digitalRead(24);
Mb.C[25] = digitalRead(25);
Mb.C[26] = digitalRead(26);
Mb.C[27] = digitalRead(27);
Mb.C[28] = digitalRead(28);
Mb.C[29] = digitalRead(29);
Mb.C[30] = digitalRead(30);
Mb.C[31] = digitalRead(31);
Mb.C[32] = digitalRead(32);
Mb.C[33] = digitalRead(33);
Mb.C[34] = digitalRead(34);
Mb.C[35] = digitalRead(35);
Mb.C[36] = digitalRead(36);
Mb.C[37] = digitalRead(37);
Mb.C[38] = digitalRead(38);
Mb.C[39] = digitalRead(39);
Mb.C[40] = digitalRead(40);
Mb.C[41] = digitalRead(41);
Mb.C[42] = digitalRead(42);
Mb.C[43] = digitalRead(43);
Mb.C[44] = digitalRead(44);
Mb.C[45] = digitalRead(45);
Mb.C[46] = digitalRead(46);
Mb.C[47] = digitalRead(47);
Mb.C[48] = digitalRead(48);
Mb.C[49] = digitalRead(49);
Mb.C[50] = false;
Mb.C[51] = false;
Mb.C[52] = false;
Mb.C[53] = false;
//Analog inputs 0-1023
// Temperature readings using a 3k NTC thermistor
Mb.R[0] = int(tempFromADC(analogRead(1)) * 10); //pin A0 to Mb.R[0]
Mb.R[1] = int(tempFromADC(analogRead(2)) * 10);
Mb.R[2] = int(tempFromADC(analogRead(2)) * 10);
Mb.R[3] = analogRead(A3);
Mb.R[4] = analogRead(A4);
// Basic light measurement using a photoresistor
Mb.R[5] = lightScale();
Mb.R[6] = analogRead(A6);
Mb.R[7] = analogRead(A7);
Mb.R[8] = analogRead(A8);
Mb.R[9] = analogRead(A9);
Mb.R[10] = analogRead(A10);
Mb.R[11] = analogRead(A11);
Mb.R[12] = analogRead(A12);
Mb.R[13] = analogRead(A13);
Mb.R[14] = analogRead(A14);
Mb.R[15] = analogRead(A15);
// Slave Memory
Mb.R[16] = freeRam();
// Slave uptime, 32 bits carried by two (2) 16 bit registers
long uptime = millis();
// High word
Mb.R[17] = uptime >> 16 & 0xFFFF;
// Low word
Mb.R[18] = uptime & 0xFFFF;
// Listen for the master
Mb.Run();
// This delay improves stability by orders of magnitude
delay(10);
// Set digital outs
digitalWrite(2, Mb.C[2]);
digitalWrite(3, Mb.C[3]);
digitalWrite(5, Mb.C[5]);
digitalWrite(6, Mb.C[6]);
}
double tempFromADC(int adcVal)
{
double temp;
// See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
temp = log(((10240000/adcVal) - 10000));
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
temp = temp - 273.15; // Convert Kelvin to Celcius
temp = (temp * 1.8) + 32.0; // Comment this line for C instead of F
return temp;
}
int lightScale()
{
double val = analogRead(5) / 1023.0;
int returnVal = int(val * 1000);
return returnVal;
}
int freeRam()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}