8 Commits

Author SHA1 Message Date
479942a4e7 config parsing 2022-12-23 02:35:55 +01:00
7643585856 display stuff 2022-12-19 00:06:14 +01:00
7c3e49c299 display stuff 2022-12-19 00:05:27 +01:00
89d03cf75d docu fix 2022-12-18 23:38:17 +01:00
949bbce513 pictures cropped 2022-12-18 23:37:26 +01:00
a01efcf04a docu 2022-12-18 23:34:47 +01:00
4052bea374 start configuration stuff 2022-12-18 18:08:39 +01:00
f4bcf7fc01 move code to production file 2022-12-18 17:12:04 +01:00
15 changed files with 684 additions and 215 deletions

38
ConfigGen/configGen.py Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/python
from Cheetah.Template import Template
from ConfigDataStructure import configItems, magic, appName
#configItems = [
# {"label":"_", "key":"magic", "type":"I", "default": ""},
# {"label":"Config Username", "key":"confUser", "type":"C", "length":16, "default":"admin"},
# {"label":"Config Password", "key":"confPasswd", "type":"C", "length":16, "default":"geheim123"},
# {"label":"Wifi SSID", "key":"wifiSsid", "type":"C", "length":32, "default":"test"},
# {"label":"Wifi Key", "key":"wifiKey", "type":"C", "length":64, "default":"geheim"},
# {"label":"MQTT Broker", "key":"mqttBroker", "type":"C", "length":32, "default":"broker.hottis.de"},
# {"label":"MQTT Username", "key":"mqttUser", "type":"C", "length":32, "default":"RgbLed1"},
# {"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
# {"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"},
# {"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883},
# {"label":"MQTT Topic Color Command", "key":"mqttTopicColorCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorCommand"},
# {"label":"MQTT Topic Command", "key":"mqttTopicCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/Command"},
# {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"},
# {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0}
#]
confWifiSsid = "espconfig"
params = {
"magic":magic,
"appName":appName,
"confWifiSsid":confWifiSsid,
"configItems":configItems
}
h_file = Template(file="configuration_h.tmpl", searchList=[params])
open('configuration.h','w').write(str(h_file))
c_file = Template(file="configuration_c.tmpl", searchList=[params])
open('configuration.cpp','w').write(str(c_file))

12
ConfigGen/configGen.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
export PROJECTDIR=$PWD/../../..
export PYTHONPATH=$PYTHONPATH:$PROJECTDIR/sketch
python -B configGen.py
mv configuration.cpp configuration.h $PROJECTDIR/libraries/includes

View File

@ -0,0 +1,153 @@
#raw
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include "defines.h"
#include "configuration.h"
#end raw
tConfigBlock configBlock;
const uint32_t MAGIC = $magic;
const char* CONFIG_SSID = "$confWifiSsid";
extern ESP8266WebServer webServer;
bool configSaved = false;
static bool checkAuthentication() {
Serial.print("User: "); Serial.println(configBlock.confUser);
Serial.print("Pass: "); Serial.println(configBlock.confPasswd);
return webServer.authenticate(configBlock.confUser, configBlock.confPasswd);
}
void configServeIndex() {
bool configValid = (configBlock.magic == MAGIC);
if (! configValid) {
configBlock.magic = MAGIC;
#for $configItem in $configItems
#if $configItem.label != "_"
#if $configItem.type == "C"
strcpy(configBlock.$configItem.key, "$configItem.default");
#else if $configItem.type == "I"
configBlock.$configItem.key = $configItem.default;
#end if
#end if
#end for
}
if (! checkAuthentication()) {
return webServer.requestAuthentication();
}
String buffer =
"<!doctype html"
"<html lang=\"en\">"
" <head>"
" <title>$appName</title>"
" </head>"
" <body>"
" <h1>$appName - ESP8266 Configuration Page</h1>";
if (configSaved) {
configSaved = false;
buffer += "<h2>Configuration saved</h2>";
}
buffer +=
" <form action=\"/config\" method=\"GET\">"
" <table>"
#for $configItem in $configItems
#if $configItem.label != "_"
" <tr>"
" <td>"
" <label for\"$configItem.key\">$configItem.label</label>"
" </td><td>"
" <input type=\"text\" name=\"$configItem.key\" id=\"$configItem.key\" ";
#if $configItem.type == "C"
buffer += " size=\"$configItem.length\" ";
buffer += " value=\"";
buffer += configBlock.$configItem.key;
buffer += "\"";
#else if $configItem.type == "I"
buffer += " value=\"";
buffer += configBlock.$configItem.key;
buffer += "\"";
#end if
buffer +=
" />"
" </td>"
" </tr>"
#end if
#end for
" <tr>"
" <td colspan=\"2\">"
" <button type=\"submit\">Save</button>"
" </td>"
" </tr>"
" </table>"
" </form>"
" </body>"
"</html>";
webServer.send(200, "text/html", buffer);
#ifdef DEBUG
Serial.println("indexHtml request served");
#endif
}
void configServeGetConfiguration() {
if (! checkAuthentication()) {
return webServer.requestAuthentication();
}
String arg;
#for $configItem in $configItems
#if $configItem.label != "_"
arg = webServer.arg("$configItem.key");
#if $configItem.type == "C"
strcpy(configBlock.$configItem.key, arg.c_str());
#else if $configItem.type == "I"
configBlock.$configItem.key = atoi(arg.c_str());
#end if
#end if
#end for
configBlock.magic = MAGIC;
showConfiguration();
EEPROM.begin(512);
EEPROM.put(EEPROM_ADDR, configBlock);
EEPROM.commit();
Serial.println("EEPROM saved");
configSaved = true;
webServer.sendHeader("Location", String("/"), true);
webServer.send(302, "text/plain", "");
//webServer.send(200, "text/html", "configuration saved");
}
void showConfiguration() {
Serial.println("Configuration is");
#for $configItem in $configItems
Serial.print("$configItem.key = <");
Serial.print(configBlock.$configItem.key);
Serial.println(">");
#end for
Serial.println("---");
}

View File

@ -0,0 +1,17 @@
typedef struct {
#for $configItem in $configItems
#if $configItem.type == 'C'
char ${configItem.key}[$configItem.length];
#else if $configItem.type == 'I'
uint32_t $configItem.key;
#end if
#end for
} tConfigBlock;
extern const uint32_t MAGIC;
extern tConfigBlock configBlock;
extern const char* CONFIG_SSID;
void configServeIndex();
void configServeGetConfiguration();
void showConfiguration();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 MiB

After

Width:  |  Height:  |  Size: 2.0 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

@ -1,3 +1,16 @@
# LoRaWAN Modbus Gateway
## Some pictures
![The whole setup](./docs/IMG_3854.jpg)
![The device](./docs/IMG_3853.jpg)
![Modbus signals in front of RS485 transceiver on the osciloscope](./docs/modbus01.png)
![Notes on schematics](./docs/IMG_3855.jpg)
## Notes on the development environment
* start dev environment using `startDevEnv.sh` * start dev environment using `startDevEnv.sh`
* build application in container in directory `~/project` using `arduino-cli compile --fqbn=Heltec-esp32:esp32:WIFI_LoRa_32_V3 --export-binaries /home/arduino/project/sketch/` * build application in container in directory `~/project` using `arduino-cli compile --fqbn=Heltec-esp32:esp32:WIFI_LoRa_32_V3 --export-binaries /home/arduino/project/sketch/`
* flash device using `esptool.py --port /dev/tty.usbserial-0001 --baud 921600 --chip esp32s3 write_flash --flash_mode dio --flash_size 8MB 0x0 sketch.ino.bootloader.bin 0x8000 sketch.ino.partitions.bin 0x10000 sketch.ino.bin` * flash device using `esptool.py --port /dev/tty.usbserial-0001 --baud 921600 --chip esp32s3 write_flash --flash_mode dio --flash_size 8MB 0x0 sketch.ino.bootloader.bin 0x8000 sketch.ino.partitions.bin 0x10000 sketch.ino.bin`

View File

