Can't ping Arduino with ethernet shield
hi
i got arduino hookt ethernet shield , im running mqtt program on sends , receives sensor data etc raspberry pi im running node-red on. running , there no problem till decided wanted node-red ping arduino check if running. couldn't. ping got timeout time. pinged computer , got strange arduino on 192.168.0.102. doesn't when pin computer. , looks working if want belive last line sent = 4, received = 4. missing in code or has been in same situation me , maybe fixed problem?
my code.
i got arduino hookt ethernet shield , im running mqtt program on sends , receives sensor data etc raspberry pi im running node-red on. running , there no problem till decided wanted node-red ping arduino check if running. couldn't. ping got timeout time. pinged computer , got strange arduino on 192.168.0.102. doesn't when pin computer. , looks working if want belive last line sent = 4, received = 4. missing in code or has been in same situation me , maybe fixed problem?
code: [select]
pinging 192.168.0.102 32 bytes of data:
reply 192.168.0.1: destination host unreachable.
reply 192.168.0.1: destination host unreachable.
reply 192.168.0.1: destination host unreachable.
reply 192.168.0.1: destination host unreachable.
ping statistics 192.168.0.102:
packets: sent = 4, received = 4, lost = 0 (0% loss),
my code.
code: [select]
#include <spi.h>
#include <dht.h>
#include <ethernet.h>
#include <pubsubclient.h>
#include <remotereceiver.h>
#include <remotetransmitter.h>
// dht11 temperature , humidty sensor
#define dhtpin 3
#define dhttype dht11 //21 or 22 option
dht dht(dhtpin, dhttype);
unsigned long readtime;
unsigned long time;
// analog 0 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 , variable sensor/control mode
int sensemode = 0;
// update these values suitable 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]; // buffers our incoming messages can on 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;
(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 connect
if (client.connect("arduinoclient")) {
serial.println("connected");
// once connected, publish announcement...
client.publish("openhab","himitsu sensor, reporting in");
// ... , 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 hardware sort 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 last time read sensors.
if(millis() > readtime+6000){
sensorread();
}
// publish light reading every 5 seconds
if(millis() > (time + 5000)) {
sensorreadlight();
}
}
void sensorreadlight()
{
time = millis();
// read 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 250 milliseconds!
// sensor readings may 2 seconds 'old' (its slow sensor)
float h = dht.readhumidity();
// read temperature celsius (the default)
float t = dht.readtemperature();
// read temperature fahrenheit (isfahrenheit = true)
float f = dht.readtemperature(true);
// check if reads failed , exit (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
serial.println("failed read 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));
}
your ardunio shield doesn't have network connection.
the response gateway (your router), ping made gateway, host (ardunio) unreachable.
is led on shield marked "link" lit? should flash when receives data, should remain lit when link active (network connected).
the response gateway (your router), ping made gateway, host (ardunio) unreachable.
is led on shield marked "link" lit? should flash when receives data, should remain lit when link active (network connected).
Arduino Forum > Using Arduino > Programming Questions > Can't ping Arduino with ethernet shield
arduino
Comments
Post a Comment