Monday, March 23, 2015

[Wireless Room Temperature Monitoring System] Labeling a temperature (DS18B20) from an Arduino Pro Mini to a Raspberry Pi

Back to square 1. Unfortunately the code i have been using since the beginning to send the data from the Arduino to the Raspberry Pi.

Thanks to smart people on the internet i found out there was a way to have an Arduino talk to a Raspberry Pi using the VirtualWire libraries (initially made for only Arduino to Arduino communications).

1) The Arduino part :

Download and Install the VirtualWire library : Information // Dowload Link

I uploaded the following code to my Arduino Pro Mini:
#include <VirtualWire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3 //DS18B20 Connected to Pin 3

int compteur = 0;

const char *msg = "Temp1 ";
const char *celsius = "C: ";
const char *diese = " #";
char nombre[VW_MAX_MESSAGE_LEN];
char message[VW_MAX_MESSAGE_LEN];
char tempCx100char[VW_MAX_MESSAGE_LEN];
OneWire oneWire(ONE_WIRE_BUS); // For the DS18B20
DallasTemperature sensors(&oneWire);
DeviceAddress Thermometer = { 0x28, 0xFF, 0xED, 0x0F, 0x11, 0x14, 0x00, 0xD2 }; //Obtained using another program
void setup()
{
   Serial.begin(9600);  // Debugging only
   sensors.begin();
   sensors.setResolution(Thermometer, 10);
 
   // Initialise the IO and ISR
   vw_set_tx_pin(10);
   vw_set_ptt_inverted(true); // Required for DR3100
   vw_setup(2000); // Bits per sec
}
void printTemperature(DeviceAddress deviceAddress)
{
  compteur++;

  float tempC = sensors.getTempC(deviceAddress);
  int tempCx100 = (int)(tempC*100); // Multiply the float value to have a full int with 2 digits
  itoa(tempCx100,tempCx100char,10); //Convert int to char
  itoa(compteur,nombre,10); // compteur de message

  strcpy (message,msg);
  strcat (message,tempCx100char);
  strcat (message, diese);
  strcat (message,nombre);

  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
 
  } else {
    digitalWrite(13, true); // Flash a light to show transmitting
    vw_send((uint8_t *)message, strlen(message));
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);
  }
}
void loop()
{
  sensors.requestTemperatures();
  printTemperature(Thermometer);
  Serial.print(message);
  Serial.print("\n\r");
  delay(1000);
}

I basically compiled all the DS18B20 related codes i found out about earlier along with the specific code available to send stuff from VirtualWire library.
The code is sending a message with
    - a string that enables me to identify which temperature sensor sends the message.
    - the temperature as a 4 digit integer (2125 > 21.25C)
    - a message count in order to follow which messages are being lost for prototyping purposes

2) The Raspberry Pi part :

I basically used a python code suggest by this Joan on the raspberry pi forum : Link
You also need the pigpio library to be installed on the RPi : Link
wget abyz.co.uk/rpi/pigpio/pigpio.zipunzip pigpio.zipcd PIGPIOmakemake install
The code (vw.py) is a massive program which allows me to either receive or send using the RF433 Module : Link

I copied the program file in a dedicated folder on the Raspberry Pi along with another python program that will import the vw.py and once started, will only do what i need it to be doing
import timeimport pigpio
import vw
RX=27
BPS=2000pi = pigpio.pi() rx = vw.rx(pi, RX, BPS) start = time.time()print("En attente de la reception des donnees")
while (time.time()-start) < 100: while rx.ready(): print("".join(chr (c) for c in rx.get()))rx.cancel()
pi.stop()
 The result :

The Arduino Pro Mini with DS18B20 + TX433 and the relevant code is plugged in, and i start the program above and here the result :


It works ! :) Thank you to all contributor in the internet for their python programs and other libraries !

Now i can identify each temperature sensor. Next i will probably build another sensor module and try to receive both temperature and see if they don't interact too much and create a mess :)

2 comments:

  1. I'm currently looking into doing something like this myself, but I'm quite new to hardware-hacking. Thanks a million for posting this - it should help me get going!

    ReplyDelete
  2. I'm loving the thread. Thank you so much for putting this up!

    The python is getting mangled a bit (spacing/indentation is very important in python). Any chance you could post a less-mangled version?

    ReplyDelete