237 lines
7.2 KiB
C++
Raw Permalink Normal View History

2025-04-03 20:55:28 +02:00
#include <Arduino.h>
#include "ESP8266WiFi.h"
#include <shell.h>
#include <Wire.h>
#include <inttypes.h>
#include <Esp.h>
#include "gpio_funcs.h"
void scanWifi();
void scanI2c();
void cmdHandler(char* buf, int len) {
char* tok = strtok(buf, " ");
if (tok == NULL) return;
if (strncmp(tok, "help\0", 5) == 0) {
Serial.println("echo Toggle hard echo");
Serial.println("status Show device status");
Serial.println("set Update a setting value");
Serial.println("get Get a setting value");
Serial.println("list List all settings");
Serial.println("undo Reload NVRAM and discard changes");
Serial.println("commit Commit settings to NVRAM");
Serial.println("scan Scan wifi,i2c");
Serial.println("gpio Manipulate GPIO");
Serial.println("reset Reset (restart) the device");
return;
}
if (strncmp(tok, "status\0", 7) == 0) {
// char* status = strtok(NULL, " ");
Serial.printf("Hardware.: %s (%s) %s\n", BOARD, BOARD_VARIANT, BOARD_MCU);
Serial.printf("Serial...: %08x\n", system_get_chip_id());
Serial.printf("WiFi.....: %s (ssid:%s,ip:%s)\n", "connected", "HOtSpOt", "0.0.0.0");
Serial.printf("MQTT.....: %s (server:%s,user:%s)\n", "disconnected", "10.1.4.2", "d1");
return;
}
/*
echo on
echo off
echo "message"
*/
if (strncmp(tok, "echo\0", 5) == 0) {
char* echostate = strtok(NULL, " ");
if (echostate == NULL) {
Serial.println("E: Expected on or off");
} else {
if (strncmp(echostate, "on\0", 3) == 0) {
Serial.println("Echo on");
shell_echo(true);
} else if (strncmp(echostate, "off\0", 4) == 0) {
Serial.println("Echo off");
shell_echo(false);
} else {
}
}
return;
}
/*
config
config open GROUP
config set KEY VALUE
config get KEY
config commit
*/
if (strncmp(tok, "set\0", 4) == 0) {
char* arg1 = strtok(NULL, " ");
if (arg1 == NULL) {
Serial.println("E: Missing setting name");
} else {
char* arg2 = strtok(NULL, "\0");
if (arg2 == NULL) {
Serial.println("E: Missing setting value");
} else {
Serial.printf("set: %s = %s\n", arg1, arg2);
}
}
return;
}
/*
scan show usage
scan wifi
scan i2c
*/
if (strncmp(tok, "scan\0", 5) == 0) {
char* scantype = strtok(NULL, " ");
if (scantype == NULL) {
Serial.println("E: Missing scan type");
} else {
if (strncmp(scantype,"wifi\0",5) == 0) {
scanWifi();
} else if (strncmp(scantype,"i2c\0",4) == 0) {
scanI2c();
} else {
Serial.println("E: Invalid scan type");
}
return;
}
return;
}
/*
gpio show usage
gpio scan show all GPIO
gpio N show GPIO N
gpio N {in|out} set GPIO N direction
gpio N {high|low} set GPIO N state
*/
if (strncmp(tok, "gpio\0", 5) == 0) {
char* scantype = strtok(NULL, " ");
if (scantype == NULL) {
// Serial.println("E: Missing gpio operation");
gpioScan();
return;
} else {
if (strncmp(scantype,"set\0",4) == 0) {
char* gpiosetpin = strtok(NULL, " ");
if (NULL == gpiosetpin) {
Serial.println("E: missing pin");
return;
}
int dpin = atoi(gpiosetpin);
char* gpioset = strtok(NULL, " ");
if (NULL == gpioset) {
Serial.println("E: missing new state");
return;
}
if (strncmp(gpioset, "high\0", 5) == 0) {
digitalWrite(dpin, HIGH);
Serial.printf(" D%d => high\n", dpin);
} else if (strncmp(gpioset, "low\0", 4) == 0) {
digitalWrite(dpin, LOW);
Serial.printf(" D%d => low\n", dpin);
}
return;
} else if (strncmp(scantype,"get\0",4) == 0) {
char* gpiogetpin = strtok(NULL, " ");
if (NULL == gpiogetpin) {
Serial.println("E: missing pin");
return;
}
int dpin = atoi(gpiogetpin);
int getval = digitalRead(dpin);
Serial.printf(" D%d: %s\n", dpin, getval?"HIGH":"LOW");
} else if (strncmp(scantype,"mode\0",5) == 0) {
char* gpiomodepin = strtok(NULL, " ");
if (NULL == gpiomodepin) {
Serial.println("E: missing pin");
return;
}
int dpin = atoi(gpiomodepin);
char* gpiomode = strtok(NULL, " ");
if (NULL == gpiomode) {
Serial.println("E: missing mode");
return;
}
if (strncmp(gpiomode, "in\0", 3) == 0) {
pinMode(dpin, INPUT);
Serial.printf(" D%d -> input\n", dpin);
} else if (strncmp(gpiomode, "out\0", 4) == 0) {
pinMode(dpin, OUTPUT);
Serial.printf(" D%d -> output\n", dpin);
} else {
Serial.printf("E: invalid mode");
}
} else {
Serial.println("E: Invalid gpio operation");
}
return;
}
return;
}
/*
i2c
i2c scan same as 'scan i2c'
i2c write ADDR DATA..
i2c read ADDR LEN read LEN bytes
*/
Serial.println("E: Invalid command");
}
void scanI2c() {
byte error, address;
int nDevices;
Serial.println("Starting I2C scan...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.printf(" Found device at 0x%02x\n", address);
nDevices++;
} else if (error==4) {
Serial.printf(" Unknown error at 0x%02x\n", address);
}
}
Serial.printf("Scan complete (%d items)\n", nDevices);
}
void scanWifi() {
Serial.println("Starting WiFi scan...");
int numNetworks = WiFi.scanNetworks();
for (int i = 0; i < numNetworks; i++) {
Serial.printf(" %s rssi=%d bssid=%s ch=%d enc=%d\n", WiFi.SSID(i).c_str(), WiFi.RSSI(i), WiFi.BSSIDstr(i).c_str(), WiFi.channel(i), WiFi.encryptionType(i));
}
Serial.printf("Scan complete (%d items)\n", numNetworks);
}
void setup() {
Serial.begin(115200);
shell_init();
shell_prompt("cmd> ");
shell_callback(cmdHandler);
}
void loop() {
shell_loop();
}