Skip to content
Snippets Groups Projects
Commit 3a24d2bc authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

WIP: read from serial on arduino and make a network call, save to SD card on...

WIP: read from serial on arduino and make a network call, save to SD card on arduino, task queue on esp32 espnow receiver
parent 70fc7c08
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!14Merge gsm host into development
......@@ -12,7 +12,17 @@
platform = atmelsam
board = mkrnb1500
framework = arduino
lib_deps =
arduino-libraries/MKRNB@^1.5.1
bblanchon/ArduinoJson@^6.19.4
arduino-libraries/Arduino Low Power@^1.2.2
monitor_speed = 9600
build_flags =
-I include
-DCORE_DEBUG_LEVEL=5
-std=gnu++17
build_unflags = -std=gnu++11
monitor_port = /dev/ttyACM0
upload_port = /dev/ttyACM0
lib_deps =
arduino-libraries/MKRNB@^1.5.1
bblanchon/ArduinoJson@^6.19.4
arduino-libraries/Arduino Low Power@^1.2.2
arduino-libraries/SD@^1.2.4
......@@ -6,9 +6,9 @@ this example downloads the URL "http://example.org/" and
prints it to the Serial monitor.
Circuit:
- MKR NB 1500 board
- Antenna
- SIM card with a data plan
- MKR NB 1500 board
- Antenna
- SIM card with a data plan
created 8 Mar 2012
by Tom Igoe
......@@ -18,8 +18,9 @@ by Tom Igoe
#include "ArduinoLowPower.h"
#include <ArduinoJson.h>
#include <MKRNB.h>
#include <SD.h>
#include <SPI.h>
#include <string>
// initialize the library instance
GPRS gprs;
NB nbAccess;
......@@ -28,6 +29,7 @@ void attachToNetwork();
std::string getResponse(NBClient &client);
std::string getResponseBody(const std::string &response);
void sendData(std::string data);
std::string getResponse(std::string server, std::string path, int port)
{
NBClient client;
......@@ -108,13 +110,37 @@ std::string postRequest(std::string server, std::string path, int port, std::str
return token;
}
const int wakeUpPin = 0;
const int chipSelect = 6;
void wakeUp()
{
Serial.println("Wake up");
}
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(wakeUpPin, INPUT);
// WAKE UP FROM WATCHDOG (PIN)
// LowPower.attachInterruptWakeup(wakeUpPin, wakeUp, RISING);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (true)
;
}
Serial.println("Attaching to network");
attachToNetwork();
}
void attachToNetwork()
......@@ -138,7 +164,47 @@ void attachToNetwork()
void loop()
{
attachToNetwork();
// LowPower.deepSleep();
// Serial.println("Waking up..");
String dataString;
while (Serial1.available()) {
dataString += Serial1.readString();
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
if (dataString.length() > 0) {
dataFile.println(dataString);
Serial.println(dataString);
sendData(dataString.c_str());
}
dataFile.close();
// print to the serial port too:
// Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(100);
//
// Serial.println("Going to sleep");
// LowPower.deepSleep(10000);
// TODO: SAVE TO EEPROM OR SD CARD VIA SPI
// TODO: REFACTOR CODE
// TODO: TEST CODE
}
void sendData(std::string data)
{
Serial.println("connecting...");
......@@ -157,13 +223,4 @@ void loop()
Serial.println(date);
Serial.println(milliseconds_since_epoch);
Serial.println(time);
nbAccess.shutdown();
// TODO: WAKE UP FROM WATCHDOG (PIN)
LowPower.deepSleep(10000);
// TODO: SAVE TO EEPROM OR SD CARD VIA SPI
// TODO: REFACTOR CODE
// TODO: TEST CODE
}
......@@ -13,8 +13,8 @@ platform = espressif32
board = esp32-c3-devkitm-1
monitor_speed = 115200
framework = arduino
monitor_port = /dev/ttyUSB1
upload_port = /dev/ttyUSB1
monitor_port = /dev/ttyUSB0
upload_port = /dev/ttyUSB0
build_flags =
-I include
-DCORE_DEBUG_LEVEL=5
......
......@@ -4,12 +4,42 @@
#include <Arduino.h>
#include <WiFi.h>
#include <esp_now.h>
#include <queue>
#include <sys/unistd.h>
#define RXD2 18
#define TXD2 19
//SoftwareSerial mySerial(RXD2, TXD2);
TaskHandle_t Task1;
static std::queue<std::string> queue;
const int wakeUpPin = 4;
[[noreturn]] void Task1code(void *parameter)
{
Serial.println(xPortGetCoreID());
while (true) {
if (!queue.empty()) {
std::string message = queue.front();
queue.pop();
digitalWrite(wakeUpPin, HIGH);
delay(100);
Serial.println(message.c_str());
Serial1.println(message.c_str());
digitalWrite(wakeUpPin, LOW);
}
delay(5000);
// Code for task 1 - infinite loop
// (...)
}
}
// SoftwareSerial mySerial(RXD2, TXD2);
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
......@@ -25,17 +55,17 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
Serial.print(":");
}
Serial.println();
char data[len];
memcpy(data, incomingData, len);
Serial.println(data);
Serial1.write(data);
queue.push(data);
}
void setup()
{
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, RXD2, TXD2);
Serial1.begin(9600, SERIAL_8N1, RXD2, TXD2);
pinMode(wakeUpPin, OUTPUT);
digitalWrite(wakeUpPin, LOW);
WiFi.mode(WIFI_STA);
Serial.println("ESPNow init");
......@@ -46,9 +76,16 @@ void setup()
}
Serial.println("ESPNow init success");
xTaskCreatePinnedToCore(Task1code, /* Function to implement the task */
"Task1", /* Name of the task */
10000, /* Stack size in words */
nullptr, /* Task input parameter */
0, /* Priority of the task */
&Task1, /* Task handle. */
0); /* Core where the task should run */
esp_now_register_recv_cb(on_data_recv);
// write your initialization code here
}
void loop() {
}
\ No newline at end of file
void loop() {}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment