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

WIP: initial commit of the esp-sim

parent 3a24d2bc
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
.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("esp-t-sim7000g" 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:esp-wrover-kit]
platform = espressif32
board = esp-wrover-kit
framework = arduino
lib_deps =
vshymanskyy/TinyGSM@^0.11.5
vshymanskyy/StreamDebugger@^1.0.1
bblanchon/ArduinoJson@^6.19.4
/*
FILE: AllFunctions.ino
AUTHOR: Koby Hale
PURPOSE: Test functionality
*/
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
// See all AT commands, if wanted
#define DUMP_AT_COMMANDS
/*
Tests enabled
*/
#define TINY_GSM_TEST_GPRS true
#define TINY_GSM_POWERDOWN true
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "m2m.public.at"; // SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
#include <SD.h>
#include <SPI.h>
#include <Ticker.h>
#include <TinyGsmClient.h>
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds)
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
#include <ArduinoJson.h>
int counter, lastIndex, numberOfPieces = 24;
String pieces[24], input;
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(TinyGsmClient &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 getResponse(std::string server, std::string path, int port)
{
TinyGsmClient client(modem);
// 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;
}
void modemDisconnect();
bool do_gsm_test()
{
SerialAT.println("AT+CGDCONT?");
delay(500);
if (SerialAT.available()) {
input = SerialAT.readString();
for (int i = 0; i < input.length(); i++) {
if (input.substring(i, i + 1) == "\n") {
pieces[counter] = input.substring(lastIndex, i);
lastIndex = i + 1;
counter++;
}
if (i == input.length() - 1) {
pieces[counter] = input.substring(lastIndex, i);
}
}
// Reset for reuse
input = "";
counter = 0;
lastIndex = 0;
for (int y = 0; y < numberOfPieces; y++) {
for (char c : pieces[y]) {
// gets one byte from buffer
if (c == ',') {
if (input.indexOf(": ") >= 0) {
String data = input.substring((input.indexOf(": ") + 1));
if (data.toInt() > 0 && data.toInt() < 25) {
modem.sendAT("+CGDCONT=" + String(data.toInt()) + ",\"IP\",\"" + String(apn)
+ "\",\"0.0.0.0\",0,0,0,0");
}
input = "";
break;
}
// Reset for reuse
input = "";
} else {
input += c;
}
}
}
} else {
Serial.println("Failed to get PDP!");
}
Serial.println("\n\n\nWaiting for network...");
if (!modem.waitForNetwork()) {
delay(10000);
return false;
}
if (modem.isNetworkConnected()) {
Serial.println("Network connected");
}
Serial.println("\n---Starting GPRS TEST---\n");
Serial.println("Connecting to: " + String(apn));
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return false;
}
Serial.print("GPRS status: ");
if (modem.isGprsConnected()) {
Serial.println("connected");
} else {
Serial.println("not connected");
}
String ccid = modem.getSimCCID();
Serial.println("CCID: " + ccid);
String imei = modem.getIMEI();
Serial.println("IMEI: " + imei);
String cop = modem.getOperator();
Serial.println("Operator: " + cop);
IPAddress local = modem.localIP();
Serial.println("Local IP: " + String(local));
int csq = modem.getSignalQuality();
Serial.println("Signal quality: " + String(csq));
SerialAT.println("AT+CPSI?"); // Get connection type and band
delay(500);
if (SerialAT.available()) {
String r = SerialAT.readString();
Serial.println(r);
}
Serial.println("\n---End of GPRS TEST---\n");
return true;
}
void setup()
{
// Set console baud rate
Serial.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("SDCard MOUNT FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
String str = "SDCard Size: " + String(cardSize) + "MB";
Serial.println(str);
}
Serial.println("\nWait...");
delay(1000);
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.restart()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
}
bool modemConnect()
{
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.init()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
String name = modem.getModemName();
delay(500);
Serial.println("Modem Name: " + name);
String modemInfo = modem.getModemInfo();
delay(500);
Serial.println("Modem Info: " + modemInfo);
// Set SIM7000G GPIO4 LOW ,turn off GPS power
// CMD:AT+SGPIO=0,4,1,0
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,0 false ");
}
#if TINY_GSM_TEST_GPRS
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3) {
modem.simUnlock(GSM_PIN);
}
#endif
modem.sendAT("+CFUN=0 ");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=0 false ");
}
delay(200);
/*
2 Automatic
13 GSM only
38 LTE only
51 GSM and LTE only
* * * */
String res;
res = modem.setNetworkMode(38);
if (res != "1") {
DBG("setNetworkMode false ");
return false;
}
delay(200);
/*
1 CAT-M
2 NB-Iot
3 CAT-M and NB-IoT
* * */
res = modem.setPreferredMode(1);
if (res != "1") {
DBG("setPreferredMode false ");
return false;
}
delay(200);
/*AT+CBANDCFG=<mode>,<band>[,<band>…]
* <mode> "CAT-M" "NB-IOT"
* <band> The value of <band> must is in the band list of getting from AT+CBANDCFG=?
* For example, my SIM card carrier "NB-iot" supports B8. I will configure +CBANDCFG= "Nb-iot ",8
*/
modem.sendAT("+CBANDCFG=\"CAT-M\",8 ");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CBANDCFG=\"NB-IOT\" ");
}
delay(200);
modem.sendAT("+CFUN=1 ");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=1 false ");
}
delay(200);
#if TINY_GSM_TEST_GPRS
if (!do_gsm_test()) {
return false;
}
modem.gprsDisconnect();
if (!modem.isGprsConnected()) {
Serial.println("GPRS disconnected");
} else {
Serial.println("GPRS disconnect: Failed.");
}
#endif
return true;
}
void loop()
{
if (!modemConnect()) {
return;
}
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);
modemDisconnect();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(200);
esp_deep_sleep_start();
// Do nothing forevermore
while (true) {
modem.maintain();
}
}
void modemDisconnect()
{
#if TINY_GSM_POWERDOWN
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
modem.sendAT("+CPOWD=1");
if (modem.waitResponse(10000L) != 1) {
DBG("+CPOWD=1");
}
modem.poweroff();
Serial.println("Poweroff.");
#endif
}
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