Hi
I got my arduino hookt to an ethernet shield and im running an mqtt program on it which sends and receives some sensor data etc from my raspberry pi where im running node-red on. Everything is running good and there are no problem till i decided i wanted node-red to ping my arduino just to check if its running. It couldn't. The ping got timeout all the time. So i pinged it from my computer and i got something strange back The arduino is on 192.168.0.102. It usually doesn't look like this when i pin another computer. And its looks like its working if you want to belive the last line Sent = 4, Received = 4. Am i missing something in my code or has anyone been in the same situation as me and maybe fixed the problem?
Pinging 192.168.0.102 with 32 bytes of data:
Reply from 192.168.0.1: Destination host unreachable.
Reply from 192.168.0.1: Destination host unreachable.
Reply from 192.168.0.1: Destination host unreachable.
Reply from 192.168.0.1: Destination host unreachable.
Ping statistics for 192.168.0.102:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
my code.
#include <SPI.h>
#include <DHT.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <RemoteReceiver.h>
#include <RemoteTransmitter.h>
// DHT11 temperature and humidty sensor
#define DHTPIN 3
#define DHTTYPE DHT11 //21 or 22 also an option
DHT dht(DHTPIN, DHTTYPE);
unsigned long readTime;
unsigned long time;
// Analog 0 is the input pin
int lightPinIn = 0;
// Receiver/Transmitter pin
ActionTransmitter actionTransmitter(9);
// relays
#define RELAY1 5
#define RELAY2 6
#define RELAY3 7
#define RELAY4 8
// defines and variable for sensor/control mode
int senseMode = 0;
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 102);
IPAddress server(192, 168, 0, 110);
char message_buff[100]; // this buffers our incoming messages so we can do something on certain commands
EthernetClient ethClient;
PubSubClient client(ethClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
int i=0;
for (i=0;i<length;i++) {
Serial.print((char)payload[i]);
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
if (msgString.equals("OFF1")) {
digitalWrite(RELAY1, HIGH);
client.publish("openhab/himitsu/command","Relay 1 OFF");
}
else if(msgString.equals("ON1")){
digitalWrite(RELAY1, LOW);
client.publish("openhab/himitsu/command","Relay 1 ON");
}
if (msgString.equals("OFF2")) {
digitalWrite(RELAY2, HIGH);
client.publish("openhab/himitsu/command","Relay 2 OFF");
}
else if(msgString.equals("ON2")){
digitalWrite(RELAY2, LOW);
client.publish("openhab/himitsu/command","Relay 2 ON");
}
if (msgString.equals("OFF3")) {
digitalWrite(RELAY3, HIGH);
client.publish("openhab/himitsu/command","Relay 3 OFF");
}
else if(msgString.equals("ON3")){
digitalWrite(RELAY3, LOW);
client.publish("openhab/himitsu/command","Relay 3 ON");
}
if (msgString.equals("OFF4")) {
digitalWrite(RELAY4, HIGH);
client.publish("openhab/himitsu/command","Relay 4 OFF");
}
else if(msgString.equals("ON4")){
digitalWrite(RELAY4, LOW);
client.publish("openhab/himitsu/command","Relay 4 ON");
}
if (msgString.equals("RemoteOFF1")) {
actionTransmitter.sendSignal(1, 'A', false);
client.publish("openhab/himitsu/command","Remote Switch 1 OFF");
}
else if(msgString.equals("remoteON1")){
actionTransmitter.sendSignal(1, 'A', true);
client.publish("openhab/himitsu/command","Remote Switch 1 ON");
}
if (msgString.equals("RemoteOFF2")) {
actionTransmitter.sendSignal(1, 'B', false);
client.publish("openhab/himitsu/command","Remote Switch 2 OFF");
}
else if(msgString.equals("remoteON2")){
actionTransmitter.sendSignal(1, 'B', true);
client.publish("openhab/himitsu/command","Remote Switch 2 ON");
}
if (msgString.equals("RemoteOFF3")) {
actionTransmitter.sendSignal(1, 'C', false);
client.publish("openhab/himitsu/command","Remote Switch 3 OFF");
}
else if(msgString.equals("remoteON3")){
actionTransmitter.sendSignal(1, 'C', true);
client.publish("openhab/himitsu/command","Remote Switch 3 ON");
}
if (msgString.equals("reboot")) {
actionTransmitter.sendSignal(1, 'C', false);
delay(20000);
actionTransmitter.sendSignal(1, 'C', true);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("openhab","himitsu sensor, reporting in");
// ... and resubscribe
client.subscribe("openhab/himitsu/command");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac);
dht.begin();
// Allow the hardware to sort itself out
delay(1500);
Serial.println(Ethernet.localIP());
readTime = 0;
// Relay board pins
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
//Relay default off
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
//check if 6 seconds has elapsed since the last time we read the sensors.
if(millis() > readTime+6000){
sensorRead();
}
// publish light reading every 5 seconds
if(millis() > (time + 5000)) {
sensorReadlight();
}
}
void sensorReadlight()
{
time = millis();
// read from light sensor (photocell)
int lightRead = analogRead(lightPinIn);
String pubString = String(lightRead);
pubString.toCharArray(message_buff, pubString.length()+1);
Serial.println(message_buff);
client.publish("openhab/himitsu/lightsensor", message_buff);
}
void sensorRead(){
readTime = millis();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
char buffer[10];
dtostrf(t,0, 0, buffer);
client.publish("openhab/himitsu/temperature",buffer);
//Serial.println(buffer);
dtostrf(h,0, 0, buffer);
client.publish("openhab/himitsu/humidity",buffer);
//client.publish("inTopic/humidity",sprintf(buf, "%f", h));
}