shaba
Posts: 3
Joined: Fri Mar 30, 2018 10:44 am

Raspberry Pi with multiple wireless sensors

Fri Mar 30, 2018 11:17 am

Experts,
I am about to try making a remote temperature monitoring system. I have around 20 Bee hives and each hive needs two sensors to read the hive temperature - one at the bottom area and one at the top.

New to the world of raspberry and sensors ! With my limited understanding, I plan to place a Raspberry Pi3 as a central node and each hive with a audrino connected with sensors. Audrino needs to send the data ti raspberry pi through wireless.

Is this viable? Or any better approach?
Hence, would like to hear your opinion if its something which is possible.

Cheers !

User avatar
DougieLawson
Posts: 33785
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Raspberry Pi with multiple wireless sensors

Fri Mar 30, 2018 2:45 pm

Use ESP8266s rather than Arduinos. It's a billion times easier to get an ESP8266 connected to your WiFi.

Run Mosquitto on your Raspberry.

Get the sensors to publish MQTT messages with readings from your sensors. You can then subscribe to those MQTT topics in any way you like.

This is running on my Wemos ESP8266 (reading a MAX31855 thermocouple), it publishes a small website at http://192.168.3.40/ and sends MQTT messages whenever the temp reading changes.

Code: Select all

#include <FS.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <Wire.h>
#include "Adafruit_MAX31855.h"
#include <PubSubClient.h>

#define MAXDO   13
#define MAXCS   15
#define MAXCLK  14
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

const char* MQTT_broker = "192.168.3.14";

int notConnected = true;
unsigned long mqttSavedMillis = 0;
unsigned long loopSavedMillis = 0;
const long intervalMillis = 500;

ESP8266WebServer webserver(80);
WiFiClient WemosCL;
PubSubClient client(WemosCL);

const int led = LED_BUILTIN;
double currentC;

char* readTcouple() {
  unsigned long mqttMillis = millis();
  char degC[20] = {"?"};
  double c = thermocouple.readCelsius();
  dtostrf(c, 6, 2, degC);
  if (c != currentC) {
    //Serial.println(degC);
    if (notConnected) {
      waitForConnection();
    }
    if (mqttMillis - mqttSavedMillis >= intervalMillis) {
      client.connect("WemosClient");
      client.publish("wemos/out/31855", degC);
      currentC = c;
      mqttSavedMillis = mqttMillis;
    }
  }
  return degC;
}

void handleRoot() {
  digitalWrite(led, 1);
  char temp[400];
  char* C = readTcouple();
  char degC[20];
  strcpy(degC, C);
  Serial.print("handleRoot:");
  Serial.print(degC);
  snprintf(temp, 400, "<html><head><meta http-equiv=\"Refresh\" content=\"5\"><title>MAX31855 Thermocouple</title><style>body{background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088;}</style></head><body><h1>MAX31855 Thermocouple</h1><h2>Temp: %s</h2></body></html>", degC);
  webserver.send(200, "text/html", temp);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += webserver.uri();
  message += "\nMethod: ";
  message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += webserver.args();
  message += "\n";
  for (uint8_t i = 0; i < webserver.args(); i++) {
    message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
  }
  webserver.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void onDisconnected(const WiFiEventStationModeDisconnected& event) {
  notConnected = true;
  waitForConnection();
}

void waitForConnection() {
  webserver.stop();
  WiFi.begin(); // ssid, password);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    client.setServer(MQTT_broker, 1883);
    delay(500);
  }
  webserver.begin();
  webserver.on("/", handleRoot);
  webserver.onNotFound(handleNotFound);
  notConnected = false;
}

void setup(void) {
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(9600);
  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  // wifiManager.resetSettings();
  if (!wifiManager.autoConnect("auto-Wemos", "configuration")) {
    delay(100);
    digitalWrite(led, 1);
    ESP.reset();
    delay(100);
    digitalWrite(led, 0);
  }

  waitForConnection();
  client.setServer(MQTT_broker, 1883);

  digitalWrite(led, 1);
}

void loop(void) {
  unsigned long loopMillis = millis();
  webserver.handleClient();
  if (loopMillis - loopSavedMillis >= intervalMillis) {
    char* temp = readTcouple();
    char degC[20];
    loopSavedMillis = loopMillis;
  }
  delay(10);
}
When it first starts up it runs as an access point so you can enter the WiFi credentials to get it connected to the home network. Once that's done it reboots and start publishing.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

countxerox
Posts: 17
Joined: Fri Sep 08, 2017 4:06 pm

Re: Raspberry Pi with multiple wireless sensors

Fri Mar 30, 2018 7:19 pm

