Showing posts with label HC-11. Show all posts
Showing posts with label HC-11. Show all posts

2016/08/24

How to use the UART of Raspberry Pi for wireless communication - part 2

This is a quick summary of how to connect Raspberry Pi to a 433MHz Serial to Wireless module (HC-11 / HC-12) and how to program the Pi using Python to receive incoming data.

Before the Pi's UART can be used for this purpose, there are some preparations that need to be made. Please refer to http://wei48221.blogspot.tw/2016/08/how-to-use-uart-of-raspberry-pi-for_99.html for info. on how to prepare the Pi's UART.

Schematic:




Checking the version of the installed Python.

Issue "python --version".


Install pySerial

Issue "sudo apt-get install python-serial".

Other preparation works

Create a folder call "python" (if it hasn't been created) to keep the python programs.

1. Issue "mkdir python" to create the python folder.

2. Issue "ls" to make sure the folder is created.


Change working directory to the newly created python folder.

"cd python"

Launch nano text editor and name the file created "serial_read.py"

"nano serial_read.py"

Code

Type in the code between the 2 dash lines.
----------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python

import time
import serial

ser = serial.Serial(
         port='/dev/ttyAMA0',
         baudrate = 9600,
         parity=serial.PARITY_NONE,
         stopbits=serial.STOPBITS_ONE,
         bytesize=serial.EIGHTBITS,
         timeout=1
         )
counter=0
   
while 1:
         x=ser.readline()
         print x
---------------------------------------------------------------------------------------------------------------------
IMPORTANT!!

Watch out for the indent as python uses spacing at the start of the line to determine when code blocks start and end (http://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python).

Save the program.
Ctrl-X to exit the editing mode, then "Y" to save the file.

Running the program 

"python serial_read.py".

Here I have an Arduino Uno with some sensors and a HC-11 module attached to it. Whenever a sensor is triggered, the Uno will send a message out via the HC-11 module. The message is picked up by the "serial_read.py " running on the Raspberry Pi and displayed in the PuTTY terminal.


When we are done with the program, use Ctrl-C to stop its execution.

References:

Using UART on Raspberry Pi – Python
https://electrosome.com/uart-raspberry-pi-python/

Arduino Lesson 17. Email Sending Movement Detector
https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/python-code

Read and write from serial port with Raspberry Pi
http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/

---------------------------------------------------------------------------------------------------------------------

Product Offered:

Note, the 2 modules listed here have been tested to work with Arduino Uno and Raspberry Pi.

433MHz Serial RF Module -- HC-11


- Price: US$6.99/ea;
- Item Location: Taipei, Taiwan;
- Range: ~100 Meters (direct line of sight without obstruction);
- Frequency: 433MHz;
- Operating Voltage: DC 3.3V ~ 5V (when feeding DC 3.3V to the VCC of the module, the module works with Raspberry Pi without the need for level shifting / voltage division);
- Serial baud rate: 1.2Kbps to 115.2Kbps (default 9.6Kbps);
- Interface protocol: UART/TTL;
- At least 2 HC-11 modules are needed to form a complete wireless link.

IMPORTANT - THIS MODULE DOESN'T WORK WITH THE 433MHz Serial RF Module HC-12.

-------------------------------------------------------------------------------------------------------------------------

433MHz Serial RF Module -- HC-12
- Price: US$7.99/ea;
- Item Location: Taipei, Taiwan;
- Range: ~1000 Meters (@ 5000bps) direct line of sight without obstruction);
- Frequency: 433MHz;
- Operating Voltage: DC 3.3V ~ 5V (when feeding DC 3.3V to the VCC of the module, the module works with Raspberry Pi without the need for level shifting / voltage division);
- Serial baud rate: 1.2Kbps to 115.2Kbps (default 9.6Kbps);
- Interface protocol: UART/TTL;
- At least 2 HC-12 modules are needed to form a complete wireless link.

IMPORTANT - THIS MODULE DOESN'T WORK WITH THE 433MHz Serial RF Module HC-11.

How to purchase:

Leave your message in the comment section below or send me an e-mail. I will contact you for detail.

2016/06/07

Simple wireless instant messenger using Arduino + HC-11

This is a quick summary of a simple wireless instant messenger using Arduino + HC-11.

Wiring - Sender (Arduino Uno)




Wiring - Receiver (Arduino Nano)



Code - Sender

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {


  if (Serial.available() > 0){             //Read from serial monitor and send over HC-11
    String input = Serial.readString();
    mySerial.println(input);  
  }

  if(mySerial.available() > 1){        //Read from HC-11 and send to serial monitor
    String input = mySerial.readString();
    Serial.println(input);  
  }
  delay(5);
}

Code - Receiver

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {


  if (Serial.available() > 0){             //Read from serial monitor and send over HC-11
    String input = Serial.readString();
    mySerial.println(input);  
  }

  if(mySerial.available() > 1){        //Read from HC-11 and send to serial monitor
    String input = mySerial.readString();
    Serial.println(input);  
  }
  delay(5);
}

Reference

Long range, 1.8km, Arduino to Arduino wireless communication with the HC-12
https://www.youtube.com/watch?v=DGRPqeacJns

2016/06/05

Remotely control servo motor via wireless interface that supports link loss indication

This is a prototype for verifying the design to control a servo motor via a wireless interface that supports link loss indication.

Schematic

Master (Local)



Slave (Remote)


Source Code - Master (Local)

const int LED_Alive =  2;          // Alive indicator LED
const int LED_Error =  3;          // Error indicator LED

int state = 0;                  // state of output signal, 0 = "L", 1 = "H".
int incomingByte;               // a variable to read incoming serial data into

void setup() {
  Serial.begin(9600);
  pinMode(LED_Alive, OUTPUT);
}

void loop() {

long interval = 300000; // 1 second?
int ok = 0;             // 0 = reply not ok, 1 = reply ok.

// switch the output status
  if (state == 0) {
    state = 1;
  } else {
    state = 0;
  }

  if (state == 1) {
    Serial.print("H");      // Send "H"
    digitalWrite(LED_Alive, HIGH);  
  } else {
    Serial.print("L");      // Send "L"
    digitalWrite(LED_Alive, LOW);
  }

  // Listen to reply from remote site for 1 second

  while (interval >= 0) {
    if (Serial.available() > 0) {
      incomingByte = Serial.read();
      //Serial.print(incomingByte);
      //break;            // break from the while loop
    }
    interval = interval - 1;      
  }

  switch (incomingByte) {
    case 72:                              // "H"
        if (state == 1) ok = 1;           // Send "H", receive "H".
      break;
    case 76:                              // "L"
      if (state == 0) ok = 1;             // Send "L", receive "L".
      break;
    default:
      ok = 0;                             // if nothing else matches, do the default
    break;
  }

  if (ok == 0)
   digitalWrite(LED_Error, HIGH);         // Turn on the error indicator
  else
   digitalWrite(LED_Error, LOW);          // Turn off the error indicator
}

Source Code - Slave (Remote)

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int pos = 0;    // variable to store the servo position

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);

  myservo.attach(A0);  // attaches the servo on pin 0 to the servo object
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      Serial.print("H");                // send reply back to remote site
      digitalWrite(ledPin, HIGH);
      myservo.write(180);              // tell servo to go to position 180
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      Serial.print("L");
      digitalWrite(ledPin, LOW);
      myservo.write(0);              // tell servo to go to position 0
    }
  }
}

Note,

There seems to be a problem with the serial interface of the Arduino Nano used for this project. If the Nano is powered off (unplug the USB from the computer) then powered on (plug the USB cable back to the computer) while the above program is running, the Nano won't be able to send feedback to the Master device (the Link Loss Indicator on the Master device will keep flashing). When this happens, launching the Serial Monitor on the computer attaching to the Nano or re-uploading the program to the Nano could solve this problem. However, this problem doesn't exist if the Nano is replaced with Uno.