SparkCore + MicroView + HTTPS POST Interaction

Just got my Sparkcore talking to my MicroView via I2C bus, and added a simple HTTPS POST trigger to implement a counter on the MicroView via the Internet!

Video here: https://www.youtube.com/watch?v=9xv4E0wF9ac

Thanks to @peekay123 for the I2C communication example here: https://community.spark.io/t/i2c-issues-between-core-and-uno-solved/3099/13

Here’s the breadboard layout, the whole thing is powered via the SparkCore micro-USB port, as the VIN pins are tied together. The reset lines are also tied together to allow the SparkCore reset button to reset the Microview too. You can leave in the MicroView programmer, and power the circuit from there while you debug the microview code, and then remove the programmer to make the circuit prettier, and power with the SparkCore.

Microview Code
SparkCore Code:

 #include "application.h"

#define BUTTON D4

#define OTHER_ADDRESS 0x09 //Address of the I2C slave we're sending to

int last_state = HIGH;

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  Spark.function("increment", wireControl); //Register a Spark Cloud function
  Wire.begin();
  // take control of the Spark Core LED, and set the brightness to low to save some power
  RGB.control(true);
  RGB.brightness(32);
  RGB.control(false);
}

void loop()
{
    // Diagnostic if Cloud service is not working
    if (digitalRead(BUTTON) != last_state){ //send a '1' if button D4 is pressed
        last_state = digitalRead(BUTTON);
        Wire.beginTransmission(OTHER_ADDRESS);
        Wire.write(last_state);
        Wire.endTransmission();
        }
}

// This function gets called whenever there is a matching API request

int wireControl(String command)
{

   Wire.beginTransmission(OTHER_ADDRESS);
   Wire.write(1);
   Wire.endTransmission();
   return 1;
}

On power up you should see ‘Ready’ on the Microview screen, then use this HTTP Post to increment the counter on the MicroView screen, substituting DEVICE_ID and ACCESS_TOKEN according to your core:

https://api.spark.io/v1/devices/{{DEVICE_ID}}/increment?access_token={{ACCESS_TOKEN}}

I have also turned down the brightness of the SparkCore LED, and implemented a trigger on pin D4 too - which you can short to GND to see the same effect without using a HTTP Post.

iOS App here: https://www.dropbox.com/s/xlh3bh2yt4djv3m/TestHTTP_Anonymised.zip
You will need to edit XYZViewController.h to add your Device ID and Access Token.

2 Likes

Here’s a variation - the Spark Core runs a Telnet service, and whatever you type is sent to the Microview, press ‘Return’ to clear the screen. You will need to know your Sparkcore’s local IP address on your LAN.

Sparkcore Code:

#define OTHER_ADDRESS 0x09 //Address of the 1-Wire slave we're sending to
// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
  // start listening for clients
  server.begin();
  Wire.begin();


}

void loop()
{
  char c;
  if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
        c = client.read();
      server.write(c);
      Wire.beginTransmission(OTHER_ADDRESS);
      Wire.write(c);
      Wire.endTransmission();
    }
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }
}

Microview Code: https://codebender.cc/sketch:44130