From 7f11ca9691a58f721474a69c1e98b4b1c1178771 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Thu, 21 Jul 2022 14:54:23 +0200
Subject: [PATCH] first running snippet espnow communication

---
 code-snippets/espnow/espNowTest/.gitignore    |   5 +
 .../espnow/espNowTest/.vscode/extensions.json |  10 ++
 .../espnow/espNowTest/.vscode/settings.json   |   3 +
 code-snippets/espnow/espNowTest/espnow.md     |  20 +++
 .../espnow/espNowTest/include/README          |  39 ++++++
 .../espnow/espNowTest/platformio.ini          |  16 +++
 code-snippets/espnow/espNowTest/src/main.cpp  | 118 ++++++++++++++++++
 code-snippets/espnow/espNowTest/test/README   |  11 ++
 8 files changed, 222 insertions(+)
 create mode 100644 code-snippets/espnow/espNowTest/.gitignore
 create mode 100644 code-snippets/espnow/espNowTest/.vscode/extensions.json
 create mode 100644 code-snippets/espnow/espNowTest/.vscode/settings.json
 create mode 100644 code-snippets/espnow/espNowTest/espnow.md
 create mode 100644 code-snippets/espnow/espNowTest/include/README
 create mode 100644 code-snippets/espnow/espNowTest/platformio.ini
 create mode 100644 code-snippets/espnow/espNowTest/src/main.cpp
 create mode 100644 code-snippets/espnow/espNowTest/test/README

diff --git a/code-snippets/espnow/espNowTest/.gitignore b/code-snippets/espnow/espNowTest/.gitignore
new file mode 100644
index 0000000..89cc49c
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/.gitignore
@@ -0,0 +1,5 @@
+.pio
+.vscode/.browse.c_cpp.db*
+.vscode/c_cpp_properties.json
+.vscode/launch.json
+.vscode/ipch
diff --git a/code-snippets/espnow/espNowTest/.vscode/extensions.json b/code-snippets/espnow/espNowTest/.vscode/extensions.json
new file mode 100644
index 0000000..080e70d
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/.vscode/extensions.json
@@ -0,0 +1,10 @@
+{
+    // See http://go.microsoft.com/fwlink/?LinkId=827846
+    // for the documentation about the extensions.json format
+    "recommendations": [
+        "platformio.platformio-ide"
+    ],
+    "unwantedRecommendations": [
+        "ms-vscode.cpptools-extension-pack"
+    ]
+}
diff --git a/code-snippets/espnow/espNowTest/.vscode/settings.json b/code-snippets/espnow/espNowTest/.vscode/settings.json
new file mode 100644
index 0000000..0db5873
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+    "cmake.configureOnOpen": true
+}
\ No newline at end of file
diff --git a/code-snippets/espnow/espNowTest/espnow.md b/code-snippets/espnow/espNowTest/espnow.md
new file mode 100644
index 0000000..8928df7
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/espnow.md
@@ -0,0 +1,20 @@
+# Esp Now Basic Steps
+
+- Define Mac address of receiving esp32
+- create a ```esp_now_peer_info_t```
+- define a function for sending/receiving
+  
+  ```
+    setup(){
+    Serial.begin(115200);
+    while(!Serial); //open serial connection and wait
+    WiFi.mode(WIFI_STA); //enable wifi and set it to the correct mode
+    esp_now_init();
+    esp_now_register_send_cb(onDataSent);
+    esp_now_register_recv_cb(onDataRecv);
+    // fill out your esp_now_peer_info_t
+    esp_now_add_peer(peerInfo);
+    }
+  ```
+
+- Now you can send in your ```loop()```, or leave ```loop()``` empty to only receive
\ No newline at end of file
diff --git a/code-snippets/espnow/espNowTest/include/README b/code-snippets/espnow/espNowTest/include/README
new file mode 100644
index 0000000..194dcd4
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/include/README
@@ -0,0 +1,39 @@
+
+This directory is intended for project header files.
+
+A header file is a file containing C declarations and macro definitions
+to be shared between several project source files. You request the use of a
+header file in your project source file (C, C++, etc) located in `src` folder
+by including it, with the C preprocessing directive `#include'.
+
+```src/main.c
+
+#include "header.h"
+
+int main (void)
+{
+ ...
+}
+```
+
+Including a header file produces the same results as copying the header file
+into each source file that needs it. Such copying would be time-consuming
+and error-prone. With a header file, the related declarations appear
+in only one place. If they need to be changed, they can be changed in one
+place, and programs that include the header file will automatically use the
+new version when next recompiled. The header file eliminates the labor of
+finding and changing all the copies as well as the risk that a failure to
+find one copy will result in inconsistencies within a program.
+
+In C, the usual convention is to give header files names that end with `.h'.
+It is most portable to use only letters, digits, dashes, and underscores in
+header file names, and at most one dot.
+
+Read more about using header files in official GCC documentation:
+
+* Include Syntax
+* Include Operation
+* Once-Only Headers
+* Computed Includes
+
+https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
diff --git a/code-snippets/espnow/espNowTest/platformio.ini b/code-snippets/espnow/espNowTest/platformio.ini
new file mode 100644
index 0000000..65ef8ee
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/platformio.ini
@@ -0,0 +1,16 @@
+; PlatformIO Project Configuration File
+;
+;   Build options: build flags, source filter
+;   Upload options: custom upload port, speed and extra flags
+;   Library options: dependencies, extra library storages
+;   Advanced options: extra scripting
+;
+; Please visit documentation for the other options and examples
+; https://docs.platformio.org/page/projectconf.html
+
+[env:esp32-c3-devkitm-1]
+platform = espressif32
+board = esp32-c3-devkitm-1
+framework = arduino
+monitor_speed = 115200
+lib_deps = makuna/NeoPixelBus@^2.7.0
diff --git a/code-snippets/espnow/espNowTest/src/main.cpp b/code-snippets/espnow/espNowTest/src/main.cpp
new file mode 100644
index 0000000..3879284
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/src/main.cpp
@@ -0,0 +1,118 @@
+#include <Arduino.h>
+#include <NeoPixelBus.h>
+#include <esp_now.h>
+#include "WiFi.h"
+
+#define PIN 8
+#define NUMPIXELS 1
+#define LED 0
+
+// uint8_t peerMAC[] = {0x58, 0xCF, 0x79, 0x04, 0x45, 0x9C}; //battery
+// uint8_t peerMAC[] = {0x58, 0xCF, 0x79, 0x04, 0x3A, 0xF8}; //wired
+uint8_t peerMAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+
+typedef struct espMessage{
+  String macAddr;
+  int temp;
+  int co;
+} espMessage;
+
+espMessage lastMessage;
+
+esp_now_peer_info_t peerInfo;
+
+NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> led(NUMPIXELS, PIN);
+
+void blink()
+{
+  // RgbColor current = led.GetPixelColor(0);
+  // if(current == RgbColor(0,0,0)){
+  //   led.SetPixelColor(LED, RgbColor(55, 55, 55));
+  //   led.Show();
+  //   Serial.println("Led is on at peer" + WiFi.macAddress());
+  // }
+  // else{
+  //   led.SetPixelColor(LED, RgbColor(0,0,0));
+  //   led.Show();
+  //   Serial.println("led is off");
+  // }
+}
+
+void flash(RgbColor color){
+  led.SetPixelColor(LED, color);
+  led.Show();
+  delay(500);
+  led.SetPixelColor(LED, RgbColor(0,0,0));
+  led.Show();
+}
+
+void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
+{
+  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Message recieved" : "Message lost");
+  flash(RgbColor(255, 255, 255));
+}
+
+void onDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
+{
+  Serial.println("message recieved");
+  memcpy(&lastMessage, incomingData, sizeof(lastMessage));
+  Serial.println(lastMessage.macAddr);
+  Serial.println(lastMessage.co);
+  Serial.println(lastMessage.temp);
+  Serial.println("--------------");
+}
+
+void setup() {
+  // put your setup code here, to run once:
+  Serial.begin(115200);
+  while (!Serial);
+
+  //Led stuff
+  led.Begin();
+  led.SetPixelColor(LED, RgbColor(0,0,0));
+  led.Show();
+
+  //esp now
+  WiFi.mode(WIFI_STA);
+  if(esp_now_init() != ESP_OK){
+    Serial.println("Error initializing espnow");
+    flash(RgbColor(0, 0, 55));
+    return;
+  }
+
+  //comment out whichever you don't need
+  esp_now_register_send_cb(onDataSent);
+  // esp_now_register_recv_cb(onDataRecv);
+
+  //register peer
+  memcpy(peerInfo.peer_addr, peerMAC, sizeof(peerMAC));
+  peerInfo.channel = 0;
+  peerInfo.encrypt = false;
+  if (esp_now_add_peer(&peerInfo) != ESP_OK){
+    flash(RgbColor(0, 55, 55));
+    return;
+  }
+
+
+}
+
+void loop() {
+  // put your main code here, to run repeatedly:
+  
+  //comment this out if you only want to recieve
+  espMessage data;
+  data.macAddr = WiFi.macAddress();
+  data.co = (int) random(5,30);
+  data.temp = (int) random(0,100);
+
+  esp_err_t result = esp_now_send(peerMAC, (uint8_t *) &data, sizeof(data));
+  if(result == ESP_OK){
+    flash(RgbColor(0, 55, 0));
+  }
+  else{
+    flash(RgbColor(55, 0, 0));
+    Serial.println(result);
+  }
+
+  delay(5000);
+}
\ No newline at end of file
diff --git a/code-snippets/espnow/espNowTest/test/README b/code-snippets/espnow/espNowTest/test/README
new file mode 100644
index 0000000..9b1e87b
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/test/README
@@ -0,0 +1,11 @@
+
+This directory is intended for PlatformIO Test Runner and project tests.
+
+Unit Testing is a software testing method by which individual units of
+source code, sets of one or more MCU program modules together with associated
+control data, usage procedures, and operating procedures, are tested to
+determine whether they are fit for use. Unit testing finds problems early
+in the development cycle.
+
+More information about PlatformIO Unit Testing:
+- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
-- 
GitLab