Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. /*************************************************************************
  2.   Lille til pin2 fra venstre og rød pin1 på wifi
  3.  
  4.   Læs serial, så skal de byttes om og gns og rst skal forbindes på arduini
  5. **************************************************************************/
  6. // esp8266 module
  7. #include <SoftwareSerial.h>
  8. //#define esp8266 Serial2
  9. SoftwareSerial esp8266(0, 1); // RX | TX
  10. #define CH_PD 7
  11. #define speed8266 9600 // This is the speed that worked with my ESP8266
  12. #define DEBUG false
  13.  
  14. // oled module
  15. #include <SPI.h>
  16. #include <Wire.h>
  17. // #include <Adafruit_GFX.h>
  18. #include <Adafruit_SSD1306.h>
  19. #define OLED_RESET 4
  20. Adafruit_SSD1306 display(OLED_RESET);
  21.  
  22. #if (SSD1306_LCDHEIGHT != 32)
  23. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  24. #endif
  25.  
  26. // pumps
  27. int i;
  28. int pump = 2;
  29. char t;
  30.  
  31. void setup()
  32. {
  33.   // PUMP SECTION
  34.   pinMode(pump, OUTPUT);
  35.   digitalWrite(pump, LOW);
  36.  
  37.  
  38.   Serial.begin(9600);
  39.   Serial.println("Initialising wifi");
  40.   esp8266.begin (9600);
  41.   reset8266(); // Pin CH_PD needs a reset before start communication
  42.   InitWifiModule(); // Inciate module as WebServer
  43.   delay(1000);
  44.   Serial.println("Done");
  45.   Serial.println("Initialising display");
  46.   Serial.flush();
  47.   Serial.begin(9600);
  48.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32
  49.   display.clearDisplay();
  50.   initdisplay();
  51.   display.clearDisplay();
  52.   delay(1000);
  53.   Serial.flush();
  54.   Serial.begin(9600);
  55.   Serial.print("Initialising done");
  56. }
  57.  
  58. void loop()
  59. {
  60.   Serial.flush();
  61.   Serial.begin(9600);
  62.   cntdisplay(i);
  63.   delay(10);
  64.   Serial.flush();
  65.   Serial.begin(speed8266);
  66.   if (esp8266.available()) // check if 8266 is sending data
  67.   {
  68.     if (esp8266.find("+IPD,"))
  69.     {
  70.       delay(300);
  71.       int connectionId = esp8266.read() - 48;
  72.       // find reply
  73.       esp8266.find("/?pin=");
  74.       delay(300);
  75.       if (esp8266.available())
  76.       {
  77.         // The esp has data so display its output to the serial window
  78.         t = esp8266.read(); // read the next character.
  79.       }
  80.       delay(300);
  81.  
  82.  
  83.       String webpage = "<h1>Hello, world!</h1><h2> Number is counting ";
  84.       webpage += i;
  85.       webpage += "<form action=""/"">";
  86.       webpage += "Pin:<br>";
  87.       webpage += "<input type=""text"" name=""pin"" value=""><br>";
  88.       webpage += "Last selection: ";
  89.       webpage += t;
  90.       webpage += "<input type=""submit"" value=""Submit"">";
  91.       webpage += "</form>";
  92.       webpage += "</h2>";
  93.       Serial.print(webpage);
  94.       String cipSend = "AT+CIPSEND=";
  95.       cipSend += connectionId;
  96.       cipSend += ",";
  97.       cipSend += webpage.length();
  98.       cipSend += "\r\n";
  99.       sendData(cipSend, 1000, DEBUG);
  100.       sendData(webpage, 1000, DEBUG);
  101.       delay(300);
  102.       String closeCommand = "AT+CIPCLOSE=";
  103.       closeCommand += connectionId; // append connection id
  104.       closeCommand += "\r\n";
  105.       sendData(closeCommand, 3000, DEBUG);
  106.  
  107.     }
  108.   }
  109.   //Serial.print(t);
  110.   //Serial.flush();
  111.   if (t == 0)
  112.   {
  113.     motorrun();
  114.     //  Serial.begin(9600);
  115.     //  Serial.println("gooo");
  116.     t = "1";
  117.   }
  118.   i++;
  119. }
  120.  
  121. /*************************************************/
  122. void motorrun(void) {
  123.   digitalWrite(pump, HIGH);
  124.   delay(2000);
  125.   digitalWrite(pump, LOW);
  126.   delay(2000);
  127. }
  128.  
  129. void InitWifiModule()
  130. {
  131.   sendData("AT+RST\r\n", 2000, DEBUG); // reset
  132.   // sendData("AT+CIOBAUD=9600\r\n", 1000, DEBUG);
  133.   sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
  134.   delay(1000);
  135.   sendData("AT+CWJAP_CUR=\"Cup\",\"3rhm\"\r\n", 2000, DEBUG); //Connect network
  136.   delay(5000);
  137.   sendData("AT+CIFSR\r\n", 1000, DEBUG); // Show IP Adress
  138.   delay(1000);
  139.   sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // Multiple conexions
  140.   delay(1000);
  141.   sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // start comm port 80
  142.   delay(1000);
  143. }
  144.  
  145. /*************************************************/
  146. // Send AT commands to module
  147. String sendData(String command, const int timeout, boolean debug)
  148. {
  149.   delay(50);
  150.   String response = "";
  151.   Serial.println(command);
  152.   esp8266.print(command);
  153.   long int time = millis();
  154.   while ( (time + timeout) > millis())
  155.   {
  156.     while (esp8266.available())
  157.     {
  158.       // The esp has data so display its output to the serial window
  159.       char c = esp8266.read(); // read the next character.
  160.       response += c;
  161.     }
  162.   }
  163.   //  if (debug)
  164.   //  {
  165.   //    Serial.print(response);
  166.   //  }
  167.   return response;
  168. }
  169.  
  170. /*************************************************/
  171. // Reset funtion to accept communication
  172. void reset8266 ()
  173. {
  174.   pinMode(CH_PD, OUTPUT);
  175.   digitalWrite(CH_PD, LOW);
  176.   delay(300);
  177.   digitalWrite(CH_PD, HIGH);
  178. }
  179.  
  180. /*************************************************/
  181. // Display
  182.  
  183. void initdisplay(void) {
  184.   display.setTextSize(1);
  185.   display.setTextColor(WHITE);
  186.   display.setCursor(20, 20);
  187.   display.clearDisplay();
  188.   display.println("Initialising");
  189.   display.display();
  190.   delay(50);
  191. }
  192.  
  193. void cntdisplay(int c) {
  194.   display.clearDisplay();
  195.   display.setCursor(50, 20);
  196.   display.println(c);
  197.   display.display();
  198.   delay(1000);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement