Skip to content

Instantly share code, notes, and snippets.

@fxprime
Last active March 6, 2021 09:45
Show Gist options
  • Save fxprime/0715218a7daf43cbf50af3b24b576e3e to your computer and use it in GitHub Desktop.
Save fxprime/0715218a7daf43cbf50af3b24b576e3e to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <Fsm.h>
#include <WiFi.h>
#include "macro.h"
#include "config.h"
#include "modules/siren.h"
#include "modules/switch_sensor.h"
#include "modules/smoke_sensor.h"
#include "modules/temp_humid.h"
#include "user_input.h"
#include "states.h"
#define NUMSLAVES 3
#define DEFAULT_UID 0
#define NORMPORT 2468
#define PREFIX_MAC 0xAE, 0xEE, 0x00, 0x00
char ssid[32] ;
char password[18] ;
uint8_t module_num;
uint8_t module_type[NUMSLAVES+1][2];
fime_t last_update[NUMSLAVES+1][2];
const uint16_t masterId = 0;
uint16_t thisId = DEFAULT_UID;
boolean isMaster = (thisId == masterId);
boolean verboseMode = true;
char node_name[30] = {};
void setup() {
Serial.begin(115200);
/* ----------------------------- ดึงพารามิเตอร์ ----------------------------- */
Serial.println("Parameter initing.");
if (!EEPROM.begin(1000)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
delay(100);
/* ----------------------------- ทดสอบ signature ---------------------------- */
int test_signature;
test_signature = EEPROM.readUInt(_signature_address);
Serial.printf("Signature = %d\n", test_signature);
if(test_signature == eeprom_signature) {
/* -------------------------------- Read uid -------------------------------- */
thisId = EEPROM.readUInt(_uid_address);
String name = EEPROM.readString(_node_name_address);
stpcpy(node_name, name.c_str());
String params = "Found old parameter :\n";
params += "uid=" + String(thisId) + "\n";
params += "name=" + String(node_name) + "\n";
Serial.println(params);
}else{
/* ---------- if not found let write first time using default value --------- */
EEPROM.writeUInt (_signature_address , eeprom_signature);
EEPROM.writeUInt (_uid_address , DEFAULT_UID);
EEPROM.writeString (_node_name_address , "unknown");
EEPROM.commit();
Serial.println("Not found old parameter. Using default.");
}
/* ----------------------- Update some parameter value ---------------------- */
isMaster = (thisId == masterId);
Serial.println("Parameter inited.");
/* --------------------------- ตั้งค่า WiFi ที่นี่ -------------------------- */
sprintf(ssid, "Kanuengnit_kiss2G");
sprintf(password, "3213213213");
/* --------------------------- ตั้งค่า WiFi อื่นๆ --------------------------- */
WiFi.mode(WIFI_STA);
WiFiSetInternalRate();
WiFiSetDontSleep();
WiFiSetMacAndBandwidth();
/* -------------------------- เชื่อมต่อ Accesspoint ------------------------- */
connectToAP();
}
void loop() {
/* ---------------------------- วนเช็คค่า Serial ---------------------------- */
if(Serial.available()) {
size_t bytes = Serial.available();
char buff[250];
Serial.readBytes(buff, bytes);
cmdParse(buff, bytes);
}
/* ---------------------------- วนเช็คค่า Network --------------------------- */
struct sockaddr_in sender = {};
socklen_t senlen = sizeof(_slave_addr);
uint8_t* buffer_recv = new uint8_t[1024];
if(!buffer_recv) return;
ssize_t bytes_recv = lwip_recvfrom_r(_sock, buffer_recv, 1024, 0, (struct sockaddr *) &sender, &senlen);
if (bytes_recv > 0) {
//Regain connection every time receive something.
Serial.printf("Data in size = %d\n", bytes_recv);
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &sender.sin_addr, str, INET_ADDRSTRLEN);
udp_in_handle(buffer_recv, bytes_recv, FROM_UDP);
}
delete[] buffer_recv;
time_t time_now = micros();
static time_t last_time = time_now;
if( time_now - last_time > 100000 && st == _slave_loop) {
last_time = time_now;
static int search_uid = 0;
static fime_t last_req = 0;
static fime_t last_try = 0;
fime_t cur_time = esp_timer_get_time();
if(_searchPhase == IDLE) {
if(search_uid == thisId) {
update_sensor();
search_uid ++;
}else{
last_req = cur_time;
last_try = cur_time;
_searchPhase = REQ;
setTarget(search_uid);
}
}else if(_searchPhase == REQ) {
if(cur_time - last_try > RETRY_EVERY) {
if(nodes[search_uid].last_update < last_req) {
sendRequest();
last_try = cur_time;
}
}
if(cur_time - last_req > REQ_TIMEOUT) {
_searchPhase = IDLE;
search_uid++;
search_uid = search_uid%(NUMSLAVES+1);
}
}
/* -------------------------------------------------------------------------- */
/* Check fire from nodes */
/* -------------------------------------------------------------------------- */
static bool firealarm = false;
bool emerswitch = false;
bool smoketoomuch = false;
bool temptoohigh = false;
bool resetalarm = false;
/* -- เช็คทุก Node ที่ได้รับข้อมูลโดยอิงเกณฑ์ตามชื่อโมดูลที่ต่อกับ Nodeนั้น - */
static fime_t last_firecheck = 0;
if(cur_time - last_firecheck > 100000) {
last_firecheck = cur_time;
for(int nodei=0;nodei<NUMSLAVES+1;nodei++) {
node_data_t node = nodes[nodei];
for(int midx=0;midx<node.module_num;midx++) {
module_t module = node.modules[midx];
if(!strcmp(module.module_name, "MQ2")) {
smoketoomuch = module.data_centi>100;
}else if(!strcmp(module.module_name, "Temp")) {
temptoohigh = module.data_centi>3000;
}else if(!strcmp(module.module_name, "emerswitch")) {
emerswitch = module.data_centi==1;
}else if(!strcmp(module.module_name, "resetswitch")) {
resetalarm = module.data_centi==1;
}
}
}
}
if(resetalarm) {
firealarm = false;
}
if(smoketoomuch || temptoohigh || emerswitch) {
firealarm = true;
}
siren_set(firealarm);
/* -------------------------------------------------------------------------- */
/* Check nodes valid */
/* -------------------------------------------------------------------------- */
static fime_t last_nodecheck = 0;
if(cur_time - last_nodecheck > 2000000) {
last_nodecheck = cur_time;
Serial.printf("Validity check\n");
for (int nodei = 0; nodei < NUMSLAVES+1; nodei++)
{
bool timeout = cur_time - nodes[nodei].last_update > 10000000;
bool notready = nodes[nodei].last_update==0;
if( timeout || notready) {
memset(&nodes[nodei], 0, sizeof(node_data_t));
Serial.printf(" - node %d not exist.\n", nodei);
}else{
Serial.printf(" - node %d ready.\n", nodei);
}
}
}
delay(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment