Raspberry pi 3 & Wifi & blynk cloud problem

Hi all, i’m using my raspberry pi 3 with relays to heat my chicken coop. It’s coming colder here in Canada :wink:

My chicken cool is on the edge of my wifi range and my problem is my script crash when the pi loose the wifi signal.

Is there a solution to avoid the crash from blynk… and let the script try to reconnect to blynk-cloud later?

I’m using blynk-library on node.

This is my code feel free to optimize it if you want… i’m new to coding :wink:

	var gpio = require('rpi-gpio');
	var blynkLib = require('blynk-library');
	var sensorLib = require('node-dht-sensor');
	var AUTH = 'b8ab71b111a44231a2d7de9a858b4a3a';

	// Setup Blynk
	var blynk = new blynkLib.Blynk(AUTH);

	// Setup Relais
	var v0 = new blynk.VirtualPin(0);

	// ACTIVATION LAMPE VIA PUSH BUTTON -- WORKING
	var lamp = 1
	v0.on('write', function() {
	if (lamp == 0) {
	lamp = 1;
	gpio.setup(11, gpio.DIR_OUT, write);
	function write() {
	gpio.write(11, true, function(err) {

	});
	}

	console.log ('Heater On');
	} else {
	lamp = 0;
	gpio.setup(11, gpio.DIR_OUT, write2);
	function write2() {
	gpio.write(11, false, function(err) {
	});
	}
	console.log ('Heater Off');
	}

	});

	// LAMPE D'APPOINT
	gpio.setup(13, gpio.DIR_OUT, write6);
	function write6() {
	gpio.write(13, false, function(err) {
	});
	}
	blynk.virtualWrite(5, 0);



	// Setu2sensor, exit if failed
	var sensorType = 22; // 11 for DHT11, 22 for DHT22 and AM2302
	var sensorPin  = 4;  // The GPIO pin number for sensor signal
	if (!sensorLib.initialize(sensorType, sensorPin)) {
	console.warn('Failed to initialize sensor');
	process.exit(1);
	}


	var d = new Date();
	var lampe_appoint = 0;
	if ( d.getHours() > 5 && d.getHours() < 20 ) {
	lampe_appoint = 0;
	blynk.virtualWrite(5, 1);
	} else if( d.getHours() < 5 || d.getHours() > 19 )  {
	lampe_appoint = 1;
	blynk.virtualWrite(5, 0);
	}
	console.log('Lampe de jour = ' + lampe_appoint);
	console.log(d.getHours());

	var readout = sensorLib.read();
	blynk.virtualWrite(3, readout.temperature.toFixed(1));
	blynk.virtualWrite(4, readout.humidity.toFixed(1));
	var heat_relay = 0;
	if ( readout.temperature.toFixed(1) < 18 ) { 
	heat_relay = 0;
	//blynk.virtualWrite(0, 1);
	console.log('Température Froide');
	//} else if ( readout.temperature.toFixed(1) > 22 ) {
	//	heat_relay = 0;
	//	blynk.virtualWrite(0, 0);
	//	console.log('Température Chaude');

	} else {
	heat_relay = 1;
	//blynk.virtualWrite(0, 0);
	console.log('Température OK');
	}



	// Automatically update sensor value every 2 seconds
	setInterval(function() {
	var dh = new Date();
	if (dh.getHours() > 5 && dh.getHours() < 20 && lampe_appoint == 0 ) {
	gpio.setup(13, gpio.DIR_OUT, write7);
	function write7() {
	gpio.write(13, true, function(err) {
	});
	}
	blynk.virtualWrite(5, 1);
	lampe_appoint = 1;
	console.log('Activation lampe d\'appoint');

	} else if (dh.getHours() < 5 || dh.getHours() > 19 && lampe_appoint == 1) {
	gpio.setup(13, gpio.DIR_OUT, write8);
	function write8() {
	gpio.write(13, false, function(err) {
	});
	}
	lampe_appoint = 0;
	blynk.virtualWrite(5, 0);
	}

	if (dh.getHours() > 5 && dh.getHours() < 20 && lampe_appoint == 1 ) {
	console.log('Heure plus grande que 5 et plus petit que 20');
	console.log(dh.getHours());
	} else {
	console.log('Heure plus petit que 6 et plus grand que 20');
	console.log(dh.getHours());
	}

	if (lampe_appoint == 1) {
	console.log('----------------------------\nLumière d\'appoint ON\n----------------------------');
	console.log(lampe_appoint);
	} else {
	console.log('----------------------------\nLumière d\'appoint OFF\n----------------------------');
	console.log(lampe_appoint);
	}



	// AFFICHAGE DES VALEURS DANS L'APPLICATION BLYNK - HUMIDITY & TEMPERATURE
	var readout = sensorLib.read();
	blynk.virtualWrite(3, readout.temperature.toFixed(1));
	blynk.virtualWrite(4, readout.humidity.toFixed(1));



	console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
	console.log('Humidity:   ', readout.humidity.toFixed(1)    + '%');
	console.log(heat_relay);
	// ACTIVATION DE LA LAMPE CHAUFFANTE EN FONCTION DE LA TEMP…RATURE INTERIEUR
	if ( readout.temperature.toFixed(1) > 22 && heat_relay == 1 ) {

	heat_relay = 0;
	gpio.setup(11, gpio.DIR_OUT, write3);
	function write3() {
	gpio.write(11, false, function(err) {
	});
	}
	blynk.virtualWrite(0, 0);

	} else if ( readout.temperature.toFixed(1) < 18 && heat_relay == 0) { 
	heat_relay = 1;
	gpio.setup(11, gpio.DIR_OUT, write4);
	function write4() {
	gpio.write(11, true, function(err) {
	});
	}
	console.log('Activation de la lampe chauffante');
	blynk.virtualWrite(0, 1);


	}



	if (heat_relay == 1) {
	console.log('----------------------------\nLumière chauffantee ON\n----------------------------'); 
	console.log(heat_relay);
	} else {
	console.log('----------------------------\nLumière chauffantee OFF\n----------------------------');
	console.log(heat_relay);
	}
	}, 2000);

Thanks all

@Patrice_Bois it’s nice to see some “Pi code” as we see very little of it on this site as you will have noticed from your post about the LCD Widget and Node.js.

Blynkers can probably advise how they reconnect to blynk-cloud in “Arduino code” and then you might be able to convert to Pi code.

Could you please explain how the Pi is connecting to the internet (WiFi, Ethernet, other).

I think he mentioned WiFi :wink:

Anyway, is it possible to use an ethernet cable? It’s much more reliable and you don’t want your chicks to suffer during the cold harsh winter of Canada. I’ve always found Wifi reliable inside the house, but for outside applications I’d strongly recommend using a cable.

If this is not possible, there is probably not too much to do. WiFi is a medium which relies on a transmitter and receiver and if either side of the connection is bad you have a problem. So if you have a very good WiFi accesspoint and a really bad WiFi chip in your device, you will have issues.

So, all in all, it may be worth investing in a better USB wifi dongle with an external antenna on the Pi so as to increase the quality of the wifi.

And…a Wi-Fi Range Extender? Yes… A bit tatty (I recognize it)… But if it works, your problem is solved…:slight_smile:

but:

changing code = $0

hardware = $???

Only in case your time is free that is never true :slight_smile:

2 Likes

@Patrice_Bois what distance is the WiFi covering and is it through any walls / trees etc.

If it is a short distance and the dropouts are not too frequent then a software solution will probably work but if it is a long distance and quite frequent it probably requires a hardware tweak.

Perhaps you can indicate the exact make and model of WiFi equipment you are using at present.

wife thinks me coding is better than me being at the pub!

OP - some hardware can just have an antenna ‘upgrade’ so hopefully that might help… i am interested in the Pi code - never seen it before!

2 Likes

Very rare stuff. I’ve copied it and might put it on Ebay later :grinning:

Actually it doesn’t look a lot different to Arduino code when you start to study it. Not that I really want to start learning another language.

it kinda looks like what it would be behind the compiler, right?

(i started my MCU journey trying to learn LUA coding, waaaay over my head!)

PS - lets hope we can help these chicken to survive the winter! :slight_smile:

@Dave1829 the Pi code is java and doesn’t use a compiler AFAIK. You can compile code with the Pi though.

I’m told LUA is easier to learn than Arduino code but I haven’t got a clue when I look at LUA code.

Thanks all,

The Pi is connected Wifi… and my router is 35-40 feet away from the Pi (inside isolated coop chicken.) Ethernet cable is not an option. I have tried a netgear wifi extander in the house, less away from the router without more results.

Do you think if i try a usb wifi dongle on the Pi i will have more signal from the wifi router?

What make and model of WiFi dongle are you currently using and surely it is already a USB WiFi dongle?

Is the 35 to 40 feet through fresh air and line of sight? If not please advise what makes up the 35 to 40 feet.

I’m using the builtin wifi adapter in the PI 3. Between the PI and the router, i got the concrete wall of the house and the wood wall of the chicken coop…

With the concrete wall, even without the possible Faraday effect of steel reinforcing in the concrete, then I think you are pushing the signal to the limit.

If you are seeing a lot of dropouts I would probably look at a high powered USB WiFi adaptor with twin aerials. I have a Geetek Janus II that will do 1 to 2km in open air (much, much less through concrete walls). Obviously needs to be compatible with the Pi though.

But the problem is if my signal drop my script crash… if the script can run without crashing if i loose wifi signal it will be a big improvement :wink:

if i can do something like :

If blynk is connected do {
stuff
} if conexion lost do {
stuff
}

at -20 degrees in the night i don’t want my app to crash hehe

I will give you some Arduino code that you might be able to convert to Pi code:

in loop()

  if(Blynk.connected()){  // only run Blynk functions if connected to Blynk
    Blynk.run();
  }

in setup()

timer.setInterval(60000, reconnectBlynk); // check every 60s if connected, needs SimpleTimer library

New function:

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

Provided by @vshymanskyy at Continuing loop while reconnecting

That probably has an antenna the size of a matchstick, so that isn’t too good. @Costas suggestion of bigger antenna’s on the Pi will probably be a very effective solution. Your Accesspoint usually has better antenna’s than devices’s like the Pi.

Using Rangeextenders will usually not make things better, but worse. This is due to the technical aspects of Wifi. You must remember Wifi is a half duplex medium, in contrast with Ethernet. When a device receives it cannot send and vice versa. This is because the radio protocol works like this. Ethernet can do both simultaniously, hence full duplex. Because of highly scientific stuff the decay of the radio signal is logarithmic, so if you move away from a certain device 50% of the distance, the signal degradation will be much worse. This is why the wifi range will drop dramatically if there is a wall between the receiver and sender.

Anyway, long story short. try and find a USB dongle which supports external antenna’s and order some huge ass antenna’s from DX or ebay. Make sure it’s compatible with Linux because a lot of sticks are not! Depends on the chipset mostly.

1 Like

Does something like that can improve my pi wifi range?

Yes they are high powered but check the Pi site for compatibility i.e is a driver available or already included in Jessie.

Do you have mains power with the Pi?

Pi USB ports are now much better than they were in the early days but you might need a high powered USB hub to go with the high powered WiFi adaptor.