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 "safetyhelmetiot.000webhostapp.com" // Enter HOST URL without "http:// " and "/" at the end of URL
- // Declare global variables which will be uploaded to server
- int humidity = 0;
- int temp = 0;
- int gas = 0;
- int pressure = 0;
- int hr = 0;
- int o_r = 0;
- int gpslat = 0;
- int gpslon = 0;
- String sendhumidity, sendtemp, sendgas, sendpressure, sendhr, sendo_r, sendgpslat, sendgpslon, 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
- // Convert integer variables to string
- sendhumidity = String(100);
- sendtemp = String(20);
- sendgas = String(1503);
- sendpressure = String(1224);
- sendhr = String(85);
- sendo_r = String(96);
- sendgpslat = String("7.12654");
- sendgpslon = String("8.12356");
- postData = "sendhumidity=" + sendhumidity + "&sendtemp=" + sendtemp + "&sendgas=" + sendgas + "&sendpressure=" + sendpressure + "&sendhr=" + sendhr + "&sendo_r=" + sendo_r + "&sendgpslat=" + sendgpslat + "&sendgpslon=" + sendgpslon;
- http.begin("http://safetyhelmetiot.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
- // 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);
- }
Add Comment
Please, Sign In to add comment