Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <HTTPClient.h>
- // Replace with your network credentials
- const char* ssid = "network";
- const char* password = "123456789";
- // Update HOST URL here
- #define HOST "iotdashboard2792021.000webhostapp.com" // Enter HOST URL without "http:// " and "/" at the end of URL
- // Declare global variables which will be uploaded to server
- int parking1 = 0;
- int parking2 = 0;
- int parking3 = 0;
- int parking4 = 0;
- int parking5 = 0;
- int parking6 = 0;
- String sendparking1, sendparking2, sendparking3, sendparking4, sendparking5, sendparking6, sendled1, sendled2, sendled3, sendled4, sendled5, sendled6, sendtemp, sendhum, sendpanic, postData;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- Serial.println("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- HTTPClient http; // http object of clas HTTPClient
- parking1 = digitalRead(5); // D1 = 5
- parking2 = digitalRead(4); // D2 = 4
- parking3 = digitalRead(14); // D5 = 14
- parking4 = digitalRead(12); // D6 = 12
- parking5 = digitalRead(13); // D7 = 13
- parking6 = digitalRead(16); // D0 = 16
- if (parking1 == 0) {
- parking1 = 2;
- }
- if (parking2 == 0) {
- parking2 = 2;
- }
- if (parking3 == 0) {
- parking3 = 2;
- }
- if (parking4 == 0) {
- parking4 = 2;
- }
- if (parking5 == 0) {
- parking5 = 2;
- }
- if (parking6 == 0) {
- parking6 = 2;
- }
- // Convert integer variables to string
- sendparking1 = String(parking1);
- sendparking2 = String(parking2);
- sendparking3 = String(parking3);
- sendparking4 = String(parking4);
- sendparking5 = String(parking5);
- sendparking6 = String(parking6);
- postData = "sendparking1=" + sendparking1 + "&sendparking2=" + sendparking2 + "&sendparking3=" + sendparking3 + "&sendparking4=" + sendparking4 + "&sendparking5=" + sendparking5 + "&sendparking6=" + sendparking6;
- http.begin("http://iotdashboard2792021.000webhostapp.com/dbwrite.php"); // Connect to host where MySQL databse is hosted
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
- int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
- //Serial.println("Values are, sp1 = " + sendparking1 + " and sp2 = "+sendparking2 + " and sp3 = "+sendparking3 + " and sp4 = "+sendparking4 + " and sp5 = "+sendparking5 + " and sp6 = "+sendparking6 );
- // if connection eatablished then do this
- if (httpCode == 200) {
- Serial.println("Values uploaded successfully."); Serial.println(httpCode);
- String webpage = http.getString(); // Get html webpage output and store it in a string
- Serial.println(webpage + "\n");
- }
- // if failed to connect then return and restart
- else {
- Serial.println(httpCode);
- Serial.println("Failed to upload values. \n");
- http.end();
- return;
- }
- delay(3000);
- delay(3000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement