Ver Fonte

Implement basic communication, complete with button

Robert Marshall há 5 anos atrás
pai
commit
73c5daef39
4 ficheiros alterados com 44 adições e 6 exclusões
  1. 4 0
      Receiver/platformio.ini
  2. 16 4
      Receiver/src/main.cpp
  3. 4 0
      Transmitter/platformio.ini
  4. 20 2
      Transmitter/src/main.cpp

+ 4 - 0
Receiver/platformio.ini

@@ -12,4 +12,8 @@
 platform = atmelavr
 board = nanoatmega328
 framework = arduino
+upload_port = /dev/ttyUSB1
 
+lib_deps=
+	RadioHead
+	SPI

+ 16 - 4
Receiver/src/main.cpp

@@ -1,9 +1,21 @@
 #include <Arduino.h>
+#include <RH_ASK.h>
 
-void setup() {
-  // put your setup code here, to run once:
+RH_ASK driver;
+
+void setup(){
+	Serial.begin(9600);
+	Serial.println("Starting...");
+	if (!driver.init())
+		Serial.println("init failed");
+	Serial.println("Started");
 }
 
-void loop() {
-  // put your main code here, to run repeatedly:
+void loop(){
+	uint8_t buf[12];
+	uint8_t buflen = sizeof(buf);
+	if (driver.recv(buf, &buflen)) {// Non-blocking
+		Serial.print("Message: ");
+		Serial.println((char *)buf);
+	}
 }

+ 4 - 0
Transmitter/platformio.ini

@@ -12,4 +12,8 @@
 platform = atmelavr
 board = nanoatmega328
 framework = arduino
+upload_port = /dev/ttyUSB0
 
+lib_deps=
+	RadioHead
+	SPI

+ 20 - 2
Transmitter/src/main.cpp

@@ -1,9 +1,27 @@
 #include <Arduino.h>
+#include <RH_ASK.h>
+
+RH_ASK _driver;
+int _buttonPin=13;
+unsigned int _counter;
 
 void setup() {
-  // put your setup code here, to run once:
+	pinMode(_buttonPin, INPUT);
+	Serial.begin(9600);
+	Serial.println("Starting...");
+    if (!_driver.init())
+		Serial.println("init failed");
+	Serial.println("Started");
 }
 
 void loop() {
-  // put your main code here, to run repeatedly:
+	if (digitalRead(_buttonPin)){
+		Serial.println("Transmitting...");
+		char msg[12];
+		sprintf(msg, "Hello World%d", _counter++);
+		_driver.send((uint8_t *)msg, strlen(msg));
+		_driver.waitPacketSent();
+		Serial.println("Transmitted");
+		delay(1000);
+	}
 }