// TEST IO for GEVINO civile v 1.1 // 11/09/2018 Giorgio Evangelista // Install "Arduino SAMD board" // Use Arduino Zero Native Port // Or add gevino device (See "Arduino IDE" folder) // github.com/ekstrand/ESP8266wifi // install eeprom library https://github.com/cmaglie/FlashStorage // Install SdFat #include "gevino_civile_io.h" #define SIM800L Serial3 String risposta; #include #include "SdFat.h" SdFat sd; // RTC #include #include DS3231 ck; RTCDateTime dt; // W5500 //#include #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 199); EthernetServer server(80); String a; int tmp; unsigned long Timer; void setup() { Serial.begin(115200); // ESP01 WiFi Serial1.setTimeout(300); Serial1.begin(57600); // RX485 Serial2.begin(2400); // M-BUS Serial3.begin(115200); // SIM8000 Serial3.setTimeout(300); SerialUSB.begin(115200); SerialUSB.println("If there are ETH must be connected to the LAN\n\n");; testLeds(); gevino_io_setup(); // Important that it is after Serial2/3.begin() // W5500 Ethernet.init(5); if( Ethernet.begin(mac) ) SerialUSB.println("ETH OK"); else SerialUSB.println("ETH FAIL"); // Test RTC ck.begin(); if( ck.isReady() ) SerialUSB.println("RTC OK"); else SerialUSB.println("RTC FAIL"); // Copy f.bin on firmware.bin SdFile myOrigFile, myDestFile; // if( sd.begin(12, SD_SCK_MHZ(25) ) ) if( sd.begin(12, SPI_HALF_SPEED) ){ SerialUSB.println("SD Done"); if (sd.exists("f.bin")){ SerialUSB.println("Copy f.bin on firmware.bin"); if( myOrigFile.open("f.bin", FILE_READ) && myDestFile.open("firmware.bin", FILE_WRITE)) { size_t n; uint8_t buf[64]; while ((n = myOrigFile.read(buf, sizeof(buf))) > 0) { myDestFile.write(buf, n); } myOrigFile.close(); myDestFile.close(); SerialUSB.println(" Copy Done"); n = 0; do{ resLed; resK1; resK2; resK3; resK4; resK5; resK6; resK7; delay(100); setLed; setK1; setK2; setK3; setK4; setK5; setK6; setK7; delay(100); }while( ++n < 10); delay(500); }else SerialUSB.println("Copy Not Done"); } }else SerialUSB.println("SD Fail"); setResSim800; delay(300); resResSim800; delay(3000); SerialUSB.println("SIM800"); Serial3.println("AT"); // For AutoBaud delay(1000); WaitForOk("OK"); Serial3.println("ATI"); delay(1000); WaitForOk("OK"); Serial3.println("AT+CMEE=2"); delay(1000); WaitForOk("OK"); Serial3.println("AT+cpin?"); WaitForOk("OK"); Serial.println("AT"); // For AutoBaud SerialUSB.println("ESP01"); Timer = millis(); while(millis() - Timer < 500) SerialCopy(); Serial.println("AT+GMR"); Timer = millis(); while(millis() - Timer < 500) SerialCopy(); sendMail(); } void loop() { SerialCopy(); if( In1 ) setLed; else resLed; if( In1 ) setK2; else resK2; if( In2 ) setK3; else resK3; if( In3 ) setK4; else resK4; if( In4 ) setK5; else if(!In1) resK5; if( In3 ) setK6; else if(!In1) resK6; if( In2 ) setK7; else if(!In1) resK7; if( In1 ){ setK1; setK6; setK7; } else resK1; if (SERCOM0->USART.INTFLAG.bit.TXC){ //Remove Tx Enable when the last character has been sent out. res_RS485_TxEn; SERCOM0->USART.INTFLAG.bit.TXC = 1; } } void SerialCopy( void ){ byte tmp; if( Serial.available() ) SerialUSB.write(Serial.read()); if( Serial1.available() ) SerialUSB.write(Serial1.read()); if( Serial2.available() ) SerialUSB.write(Serial2.read()); if( Serial3.available() ) SerialUSB.write(Serial3.read()); if( SerialUSB && SerialUSB.available() ) { // Send USB to SIM800 and RS485 tmp = SerialUSB.read(); set_RS485_TxEn; Serial.write(tmp); Serial1.write(tmp); Serial2.write(tmp); Serial3.write(tmp); } } //------------------------------------------------------------------------------------- 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); } } //------------------------------------------------------------------------------------- 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("AT+SAPBR=3,1,\"APN\",\"mobile.vodafone.it\""); WaitForOk("OK"); SIM800L.println("AT+SAPBR=1,1"); // open GPRS context WaitForOk("OK"); SIM800L.println("AT+EMAILCID=1"); //set email parameters WaitForOk("OK"); SIM800L.println("AT+EMAILTO=30"); WaitForOk("OK"); SIM800L.println("AT+SMTPSRV=\"mail.gardiol.org\",25"); WaitForOk("OK"); SIM800L.println("AT+SMTPAUTH=1,\"allarme@gevaelettronica.it\",\"AllarMEVA02=\""); WaitForOk("OK"); SIM800L.println("AT+SMTPFROM=\"allarme@gevaelettronica.it\",\"allarmi\""); WaitForOk("OK"); SIM800L.println("AT+SMTPRCPT=0,0,\"x-giorgio-x@hotmail.com\",\"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"); }