Created
July 30, 2021 02:29
-
-
Save bungernut/bb80d2c5f84e63f62e6de2422be67f18 to your computer and use it in GitHub Desktop.
MQTT upload simulated trigger scope waveforms tester
This file contains hidden or 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 "WPA_secrets.h" | |
#include <Arduino.h> | |
#include <WiFi.h> | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <time.h> | |
#include <NTPClient.h> | |
#include <WiFiUdp.h> | |
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
const char* ssid = SECRET_SSID; | |
const char* password = SECRET_WIFI_PASS; | |
IPAddress local_IP(192, 168, 1, 116); | |
IPAddress gateway(192, 168, 1, 1); | |
IPAddress subnet(255, 255, 255, 0); | |
IPAddress primaryDNS(8, 8, 8, 8); //optional | |
IPAddress secondaryDNS(8, 8, 4, 4); //optional | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP, "pool.ntp.org"); | |
struct tm utc; | |
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h 135x240 pix | |
WiFiClient client; | |
#define ARB_SERVER "192.168.1.142" | |
#define ARB_SERVERPORT 1883 // use 8883 for SSL | |
//#define ARB_USERNAME "TestUser" | |
//#define ARB_PW "TestUser" | |
//Adafruit_MQTT_Client mqtt(&client, ARB_SERVER, ARB_SERVERPORT, ARB_USERNAME, ARB_PW); | |
Adafruit_MQTT_Client mqtt(&client, ARB_SERVER, ARB_SERVERPORT); | |
#define ARB_FEED "/feeds/arb_packet" | |
Adafruit_MQTT_Publish ap = Adafruit_MQTT_Publish(&mqtt, ARB_FEED); | |
const uint16_t DACNUM =4; | |
const uint16_t WFLEN =1000; | |
uint32_t trigNum; | |
uint32_t currMillis; | |
typedef union{ | |
struct __attribute__((__packed__)){ // packed to eliminate padding for easier parsing. | |
uint16_t data_array[WFLEN]; | |
uint16_t det_num; | |
uint32_t timestamp_ms; | |
//char timestamp; //YYMMDDHHMMSS | |
uint32_t trig_num; | |
}s; | |
uint8_t raw[sizeof(s)]; // For publishing | |
} packet_t; | |
void setup() { | |
/* Setup Serial */ | |
Serial.begin(115200); | |
/* Setup WiFi */ | |
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { | |
Serial.println("STA Failed to configure"); | |
} | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected!"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("ESP Mac Address: "); | |
Serial.println(WiFi.macAddress()); | |
Serial.print("Subnet Mask: "); | |
Serial.println(WiFi.subnetMask()); | |
Serial.print("Gateway IP: "); | |
Serial.println(WiFi.gatewayIP()); | |
Serial.print("DNS: "); | |
Serial.println(WiFi.dnsIP()); | |
timeClient.begin(); | |
Serial.print("NTP Begin: "); | |
timeClient.update(); | |
Serial.println(timeClient.getFormattedTime()); | |
/* Setup Screen */ | |
tft.init(); | |
tft.setRotation(2); | |
tft.fillScreen(TFT_BLACK); | |
tft.setTextColor(TFT_WHITE, TFT_BLACK); | |
delay(500); | |
} | |
packet_t daqEvent; | |
void loop() { | |
MQTT_connect(); | |
fillEvent(); | |
if (! ap.publish(daqEvent.raw, sizeof(packet_t))) | |
Serial.println(F("Publish Failed.")); | |
else { | |
Serial.println(F("Publish Success!")); | |
delay(500); | |
} | |
delay(10000); | |
} | |
void fillEvent() { | |
int amp = random(100,1000); | |
for(int i=0;i<WFLEN;i++){ | |
if (i<50){ | |
daqEvent.s.data_array[i] = random(0,5); | |
} | |
else { | |
daqEvent.s.data_array[i] = amp*exp(-(float)(i-50)/200.); | |
} | |
} | |
trigNum = trigNum +1; | |
daqEvent.s.trig_num = trigNum; | |
daqEvent.s.timestamp_ms = millis(); | |
daqEvent.s.det_num = DACNUM; | |
} | |
void MQTT_connect() { | |
int8_t ret; | |
// Stop if already connected. | |
if (mqtt.connected()) { | |
return; | |
} | |
Serial.print(F("Connecting to MQTT... ")); | |
uint8_t retries = 3; | |
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println(F("Retrying MQTT connection in 5 seconds...")); | |
mqtt.disconnect(); | |
delay(5000); // wait 5 seconds | |
retries--; | |
if (retries == 0) { | |
// basically die and wait for WDT to reset me | |
while (1); | |
} | |
} | |
Serial.println(F("MQTT Connected!")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment