From 78a5059c43a358fb347acde60b87831b67619ddc Mon Sep 17 00:00:00 2001
From: Zoe Pfister <zoe.pfister@uibk.ac.at>
Date: Mon, 17 Oct 2022 15:34:50 +0200
Subject: [PATCH] WIP: lte-m connection using Arduino MKR NB 1500. get and post
 possible to port 80 (http). WIP: parsing of json response body

---
 host/arduino/.gitignore     |   3 +
 host/arduino/CMakeLists.txt |  33 ++++++++
 host/arduino/include/README |  39 +++++++++
 host/arduino/platformio.ini |  18 ++++
 host/arduino/src/main.cpp   | 162 ++++++++++++++++++++++++++++++++++++
 host/arduino/test/README    |  11 +++
 6 files changed, 266 insertions(+)
 create mode 100644 host/arduino/.gitignore
 create mode 100644 host/arduino/CMakeLists.txt
 create mode 100644 host/arduino/include/README
 create mode 100644 host/arduino/platformio.ini
 create mode 100644 host/arduino/src/main.cpp
 create mode 100644 host/arduino/test/README

diff --git a/host/arduino/.gitignore b/host/arduino/.gitignore
new file mode 100644
index 0000000..3fe18ad
--- /dev/null
+++ b/host/arduino/.gitignore
@@ -0,0 +1,3 @@
+.pio
+CMakeListsPrivate.txt
+cmake-build-*/ 
diff --git a/host/arduino/CMakeLists.txt b/host/arduino/CMakeLists.txt
new file mode 100644
index 0000000..c357da0
--- /dev/null
+++ b/host/arduino/CMakeLists.txt
@@ -0,0 +1,33 @@
+# !!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE
+# https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags
+#
+# If you need to override existing CMake configuration or add extra,
+# please create `CMakeListsUser.txt` in the root of project.
+# The `CMakeListsUser.txt` will not be overwritten by PlatformIO.
+
+cmake_minimum_required(VERSION 3.13)
+set(CMAKE_SYSTEM_NAME Generic)
+set(CMAKE_C_COMPILER_WORKS 1)
+set(CMAKE_CXX_COMPILER_WORKS 1)
+
+project("arduino" C CXX)
+
+include(CMakeListsPrivate.txt)
+
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeListsUser.txt)
+include(CMakeListsUser.txt)
+endif()
+
+add_custom_target(
+    Production ALL
+    COMMAND platformio -c clion run "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_target(
+    Debug ALL
+    COMMAND platformio -c clion debug "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_executable(Z_DUMMY_TARGET ${SRC_LIST})
diff --git a/host/arduino/include/README b/host/arduino/include/README
new file mode 100644
index 0000000..194dcd4
--- /dev/null
+++ b/host/arduino/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/host/arduino/platformio.ini b/host/arduino/platformio.ini
new file mode 100644
index 0000000..58dec1a
--- /dev/null
+++ b/host/arduino/platformio.ini
@@ -0,0 +1,18 @@
+; 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:mkrnb1500]
+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
diff --git a/host/arduino/src/main.cpp b/host/arduino/src/main.cpp
new file mode 100644
index 0000000..58a834c
--- /dev/null
+++ b/host/arduino/src/main.cpp
@@ -0,0 +1,162 @@
+/*
+Web client
+
+This sketch connects to a website through a MKR NB 1500 board. Specifically,
+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
+
+created 8 Mar 2012
+by Tom Igoe
+*/
+
+// libraries
+#include "ArduinoLowPower.h"
+#include <ArduinoJson.h>
+#include <MKRNB.h>
+#include <string>
+
+// initialize the library instance
+GPRS gprs;
+NB nbAccess;
+
+void attachToNetwork();
+
+std::string getResponse(NBClient &client);
+std::string getResponseBody(const std::string &response);
+std::string getResponse(std::string server, std::string path, int port)
+{
+	NBClient client;
+	// if you get a connection, report back via serial:
+	if (client.connect(server.c_str(), port)) {
+		Serial.println("connected to server");
+		// Make a HTTP request:
+		client.print("GET ");
+		client.print(path.c_str());
+		client.println(" HTTP/1.1");
+		client.print("Host: ");
+		client.println(server.c_str());
+		client.println("Connection: close");
+		client.println();
+	} else {
+		// if you didn't get a connection to the server:
+		Serial.println("connection failed");
+	}
+
+	std::string response = getResponse(client);
+
+	std::string token = getResponseBody(response);
+	Serial.println(token.c_str());
+	client.stop();
+	return token;
+}
+std::string getResponseBody(const std::string &response)
+{
+	std::string delimiter = "\r\n\r\n";
+	std::string token = response.substr(response.find(delimiter)).replace(0, delimiter.length(), "");
+	return token;
+}
+
+std::string getResponse(NBClient &client)
+{
+	String status = client.readStringUntil('\r');
+	Serial.println(status);
+
+	std::string response;
+	while (client.connected()) {
+		if (client.available()) {
+			char c = client.read();
+			response += c;
+		}
+	}
+	return response;
+}
+
+std::string postRequest(std::string server, std::string path, int port, std::string body)
+{
+	NBClient client;
+	// if you get a connection, report back via serial:
+	if (client.connect(server.c_str(), port)) {
+		Serial.println("connected to server");
+		// Make a HTTP request:
+		client.print("POST ");
+		client.print(path.c_str());
+		client.println(" HTTP/1.1");
+		client.print("Host: ");
+		client.println(server.c_str());
+		client.println("Connection: close");
+		client.println("Content-Type: application/json");
+		client.print("Content-Length: ");
+		client.println(body.length());
+		client.println();
+		client.println(body.c_str());
+	} else {
+		// if you didn't get a connection to the server:
+		Serial.println("connection failed");
+	}
+
+	std::string response = getResponse(client);
+
+	std::string token = getResponseBody(response);
+
+	Serial.println(token.c_str());
+	client.stop();
+	return token;
+}
+
+void setup()
+{
+	// initialize serial communications and wait for port to open:
+	Serial.begin(9600);
+	while (!Serial) {
+		; // wait for serial port to connect. Needed for native USB port only
+	}
+}
+
+void attachToNetwork()
+{
+	Serial.println("Starting Arduino web client.");
+	// connection state
+	boolean connected = false;
+
+	// After starting the modem with NB.begin()
+	// attach to the GPRS network with the APN, login and password
+	while (!connected) {
+		if ((nbAccess.begin("", "m2m.public.at") == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) {
+			connected = true;
+		} else {
+			Serial.println("Not connected");
+			exit(1);
+			delay(1000);
+		}
+	}
+}
+
+void loop()
+{
+	attachToNetwork();
+
+	Serial.println("connecting...");
+
+	auto response = getResponse("date.jsontest.com", "/", 80);
+	//	convert response to json object
+	DynamicJsonDocument doc(1024);
+	auto error = deserializeJson(doc, response);
+	if (error) {
+		Serial.print(F("deserializeJson() failed: "));
+		Serial.println(error.f_str());
+		return;
+	}
+	const char *date = doc["date"];
+	long long milliseconds_since_epoch = doc["milliseconds_since_epoch"].as<long long>();
+	const char *time = doc["time"];
+	Serial.println(date);
+	Serial.println(milliseconds_since_epoch);
+	Serial.println(time);
+	nbAccess.shutdown();
+	LowPower.deepSleep(10000);
+}
diff --git a/host/arduino/test/README b/host/arduino/test/README
new file mode 100644
index 0000000..9b1e87b
--- /dev/null
+++ b/host/arduino/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