@ -1,61 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "LoRaWan_APP.h"
#if 0
/* OTAA para*/
uint8_t devEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 };
uint8_t appEui[] = { 0xa0, 0x57, 0x81, 0x00, 0x01, 0x12, 0xaa, 0xf3 };
uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 };
/* ABP para*/
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };
uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };
uint32_t devAddr = ( uint32_t )0x007e6ae1;
void configInit() {
}
#else
config_t myConfig = {
.devEui = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 },
.appEui = { 0xa0, 0x57, 0x81, 0x00, 0x01, 0x12, 0xaa, 0xf3 },
.appKey = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 },
.nwkSKey = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
.appSKey = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
.devAddr = 0,
.overTheAirActivation = true,
.modbus_poll_slots = {
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x01 },
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x02 },
{ .typ = UNASSIGNED, .id = 0, .address = 0 },
}
};
// these variables are defined as externals in the LoRaWAN stack
uint8_t devEui[sizeof(myConfig.devEui)];
uint8_t appEui[sizeof(myConfig.appEui)];
uint8_t appKey[sizeof(myConfig.appKey)];
uint8_t nwkSKey[sizeof(myConfig.nwkSKey)];
uint8_t appSKey[sizeof(myConfig.appSKey)];
uint32_t devAddr;
LoRaMacRegion_t loraWanRegion;
bool overTheAirActivation;
void configInit() {
memcpy(devEui, myConfig.devEui, sizeof(devEui));
memcpy(appEui, myConfig.appEui, sizeof(appEui));
memcpy(appKey, myConfig.appKey, sizeof(appKey));
memcpy(nwkSKey, myConfig.nwkSKey, sizeof(nwkSKey));
memcpy(appSKey, myConfig.appSKey, sizeof(appSKey));
devAddr = myConfig.devAddr;
loraWanRegion = LORAMAC_REGION_EU868;
overTheAirActivation = myConfig.overTheAirActivation;
}
#endif

252
sketch/configuration.cpp Normal file
View File

@ -0,0 +1,252 @@
#include "configuration.h"
#include "defines.h"
#include "LoRaWan_APP.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <Wire.h>
#include "HT_SSD1306Wire.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <WiFiAP.h>
#include <sstream>
#include <string>
#include <iomanip>
const uint32_t MAGIC = 0xaffe0001;
config_t myConfig = {
.magic = MAGIC,
.appEui = { 0, 0, 0, 0, 0, 0, 0, 0 },
.appKey = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
.modbus_poll_slots = {
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x01 },
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x02 },
{ .typ = UNASSIGNED, .id = 0, .address = 0 },
}
};
uint8_t devEui[8];
uint8_t appEui[8];
uint8_t appKey[16];
uint8_t nwkSKey[16];
uint8_t appSKey[16];
uint32_t devAddr;
LoRaMacRegion_t loraWanRegion;
bool overTheAirActivation;
SSD1306Wire confDisplay(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED);; // addr , freq , i2c group , resolution , rst
WebServer server(80);
const char *ssid = "ModbusLoraConf";
#define TEST_PASSWORD
#ifndef TEST_PASSWORD
char password[12];
#else
const char *password = "test1234";
#endif
extern uint8_t devEui[8];
bool configSaved = false;
int configParsingFailed = 0;
enum { PARSING_OK, PARSING_INVALID, PARSING_TOO_MANY, PARSING_TOO_FEW } configParsingError;
static void displayStatus(uint8_t numOfStations) {
confDisplay.clear();
confDisplay.display();
confDisplay.drawString(1, 0, "Config Mode running");
char buf[64];
memset(buf, 0, sizeof(buf));
sprintf(buf, "SSID: %s", ssid);
confDisplay.drawString(1, 12, buf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "Key: %s", password);
confDisplay.drawString(1, 24, buf);
confDisplay.display();
memset(buf, 0, sizeof(buf));
sprintf(buf, "Num of stations: %d", numOfStations);
confDisplay.drawString(1, 36, buf);
confDisplay.display();
}
static void displayInit() {
digitalWrite(Vext,LOW);
confDisplay.init();
confDisplay.setFont(ArialMT_Plain_10);
confDisplay.setTextAlignment(TEXT_ALIGN_LEFT);
confDisplay.screenRotate(ANGLE_180_DEGREE);
confDisplay.clear();
confDisplay.drawString(1, 0, "Config Mode starting");
confDisplay.display();
}
static void handleRoot() {
// bool configValid = (configBlock.magic == MAGIC);
std::stringstream buffer;
buffer << "<!doctype html"
"<html lang=\"en\">"
" <head>"
" <title>Modbus LoRaWAN Gateway</title>"
" </head>"
" <body>"
" <h1>Modbus LoRaWAN Gateway - Configuration Page</h1>";
if (configSaved) {
configSaved = false;
buffer << "<h2>Configuration saved</h2>";
}
if (configParsingFailed) {
configParsingFailed = 0;
buffer << "<h2>Error when parsing field " << configParsingFailed << ", error " << configParsingError << "</h2>";
}
buffer << " <form action=\"config\" method=\"GET\">"
" <table>"
" <tr>"
" <td>"
" DevEui"
" </td><td>";
buffer << " <input type=\"text\" readonly name=\"DevEui\" id=\"DevEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)devEui[i];
}
buffer << "\"> (readonly)";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppEui"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppEui\" id=\"AppEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appEui[i];
}
buffer << "\">";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppKey"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppKey\" id=\"AppKey\" size=\"47\" value=\"";
for (int i = 0; i < 16; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appKey[i];
}
buffer << "\">";
buffer << " </td>"
" </tr>"
" <tr><td colspan=\"2\">"
" <button type=\"submit\">Save</button>"
" </td></tr>"
" </table>"
" </form>"
" </body"
"</html";
server.send(200, "text/html", buffer.str().c_str());
}
static void parseField(char* arg, int fieldNum, int reqTokens, uint8_t *dest) {
int i = 0;
for (char *part = strtok(arg, ":"); part; part = strtok(NULL, ":")) {
Serial.println(part);
char *errPtr;
long v = strtol(part, &errPtr, 16);
if (errPtr && *errPtr) {
Serial.printf("some error happened, %p %02x\n\r", errPtr, *errPtr);
configParsingFailed = fieldNum;
configParsingError = PARSING_INVALID;
break;
}
if (i >= reqTokens) {
Serial.println("too many tokens");
configParsingFailed = fieldNum;
configParsingError = PARSING_TOO_MANY;
break;
}
dest[i] = v;
i++;
}
if ((i != reqTokens) && (configParsingFailed == 0)) {
Serial.println("too few tokens");
configParsingFailed = fieldNum;
configParsingError = PARSING_TOO_FEW;
}
}
static void handleConfigSave() {
char *arg1 = (char*)server.arg("AppEui").c_str();
Serial.printf("AppEui: %s\n\r", arg1);
parseField(arg1, 1, 8, myConfig.appEui);
if (!configParsingFailed) {
char *arg2 = (char*)server.arg("AppKey").c_str();
Serial.printf("AppKey: %s\n\r", arg2);
parseField(arg2, 2, 16, myConfig.appKey);
}
configSaved = !configParsingFailed;
server.sendHeader("Location", String("/"), true);
server.send(302, "text/plain", "");
}
void configurationSetup() {
#ifndef TEST_PASSWORD
memset(password, 0, sizeof(password));
for (int i = 0; i < sizeof(password) - 1; i++) {
password[i] = random(65, 90);
}
#endif
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
displayInit();
displayStatus(0);
LoRaWAN.generateDeveuiByChipID();
server.on("/", handleRoot);
server.on("/config", handleConfigSave);
server.begin();
}
void configurationLoop() {
static uint8_t numOfStations = 255;
uint8_t currentNumOfStations = WiFi.softAPgetStationNum();
if (numOfStations != currentNumOfStations) {
displayStatus(currentNumOfStations);
numOfStations = currentNumOfStations;
}
server.handleClient();
}
void configLoad() {
memcpy(appEui, myConfig.appEui, sizeof(appEui));
memcpy(appKey, myConfig.appKey, sizeof(appKey));
loraWanRegion = LORAMAC_REGION_EU868;
overTheAirActivation = true;
}

View File

@ -1,5 +1,7 @@
#ifndef _CONFIG_H_ #ifndef _CONFIGURATION_H_
#define _CONFIG_H_ #define _CONFIGURATION_H_
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -21,19 +23,18 @@ typedef struct modbus_poll_s {
} modbus_poll_t; } modbus_poll_t;
typedef struct config_s { typedef struct config_s {
uint8_t devEui[8]; uint32_t magic;
uint8_t appEui[8]; uint8_t appEui[8];
uint8_t appKey[16]; uint8_t appKey[16];
uint8_t nwkSKey[16];
uint8_t appSKey[16];
uint32_t devAddr;
bool overTheAirActivation;
modbus_poll_t modbus_poll_slots[MAX_MODBUS_POLL_SLOTS]; modbus_poll_t modbus_poll_slots[MAX_MODBUS_POLL_SLOTS];
} config_t; } config_t;
void configInit();
void configurationSetup();
void configurationLoop();
void configLoad();
#endif // _CONFIGURATION_H_
#endif /* _CONFIG_H_ */

165
sketch/production.cpp Normal file
View File

@ -0,0 +1,165 @@
#include "LoRaWan_APP.h"
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
#include "defines.h"
#include "configuration.h"
#include <string.h>
// from config.cpp
extern config_t myConfig;
/*LoraWan channelsmask, default channels 0-7*/
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };
/*LoraWan Class, Class A and Class C are supported*/
DeviceClass_t loraWanClass = CLASS_A;
/*the application data transmission duty cycle. value in [ms].*/
uint32_t appTxDutyCycle = 15000;
/*ADR enable*/
bool loraWanAdr = true;
/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = true;
/* Application port */
uint8_t appPort = 2;
/*!
* Number of trials to transmit the frame, if the LoRaMAC layer did not
* receive an acknowledgment. The MAC performs a datarate adaptation,
* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
* to the following table:
*
* Transmission nb | Data Rate
* ----------------|-----------
* 1 (first) | DR
* 2 | DR
* 3 | max(DR-1,0)
* 4 | max(DR-1,0)
* 5 | max(DR-2,0)
* 6 | max(DR-2,0)
* 7 | max(DR-3,0)
* 8 | max(DR-3,0)
*
* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
* the datarate, in case the LoRaMAC layer did not receive an acknowledgment
*/
uint8_t confirmedNbTrials = 4;
RS485Class* pRS485_1;
ModbusRTUClientClass* pModbusClient;
/* Prepares the payload of the frame */
static void prepareTxFrame( uint8_t port )
{
Serial.println("modbus operation");
appDataSize = 0;
for (modbus_poll_t *slot = myConfig.modbus_poll_slots; slot->typ != 0; slot++) {
Serial.printf("Slot: %d, %d, %d\r\n", slot->typ, slot->id, slot->address);
switch (slot->typ) {
case HOLDING_REGISTERS:
{
long v;
if (appDataSize + sizeof(v) < LORAWAN_APP_DATA_MAX_SIZE) {
v = pModbusClient->holdingRegisterRead(slot->id, slot->address);
Serial.println(v);
memcpy(appData + appDataSize, &v, sizeof(v));
appDataSize += sizeof(v);
} else {
Serial.println("too much data for LoRaWAN packet");
}
}
break;
case INPUT_REGISTERS:
{
long v;
if (appDataSize + sizeof(v) < LORAWAN_APP_DATA_MAX_SIZE) {
v = pModbusClient->inputRegisterRead(slot->id, slot->address);
Serial.println(v);
memcpy(appData + appDataSize, &v, sizeof(v));
appDataSize += sizeof(v);
} else {
Serial.println("too much data for LoRaWAN packet");
}
}
break;
default:
Serial.println("unknown typ, doing nothing");
break;
}
}
}
RTC_DATA_ATTR bool firstrun = true;
void productionSetup() {
Serial1.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
pRS485_1 = new RS485Class(Serial1, TX_PIN, RE_PIN, DE_PIN);
pModbusClient = new ModbusRTUClientClass(*pRS485_1);
pModbusClient->begin(9600, SERIAL_8N1);
Mcu.begin();
if(firstrun) {
LoRaWAN.displayMcuInit();
firstrun = false;
}
deviceState = DEVICE_STATE_INIT;
}
void productionLoop()
{
digitalWrite(LED_GREEN, HIGH);
switch( deviceState )
{
case DEVICE_STATE_INIT:
digitalWrite(LED_GREEN, LOW);
{
LoRaWAN.generateDeveuiByChipID();
LoRaWAN.init(loraWanClass,loraWanRegion);
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.displayJoining();
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
digitalWrite(LED_BLUE, HIGH);
{
LoRaWAN.displaySending();
Serial.println("sending");
prepareTxFrame( appPort );
LoRaWAN.send();
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
digitalWrite(LED_BLUE, LOW);
{
// Schedule next packet transmission
txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.displayAck();
LoRaWAN.sleep(loraWanClass);
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}

9
sketch/production.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _PRODUCTION_H_
#define _PRODUCTION_H_
void productionSetup();
void productionLoop();
#endif // _PRODUCTION_H_

View File

@ -1,100 +1,14 @@
#include "LoRaWan_APP.h"
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
#include "defines.h" #include "defines.h"
#include "config.h" #include "config.h"
#include <string.h> #include "production.h"
#include "configuration.h"
// from config.cpp // from config.cpp
extern config_t myConfig; extern config_t myConfig;
bool productionMode = false;
/*LoraWan channelsmask, default channels 0-7*/
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };
/*LoraWan Class, Class A and Class C are supported*/
DeviceClass_t loraWanClass = CLASS_A;
/*the application data transmission duty cycle. value in [ms].*/
uint32_t appTxDutyCycle = 15000;
/*ADR enable*/
bool loraWanAdr = true;
/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = true;
/* Application port */
uint8_t appPort = 2;
/*!
* Number of trials to transmit the frame, if the LoRaMAC layer did not
* receive an acknowledgment. The MAC performs a datarate adaptation,
* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
* to the following table:
*
* Transmission nb | Data Rate
* ----------------|-----------
* 1 (first) | DR
* 2 | DR
* 3 | max(DR-1,0)
* 4 | max(DR-1,0)
* 5 | max(DR-2,0)
* 6 | max(DR-2,0)
* 7 | max(DR-3,0)
* 8 | max(DR-3,0)
*
* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
* the datarate, in case the LoRaMAC layer did not receive an acknowledgment
*/
uint8_t confirmedNbTrials = 4;
RS485Class* pRS485_1;
ModbusRTUClientClass* pModbusClient;
/* Prepares the payload of the frame */
static void prepareTxFrame( uint8_t port )
{
Serial.println("modbus operation");
appDataSize = 0;
for (modbus_poll_t *slot = myConfig.modbus_poll_slots; slot->typ != 0; slot++) {
Serial.printf("Slot: %d, %d, %d\r\n", slot->typ, slot->id, slot->address);
switch (slot->typ) {
case HOLDING_REGISTERS:
{
long v;
if (appDataSize + sizeof(v) < LORAWAN_APP_DATA_MAX_SIZE) {
v = pModbusClient->holdingRegisterRead(slot->id, slot->address);
Serial.println(v);
memcpy(appData + appDataSize, &v, sizeof(v));
appDataSize += sizeof(v);
} else {
Serial.println("too much data for LoRaWAN packet");
}
}
break;
case INPUT_REGISTERS:
{
long v;
if (appDataSize + sizeof(v) < LORAWAN_APP_DATA_MAX_SIZE) {
v = pModbusClient->inputRegisterRead(slot->id, slot->address);
Serial.println(v);
memcpy(appData + appDataSize, &v, sizeof(v));
appDataSize += sizeof(v);
} else {
Serial.println("too much data for LoRaWAN packet");
}
}
break;
default:
Serial.println("unknown typ, doing nothing");
break;
}
}
}
void setup() { void setup() {
pinMode(LED_BLUE, OUTPUT); pinMode(LED_BLUE, OUTPUT);
@ -106,64 +20,20 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); configLoad();
pRS485_1 = new RS485Class(Serial1, TX_PIN, RE_PIN, DE_PIN);
pModbusClient = new ModbusRTUClientClass(*pRS485_1);
pModbusClient->begin(9600, SERIAL_8N1);
configInit(); if (productionMode) {
productionSetup();
Mcu.begin(); } else {
deviceState = DEVICE_STATE_INIT; configurationSetup();
}
} }
void loop() void loop()
{ {
digitalWrite(LED_GREEN, HIGH); if (productionMode) {
productionLoop();
switch( deviceState ) } else {
{ configurationLoop();
case DEVICE_STATE_INIT:
digitalWrite(LED_GREEN, LOW);
{
#if(LORAWAN_DEVEUI_AUTO)
LoRaWAN.generateDeveuiByChipID();
#endif
LoRaWAN.init(loraWanClass,loraWanRegion);
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
digitalWrite(LED_BLUE, HIGH);
{
Serial.println("sending");
prepareTxFrame( appPort );
LoRaWAN.send();
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
digitalWrite(LED_BLUE, LOW);
{
// Schedule next packet transmission
txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep(loraWanClass);
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
} }
} }

View File

@ -4,6 +4,6 @@ docker run \
-it \ -it \
--rm \ --rm \
-v $PWD:/home/arduino/project \ -v $PWD:/home/arduino/project \
registry.hottis.de/dockerized/build-env-arduino:0.29.0-9 \ registry.hottis.de/dockerized/build-env-arduino:0.29.0-10 \
bash bash