Created
March 16, 2024 04:22
-
-
Save jaggzh/7d16c633e8ef42ecfdfd790983083110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _NETDATA_SETTINGS_H | |
#define _NETDATA_SETTINGS_H | |
// *********************** | |
// ** IMPORTANT | |
// *********************** | |
// You need to #define NETDATA_SETTINGS_MAIN at least | |
// in one place that's including this file, or otherwise | |
// declare those two char [] arrays: stmag and enmag | |
// This file is for setting your network MAGIC Sequences | |
// for the starts/ends of data bundles | |
// See MAGIC_ST, MAGIC_EN, etc. | |
// Define this in the .c where you want variables to be declared | |
// (Before including this .h) | |
// (Ie. do not define in other .c files so the variables | |
// end up being externs) | |
// Example: #define NETDATA_SETTINGS_MAIN | |
// You can see, below, the externs | |
//#define MAGICBUF_TEST | |
// Slow down how often we test for TCP | |
// (and the reconnections) | |
#define US_NETDATA_TEST 2000000 | |
#define NET_MAXLEN 1000 // Ideally, the max packet size before your | |
// MCU will fail on them | |
// 'Magic' start/end sequences surrounding sensor bundles | |
// * array versions are used so sizeof() works directly | |
// * string versions are included mostly only for tests | |
// where you might want to concatenate them (see bottom of | |
// magicbuf.c) | |
#ifndef MAGICBUF_TEST | |
#define MAGIC_ST { 0375, 003, 0374 } // start packet bundle magic | |
#define MAGIC_EN { 0371, 004, 0372 } // end | |
#define MAGIC_ST_STR "\375\003\374" // Some might like to use this | |
#define MAGIC_EN_STR "\371\004\372" // in string concatenation | |
// ^ but otherwise the _STR versions are only used in the magicbuf | |
// test main() routine, and aren't needed unless you use them somewhere | |
#else | |
#define MAGIC_ST { 0101, 0132, 0102 } // start packet bundle magic | |
#define MAGIC_EN { 0104, 0131, 0103 } // end | |
//#define MAGIC_ST_STR "AZB" // Some might like to use this | |
//#define MAGIC_EN_STR "DYC" // in string concatenation | |
// ^ but otherwise the _STR versions are only used in the magicbuf | |
// test main() routine, and aren't needed unless you use them somewhere | |
#endif | |
#ifdef NETDATA_SETTINGS_MAIN | |
uint8_t stmag[]=MAGIC_ST; | |
uint8_t enmag[]=MAGIC_EN; | |
#else | |
extern uint8_t stmag[]; | |
extern uint8_t enmag[]; | |
#endif | |
//static_assert(sizeof(stmag)!= sizeof(enmag), "Let's keep MAGIC_ST and MAGIC_EN the same length"); | |
//static_assert(sizeof((uint8_t[])MAGIC_ST) != sizeof((uint8_t[])MAGIC_ST), "Let's keep MAGIC_ST and MAGIC_EN the same length"); | |
#define MAGIC_SIZE sizeof((uint8_t [])MAGIC_ST) | |
// Additional FULL net package info | |
// Must match client .ino | |
#define SAMPLE_SIZE (4+2) | |
#define PAKTYPE_SIZE 1 | |
#define PAKTYPE_TYPE uint8_t | |
// packet/bundle types: | |
#define PAK_T_DATA 1 | |
#define PAK_T_BTN1 2 | |
#define PAK_T_BTN2 3 | |
// Button | |
#define BTNPAK_SIZE (MAGIC_SIZE*2 + PAKTYPE_SIZE) | |
// For streaming data: | |
/* #define MAX_PACKETS (NET_MAXLEN/SAMPLE_SIZE) // fit within ESP's packet (524?) */ | |
// For individual single-packet events like buttons | |
#define MAX_PACKETS 1 | |
// Main data | |
#define FULLPAK_SIZE (MAGIC_SIZE*2 + PAKTYPE_SIZE + SAMPLE_SIZE*MAX_PACKETS) | |
// ^ this isn't fully accurate, if we were precise with the NET_MAXLEN, | |
// since we don't include the magic and stuff | |
#define PAKFULL_MAGIC_OFFSET (0) // start of buffer | |
#define PAKFULL_TYPE_OFFSET (MAGIC_SIZE) // right after magic | |
#define PAKFULL_DATA_OFFSET (MAGIC_SIZE + PAKTYPE_SIZE) // after magic and type | |
// ^ this is how far into the network package.. where the sensor data begins | |
#endif // _NETDATA_SETTINGS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctype.h> | |
#define _IN_NETDATA_C | |
#include "netdata.h" | |
#define NETDATA_SETTINGS_MAIN // declares char[] stmag and enmag | |
#include "netdata-settings.h" | |
#include "wifi_config.h" | |
#include "wifi.h" | |
#include "serialize.h" | |
#include "printutils.h" | |
WiFiClient server; | |
unsigned long us_last_nettest=micros(); | |
// Full package to send out to net | |
// Includes: start and end MAGIC | |
// package type | |
// data | |
uint8_t ecg_fullpackage[FULLPAK_SIZE+1]; // +1 for safety. it's unused. | |
// \/ Points to where the data actually starts | |
uint8_t *ecg_netdata = ecg_fullpackage + MAGIC_SIZE + PAKTYPE_SIZE; | |
int nextpacketi=0; | |
// Full button package | |
struct ecg_btn1_package { | |
uint8_t magic_st[MAGIC_SIZE] = MAGIC_ST; | |
PAKTYPE_TYPE paktype[PAKTYPE_SIZE] = { PAK_T_BTN1 }; | |
uint8_t magic_en[MAGIC_SIZE] = MAGIC_EN; | |
} ecg_btn1_package; | |
struct ecg_btn2_package { | |
uint8_t magic_st[MAGIC_SIZE] = MAGIC_ST; | |
PAKTYPE_TYPE paktype[PAKTYPE_SIZE] = { PAK_T_BTN2 }; | |
uint8_t magic_en[MAGIC_SIZE] = MAGIC_EN; | |
} ecg_btn2_package; | |
void setup_netdata() { | |
static_assert(sizeof(stmag) == sizeof(enmag), "Let's keep MAGIC_ST and MAGIC_EN the same length"); | |
static_assert(PAKTYPE_SIZE == 1, "PAKTYPE_SIZE should be 1, or you'll need to modify the data sending to encode it for the network"); | |
} | |
void netdata_add(uint16_t v) { // call to add value to send | |
/* packets[nextpacketi].us = micros(); */ | |
/* packets[nextpacketi].val = v; */ | |
// void packi16(unsigned char *buf, unsigned int i) | |
// void packi32(unsigned char *buf, unsigned long int i) | |
uint32_t us; | |
uint16_t testval; | |
/* #warning "netdata_add() is disabled" */ | |
/* return; */ | |
//us = micros(); | |
us = millis(); | |
/* us = *((uint32_t *)"abcd"); */ | |
/* v = *((uint16_t *)"yz"); */ | |
/* us = 123456; */ | |
/* v = 45678; */ | |
packi32(ecg_netdata + (nextpacketi*SAMPLE_SIZE), us); | |
packi16(ecg_netdata + (nextpacketi*SAMPLE_SIZE) + 4, v); | |
/* dbsp(" u:"); */ | |
/* dbsp(packets[nextpacketi].us); */ | |
/* dbsp(" v:"); */ | |
/* dbsp(packets[nextpacketi].val); */ | |
/* dbsp(". "); */ | |
nextpacketi++; | |
if (nextpacketi >= MAX_PACKETS) { | |
netdata_send(); | |
nextpacketi=0; | |
} | |
} | |
void netdata_send_btn1() { | |
#warning "server.connected() might be unreliable. Disabling for now." | |
if (!server.connected()) { | |
spl(F("TCP not connected. Button not sent")); | |
} else { | |
/* server.write((uint8_t *)(&ecg_btn1_package), BTNPAK_SIZE); */ | |
spl("Sending <b1-1>"); | |
int rc=server.write("<b1-1>", 5); | |
server.flush(); | |
sp(F("BTN1 sent (")); | |
sp(rc); | |
spl(F(" bytes)")); | |
} | |
} | |
void netdata_send_btn2() { | |
if (0 && !server.connected()) { | |
spl(F("TCP not connected. Button not sent")); | |
} else { | |
spl("Sending <b2-1>"); | |
/* server.write((uint8_t *)(&ecg_btn2_package), BTNPAK_SIZE); */ | |
int rc=server.write("<b2-1>", 5); | |
server.flush(); | |
sp(F("BTN2 sent (")); | |
sp(rc); | |
spl(F(" bytes)")); | |
} | |
} | |
void netdata_send() { | |
#warning "We're not presently using netdata_send()" | |
return; | |
if (!server.connected()) { | |
// don't bother sending out msg if wifi's down, since it | |
// should already be complaining through serial: | |
spl(F("TCP not connected. Losing data")); | |
} else { | |
spl(F("netadata_send() called. Not sure why; we're only doing events.")); | |
return; | |
/* dbsp("[>"); */ | |
/* dbsp(PACKETCNT); */ | |
/* dbsp("="); */ | |
/* dbsp(sizeof(packets)); */ | |
/* dbsp("b "); */ | |
memcpy(ecg_fullpackage + PAKFULL_MAGIC_OFFSET, stmag, MAGIC_SIZE); | |
// \/ we static_assert()ed that this is one byte: | |
ecg_fullpackage[PAKFULL_TYPE_OFFSET] = PAK_T_DATA; | |
//memcpy(ecg_fullpackage + PAKFULL_DATA_OFFSET, stmag, sizeof(stmag)); | |
// ^ data should already be in place from ecg_netdata pointing there | |
// \/ here we have to add magic to the end: | |
memcpy(ecg_fullpackage + PAKFULL_DATA_OFFSET + (nextpacketi*SAMPLE_SIZE), | |
enmag, MAGIC_SIZE); | |
/* No longer do as separate writes: | |
server.write(stmag, sizeof(stmag)); | |
server.write(ecg_netdata, sizeof(ecg_netdata)); | |
server.write(enmag, sizeof(enmag)); */ | |
server.write(ecg_fullpackage, | |
PAKFULL_DATA_OFFSET + (nextpacketi*SAMPLE_SIZE) + MAGIC_SIZE); | |
/* dbspl(""); */ | |
dbsp("Data size: "); | |
dbspl(PAKFULL_DATA_OFFSET + (nextpacketi*SAMPLE_SIZE) + MAGIC_SIZE); | |
//dbspl(sizeof(ecg_netdata) + sizeof(stmag) + sizeof(enmag)); | |
//dbspl(""); | |
} | |
} | |
void netdata_server_reconnect_test() { | |
unsigned long us_cur = micros(); | |
if (us_cur - us_last_nettest > US_NETDATA_TEST) { | |
us_last_nettest = us_cur; | |
if (!server.connected()) { | |
spl("TCP Connecting..."); | |
if (server.connect(DATA_HOST, DATA_PORT)) { | |
spl(" TCP Connected! Logging in..."); | |
int rc = server.write("login 123456\n", 13); | |
sp(" TCP Hopefully login worked, but we're moving on now... (Wrote "); | |
sp(rc); | |
spl(" bytes)"); | |
server.setNoDelay(true); | |
} | |
spl("/TCP Connecting"); | |
} else { | |
} | |
} | |
} | |
void loop_netdata() { | |
if (!(wifi_connflags & WIFI_FLAG_CONNECTED)) { // only bother if wifi connected | |
spl("WIFI not connected"); | |
} else { | |
/* netdata_server_reconnect_test(); */ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _NETDATA_H | |
#define _NETDATA_H | |
#include <stdint.h> // for uint8_t etc. | |
#include <assert.h> // for static_assert(), for sizeof() in #if | |
void setup_netdata(); // once in setup() | |
void loop_netdata(); // each loop() probably.. maybe. | |
void netdata_add(uint16_t v); // call to add value to send | |
void netdata_send(); | |
void netdata_send_btn1(); | |
void netdata_send_btn2(); | |
void netdata_server_reconnect_test(); | |
#endif // _NETDATA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
/* #include <Arduino.h> */ | |
/* #include <WiFiGeneric.h> */ | |
#include <WiFiClient.h> | |
#define PLOT_TO_SERIAL | |
#define WIFI_CONFIG_GET_IPS | |
#define __WIFI_CPP | |
#include "wifi_config.h" | |
#define _IN_WIFI_CPP | |
#include "wifi.h" | |
#include "printutils.h" | |
#include "netdata.h" | |
uint16_t wifi_connflags = 0; | |
/* WiFiEventHandler wifiConnectHandler; */ | |
/* WiFiEventHandler wifiDisconnectHandler; */ | |
/* WiFiEventHandler wifiGotIPHandler; */ | |
void loop_wifi(void) { | |
/* long rssi = WiFi.RSSI(); */ | |
/* unsigned long cur_millis = millis(); */ | |
/* static unsigned long last_wifi_strength = cur_millis; */ | |
/* if (cur_millis - last_wifi_strength > 500) { */ | |
/* last_wifi_strength = cur_millis; */ | |
/* sp("WiFi strength: "); */ | |
/* spl(rssi); */ | |
/* } */ | |
} | |
void setup_wifi(void) { | |
WiFi.mode(WIFI_STA); | |
WiFi.config(ip, gw, nm); | |
/* WiFi.setOutputPower(20.5); // 0 - 20.5 (multiples of .25) */ | |
spl(F("Connecting to wife (WiFi.begin())...")); | |
WiFi.onEvent(onWifiConnect, ARDUINO_EVENT_WIFI_STA_CONNECTED); | |
WiFi.onEvent(onWifiDisconnect, ARDUINO_EVENT_WIFI_STA_DISCONNECTED); | |
WiFi.onEvent(onWifiGotIP, ARDUINO_EVENT_WIFI_STA_GOT_IP); | |
WiFi.setAutoReconnect(true); | |
WiFi.persistent(true); // reconnect to prior access point | |
WiFi.begin(ssid, password); | |
spl(F("We're also going to wait until WL_CONNECTED")); | |
/* while (WiFi.waitForConnectResult() != WL_CONNECTED) { */ | |
/* //spl(F("Conn. fail! Rebooting...")); */ | |
/* delay(500); */ | |
/* spl(F("Conn. fail!")); */ | |
/* //ESP.restart(); */ | |
/* } */ | |
WiFi.setAutoReconnect(true); | |
WiFi.persistent(true); // reconnect to prior access point | |
wifi_connflags = WIFI_FLAG_CONNECTED; | |
} | |
void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { | |
spl(F("EVENT: IP established sucessfully.")); | |
sp(F("IP address: ")); | |
spl(WiFi.localIP()); | |
wifi_connflags = WIFI_FLAG_CONNECTED; | |
} | |
void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info) { | |
spl(F("EVENT: Disconnected from Wi-Fi. Auto-reconnect should happen.")); | |
/* WiFi.disconnect(); */ | |
/* WiFi.begin(ssid, password); */ | |
wifi_connflags = 0; | |
} | |
void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) { | |
long rssi = WiFi.RSSI(); | |
sp(F("EVENT: Connected to Wi-Fi sucessfully. Strength: ")); | |
spl(rssi); | |
spl("Connecting to TCP server..."); | |
netdata_server_reconnect_test(); | |
} | |
// Optional call to use if trying to requiring wifi during setup() | |
// Wait max of passed seconds for wifi | |
// Returns flags immediately upon success (eg. WIFI_FLAG_CONNECTED) | |
// Return flags of 0 means NOT connected for timeout period | |
uint16_t setup_wait_wifi(unsigned int timeout_s) { | |
unsigned long mil = millis(); | |
bool ret; | |
while (((millis() - mil)/1000) < timeout_s) { | |
ret = loop_check_wifi(); // after 3s this fn will start printing to serial | |
if (ret) return ret; | |
delay(99); | |
} | |
return 0; | |
} | |
uint16_t loop_check_wifi() { | |
static int connected=false; | |
unsigned long cur_millis = millis(); | |
static unsigned long last_wifi_millis = cur_millis; | |
/* static unsigned long last_connect_millis = 0; */ | |
static unsigned long last_reconnect_millis = 0; | |
if (cur_millis - last_wifi_millis < 2000) { | |
return WIFI_FLAG_IGNORE; | |
} else { | |
last_wifi_millis = cur_millis; | |
if (WiFi.status() == WL_CONNECTED) { | |
if (!connected) { // only if we toggled state | |
connected = true; | |
/* last_connect_millis = cur_millis; */ | |
sp(F("Just connected to ")); | |
sp(ssid); | |
sp(F(". IP: ")); | |
spl(WiFi.localIP()); | |
WiFi.setAutoReconnect(true); | |
WiFi.persistent(true); // reconnect to prior access point | |
return (WIFI_FLAG_CONNECTED | WIFI_FLAG_RECONNECTED); | |
} else { | |
return WIFI_FLAG_CONNECTED; | |
} | |
} else { | |
if (!connected) { | |
#ifndef PLOT_TO_SERIAL | |
sp(F("Not connected to wifi. millis=")); | |
sp(cur_millis); | |
sp(F(", cur-last=")); | |
spl(cur_millis - last_wifi_millis); | |
#endif | |
if (cur_millis - last_reconnect_millis > MAX_MS_BEFORE_RECONNECT) { | |
#ifndef PLOT_TO_SERIAL | |
spl(F(" Not connected to WiFi. Reconnecting (disabled)")); | |
#endif | |
last_reconnect_millis = cur_millis; | |
// WiFi.reconnect(); | |
} | |
} else { // only if we toggled state | |
connected=false; | |
spl(F("Lost WiFi connection. Will try again.")); | |
} | |
} | |
} | |
return 0; // not connected | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __WIFI_CONFIG_H | |
#define __WIFI_CONFIG_H | |
#include <ESP8266WiFi.h> | |
#define MDNS_NAME "LanDevice" | |
#define SSID_NAME "YourSSID" | |
#define SSID_PW "YourPASSWORD" | |
// The below might not be implemented | |
#define WEBUPDATE_USER "webuser" | |
#define WEBUPDATE_PW "webpw" | |
#define MAX_MS_BEFORE_RECONNECT 4500 | |
// Server to connect to for logging | |
// (this is not our MCU IP. That's below.) | |
#define DATA_HOST "192.168.1.4" | |
#define DATA_ARDUINO_IP (192,168,1,4) | |
#define DATA_PORT 1234 | |
#define DATA_PATH "/" | |
#ifdef __WIFI_CPP | |
const char *ssid = SSID_NAME; | |
//#define SSPW {33+22, 3, 62+22, 4+2, 6+129, 0} | |
char password[] = SSID_PW; | |
const char *update_user = WEBUPDATE_USER; // HTTP auth user for OTA http update | |
const char *update_pw = WEBUPDATE_PW"; // HTTP auth password | |
#else // like: ifndef __MAIN_INO__ | |
extern const char *ssid; | |
extern char password[]; | |
extern const char *update_user; // HTTP auth user for OTA http update | |
extern const char *update_pw; // HTTP auth password | |
#endif | |
#ifdef WIFI_CONFIG_GET_IPS | |
IPAddress ip(192, 168, 1, 14); | |
IPAddress gw(192, 168, 1, 1); | |
IPAddress nm(255, 255, 255, 0); | |
#endif | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment