Skip to content

Instantly share code, notes, and snippets.

@m25lazi
Last active December 10, 2015 17:43
Show Gist options
  • Save m25lazi/414056db1ce90763c854 to your computer and use it in GitHub Desktop.
Save m25lazi/414056db1ce90763c854 to your computer and use it in GitHub Desktop.
/*
Web client sketch for IDE v1.0.1 and w5100/w5200
Uses POST method.
Posted November 2012 by SurferTim
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//Change to your server domain
char serverName[] = "192.168.0.103";
// change to your server's port
int serverPort = 80;
// change to the page on that server
char pageName[] = "/testpost";
EthernetClient client;
int totalCount = 0;
// insure params is big enough to hold your variables
char params[32];
// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 30000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.print(F("Starting ethernet..."));
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready"));
}
void loop()
{
// If using a static IP, comment out the next line
Ethernet.maintain();
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
// params must be url encoded.
sprintf(params,"temp1=%i",totalCount);
if(!postPage(serverName,serverPort,pageName,params)) Serial.print(F("Fail "));
else Serial.print(F("Pass "));
totalCount++;
Serial.println(totalCount,DEC);
}
}
byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
int inChar;
char outBuf[64];
Serial.print(F("connecting..."));
if(client.connect(domainBuffer,thisPort) == 1)
{
Serial.println(F("connected"));
// send the header
sprintf(outBuf,"POST %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",domainBuffer);
client.println(outBuf);
client.println(F("Connection: close\r\nContent-Type: application/x-www-form-urlencoded"));
sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
client.println(outBuf);
// send the body (variables)
client.print(thisData);
}
else
{
Serial.println(F("failed"));
return 0;
}
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
connectLoop = 0;
}
delay(1);
connectLoop++;
if(connectLoop > 10000)
{
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
}
Serial.println();
Serial.println(F("disconnecting."));
client.stop();
return 1;
}
var path = require('path');
global.appRoot = path.resolve(__dirname);
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
var port_number = process.env.PORT || 3000;
var server = app.listen(port_number, function () {
var host = server.address().address;
var port = server.address().port;
console.log("API app listening at http://%s:%s", host, port);
});
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(function(req, res, next){
res.setHeader('Access-Control-Allow-Origin', '*');
return next();
});
app.post('/testpost', function (req, res) {
console.log("GOT DATA FROM ARDUINO : "+JSON.stringify(req.body));
res.end(JSON.stringify({'status': 1}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment