basic postman protocol tested.
This commit is contained in:
parent
3c3d5a6e7b
commit
ffb8190b6b
4 changed files with 160 additions and 16 deletions
28
client/platformio.ini
Normal file
28
client/platformio.ini
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
; 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
|
||||||
|
|
||||||
|
[platformio]
|
||||||
|
env_default = teensy35
|
||||||
|
|
||||||
|
[common]
|
||||||
|
lib_deps =
|
||||||
|
721@3.0.2 ; TaskScheduler
|
||||||
|
|
||||||
|
[env:teensy35]
|
||||||
|
platform = teensy@3.6.0
|
||||||
|
board = teensy35
|
||||||
|
framework = arduino
|
||||||
|
lib_deps = ${common.lib_deps}
|
||||||
|
|
||||||
|
[env:teensy36]
|
||||||
|
platform = teensy@3.6.0
|
||||||
|
board = teensy36
|
||||||
|
framework = arduino
|
||||||
|
lib_deps = ${common.lib_deps}
|
||||||
29
client/src/main.cpp
Normal file
29
client/src/main.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
#include <Wire.h>
|
||||||
|
const int16_t I2C_ADDR = 3;
|
||||||
|
|
||||||
|
void receiveEvent(int howMany) {
|
||||||
|
(void) howMany;
|
||||||
|
while (1 < Wire.available()) {
|
||||||
|
char c = Wire.read();
|
||||||
|
Serial.print(c);
|
||||||
|
}
|
||||||
|
char x = Wire.read();
|
||||||
|
Serial.println(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
void requestEvent() {
|
||||||
|
if (random(1000) == 0) {
|
||||||
|
Wire.write("[bcdefghabcdefghabcdefghabcdefg]"); //32 bytes
|
||||||
|
} else {
|
||||||
|
Wire.write(" "); // no letter to send
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Wire.begin(I2C_ADDR);
|
||||||
|
Wire.onRequest(requestEvent);
|
||||||
|
Wire.onReceive(receiveEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
}
|
||||||
|
|
@ -19,13 +19,13 @@ lib_deps =
|
||||||
Wire
|
Wire
|
||||||
64 ; ArduinoJson
|
64 ; ArduinoJson
|
||||||
1269 ; Painless Mesh
|
1269 ; Painless Mesh
|
||||||
src_filter =
|
|
||||||
|
|
||||||
[env:nodemcuv2]
|
[env:nodemcuv2]
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = nodemcuv2
|
board = nodemcuv2
|
||||||
framework = ${common.framework}
|
framework = ${common.framework}
|
||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
|
upload_port = /dev/ttyUSB0
|
||||||
lib_deps =
|
lib_deps =
|
||||||
ESP8266WiFi
|
ESP8266WiFi
|
||||||
Servo(esp8266)
|
Servo(esp8266)
|
||||||
|
|
@ -36,6 +36,7 @@ platform = espressif8266
|
||||||
board = huzzah
|
board = huzzah
|
||||||
framework = ${common.framework}
|
framework = ${common.framework}
|
||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
|
upload_port = /dev/ttyUSB0
|
||||||
lib_deps =
|
lib_deps =
|
||||||
ESP8266WiFi
|
ESP8266WiFi
|
||||||
Servo(esp8266)
|
Servo(esp8266)
|
||||||
|
|
@ -48,6 +49,7 @@ platform = espressif32
|
||||||
board = featheresp32
|
board = featheresp32
|
||||||
framework = ${common.framework}
|
framework = ${common.framework}
|
||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
|
upload_port = /dev/ttyUSB0
|
||||||
lib_deps =
|
lib_deps =
|
||||||
1826@1.0.3 ; AsyncTCP
|
1826@1.0.3 ; AsyncTCP
|
||||||
${common.lib_deps}
|
${common.lib_deps}
|
||||||
|
|
|
||||||
115
src/main.cpp
115
src/main.cpp
|
|
@ -48,9 +48,8 @@
|
||||||
#define MESH_PORT 5555
|
#define MESH_PORT 5555
|
||||||
#define MESH_CHANNEL 5
|
#define MESH_CHANNEL 5
|
||||||
// #define MESH_ANCHOR
|
// #define MESH_ANCHOR
|
||||||
#define MSG_LENGTH_MAX 256
|
|
||||||
#define LONELY_TO_DIE (30000)
|
#define LONELY_TO_DIE (30000)
|
||||||
//============<parameters>============
|
//============</parameters>===========
|
||||||
|
|
||||||
//
|
//
|
||||||
// LED status indication
|
// LED status indication
|
||||||
|
|
@ -60,6 +59,7 @@
|
||||||
// phase 1
|
// phase 1
|
||||||
// - LED => slow blinking (syncronized)
|
// - LED => slow blinking (syncronized)
|
||||||
// - + connected.
|
// - + connected.
|
||||||
|
//
|
||||||
#if defined(ARDUINO_ESP8266_NODEMCU) // nodemcuv2
|
#if defined(ARDUINO_ESP8266_NODEMCU) // nodemcuv2
|
||||||
#define LED_PIN 2
|
#define LED_PIN 2
|
||||||
#elif defined(ARDUINO_ESP8266_ESP12) // huzzah
|
#elif defined(ARDUINO_ESP8266_ESP12) // huzzah
|
||||||
|
|
@ -70,13 +70,17 @@
|
||||||
#define LED_PERIOD (1111)
|
#define LED_PERIOD (1111)
|
||||||
#define LED_ONTIME (1)
|
#define LED_ONTIME (1)
|
||||||
|
|
||||||
//
|
//arduino
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
//
|
//i2c
|
||||||
#include <painlessMesh.h>
|
#include <Wire.h>
|
||||||
|
#define I2C_ADDR 3
|
||||||
|
#define POST_LENGTH 32
|
||||||
|
#define POST_BUFF_LEN (POST_LENGTH + 1)
|
||||||
|
|
||||||
// painless mesh
|
//painlessmesh
|
||||||
|
#include <painlessMesh.h>
|
||||||
painlessMesh mesh;
|
painlessMesh mesh;
|
||||||
|
|
||||||
//scheduler
|
//scheduler
|
||||||
|
|
@ -112,7 +116,7 @@ void taskStatusBlink_steadyOff() {
|
||||||
onFlag = false;
|
onFlag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// happy or lonely
|
//task #1 : happy or lonely
|
||||||
// --> automatic reset after some time of 'loneliness (disconnected from any node)'
|
// --> automatic reset after some time of 'loneliness (disconnected from any node)'
|
||||||
void nothappyalone() {
|
void nothappyalone() {
|
||||||
static bool isConnected_prev = false;
|
static bool isConnected_prev = false;
|
||||||
|
|
@ -135,7 +139,7 @@ void nothappyalone() {
|
||||||
// esp32 doesn't support 'reset()' yet...
|
// esp32 doesn't support 'reset()' yet...
|
||||||
// (restart() is framework-supported, reset() is more forced hardware-reset-action)
|
// (restart() is framework-supported, reset() is more forced hardware-reset-action)
|
||||||
#else
|
#else
|
||||||
#error unknown
|
#error unknown esp.
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -144,11 +148,92 @@ void nothappyalone() {
|
||||||
}
|
}
|
||||||
Task nothappyalone_task(1000, TASK_FOREVER, ¬happyalone, &runner, true); // by default, ENABLED.
|
Task nothappyalone_task(1000, TASK_FOREVER, ¬happyalone, &runner, true); // by default, ENABLED.
|
||||||
|
|
||||||
|
//task #2 : regular post collection
|
||||||
|
void collect_post() {
|
||||||
|
//
|
||||||
|
static char letter_outro[POST_BUFF_LEN] = "................................";
|
||||||
|
// ask a letter and collect the feedback.
|
||||||
|
Wire.requestFrom(I2C_ADDR, POST_LENGTH);
|
||||||
|
// error flag
|
||||||
|
bool letter_is_good = false;
|
||||||
|
// check the first byte
|
||||||
|
char first = '.';
|
||||||
|
// automatically match start byte.
|
||||||
|
while (Wire.available()) {
|
||||||
|
first = Wire.read();
|
||||||
|
if (first == '[') {
|
||||||
|
// client want to give me a letter.
|
||||||
|
letter_outro[0] = first;
|
||||||
|
// matched!
|
||||||
|
letter_is_good = true;
|
||||||
|
break;
|
||||||
|
} else if (first == ' ') {
|
||||||
|
// client says nothing to send.
|
||||||
|
Serial.print("."); // nothing to send.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
if (letter_is_good == false) {
|
||||||
|
// no more letters, but no valid char.
|
||||||
|
Serial.print("?"); // wrong client.
|
||||||
|
return;
|
||||||
|
} else if (letter_is_good == true) {
|
||||||
|
// get more contents
|
||||||
|
for (int i = 1; i < (POST_LENGTH-1); i++) {
|
||||||
|
if (Wire.available()) {
|
||||||
|
letter_outro[i] = Wire.read();
|
||||||
|
} else {
|
||||||
|
// hmm.. letter is too short.
|
||||||
|
letter_outro[i] = '.'; // fill-out with dots.
|
||||||
|
Serial.print("$"); // too $hort msg.
|
||||||
|
letter_is_good = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the last byte
|
||||||
|
char last = '.';
|
||||||
|
if (Wire.available()) {
|
||||||
|
letter_outro[POST_LENGTH-1] = last = Wire.read();
|
||||||
|
if (last != ']') {
|
||||||
|
// hmm.. last byte is strange
|
||||||
|
Serial.print("#"); // last byte error.
|
||||||
|
letter_is_good = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// hmm.. letter is too short.
|
||||||
|
letter_outro[POST_LENGTH-1] = '.'; // fill-out with dots.
|
||||||
|
Serial.print("$"); // too $hort msg.
|
||||||
|
letter_is_good = false;
|
||||||
|
}
|
||||||
|
// terminal char.
|
||||||
|
letter_outro[POST_LENGTH] = '\0';
|
||||||
|
}
|
||||||
|
// no good letter, we discard.
|
||||||
|
if (letter_is_good == false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// or, post it.
|
||||||
|
if (isConnected == true) {
|
||||||
|
mesh.sendBroadcast(String(letter_outro));
|
||||||
|
Serial.print("sendBroadcast: ");
|
||||||
|
Serial.println(letter_outro);
|
||||||
|
} else {
|
||||||
|
Serial.print("_"); // disconnected.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Task collect_post_task(10, TASK_FOREVER, &collect_post, &runner, true); // by default, ENABLED
|
||||||
|
//MAYBE... 10ms is too fast? move this to the loop() then?
|
||||||
|
|
||||||
// mesh callbacks
|
// mesh callbacks
|
||||||
void receivedCallback(uint32_t from, String & msg) { // REQUIRED
|
void receivedCallback(uint32_t from, String & msg) { // REQUIRED
|
||||||
// give it to I2C device
|
Serial.print("got msg.: ");
|
||||||
/////
|
Serial.println(msg);
|
||||||
Serial.print("Got one.");
|
// truncate any extra. letters.
|
||||||
|
msg = msg.substring(0, POST_LENGTH); // (0) ~ (POST_LENGTH-1)
|
||||||
|
// send whatever letter we postmans trust other postman.
|
||||||
|
Wire.beginTransmission(I2C_ADDR);
|
||||||
|
Wire.write(msg.c_str());
|
||||||
|
Wire.endTransmission();
|
||||||
}
|
}
|
||||||
void changedConnectionCallback() {
|
void changedConnectionCallback() {
|
||||||
// check status -> modify status LED
|
// check status -> modify status LED
|
||||||
|
|
@ -171,7 +256,7 @@ void changedConnectionCallback() {
|
||||||
}
|
}
|
||||||
// let I2C device know
|
// let I2C device know
|
||||||
/////
|
/////
|
||||||
Serial.println("Conn. change.");
|
Serial.println("hi. client, we ve got a change in the net.");
|
||||||
}
|
}
|
||||||
void newConnectionCallback(uint32_t nodeId) {
|
void newConnectionCallback(uint32_t nodeId) {
|
||||||
changedConnectionCallback();
|
changedConnectionCallback();
|
||||||
|
|
@ -213,7 +298,7 @@ void setup() {
|
||||||
delay(100);
|
delay(100);
|
||||||
Serial.println("hi, postman ready.");
|
Serial.println("hi, postman ready.");
|
||||||
#if defined(DISABLE_AP)
|
#if defined(DISABLE_AP)
|
||||||
Serial.println("INFO: we are in the WIFI_STA mode!");
|
Serial.println("!NOTE!: we are in the WIFI_STA mode!");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//understanding what is 'the nodeId' ==> last 4 bytes of 'softAPmacAddress'
|
//understanding what is 'the nodeId' ==> last 4 bytes of 'softAPmacAddress'
|
||||||
|
|
@ -246,8 +331,8 @@ void setup() {
|
||||||
// nodeId (hex) : 2D370A07
|
// nodeId (hex) : 2D370A07
|
||||||
// MAC : B6, E6, 2D, 37, A, 7
|
// MAC : B6, E6, 2D, 37, A, 7
|
||||||
|
|
||||||
//i2c
|
//i2c master
|
||||||
Serial.println("TODO. we need to setup I2C.");
|
Wire.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue