Skip to content

Instantly share code, notes, and snippets.

@williambsb
Forked from jeffThompson/UploadDataOverWifi.ino
Created December 7, 2020 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williambsb/c1eb8ddde3b1fdb9796d023b2288cfe0 to your computer and use it in GitHub Desktop.
Save williambsb/c1eb8ddde3b1fdb9796d023b2288cfe0 to your computer and use it in GitHub Desktop.
How to upload data to a server with Arduino
#include <SPI.h>
#include <WiFi101.h>
/*
UPLOAD DATA OVER WIFI
Jeff Thompson | 2017 | jeffreythompson.org
The great Arduino Wifi101 library includes lots of examples
of how to *get* data from the internet, but no examples on how
to upload!
Here, we use HTTP POST commands to send sensor readings to
a server. A simple PHP script parses those readings and
updates a text file.
<?php
$dir = $_POST["dir"];
$speed = $_POST["speed"];
$batt = $_POST["batt"];
$data = $dir . ',' . $speed . ',' . $batt;
$file = './current.txt';
file_put_contents($file, $data);
?>
Based on this example, and many hours of Google searching:
https://cdn.instructables.com/FLH/GRS2/HXB3UZG6/FLHGRS2HXB3UZG6.LARGE.jpg
*/
// server to connect to and relative path to PHP script
char server[] = "www.jeffreythompson.org";
String phpScript = "/path/update.php";
// wifi network and password
char ssid[] = "xxxx";
char pass[] = "xxxx";
int status = WL_IDLE_STATUS;
int keyIndex = 0;
WiFiClient client;
void setup() {
// some data to send (fake wind sensor readings)
float windDir = 380.0;
float windSpeed = 2.2;
float battLevel = 3.85;
// format datapoints as PHP arguments
// this means easy parsing later in the PHP script
String data = "&dir=" + String(windDir, 2) + "&speed=" + String(windSpeed, 2) + "&batt=" + String(battLevel, 2);
// required for Adafruit Feather M0 Wifi board
WiFi.setPins(8,7,4,2);
// start serial connection for feedback
Serial.begin(9600);
while (!Serial) {
//
}
// is a wifi unit attached?
Serial.println("Checking for wifi hardware...");
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("- none found, quitting...");
while (true);
}
Serial.println("- found");
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to: \"");
Serial.print(ssid);
Serial.println("\"");
status = WiFi.begin(ssid, pass);
delay(10000); // wait 10 sec for connection
}
Serial.println("- connected!");
// post data using HTTP POST
Serial.println("Connecting to server...");
if (client.connect(server, 80)) {
Serial.println("- connected");
Serial.println("Posting sensor data...");
client.print("POST ");
client.print(phpScript);
client.println(" HTTP/1.1");
client.println("Host: www.jeffreythompson.org");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
// client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
Serial.println("- done");
}
// disconnect when done
Serial.println("Disconnecting from server...");
client.stop();
Serial.println("- bye!");
}
void loop() {
// you could also move your HTTP POST code here, if you
// wanted to post repeatedly
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment