소스 검색

Convert to use ESP8266 (ESP-NOW)

Robert Marshall 4 년 전
부모
커밋
608febeed1
8개의 변경된 파일114개의 추가작업 그리고 81개의 파일을 삭제
  1. 1 1
      Common/LED.cpp
  2. 7 8
      Receiver/platformio.ini
  3. 0 27
      Receiver/src/Receiver.cpp
  4. 33 19
      Receiver/src/main.cpp
  5. 7 8
      Transmitter/platformio.ini
  6. 32 10
      Transmitter/src/Transmitter.cpp
  7. 7 8
      Transmitter/src/main.cpp
  8. 27 0
      vscode-workspace.code-workspace

+ 1 - 1
Common/LED.cpp

@@ -1,6 +1,6 @@
     #include <Arduino.h>
 
-#define PWMRANGE 255
+//#define PWMRANGE 255
 
 class LED{
 	unsigned int _pin;

+ 7 - 8
Receiver/platformio.ini

@@ -8,12 +8,11 @@
 ; Please visit documentation for the other options and examples
 ; https://docs.platformio.org/page/projectconf.html
 
-[env:nanoatmega328]
-platform = atmelavr
-board = nanoatmega328
+[env:d1_mini]
+platform = espressif8266
+board = d1_mini
 framework = arduino
-upload_port = /dev/ttyUSB1
-
-lib_deps=
-	RadioHead
-	SPI
+;monitor_speed = 74880
+monitor_speed = 115200
+upload_speed = 115200
+upload_port = /dev/ttyUSB0

+ 0 - 27
Receiver/src/Receiver.cpp

@@ -1,27 +0,0 @@
-#include <RH_ASK.h>
-
-class Receiver{
-	void (*_messageCallback)(char *);
-	unsigned int _bufferSize;
-	RH_ASK _driver;
-
-public:
-	Receiver(unsigned int pin, void(*messageCallback)(char*), unsigned int bufferSize){
-		_messageCallback = messageCallback;
-		_bufferSize = bufferSize;
-	}
-
-	void setup(){
-		if (!_driver.init())
-			Serial.println("Receiver setup failed");
-	}
-
-	void loop()	{
-		uint8_t message[_bufferSize];
-		uint8_t buflen = sizeof(message);
-
-		if (_driver.recv(message, &buflen)){
-			_messageCallback((char *)message);
-		}
-	}
-};

+ 33 - 19
Receiver/src/main.cpp

@@ -1,38 +1,37 @@
-#include <Arduino.h>
-#include <Receiver.cpp>
 #include <../../Common/LED.cpp>
 #include <../../Common/Variables.cpp>
+#include <Arduino.h>
+#include <ESP8266WiFi.h>
+#include <espnow.h>
 
 #define TIMEOUT_DURATION 30000 // 30 seconds
 
-void messageCallback(char *message);
-Receiver _receiver(2, &messageCallback, 9);
-LED _led(5, FADE_IN_DURATION, FADE_OUT_DURATION);
+void messageCallback(int value);
+LED _led(D1, FADE_IN_DURATION, FADE_OUT_DURATION);
 unsigned long _lastMessageTime;
 bool _timedOut = false;
 
-bool validateMessage(char* message){
-	return strncmp("LED On: ", message, 8) == 0; //returns 0 on match
-}
-
-bool parseMessage(char* message){
-	return validateMessage(message) && message[8] == '1';
-}
-
 void resetTimeout(){
 	_lastMessageTime = millis();
 	_timedOut = false;
 }
 
-void messageCallback(char* message){
+void messageCallback(int value){
 	Serial.print("Message: ");
-	Serial.println(message);
+	Serial.println(value);
 
-	parseMessage(message) ? _led.on() : _led.off();
+	value ? _led.on() : _led.off();
 
 	resetTimeout();
 }
 
+void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len) {
+	int value;
+	memcpy(&value, incomingData, sizeof(value));
+	Serial.println(value);
+	messageCallback(value);
+}
+
 void timeout(){
 	if (!_timedOut && millis() >= _lastMessageTime + TIMEOUT_DURATION){
 		Serial.println("No message for 30 seconds.");
@@ -41,17 +40,32 @@ void timeout(){
 	}
 }
 
+void setupReceiver() {
+	Serial.print("ESP8266 Board MAC Address:  ");
+	Serial.println(WiFi.macAddress());
+
+	WiFi.mode(WIFI_STA);
+
+	if (esp_now_init() != 0) {
+		Serial.println("Error initialising ESP-NOW");
+		return;
+	}
+
+	esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
+	esp_now_register_recv_cb(OnDataRecv);
+}
+
 void setup(){
-	Serial.begin(9600);
+	Serial.begin(115200);
 	Serial.println("Starting...");
-	_receiver.setup();
+	setupReceiver();
 	Serial.println("Started");
 	_led.off();
 	_lastMessageTime = millis();
 }
 
 void loop(){
-	_receiver.loop();
 	_led.loop();
 	timeout();
+	delay(10);
 }

+ 7 - 8
Transmitter/platformio.ini

@@ -8,12 +8,11 @@
 ; Please visit documentation for the other options and examples
 ; https://docs.platformio.org/page/projectconf.html
 
-[env:nanoatmega328]
-platform = atmelavr
-board = nanoatmega328
+[env:d1_mini]
+platform = espressif8266
+board = d1_mini
 framework = arduino
-upload_port = /dev/ttyUSB2
-
-lib_deps=
-	RadioHead
-	SPI
+;monitor_speed = 74880
+monitor_speed = 115200
+upload_speed = 115200
+upload_port = /dev/ttyUSB0

+ 32 - 10
Transmitter/src/Transmitter.cpp

@@ -1,18 +1,40 @@
 #include <Arduino.h>
-#include <RH_ASK.h>
+#include <ESP8266WiFi.h>
+#include <espnow.h>
 
-class Transmitter{
-	RH_ASK _driver;
+class Transmitter {
+	uint8_t targets[2][6] = {
+		{0x50, 0x02, 0x91, 0xEA, 0x3D, 0xFD},
+		{0x50, 0x02, 0x91, 0xEA, 0x40, 0x83}};
 
-public:
+	unsigned int _targetCount = sizeof(targets) / sizeof(targets[0]);
 
-	void setup(){
-		if (!_driver.init())
-			Serial.println("Transmitter setup failed");
+  public:
+	void setup() {
+		WiFi.mode(WIFI_STA);
+
+		if (esp_now_init() != 0) {
+			Serial.println("Error initializing ESP-NOW");
+			return;
+		}
+
+		esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
+		for (unsigned int i = 0; i < _targetCount; i++) {
+			Serial.print("Registering recipient ");
+			Serial.print(i);
+			Serial.print(": ");
+			auto result = esp_now_add_peer(targets[i], ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
+			Serial.println(result);
+		}
 	}
 
-	void send(char* message){
-		_driver.send((uint8_t *)message, strlen(message));
-		_driver.waitPacketSent();
+	void send(int value) {
+		for (unsigned int i = 0; i < _targetCount; i++) {
+			Serial.print("Sending to recipient ");
+			Serial.print(i);
+			Serial.print(": ");
+			auto result = esp_now_send(targets[i], (uint8_t *)&value, sizeof(value));
+			Serial.println(result);
+		}
 	}
 };

+ 7 - 8
Transmitter/src/main.cpp

@@ -8,19 +8,17 @@
 void turnOn();
 void turnOff();
 void resend();
-Motion _motion(13, ON_DURATION, &turnOn, &turnOff);
+Motion _motion(D7, ON_DURATION, &turnOn, &turnOff);
 unsigned int _signalValue;
 Transmitter _transmitter;
 Timer _timer(1000, &resend);
-LED _led(5, FADE_IN_DURATION, FADE_OUT_DURATION);
+LED _led(D1, FADE_IN_DURATION, FADE_OUT_DURATION);
 
 void sendSignal(int value){
 	_signalValue=value;
 	Serial.print("Transmitting: ");
-	char msg[9];
-	sprintf(msg, "LED On: %d", value);
-	Serial.println(msg);
-	_transmitter.send(msg);
+	Serial.println(value);
+	_transmitter.send(value);
 	Serial.println("Transmitted");
 }
 
@@ -39,7 +37,7 @@ void resend(){
 }
 
 void setup() {
-	Serial.begin(9600);
+	Serial.begin(115200);
 	Serial.println("Starting");
 	_transmitter.setup();
 	_motion.setup();
@@ -48,6 +46,7 @@ void setup() {
 
 void loop() {
 	_motion.loop();
-	//_timer.loop();
+	_timer.loop();
 	_led.loop();
+	delay(10);
 }

+ 27 - 0
vscode-workspace.code-workspace

@@ -0,0 +1,27 @@
+{
+	"folders": [
+		{
+			"path": "Transmitter"
+		},
+		{
+			"name": "Receiver",
+			"path": "Receiver"
+		},
+		{
+			"path": "Common"
+		}
+	],
+	"settings": {
+		"files.associations": {
+			"optional": "cpp",
+			"system_error": "cpp",
+			"array": "cpp",
+			"functional": "cpp",
+			"tuple": "cpp",
+			"type_traits": "cpp",
+			"utility": "cpp",
+			"istream": "cpp",
+			"ostream": "cpp"
+		}
+	}
+}