// Example for ZERO M0 GEVA board. // http://www.gevaelettronica.it/shop/index.php?id_category=3&controller=category&id_lang=1 // Tested with arduino.cc IDE 1.6.12 // install wifi library https://github.com/ekstrand/ESP8266wifi // install eeprom library https://github.com/cmaglie/FlashStorage // Enable example #define IO //#define TEST_MODEM_GSM // GSM - When modem blink slowly, press board reset. //#define ESP-01 // WiFi - Open with browser 192.168.4.1 //#define HC-Bluetooth // Bluetooth - Pair with PC, pin 1234 - open serial com, send n and f //#define TEST_SD #define ZERO // Otherwise M0 #ifdef TEST_MODEM_GSM #include #endif #ifdef TEST_SD #include File myFile; #endif #ifdef IO int Mors5 = A3; // Terminals M5 O int Mors6 = A2; // Terminals M6 O int Mors7 = A1; // Terminals M7 O int Mors8 = A0; // Terminals M8 O int Mors1 = 3; // Terminals M1 I/O #ifdef ZERO int Mors2 = 2; // Terminals M2 I/O - Zero .cc board #else int Mors2 = 4; // Terminals M2 I/O - M0 .org board #endif int Mors3 = 1; // Terminals M3 I/O int Mors4 = 0; // Terminals M4 I/O #endif #ifdef HC-Bluetooth char blueToothVal; //value sent over via bluetooth char lastValue; //stores last state of device (on/off) #endif void setup() { #ifdef TEST_MODEM_GSM Serial.begin(57600); // Remove PIN at the SIM delay(1000); Serial.write("AT\r\n"); // For AutoBaud delay(100); Serial.write("AT\r\n"); // For AutoBaud delay(100); Serial.write("AT+CMGF=1\r\n"); // Set SMS format to ASCII delay(100); Serial.write("AT+CMGS=\"3345787267\"\r\n"); // Send new SMS command and message number delay(100); Serial.write("TEST"); // Send SMS content delay(100); Serial.write((char)26); // Send Ctrl+Z / ESC to denote SMS message is complete delay(100); #endif #ifdef TEST_SD // Write file on SD card. SD.begin(10); myFile = SD.open("example.txt", FILE_WRITE); myFile.close(); #endif #ifdef ESP-01 Serial.begin(115200); sendData("AT\r\n",2000,1); // autobaud sendData("AT+RST\r\n",2000,1); // reset module sendData("AT+CWMODE=2\r\n",1000,1); // configure as access point sendData("AT+CIFSR\r\n",1000,1); // get ip address sendData("AT+CIPMUX=1\r\n",1000,1); // configure for multiple connections sendData("AT+CIPSERVER=1,80\r\n",1000,1); // turn on server on port 80 #endif #ifdef HC-07 Serial.begin(9600); pinMode(3,OUTPUT); // Mors1 #endif #ifdef IO // Define I/O pins pinMode(Mors1, OUTPUT); // sets M1 digital pin as output pinMode(Mors2, OUTPUT); // sets M2 digital pin as output pinMode(Mors3, OUTPUT); // sets M3 digital pin as output pinMode(Mors4, OUTPUT); // sets M4 digital pin as output pinMode(Mors5, INPUT); // sets M5 digital pin as input pinMode(Mors6, INPUT); // sets M6 digital pin as input pinMode(Mors7, INPUT); // sets M7 digital pin as input pinMode(Mors8, INPUT); // sets M8 digital pin as input #endif } void loop() { #ifdef HC-07 if(Serial.available()) {//if there is data being recieved blueToothVal=Serial.read(); //read it } if (blueToothVal=='n') {//if value from bluetooth serial is n digitalWrite(3,HIGH); //switch on LED if (lastValue!='n') Serial.println(F("LED is on")); //print LED is on lastValue=blueToothVal; } else if (blueToothVal=='f') {//if value from bluetooth serial is n digitalWrite(3,LOW); //turn off LED if (lastValue!='f') Serial.println(F("LED is off")); //print LED is on lastValue=blueToothVal; } delay(1000); #endif #ifdef ESP-01 if(Serial.available()) // check if the esp is sending a message { /* while(esp8266.available()) { // The esp has data so display its output to the serial window char c = esp8266.read(); // read the next character. Serial.write(c); } */ if(Serial.find("+IPD,")) { delay(1000); int connectionId = Serial.read()-48; // subtract 48 because the read() function returns // the ASCII decimal value and 0 (the first decimal number) starts at 48 String webpage = "

Hello

World!

"; String cipSend = "AT+CIPSEND="; cipSend += connectionId; cipSend += ","; cipSend +=webpage.length(); cipSend +="\r\n"; sendData(cipSend,1000,1); sendData(webpage,1000,1); webpage=""; cipSend = "AT+CIPSEND="; cipSend += connectionId; cipSend += ","; cipSend +=webpage.length(); cipSend +="\r\n"; sendData(cipSend,1000,1); sendData(webpage,1000,1); String closeCommand = "AT+CIPCLOSE="; closeCommand+=connectionId; // append connection id closeCommand+="\r\n"; sendData(closeCommand,3000,1); } } #endif #ifdef IO if(digitalRead(Mors5)) digitalWrite(Mors1, HIGH); else digitalWrite(Mors1, LOW); if(digitalRead(Mors6)) digitalWrite(Mors2, HIGH); else digitalWrite(Mors2, LOW); if(digitalRead(Mors7)) digitalWrite(Mors3, HIGH); else digitalWrite(Mors3, LOW); if(digitalRead(Mors8)) digitalWrite(Mors4, HIGH); else digitalWrite(Mors4, LOW); #endif } #ifdef ESP-01 String sendData(String command, const int timeout, boolean debug) { String response = ""; Serial.print(command); // send the read character to the esp8266 long int time = millis(); while( (time+timeout) > millis()) { while(Serial.available()) { // The esp has data so display its output to the serial window char c = Serial.read(); // read the next character. response+=c; } } if(debug) { // Serial999.print(response); } return response; } #endif