// Test for GEVINO opto v1.2 / v1.3 / v1.4 // 07/09/2020 Giorgio Evangelista #include "gevino_opto_npn_io.h" #define __APN "AT+SAPBR=3,1,\"APN\",\"apn.fastweb.it\"" //#define APN apn.fastweb.it "AT+SAPBR=3,1,\"APN\",\"mobile.vodafone.it\"" void WaitForOk(String cWait); #define SIM808 #define SIM800L Serial String risposta; // Install https://github.com/sandeepmistry/arduino-CAN/Also from Arduino Library Manager // Change on MCP2515.h //#define MCP2515_DEFAULT_CS_PIN 10 //#define MCP2515_DEFAULT_INT_PIN -1 #include byte CanOk = 0; // LoRa #include const int csPin = 10; // LoRa radio chip select const int resetPin = 11; // LoRa radio reset const int irqPin = -1; // change for your board; must be a hardware interrupt pin // W5500 #include #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 199); //EthernetServer server(80); EthernetClient client; //// RTC //#include //#include //DS3231 clock; //RTCDateTime dt; // TEST_SD #include File myFile; SdFat SD; int TestResult; void setup() { testLeds(); SerialUSB.begin(115200); Serial1.begin(115200); // Serial.begin(115200); // Serial.println("AT"); // delay(100); // while(!SerialUSB) SerialUSB.println("If there are ETH must be connected to the LAN\n\n");; gevino_io_setup(); // TEST_SD - Write file on SD card. TestResult = SD.begin(SD_CS); if( TestResult ) SerialUSB.println("SD OK"); else SerialUSB.println("SD FAIL"); //// Test RTC // TestResult = clock.begin(); // TestResult = clock.isReady(); // if( TestResult ) SerialUSB.println("RTC OK"); // else SerialUSB.println("RTC FAIL"); // W5500 pinMode(11, OUTPUT); digitalWrite(11, LOW); delay(300); digitalWrite(11, HIGH); delay(300); Ethernet.init(10); TestResult = Ethernet.begin(mac); if( TestResult ){ SerialUSB.println("ETH OK"); SerialUSB.print("connecting to www.google.it ..."); // if you get a connection, report back via serial: if (client.connect("www.google.it", 80)) { SerialUSB.print("connected to "); SerialUSB.println(client.remoteIP()); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: www.google.it"); client.println("Connection: close"); client.println(); delay(1000); int len = client.available(); if (len > 0) { byte buffer[80]; if (len > 80) len = 80; client.read(buffer, len); SerialUSB.write(buffer, len); // show in the serial monitor (slows some boards) } } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } }else SerialUSB.println("ETH FAIL"); // if (!CAN.begin(500E3)) SerialUSB.println("CAN FAIL"); // else{ // SerialUSB.println("CAN Ok"); // CanOk = 1; // } //// LoRa at 433Mhz // if (!LoRa.begin(433E6)) SerialUSB.println("LoRa FAIL"); // else SerialUSB.println("LoRa OK"); // gevino_io_setup(); // LoRa change In10 (D9) on output. //// Test ESP // Serial.println("AT"); // delay(500); // Serial.println("ATI"); // delay(500); // Serial.println("AT+GMR"); //// Test SIM800 su Serial1 // Serial1.println("AT"); // delay(500); // Serial1.println("ATI"); #ifdef SIM808 // Change baud at SIM808 SerialUSB.println("Test SIM808"); Serial.begin(9600); Serial.println("AT"); WaitForOk("OK"); Serial.println("AT+IPR=0"); WaitForOk("OK"); Serial.println("AT&W"); WaitForOk("OK"); Serial.println("AT+CGPSPWR=1"); // Accendo GPS WaitForOk("OK"); Serial.println("AT+CPIN=?"); // Controllo Sim e Pin WaitForOk("OK"); SerialUSB.println("0-98 segnale, 0.7 As RXQUAL values"); Serial.println("AT+CSQ"); // Segnale WaitForOk("OK"); #endif sendMail(); } unsigned long TimerForSec; void loop() { byte tmp; if( In1 ) setLed; else resLed; if( In1 || In7 ) setOut1; else resOut1; if( In2 || In8 ) setOut2; else resOut2; if( In3 || In9 ) setOut3; else resOut3; if( In4 || In10 ) setOut4; else resOut4; if( In5 || In11 ) setOut5; else resOut5; if( In6 || In12 ) setOut6; else resOut6; while( Serial.available() ) SerialUSB.write(Serial.read()); while( Serial1.available() ) SerialUSB.write(Serial1.read()); while( SerialUSB.available()){ tmp = SerialUSB.read(); Serial.write(tmp); Serial1.write(tmp); } if( millis() - TimerForSec > 1000 ){ static byte ff; TimerForSec = millis(); if(ff){ SIM800L.println("AT+CGPSINF=0"); ff=0; }else{ SIM800L.println("AT+CGPSSTATUS?"); ff=1; } // if(CanOk){ // CAN.beginPacket(0x12); // CAN.write('h'); // CAN.write('e'); // CAN.write('l'); // CAN.write('l'); // CAN.write('o'); // CAN.endPacket(); // } } } //------------------------------------------------------------------------------------- void sendMail(void){ SerialUSB.println("Invio mail in corso "); SIM800L.println("AT+IPR=0"); WaitForOk("OK"); SIM800L.println("AT E1"); WaitForOk("OK"); SIM800L.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""); WaitForOk("OK"); SIM800L.println(__APN); WaitForOk("OK"); SIM800L.println("AT+SAPBR=1,1"); // open GPRS context WaitForOk("OK"); SIM800L.println("AT+SAPBR=2,1"); WaitForOk("OK"); if ( risposta.indexOf("+SAPBR: 1,1,")>0 ) SerialUSB.println("GPRS Connection Ok"); else SerialUSB.println("GPRS Connection Fail"); SIM800L.println("AT+EMAILCID=1"); //set email parameters WaitForOk("OK"); SIM800L.println("AT+EMAILTO=30"); WaitForOk("OK"); SIM800L.println("AT+SMTPSRV=\"mail.gevaelettronica.it\",25"); WaitForOk("OK"); SIM800L.println("AT+SMTPAUTH=1,\"allarme@gevaelettronica.it\",\"password\""); WaitForOk("OK"); SIM800L.println("AT+SMTPFROM=\"allarme@gevaelettronica.it\",\"allarmi\""); WaitForOk("OK"); SIM800L.println("AT+SMTPRCPT=0,0,\"g.evangelista@gevaelettronica.it\",\"Allarmi\""); WaitForOk("OK"); SIM800L.println("AT+SMTPSUB=\"Test Scheda con modem\""); WaitForOk("OK"); SIM800L.print("AT+SMTPBODY="); SIM800L.println(strlen("Test Scheda con modem")); WaitForOk("DOWNLOAD"); SIM800L.println("Test Scheda con modem"); SIM800L.println((char)26); WaitForOk("OK"); SIM800L.println("AT+SMTPSEND"); WaitForOk("+SMTPSEND: 1"); } //------------------------------------------------------------------------------------- void WaitForOk(String cWait) // Attende un OK o altro messaggio contenuto in cWait dal modem { int nTentativi=0; risposta=""; while(1) { nTentativi +=1; risposta += SIM800L.readString(); // Tests per interruzione ciclo attesa if (nTentativi>50 || risposta.indexOf(cWait)>0 || risposta.indexOf("ERROR")>0 || risposta.indexOf("404")>0 || risposta.indexOf("408")>0 || risposta.indexOf(".")>0) { SerialUSB.println(nTentativi+" "+risposta); break; } delay(50); } }