Showing posts with label Web Client. Show all posts
Showing posts with label Web Client. Show all posts

Monday, April 6, 2015

IoT experience: Arduino Uno + Ethernet Shield send data to dweet.io and freeboard.io

It's a IoT experience, run on Arduino Uno + Ethernet Shield, read analog input from A0, send to dweet.io and freeboard.io.

VIew on dweet.io
freeboard.io


dweet.io is simple publishing and subscribing for machines, sensors, devices, robots, and gadgets (we just call them things). We call published messages ‘dweets’. It’s helpful to think of dweet.io as a Twitter for things, in fact.

With freeboard.io, we can create simple dashboards for devices, include dweet.io things.



The code on Arduino Uno, AnalogInDweetIoRepeat.ino. Modified from Web Client Repeating example (http://arduino.cc/en/Tutorial/WebClientRepeating).

/*
Reference:
 http://arduino.cc/en/Tutorial/WebClientRepeating
 */

#include <SPI.h>
#include <Ethernet.h>

const int analogIn = A0;
int analogVal = 0;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

// initialize the library instance:
EthernetClient client;

char server[] = "www.dweet.io";

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup() {
  // start serial port:
  Serial.begin(9600);
  Serial.println("--- Start ---");
  
  // give the ethernet module time to boot up:
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip); 
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    analogVal = analogRead(analogIn);
    
    // Make a HTTP request:
    String s = "POST /dweet/for/arduinotest?A0=";
    s.concat(analogVal);
    Serial.println(s);
    client.println(s);
    
    client.println("Host: www.dweet.io");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}


This video show how it run and view on dweet.io and freeboard.io. Pin A0 is open(float) here, so the value is random.


Related example on my another blog about Raspberry Pi:
IoT at dweet.io, Python on RPi 2 to send data to Cloud
- Create dashboards for dweet.io things with freeboard.io

Related:
- Arduino Due + ESP8266 + DHT11, to update dweet.io

NodeMCU version:
NodeMCU act as WiFi client to update dweet.io


Updated@2017-06-15:
Somebody complain the example not work, I tried agin, Arduino Uno + Ethernet Shield, without change. And found it still work as expected.


Once you make it run, you can check the thing at http://dweet.io/follow/arduinotest