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

WIP: lte-m connection using Arduino MKR NB 1500. get and post possible to port 80 (http).

WIP: parsing of json response body
parent 62697b8b
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client
.pio
CMakeListsPrivate.txt
cmake-build-*/
# !!! 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})
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
; 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
/*
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);
}
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
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