Hi, have you heard of espruino, they have a low power mode which only consumes 11mA - so your sensor can sleep in between temperature readings, espruinos also use ESP8266 for the WiFi connection. Charging and replacing those 20 batteries could become a full time job. However if your hives are too far apart to be hardwired, they're maybe too far for ESP8266, the range you can get between an ESP8266 and a pi hotspot could be less than you might expect, you can configure your espruino as a client and access point at the same time extending your network. (not affiliated with espruino, other devices are available).

Is your you goal to have realtime monitoring and alerts or is more like creating a log which you can look back at?

User avatar
OutoftheBOTS
Posts: 658
Joined: Tue Aug 01, 2017 10:06 am

Re: Raspberry Pi with multiple wireless sensors

Fri Mar 30, 2018 9:42 pm

As stated above the ESP8266 will be your chip for this purpose. google "nodemcu esp8266" for a mass produced really common and cheap ESP8266 dev board that everyone uses for this purposes. It can be programed in any of the following platforms : C, Ardunio IDE, Lau or Micropython

You will need ESP8266 board in each hive with 2 temp sensors wired to them. As stated above MQTT message will be the easiest way to send the temp data back to your RPi.

As stated above you will need to use the sleep mode of the ESP8266 to conserve battery life i.e put it into sleep mode for 15mins then wake up and take a temp and send the data via a MQTT message then go back to sleep for another 15mins. You will also have to ensure that the temp sensors that you use also have a sleep mode or they will drain the battery by running flat while the ESP8266 sleeps.

The ESP8266 also hasd a ADC (analog to digital converter) so it can monitor the battery level and also send this data back via MQTT. You will need a voltage divider to drop the voltage to the ADC. You will also need a little ciruit to be able to switch on/off the voltage divider or it will also drain the battery while the ESP8266 is in sleep. This is a very common circuit for a GPIO controlled battery monitor :)
Bat_voltage.png
Bat_voltage.png (25.97 KiB) Viewed 1547 times

ranpitime
Posts: 68
Joined: Sat Mar 23, 2013 5:21 am

Re: Raspberry Pi with multiple wireless sensors

Sat Mar 31, 2018 1:50 pm

Hi,
depending on how the distances are I think even here these ruuvi tags are a good option too.
https://tag.ruuvi.com/
You could collect the temp and huminity information by a Raspberry Pi... via bluetooth.
no wiring no extra powersupply needed just place these tags into your bee hives.

https://blog.ruuvi.com/datasheet-52fb00265c60
BR
Edwin

shaba
Posts: 3
Joined: Fri Mar 30, 2018 10:44 am

Re: Raspberry Pi with multiple wireless sensors

Mon Apr 02, 2018 6:34 am

DougieLawson wrote:
Fri Mar 30, 2018 2:45 pm
Use ESP8266s rather than Arduinos. It's a billion times easier to get an ESP8266 connected to your WiFi.

Run Mosquitto on your Raspberry.

Get the sensors to publish MQTT messages with readings from your sensors. You can then subscribe to those MQTT topics in any way you like.

This is running on my Wemos ESP8266 (reading a MAX31855 thermocouple), it publishes a small website at http://192.168.3.40/ and sends MQTT messages whenever the temp reading changes.

Code: Select all

#include <FS.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <Wire.h>
#include "Adafruit_MAX31855.h"
#include <PubSubClient.h>

#define MAXDO   13
#define MAXCS   15
#define MAXCLK  14
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

const char* MQTT_broker = "192.168.3.14";

int notConnected = true;
unsigned long mqttSavedMillis = 0;
unsigned long loopSavedMillis = 0;
const long intervalMillis = 500;

ESP8266WebServer webserver(80);
WiFiClient WemosCL;
PubSubClient client(WemosCL);

const int led = LED_BUILTIN;
double currentC;

char* readTcouple() {
  unsigned long mqttMillis = millis();
  char degC[20] = {"?"};
  double c = thermocouple.readCelsius();
  dtostrf(c, 6, 2, degC);
  if (c != currentC) {
    //Serial.println(degC);
    if (notConnected) {
      waitForConnection();
    }
    if (mqttMillis - mqttSavedMillis >= intervalMillis) {
      client.connect("WemosClient");
      client.publish("wemos/out/31855", degC);
      currentC = c;
      mqttSavedMillis = mqttMillis;
    }
  }
  return degC;
}

void handleRoot() {
  digitalWrite(led, 1);
  char temp[400];
  char* C = readTcouple();
  char degC[20];
  strcpy(degC, C);
  Serial.print("handleRoot:");
  Serial.print(degC);
  snprintf(temp, 400, "<html><head><meta http-equiv=\"Refresh\" content=\"5\"><title>MAX31855 Thermocouple</title><style>body{background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088;}</style></head><body><h1>MAX31855 Thermocouple</h1><h2>Temp: %s</h2></body></html>", degC);
  webserver.send(200, "text/html", temp);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += webserver.uri();
  message += "\nMethod: ";
  message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += webserver.args();
  message += "\n";
  for (uint8_t i = 0; i < webserver.args(); i++) {
    message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
  }
  webserver.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void onDisconnected(const WiFiEventStationModeDisconnected& event) {
  notConnected = true;
  waitForConnection();
}

void waitForConnection() {
  webserver.stop();
  WiFi.begin(); // ssid, password);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    client.setServer(MQTT_broker, 1883);
    delay(500);
  }
  webserver.begin();
  webserver.on("/", handleRoot);
  webserver.onNotFound(handleNotFound);
  notConnected = false;
}

void setup(void) {
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(9600);
  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  // wifiManager.resetSettings();
  if (!wifiManager.autoConnect("auto-Wemos", "configuration")) {
    delay(100);
    digitalWrite(led, 1);
    ESP.reset();
    delay(100);
    digitalWrite(led, 0);
  }

  waitForConnection();
  client.setServer(MQTT_broker, 1883);

  digitalWrite(led, 1);
}

void loop(void) {
  unsigned long loopMillis = millis();
  webserver.handleClient();
  if (loopMillis - loopSavedMillis >= intervalMillis) {
    char* temp = readTcouple();
    char degC[20];
    loopSavedMillis = loopMillis;
  }
  delay(10);
}
When it first starts up it runs as an access point so you can enter the WiFi credentials to get it connected to the home network. Once that's done it reboots and start publishing.
Thanks for giving me that insight ! I was not aware of ESP8266S which could help here.
I will do more check on these lines and come back.

Cheers !

shaba
Posts: 3
Joined: Fri Mar 30, 2018 10:44 am

Re: Raspberry Pi with multiple wireless sensors

Mon Apr 02, 2018 6:55 am

countxerox wrote:
Fri Mar 30, 2018 7:19 pm
Hi, have you heard of espruino, they have a low power mode which only consumes 11mA - so your sensor can sleep in between temperature readings, espruinos also use ESP8266 for the WiFi connection. Charging and replacing those 20 batteries could become a full time job. However if your hives are too far apart to be hardwired, they're maybe too far for ESP8266, the range you can get between an ESP8266 and a pi hotspot could be less than you might expect, you can configure your espruino as a client and access point at the same time extending your network. (not affiliated with espruino, other devices are available).

Is your you goal to have realtime monitoring and alerts or is more like creating a log which you can look back at?
Thanks for sharing this. I havent heard about Espruino. As per the Espruino webiste, they dont have a distributor in India :( . Anyways, let me check more possibilities.

Another point is on the distance which I want to cover. All my hives are in my garden which can easily served by my home wifi. Hives are placed in a single line. Hive to Hive distance is approximately 3 meters. In this scenario, Would you recommend to go for a wired option ?

Battery is something made a rough assumption to workout with a bunch of rechargeable batteries. Thanks for clarifying the power consumption part and smart options available.

Cheers !

btidey
Posts: 1589
Joined: Sun Feb 17, 2013 6:51 pm

Re: Raspberry Pi with multiple wireless sensors

Mon Apr 02, 2018 7:59 am

Wifi range from esp modules is pretty good. I have no problem with 15 metres indoors through multiple walls. Outside with no walls it should be significantly better.

If you use something like a 18650 LIPO rechargeable battery and have reasonable sleep periods in between readings then you should be able to get in excess of a years worth of operation. For battery monitoring I just a simple potential divider with a total resistance of about 1M Ohm. This will have little effect on overall battery life which will be largely determined by the period in between temperature measurements.

shreyasmishra12345
Posts: 5
Joined: Mon Jul 02, 2018 1:47 pm

Re: Raspberry Pi with multiple wireless sensors

Mon Jul 02, 2018 3:23 pm

DougieLawson wrote:
Fri Mar 30, 2018 2:45 pm
Use ESP8266s rather than Arduinos. It's a billion times easier to get an ESP8266 connected to your WiFi.

Run Mosquitto on your Raspberry.

Get the sensors to publish MQTT messages with readings from your sensors. You can then subscribe to those MQTT topics in any way you like.

This is running on my Wemos ESP8266 (reading a MAX31855 thermocouple), it publishes a small website at http://192.168.3.40/ and sends MQTT messages whenever the temp reading changes.

Code: Select all

#include <FS.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <Wire.h>
#include "Adafruit_MAX31855.h"
#include <PubSubClient.h>

#define MAXDO   13
#define MAXCS   15
#define MAXCLK  14
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

const char* MQTT_broker = "192.168.3.14";

int notConnected = true;
unsigned long mqttSavedMillis = 0;
unsigned long loopSavedMillis = 0;
const long intervalMillis = 500;

ESP8266WebServer webserver(80);
WiFiClient WemosCL;
PubSubClient client(WemosCL);

const int led = LED_BUILTIN;
double currentC;

char* readTcouple() {
  unsigned long mqttMillis = millis();
  char degC[20] = {"?"};
  double c = thermocouple.readCelsius();
  dtostrf(c, 6, 2, degC);
  if (c != currentC) {
    //Serial.println(degC);
    if (notConnected) {
      waitForConnection();
    }
    if (mqttMillis - mqttSavedMillis >= intervalMillis) {
      client.connect("WemosClient");
      client.publish("wemos/out/31855", degC);
      currentC = c;
      mqttSavedMillis = mqttMillis;
    }
  }
  return degC;
}

void handleRoot() {
  digitalWrite(led, 1);
  char temp[400];
  char* C = readTcouple();
  char degC[20];
  strcpy(degC, C);
  Serial.print("handleRoot:");
  Serial.print(degC);
  snprintf(temp, 400, "<html><head><meta http-equiv=\"Refresh\" content=\"5\"><title>MAX31855 Thermocouple</title><style>body{background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088;}</style></head><body><h1>MAX31855 Thermocouple</h1><h2>Temp: %s</h2></body></html>", degC);
  webserver.send(200, "text/html", temp);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += webserver.uri();
  message += "\nMethod: ";
  message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += webserver.args();
  message += "\n";
  for (uint8_t i = 0; i < webserver.args(); i++) {
    message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
  }
  webserver.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void onDisconnected(const WiFiEventStationModeDisconnected& event) {
  notConnected = true;
  waitForConnection();
}

void waitForConnection() {
  webserver.stop();
  WiFi.begin(); // ssid, password);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    client.setServer(MQTT_broker, 1883);
    delay(500);
  }
  webserver.begin();
  webserver.on("/", handleRoot);
  webserver.onNotFound(handleNotFound);
  notConnected = false;
}

void setup(void) {
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(9600);
  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  // wifiManager.resetSettings();
  if (!wifiManager.autoConnect("auto-Wemos", "configuration")) {
    delay(100);
    digitalWrite(led, 1);
    ESP.reset();
    delay(100);
    digitalWrite(led, 0);
  }

  waitForConnection();
  client.setServer(MQTT_broker, 1883);

  digitalWrite(led, 1);
}

void loop(void) {
  unsigned long loopMillis = millis();
  webserver.handleClient();
  if (loopMillis - loopSavedMillis >= intervalMillis) {
    char* temp = readTcouple();
    char degC[20];
    loopSavedMillis = loopMillis;
  }
  delay(10);
}
When it first starts up it runs as an access point so you can enter the WiFi credentials to get it connected to the home network. Once that's done it reboots and start publishing.
Hi, I wanted to ask if here, you are saying that there needs to be no physical connection between the Raspberry Pi and the ESP8266 module, and all sensor data can be transmitted over my home Wifi network?

User avatar
DougieLawson
Posts: 33785
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Raspberry Pi with multiple wireless sensors

Mon Jul 02, 2018 3:29 pm

Yes.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

natopal46
Posts: 16
Joined: Mon Apr 09, 2018 11:25 am

Re: Raspberry Pi with multiple wireless sensors

Tue Jul 10, 2018 6:13 am

Well For this I can suggest you Wireless Temperature sensor https://store.ncd.io/product/industrial ... 6834e7a283 which has the capability to communicate with raspberry pi or arduino using wireless communication by using PAN module,
I am using this sensor in my storage rooms and its output is very good.

Hope this sensor will meet your requirements too.

btidey
Posts: 1589
Joined: Sun Feb 17, 2013 6:51 pm

Re: Raspberry Pi with multiple wireless sensors

Tue Jul 10, 2018 9:46 am

natopal46 wrote:
Tue Jul 10, 2018 6:13 am
Well For this I can suggest you Wireless Temperature sensor https://store.ncd.io/product/industrial ... 6834e7a283 which has the capability to communicate with raspberry pi or arduino using wireless communication by using PAN module,
I am using this sensor in my storage rooms and its output is very good.

Hope this sensor will meet your requirements too.
Very nice if you need its range and want a pre-built solution, BUT very expensive too. I have 12 esp8266 based temp sensors installed around my house for half the cost of one of those.

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: mjcumming and 8 guests