Things used in this project

Schematics

Lora/GPS HAT

Code

post_processing.pyPython
#Original code by : https://github.com/CongducPham/LowCostLoRaGw/blob/master/Raspberry/log_gw.py
#Edited by Ramin Sangesari

import sys
import os
import datetime
import getopt

#gateway id/addr, just for giving a different name to various log files
_gwaddr=1

def main(argv):
	try:
		opts, args = getopt.getopt(argv,'a:',['addr'])
	except getopt.GetoptError:
		print 'logParseGateway -a'
		sys.exit(2)
	
	for opt, arg in opts:		
		if opt in ("-a", "--addr"):
			global _gwaddr
			_gwaddr = arg
			print "will use _"+str(_gwaddr)+" for post-processing log file"			

		
if __name__ == "__main__":
	main(sys.argv[1:])
	

_parselog_filename = str(_gwaddr)+".log"
the_line=sys.stdin.readline()

try:
	while the_line:
		f=open(os.path.expanduser(_parselog_filename),"a")	
		now = datetime.datetime.now()
		f.write(now.isoformat()+'> ')
		f.write(the_line)
		f.close()
		sys.stdout.write(the_line)
		the_line=sys.stdin.readline()

except KeyboardInterrupt:
   sys.stdout.flush()
   pass

	
lora_sender.cC/C++
/*
Original code by: https://github.com/Snootlab/lora_chisterapi
Edited by: Ramin Sangesari
*/

/*-----------------------------------------*/
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
/*-----------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <RH_RF95.h>

RH_RF95 rf95;

/* The address of the node which is 10 by default */
uint8_t node_number = 10;
uint8_t msg[2] = {10, 0};
int run = 1;
/*-----------------------------------------*/
DIR *dir;
struct dirent *dirent;
char dev[16];      // Dev ID
char devPath[128]; // Path to device
char buf[256];     // Data from device
char tmpData[6];   // Temp C * 1000 reported by device 
char path[] = "/sys/bus/w1/devices"; 
ssize_t numRead;
/*-----------------------------------------*/


/* Send a message every 3 seconds */
void sigalarm_handler(int signal)
{
    msg[0] = node_number;
    msg[1]++;
 
    rf95.send(msg, sizeof(msg));
    rf95.waitPacketSent();
    printf("Send!\n");
    alarm(3);
}

/* Signal the end of the software */
void sigint_handler(int signal)
{
    run = 0;
}

void setup()
{ 

	dir = opendir (path);
	if (dir != NULL){
		while ((dirent = readdir (dir)))
		// 1-wire devices are links beginning with 28-
		if (dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL) { 
			strcpy(dev, dirent->d_name);
		//	printf("\nDevice: %s\n", dev);
		}
		(void) closedir (dir);
		}
		else{
			perror ("Couldn't open the w1 devices directory");
			//return 1;
		}
	// Assemble path to OneWire device
	sprintf(devPath, "%s/%s/w1_slave", path, dev);
	
	
    wiringPiSetupGpio();

    if (!rf95.init()) 
    {
        fprintf(stderr, "Init failed\n");
        exit(1);
    }

    /* Tx power is from +5 to +23 dBm */
    rf95.setTxPower(23);
    /* There are different configurations
     * you can find in lib/radiohead/RH_RF95.h 
     * at line 437 
     */
    rf95.setModemConfig(RH_RF95::Bw125Cr45Sf128);
    rf95.setFrequency(868.0); /* Mhz */
}

void loop()
{
	int fd = open(devPath, O_RDONLY);
	if(fd == -1){
		perror ("Couldn't open the w1 device.");
		//return 1;   
	}
	while((numRead = read(fd, buf, 256)) > 0){
		strncpy(tmpData, strstr(buf, "t=") + 2, 5); 
		float tempC = strtof(tmpData, NULL);
		tempC = tempC / 1000;
	//printf("%.2f\n", tempC);
	
	/*-----------------------------------------*/
	int NegativeNumber;
	char SignNumber;

	int intpart = (int)tempC;
    double decpart = tempC - intpart;
    //printf("Num = %f, intpart = %d, decpart = %f\n", x, intpart, decpart);

    decpart = decpart * 100;
    int w = int (decpart);
    if (w<0){
        w = w * (-1);
    }


	if (intpart<0){
		NegativeNumber = intpart * (-1);
		SignNumber = 'N';
	}
	else {
        SignNumber = 'P';
        NegativeNumber = intpart;

	}
	if (intpart==0){SignNumber='Z';}

	//printf("Sign : %c, int Number = %d\n",SignNumber,NegativeNumber);
    //printf("Part1 = %d, Part2 = %d\n",intpart, w);

	/*-----------------------------------------*/

	//	printf("Device: %s  - ", dev); 			
	//	printf("%.3f F\n\n", (tempC / 1000) * 9 / 5 + 32);
	}
close(fd);


sleep(1000);
}

int main(int argc, char **argv)
{
    if( argc == 2 )
        node_number = atoi(argv[1]);
    
	
    signal(SIGINT, sigint_handler);
    signal(SIGALRM, sigalarm_handler);

    alarm(3);

    setup();

    while( run )
    {
        loop();
        usleep(1);
    }

    return EXIT_SUCCESS;
}
lora_receiver.cC/C++
/*
Original code by : https://github.com/Snootlab/lora_chisterapi
Edited by : Ramin Sangesari
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include <RH_RF95.h>

RH_RF95 rf95;

int run = 1;

/* Signal the end of the software */
void sigint_handler(int signal)
{
    run = 0;
}

void setup()
{ 
    wiringPiSetupGpio();

    if (!rf95.init()) 
    {
        fprintf(stderr, "Init failed\n");
        exit(1);
    }

    /* Tx Power is from +5 to +23 dbm */
    rf95.setTxPower(23);
    /* There are different configurations
     * you can find in lib/radiohead/RH_RF95.h 
     * at line 437 
     */
    rf95.setModemConfig(RH_RF95::Bw125Cr45Sf128);
    rf95.setFrequency(868.0); /* MHz */
}

void loop()
{

    /* If we receive one message we show on the prompt
     * the address of the sender and the Rx power.
     */
    if( rf95.available() ) 
    {
        uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
        uint8_t len = sizeof(buf);

        if (rf95.recv(buf, &len)) 
        {
			if (buf[3]==200 or buf[3]==201){
				printf("From %d, Temperature : %d.%d C\n", buf[0], buf[1],buf[2]);
			}
			else if(buf[3]==199)
			{
				printf("From %d, Temperature : %d.%d C\n", buf[0], -1*buf[1],buf[2]);	
			}
			fflush(stdout);
        }
    }
}

int main(int argc, char **argv)
{ 
    signal(SIGINT, sigint_handler);

    setup();

    while( run )
    {
        loop();
        usleep(1);
    }

    return EXIT_SUCCESS;
}

Credits

48950.jpg
Ramin Sangesari

Programmer and IoT Developer

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Simple Weather Station with amazing web monitoring
Intermediate
  • 168
  • 4

Full instructions

Do you want see from anywhere what is the temperature, pressure and humidity in your house? If yes this project will show you how to do it.

Horus: The Weather Station
Intermediate
  • 139
  • 4

Full instructions

A Raspberry Pi-based weather station that measures temperature, humidity and pressure using the Astro Pi Sense HAT and uploads the data.

How To Configure Moonlight For Steam Using JustBoom
Intermediate
  • 57
  • 2

Full instructions

This guide will show you how to install Moonlight for Raspbian and configure it for your JustBoom to stream games from your PC.

Raspberry Pi Motion Tracking Gun Turret
Intermediate
  • 7,629
  • 17

Full instructions

A motion tracking airsoft or Nerf gun turret with autonomous and manual operation modes, controlled by a Raspberry Pi.

LoRaWAN for Raspberry Pi with Worldwide Frequency Support
Intermediate
  • 4,277
  • 18

Full instructions

LoRaWAN LMIC 1.6 for Raspberry Pi with Dragino LoRA/GPS HAT or standalone RFM95W LoRa Module.

Audio DAC HAT for Pi with Headphone Jack and 3W Speaker Out
Intermediate
  • 888
  • 3

Full instructions

Make your own DAC HAT for Raspberry Pi 3, Raspberry Pi Zero, Raspberry Pi Zero W that has a headphone jack and 3W stereo speaker out.

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog