Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************************
- Lille til pin2 fra venstre og rød pin1 på wifi
- Læs serial, så skal de byttes om og gns og rst skal forbindes på arduini
- **************************************************************************/
- // esp8266 module
- #include <SoftwareSerial.h>
- //#define esp8266 Serial2
- SoftwareSerial esp8266(0, 1); // RX | TX
- #define CH_PD 7
- #define speed8266 9600 // This is the speed that worked with my ESP8266
- #define DEBUG false
- // oled module
- #include <SPI.h>
- #include <Wire.h>
- // #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- #if (SSD1306_LCDHEIGHT != 32)
- #error("Height incorrect, please fix Adafruit_SSD1306.h!");
- #endif
- // pumps
- int i;
- int pump = 2;
- char t;
- void setup()
- {
- // PUMP SECTION
- pinMode(pump, OUTPUT);
- digitalWrite(pump, LOW);
- Serial.begin(9600);
- Serial.println("Initialising wifi");
- esp8266.begin (9600);
- reset8266(); // Pin CH_PD needs a reset before start communication
- InitWifiModule(); // Inciate module as WebServer
- delay(1000);
- Serial.println("Done");
- Serial.println("Initialising display");
- Serial.flush();
- Serial.begin(9600);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32
- display.clearDisplay();
- initdisplay();
- display.clearDisplay();
- delay(1000);
- Serial.flush();
- Serial.begin(9600);
- Serial.print("Initialising done");
- }
- void loop()
- {
- Serial.flush();
- Serial.begin(9600);
- cntdisplay(i);
- delay(10);
- Serial.flush();
- Serial.begin(speed8266);
- if (esp8266.available()) // check if 8266 is sending data
- {
- if (esp8266.find("+IPD,"))
- {
- delay(300);
- int connectionId = esp8266.read() - 48;
- // find reply
- esp8266.find("/?pin=");
- delay(300);
- if (esp8266.available())
- {
- // The esp has data so display its output to the serial window
- t = esp8266.read(); // read the next character.
- }
- delay(300);
- String webpage = "<h1>Hello, world!</h1><h2> Number is counting ";
- webpage += i;
- webpage += "<form action=""/"">";
- webpage += "Pin:<br>";
- webpage += "<input type=""text"" name=""pin"" value=""><br>";
- webpage += "Last selection: ";
- webpage += t;
- webpage += "<input type=""submit"" value=""Submit"">";
- webpage += "</form>";
- webpage += "</h2>";
- Serial.print(webpage);
- String cipSend = "AT+CIPSEND=";
- cipSend += connectionId;
- cipSend += ",";
- cipSend += webpage.length();
- cipSend += "\r\n";
- sendData(cipSend, 1000, DEBUG);
- sendData(webpage, 1000, DEBUG);
- delay(300);
- String closeCommand = "AT+CIPCLOSE=";
- closeCommand += connectionId; // append connection id
- closeCommand += "\r\n";
- sendData(closeCommand, 3000, DEBUG);
- }
- }
- //Serial.print(t);
- //Serial.flush();
- if (t == 0)
- {
- motorrun();
- // Serial.begin(9600);
- // Serial.println("gooo");
- t = "1";
- }
- i++;
- }
- /*************************************************/
- void motorrun(void) {
- digitalWrite(pump, HIGH);
- delay(2000);
- digitalWrite(pump, LOW);
- delay(2000);
- }
- void InitWifiModule()
- {
- sendData("AT+RST\r\n", 2000, DEBUG); // reset
- // sendData("AT+CIOBAUD=9600\r\n", 1000, DEBUG);
- sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
- delay(1000);
- sendData("AT+CWJAP_CUR=\"Cup\",\"3rhm\"\r\n", 2000, DEBUG); //Connect network
- delay(5000);
- sendData("AT+CIFSR\r\n", 1000, DEBUG); // Show IP Adress
- delay(1000);
- sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // Multiple conexions
- delay(1000);
- sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // start comm port 80
- delay(1000);
- }
- /*************************************************/
- // Send AT commands to module
- String sendData(String command, const int timeout, boolean debug)
- {
- delay(50);
- String response = "";
- Serial.println(command);
- esp8266.print(command);
- long int time = millis();
- while ( (time + timeout) > millis())
- {
- while (esp8266.available())
- {
- // The esp has data so display its output to the serial window
- char c = esp8266.read(); // read the next character.
- response += c;
- }
- }
- // if (debug)
- // {
- // Serial.print(response);
- // }
- return response;
- }
- /*************************************************/
- // Reset funtion to accept communication
- void reset8266 ()
- {
- pinMode(CH_PD, OUTPUT);
- digitalWrite(CH_PD, LOW);
- delay(300);
- digitalWrite(CH_PD, HIGH);
- }
- /*************************************************/
- // Display
- void initdisplay(void) {
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(20, 20);
- display.clearDisplay();
- display.println("Initialising");
- display.display();
- delay(50);
- }
- void cntdisplay(int c) {
- display.clearDisplay();
- display.setCursor(50, 20);
- display.println(c);
- display.display();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement