Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to upload data to a server with Arduino
#include <SPI.h>
#include <WiFi101.h>
Jeff Thompson | 2017 |
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.
$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:
// server to connect to and relative path to PHP script
char server[] = "";
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
// start serial connection for feedback
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: \"");
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.println(" HTTP/1.1");
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: ");
Serial.println("- done");
// disconnect when done
Serial.println("Disconnecting from server...");
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
You can’t perform that action at this time.