Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "WiFiEsp.h"
- // Emulate Serial1 on pins 6/7 if not present
- #ifndef HAVE_HWSERIAL1
- #include "SoftwareSerial.h"
- SoftwareSerial Serial1(2,3); // RX, TX
- #endif
- char ssid[] = "RPi"; // your network SSID (name)
- char pass[] = "raspberry"; // your network password
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- char server[] = "192.168.50.1";
- unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
- const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
- int testLED = 12;
- // Initialize the Ethernet client object
- WiFiEspClient client;
- void setup()
- {
- pinMode(testLED, OUTPUT);
- // initialize serial for debugging
- Serial.begin(9600);
- // initialize serial for ESP module
- Serial1.begin(9600);
- // initialize ESP module
- WiFi.init(&Serial1);
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- // don't continue
- while (true);
- }
- // attempt to connect to WiFi network
- while ( status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network
- status = WiFi.begin(ssid, pass);
- }
- Serial.println("You're connected to the network");
- //printWifiStatus();
- }
- void loop()
- {
- // if there's incoming data from the net connection send it out the serial port
- // this is for debugging purposes only
- while (client.available()) {
- char c = client.read();
- //code to run relay
- if(c == '#')
- {
- while(client.available() == 0) { } // Do nothing while we wait for the next value
- // reading the second charactor
- c = client.read();
- if(c == '1'){
- Serial.println("On");
- digitalWrite(testLED, HIGH);
- delay(600000);
- }
- else if(c == '0'){
- Serial.println("Off");
- digitalWrite(testLED, LOW);
- delay(600000);
- } //end of C cheking if-else
- } //end of first charactor # if
- //end of replay code
- }
- // if 10 seconds have passed since your last connection,
- // then connect again and send data
- if (millis() - lastConnectionTime > postingInterval) {
- httpRequest();
- }
- }
- // this method makes a HTTP connection to the server
- void httpRequest()
- {
- Serial.println();
- // close any connection before send a new request
- // this will free the socket on the WiFi shield
- client.stop();
- // if there's a successful connection
- if (client.connect(server, 80)) {
- Serial.println("Connecting...");
- // send the HTTP PUT request
- client.println(F("GET /simple.txt HTTP/1.1"));
- client.println(F("Host: localhost"));
- client.println("Connection: close");
- client.println();
- // note the time that the connection was made
- lastConnectionTime = millis();
- }
- else {
- WiFi.begin(ssid, pass);
- Serial.println("Reconnecting again");
- }
- }
Add Comment
Please, Sign In to add comment