#include <WiFiManager.h>
#include <strings_en.h>
#include <wm_consts_en.h>
#include <wm_strings_en.h>
#include <wm_strings_es.h>
#include <esp_now.h>
#include <WiFi.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
const uint16_t WAIT_TIME = 1000;
const unsigned long MAX_IDLE_TIME = (25+15) * 60 * 1000;
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::ICSTATION_HW
#define MAX_DEVICES 2
#define CLK_PIN 18
#define DATA_PIN 23
#define CS_PIN 5
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
int iMinutes = 4;
bool bReached = false;
bool bOutOfServiceDisplayed = false;
unsigned long ulStartTime;
unsigned long ulEndTime;
unsigned long ulBusDelay;
unsigned long ulLastExecutionTime = 0;
uint8_t ucScrollSpeed = 25; // default frame delay value
textEffect_t eScrollEffect = PA_SCROLL_LEFT;
textPosition_t eScrollAlign = PA_LEFT;
uint16_t usScrollPause = 0;
#define BUF_SIZE 75
char acCurMessage[BUF_SIZE] = { "OUT OF SERVICE" };
const char* ssid = "BUS_STOP_1";
int iBusRSSI = 0;
int iStop2RSSI = 0;
int iStop1RSSI = 0;
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t abBusStop2Address[] = {0x30, 0xC9, 0x22, 0x28, 0x4E, 0xCC};
uint8_t abBusAddress[] = {0x30, 0xC9, 0x22, 0x28, 0x65, 0x80};
bool bStop1Receive = true; // Needs to be set true if you want to receive else set false.
bool bSendToStop2 = false; // Needs to set true if you want to send data to stop2 else false
bool bSendToBus = false; // Needs to be true if you want to send data to bus else false
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
// Variable to store the data received or data to be sent
char acData[32];
int iValue;
float fValue;
bool bFlag;
} struct_message;
// Create a struct_message called myData
struct_message stMyData;
esp_now_peer_info_t stPeerInfo1;
esp_now_peer_info_t stPeerInfo2;
// callback when data is sent
void OnDataSent1(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery to BusStop2 Success" : "Delivery to BusStop2 Fail");
}
void OnDataSent2(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery To Bus Success" : "Delivery To Bus Fail");
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
// Any variable that requires to take the value received needs to be assigned here.
memcpy(&stMyData, incomingData, sizeof(stMyData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Char: ");
Serial.println(stMyData.acData);
Serial.print("Int: ");
Serial.println(stMyData.iValue);
Serial.print("Float: ");
Serial.println(stMyData.fValue);
Serial.print("Bool: ");
Serial.println(stMyData.bFlag);
Serial.println();
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
P.begin();
P.print(String(iMinutes));
decrementTime();
// Set device as a Wi-Fi Station
WiFi.softAP(ssid,NULL);
WiFi.mode(WIFI_AP);
// Init ESP-NOW ( Do not modify these lines)
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
// Register peer
memcpy(stPeerInfo1.peer_addr, abBusStop2Address, 6);
stPeerInfo1.channel = 0;
stPeerInfo1.encrypt = false;
memcpy(stPeerInfo2.peer_addr, abBusAddress, 6);
stPeerInfo2.channel = 0;
stPeerInfo2.encrypt = false;
// Add peer
if (esp_now_add_peer(&stPeerInfo1) != ESP_OK){
Serial.println("Failed to add bus stop 2 peer");
return;
}
if (esp_now_add_peer(&stPeerInfo2) != ESP_OK){
Serial.println("Failed to add bus peer");
return;
}
// Add any code in setup after this.
}
void WiFiScanning(){
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
if(WiFi.SSID(i)=="BUS_STOP_2"){
iStop2RSSI = WiFi.RSSI(i);
}
if(WiFi.SSID(i)=="BUS_STOP_1"){
iStop1RSSI = WiFi.RSSI(i);
}
if(WiFi.SSID(i)=="BUS"){
iBusRSSI = WiFi.RSSI(i);
}
}
}
Serial.println("");
}
bool bReached = false;
bool bMove = true;
void loop() {
WiFiScanning();
if (iBusRSSI <= -75){
bReached = true;
}
else{
bReached = false;
}
if(bSendToStop2==true){
// Set values to send to stop 2. Can be done outside the if statement
strcpy(stMyData.acData, "Hi! Aditya");
stMyData.iValue = 100;
stMyData.fValue = 1.2698;
stMyData.bFlag = false;
esp_now_register_send_cb(OnDataSent1);
// Send message via ESP-NOW
esp_err_t result = esp_now_send(abBusStop2Address, (uint8_t *) &stMyData, sizeof(stMyData)); // Do not change this line until you need to use different struct variable
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
delay(1000); // This delay of 1 second is necessary otherwise you might send and receive erronius data.
if(bSendToBus == true){
// Set values to send to bus.
strcpy(stMyData.acData, "Hi! Ayush");
stMyData.iValue = 298;
stMyData.fValue = 1.2698;
stMyData.bFlag = false;
esp_now_register_send_cb(OnDataSent2);
// Send message via ESP-NOW
esp_err_t result = esp_now_send(abBusAddress, (uint8_t *) &stMyData, sizeof(stMyData)); // Do not change this line until you need to use different struct variable
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
delay(1000);
if(bStop1Receive){
// Do not change this code
esp_now_register_recv_cb(OnDataRecv);
bMove = stMyData.bFlag;
}
delay(1000);
unsigned long ulCurrentTime = millis();
//take bool reached as input depending on whther the bus has arrived or not
if (bReached & !bMove) { // it has reached and its not moving
ulLastExecutionTime = ulCurrentTime;
ulEndTime = millis();
ulBusDelay = (ulEndTime-ulStartTime)/60; //bus_delay in seconds
// data logging team can keep track of this (can be sent from this bus stop)
iMinutes = 00;
P.print(String(iMinutes));
delay(WAIT_TIME*20);
iMinutes = 15;
decrementTime();
bOutOfServiceDisplayed = false;
}
else {
if (!bOutOfServiceDisplayed && (ulCurrentTime - ulLastExecutionTime) >= MAX_IDLE_TIME) {
P.displayText(acCurMessage, eScrollAlign, ucScrollSpeed, usScrollPause, eScrollEffect, eScrollEffect);
bOutOfServiceDisplayed = true; // Set the flag to indicate "OUT OF SERVICE" has been displayed
}
}
}
void decrementTime() {
while(iMinutes > 0){
P.print(String(iMinutes));
delay(WAIT_TIME*60);
iMinutes--;
if(iMinutes == 0) ulStartTime = millis();
}
}