Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- String locationcode = "100";
- //wifi at home
- const char* ssidthuis = "ssidhome";
- const char* passwordthuis = "pass1";
- //If you have a 2nd hotspot on other place
- const char* ssidlet = "ssid2";
- const char* passwordlet = "pass2";
- //thingspeak log
- String apiKey = "FGGETUDSRFGTE8LNK5YGRHE"; //thingspeak API key,
- //change pehacommand to your peha command !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- String lokatie = "0";
- const char* ssid = ""; //temp ssid en password
- const char* password = "";
- String inData;//seriele recieve buffer
- const char* server = "api.thingspeak.com";
- String theDate; //Sat, 05 Nov 2016 13:30:29 GMT
- WiFiClient client;
- /////////////////subroutines
- int find_text(String needle, String haystack) {
- int foundpos = -1;
- for (int i = 0; i <= haystack.length() - needle.length(); i++) {
- if (haystack.substring(i, needle.length() + i) == needle) {
- foundpos = i;
- }
- }
- return foundpos;
- }
- /////////////////////////peha wifi commando
- void pehacommand()
- {
- inData = "";//reset rs232 buffer
- HTTPClient http;
- http.begin("http://192.168.1.2:80/postEvent.html?action=input&STM=0&MOD=4&CHA=7&EVT=2"); //far buiten voetpad
- int httpCode = http.GET();
- if (httpCode > 0) {
- // file found at server
- if (httpCode == HTTP_CODE_OK) {
- String payload = http.getString();
- Serial.println(payload);
- }
- } else {
- Serial.printf("[HTTP] peha workt ok but ..GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- }
- //////////////////thingspeak log sturen
- void thingspeak(String field2data) {
- if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
- String postStr = apiKey;
- postStr += "&field1="; //
- postStr += (locationcode);
- postStr += "&field2=";
- postStr += String(field2data ); //datawaarde
- // postStr += "\r\n\r\n";
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(postStr.length());
- client.print("\n\n");
- client.print(postStr);
- Serial.println(field2data + " send to Thingspeak");
- // inData = ""; // Clear recieved buffer
- }
- client.stop();
- }
- ////////////////wifi scanner
- void wifiscan() {
- Serial.println("scan start");
- // WiFi.scanNetworks will return the number of networks found
- int n = WiFi.scanNetworks();
- Serial.println("scan done");
- if (n == 0)
- Serial.println("no networks found");
- else
- {
- Serial.print(n);
- Serial.println(" networks found");
- for (int i = 0; i < n; ++i)
- {
- // Print SSID and RSSI for each network found
- Serial.print(i + 1);
- Serial.print(": ");
- Serial.print(WiFi.SSID(i));
- Serial.print(" (");
- Serial.print(WiFi.RSSI(i));
- Serial.print(")");
- Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
- delay(10);
- }
- }
- Serial.println("");
- // Wait a bit before scanning again
- delay(5000);
- }
- ///wifi scanner
- void zoekbekendewifis() {
- Serial.println("scan bekende wifis start");
- // WiFi.scanNetworks will return the number of networks found
- int n = WiFi.scanNetworks();
- // Serial.println("scan done");
- if (n == 0)
- // Serial.println("no networks found");
- n = n;
- else
- {
- // Serial.print(n);
- // Serial.println(" networks found");
- for (int i = 0; i < n; ++i)
- {
- if (WiFi.SSID(i) == ssidthuis )
- {
- ssid = ssidthuis ;
- password = passwordthuis;
- digitalWrite(0, HIGH); // led aan
- lokatie = "20"; // 20 is homespot in grafiek
- initWifi();
- pehacommand();
- thingspeak(lokatie);
- WiFi.disconnect();
- Serial.println("disconnected");
- }
- if (WiFi.SSID(i) == ssidlet )
- {
- ssid = ssidlet ;
- password = passwordlet;
- digitalWrite(2, HIGH); // led aan
- lokatie = "10"; // 10=wifi from parents in grafiek
- initWifi();
- thingspeak(lokatie);
- //pehacommand();
- WiFi.disconnect();
- Serial.println("disconnected");
- break ; //we moeten niet weten welke wifis er nog zijn
- }
- }
- }
- //Serial.println("---");
- }
- /////////////uart
- void uart() {
- while (Serial.available() > 0)
- {
- char recieved = Serial.read();
- inData += recieved;
- // Process message when new line character is recieved
- if (recieved == '!')
- { Serial.print("\nESP Received: "); //
- Serial.print(inData);
- if (inData == "rolop!") {
- pehacommand();
- inData = "";//reset rs232 buffer
- }
- if (inData == "wifi!")
- {
- Serial.println("\n" + WiFi.SSID()); //Gets the SSID of the current network
- inData = "";//reset rs232 buffer
- }
- if (inData == "thing!")
- {
- Serial.println("thingspeak");
- thingspeak(lokatie);
- inData = "";//reset rs232 buffer
- }
- if (inData == "scan!")
- {
- // Serial.println("thingspeak");
- wifiscan();
- inData = "";//reset rs232 buffer
- }
- if (inData == "zoekbekendewifis!")
- {
- // Serial.println("thingspeak");
- zoekbekendewifis() ;
- inData = "";//reset rs232 buffer
- }
- }
- }
- }
- ////////////
- void setup() {
- Serial.begin(115200);
- pinMode(0, OUTPUT); //1 is de ingebouwde blauwe led op de esp-01 //2 is pin2 //0 is pin1
- pinMode(2, OUTPUT); //1 is de ingebouwde blauwe led op de esp-01 //2 is pin2 //0 is pin1
- digitalWrite(0, LOW); // led uit
- digitalWrite(2, LOW); // led uit
- Serial.println("v12345vtm");
- zoekbekendewifis();
- }
- void loop() {
- uart();
- if (lokatie == "20" || lokatie == "10")
- {
- //we hebben al verbinding gehad , dus niks moet nog
- lokatie = "5";
- delay(60000); //reset de lokatie waarde en wacht een minuut , zo kan je wegrijden en is de lokatiecode gereset
- digitalWrite(0, LOW); // led uit
- digitalWrite(2, LOW); // led uit
- Serial.println("---");
- delay(60000);
- Serial.println("-----");
- delay(60000);
- Serial.println("--------");
- delay(60000);
- }
- else
- {
- zoekbekendewifis();
- }
- Serial.println (millis());
- delay (1000);
- Serial.println("----");
- }
- String getTime() {
- WiFiClient client;
- while (!!!client.connect("google.com", 80)) {
- Serial.println("connection failed, retrying...");
- }
- client.print("HEAD / HTTP/1.1\r\n\r\n");
- while (!!!client.available()) {
- yield();
- }
- while (client.available()) {
- if (client.read() == '\n') {
- if (client.read() == 'D') {
- if (client.read() == 'a') {
- if (client.read() == 't') {
- if (client.read() == 'e') {
- if (client.read() == ':') {
- client.read();
- theDate = client.readStringUntil('\r');
- client.stop();
- return theDate; //Sat, 05 Nov 2016 13:30:29 GMT
- }
- }
- }
- }
- }
- }
- }
- }
- void initWifi() {
- //WiFi.status() =
- // WL_NO_SHIELD = 255,
- //WL_IDLE_STATUS = 0,
- //WL_NO_SSID_AVAIL = 1
- //WL_SCAN_COMPLETED = 2
- //WL_CONNECTED = 3
- //WL_CONNECT_FAILED = 4
- //WL_CONNECTION_LOST = 5
- //WL_DISCONNECTED = 6
- Serial.print(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- if (WiFi.status() == 1) //WL_NO_SSID_AVAIL = 1
- {
- Serial.println("1e ssid not found , try 2nd ssid ");
- }
- }
- Serial.print("\nWiFi connected, IP address: ");
- Serial.println(WiFi.localIP());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement