POST http client example (#2704)

POST http client example (#2704)

* POST http client example

* avoid deprecated api

* Update PostHttpClient.ino

style, comments

* Update PostHttpClient.ino

/**
   PostHTTPClient.ino

    Created on: 21.11.2016

*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

/* this can be run with an emulated server on host:
        cd esp8266-core-root-dir
        cd tests/host
        make ../../libraries/ESP8266WebServer/examples/PostServer/PostServer
        bin/PostServer/PostServer
   then put your PC's IP address in SERVER_IP below, port 9080 (instead of default 80):
*/
//#define SERVER_IP "10.0.1.7:9080" // PC address with emulation on host
#define SERVER_IP "192.168.1.42"

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK  "your-password"
#endif

void setup() {

  USE_SERIAL.begin(115200);

  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();

  WiFi.begin(STASSID, STAPSK);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    USE_SERIAL.print(".");
  }
  USE_SERIAL.println("");
  USE_SERIAL.print("Connected! IP address: ");
  USE_SERIAL.println(WiFi.localIP());

}

void loop() {
  // wait for WiFi connection
  if ((WiFi.status() == WL_CONNECTED)) {

    WiFiClient client;
    HTTPClient http;

    USE_SERIAL.print("[HTTP] begin...\n");
    // configure traged server and url
    http.begin(client, "http://" SERVER_IP "/postplain/"); //HTTP
    http.addHeader("Content-Type", "application/json");

    USE_SERIAL.print("[HTTP] POST...\n");
    // start connection and send HTTP header and body
    int httpCode = http.POST("{\"hello\":\"world\"}");

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        const String& payload = http.getString();
        USE_SERIAL.println("received payload:\n<<");
        USE_SERIAL.println(payload);
        USE_SERIAL.println(">>");
      }
    } else {
      USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
  }

  delay(10000);
}

error: pathspec 'libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino' did not match any file(s) known to git


controller writing to serial port

controller writing to serial port

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/robreeves/robotany.git/'


Add STM32F103 Blue Pill example

Add STM32F103 Blue Pill example

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example shows how to use ordinary Arduino Serial
 * to connect your project to Blynk.
 * Feel free to apply it to any other example. It's simple!
 *
 * Requires STM32duino: https://github.com/rogerclarkmelbourne/Arduino_STM32/wiki/Installation
 *
 **************************************************************
 *            USB HOWTO: http://tiny.cc/BlynkUSB
 **************************************************************/

#define BLYNK_PRINT Serial2
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

void setup()
{
  // Debug console
  Serial2.begin(9600);

  // Blynk will work through Serial
  Serial.begin(9600);
  Blynk.begin(auth, Serial);
}

void loop()
{
  Blynk.run();
}

error: pathspec 'examples/Boards_USB_Serial/Blue_Pill_STM32F103C8/Blue_Pill_STM32F103C8.ino' did not match any file(s) known to git


Create SHRUN.ino

Create SHRUN.ino

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/TEAMarg/ID-15-Shadow-Runner.git/'


add neopixel, single frame read example

add neopixel, single frame read example

/**************************************************************************/
/*!
    @file     DMXInputTest.ino
    @author   Claude Heintz
    @license  BSD (see LXESP8266DMX LICENSE)
    @copyright 2019 by Claude Heintz

    Control Neo-Pixels with DMX input
    Demonstrates single frame read using ESP8266DMX.isReceiving()

    @section  HISTORY

    v1.00 - First release
*/
/**************************************************************************/
#include <LXESP8266UARTDMX.h>

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define DIRECTION_PIN 4

// data pin for NeoPixels
#define PIN 14
// NUM_OF_NEOPIXELS, max of 170/RGB or 128/RGBW
#define NUM_OF_NEOPIXELS 12
// LEDS_PER_NEOPIXEL, RGB = 3, RGBW = 4
#define LEDS_PER_NEOPIXEL 3
// see Adafruit NeoPixel Library for options to pass to Adafruit_NeoPixel constructor
Adafruit_NeoPixel ring = Adafruit_NeoPixel(NUM_OF_NEOPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// min sometimes raises compile error. see https://github.com/esp8266/Arduino/issues/263 solution seems to be using _min
const int total_pixels = _min(512, LEDS_PER_NEOPIXEL * NUM_OF_NEOPIXELS);
byte pixels[NUM_OF_NEOPIXELS][LEDS_PER_NEOPIXEL];

// flag for dmx data received
int got_dmx = 0;

void setup() {
  pinMode(DIRECTION_PIN, OUTPUT);		// pin allows control of tranceiver chip direction
  digitalWrite(DIRECTION_PIN, LOW);		// low is input

  ESP8266DMX.setDataReceivedCallback(&gotDMXCallback);
  delay(1000);        //avoid boot print??

  ring.begin();
  setPixel(0,255);   // initial power up indicator 1 red pixel
  sendPixels();
}

// ***************** input callback function *************

void gotDMXCallback(int slots) {
  got_dmx = slots;
}

// ***************** DMX read single frame *************

#define DMX_READ_TIMEOUT_COUNT 10000

void readDMXFrame() {   // <- Attempt to read a single DMX frame
  ESP8266DMX.startInput();

  uint32_t t_o_ct = 0;  // timeout counter should wait no more than 2 frames of DMX
                        // (? good value for limit)
                        // --will delay animation if too long
                        // --will prevent read if too short
  while ( (got_dmx == 0 ) && (t_o_ct < DMX_READ_TIMEOUT_COUNT) ) {
    t_o_ct++;
    if ( ESP8266DMX.isReceiving() ) {       // if started to read packet, wait until done
      t_o_ct = 0;                           // still timeout in case of DMX signal interruption
      while ( ESP8266DMX.isReceiving() && (got_dmx == 0 ) && (t_o_ct < DMX_READ_TIMEOUT_COUNT) ) {
        t_o_ct++;
      }

      t_o_ct = 0;
    }   //<- receiving
  }     //<- ! got_dmx || timeout

  ESP8266DMX.stop();
}

// ***************** sends pixel buffer to ring *************

void sendPixels() {
  uint16_t r,g,b;
  for (int p=0; p<NUM_OF_NEOPIXELS; p++) {
    r = pixels[p][0];
    g = pixels[p][1];
    b = pixels[p][2];
    r = (r*r)/255;    //gamma correct
    g = (g*g)/255;
    b = (b*b)/255;
    if ( LEDS_PER_NEOPIXEL == 3 ) {
    	ring.setPixelColor(p, r, g, b);		//RGB
    } else if ( LEDS_PER_NEOPIXEL == 4 ) {
    	uint16_t w = pixels[p][3];
    	w = (w*w)/255;
    	ring.setPixelColor(p, r, g, b, w);	//RGBW
    }
  }
  ring.show();
}

// ***************** set the dmx value in the pixel buffer *************

void setPixel(uint16_t index, uint8_t value) {
  uint8_t pixel = index/LEDS_PER_NEOPIXEL;
  uint8_t color = index%LEDS_PER_NEOPIXEL;
  pixels[pixel][color] = value;
}

/************************************************************************

  The main loop tries to read a single DMX frame
  It then checks to see if slots were read (got_dmx>0)
  If so, it sends the values to the NEoPixels

*************************************************************************/

void loop() {
  readDMXFrame();

  if ( got_dmx ) {
    for (int i=0; i<total_pixels; i++) {
    	setPixel(i, ESP8266DMX.getSlot(i+1));
    }
    sendPixels();
    got_dmx = 0;
  }

}

error: pathspec 'examples/DMXInputNeoPixel/DMXInputNeoPixel.ino' did not match any file(s) known to git


Arduino script to time a run

Arduino script to time a run

/*  quick_release
    Control a quick release data acquisition run
    communicate over serial to start a run, taking as inputs
     - pre-delay (time to wait before changing length)
     - step-to-exposure delay
     - exposure duration
     - post-exposure time before length is reset
    Created by CDW on 2014.12.02
*/

// Timing variables
const int PREDELAY  = 100;    // in ms
const int STEPDELAY = 10;     // in µs
const int EXPOSUREDUR = 100;  // in µs
const int POSTEXPOSURE = 100; // in ms

// Non-global variable declaration
int stepdelay = STEPDELAY;
bool run_now = false;

// Pin configuration
int LEN_OUT    = 2; // to length controller
int LEN_LED    = 3;
int SHUT_OUT   = 4; // to quick shutter
int SHUT_LED   = 5;
int EXPOSE_OUT = 6; // to exposure trigger
int EXPOSE_LED = 7;

// Begin setup of board
void setup()
{
    // Initialize serial interface
    Serial.begin(115200); // Start serial connection
    while (!Serial) {
        ; // wait for serial (for Leonardo only)
    }
    Serial.println("Hi"); // Demonstrate serial connection
    // Set pin modes
    pinMode(LEN_OUT, OUTPUT);
    pinMode(LEN_LED, OUTPUT);
    pinMode(SHUT_OUT, OUTPUT);
    pinMode(SHUT_LED, OUTPUT);
    pinMode(EXPOSE_OUT, OUTPUT);
    pinMode(EXPOSE_LED, OUTPUT);
    // Initial pin states
    digitalWrite(LEN_OUT, LOW);
    digitalWrite(LEN_LED, LOW);
    digitalWrite(SHUT_OUT, LOW);
    digitalWrite(SHUT_LED, LOW);
    digitalWrite(EXPOSE_OUT, LOW);
    digitalWrite(EXPOSE_LED, LOW);
    // Close out setup
    Serial.println("Setup complete");
}

// Loop through
void loop()
{
    check_serial(); // read input if it exists
    if (run_now) {
        run(PREDELAY, stepdelay, EXPOSUREDUR, POSTEXPOSURE);
        Serial.println("Run complete"); // tag that we've done a run
        run_now = false; // reset the run flag
    }
}

// Read data from serial, just the step-to-exposure delay
void check_serial()
{
    if(Serial.available() >= 2) {      // only when something is available
        stepdelay = Serial.parseInt(); // first int is delay
        Serial.println(stepdelay);     // echo back as sanity check
        char ending = (char)Serial.read();
        if (ending == '\n') {          // ignore out-of-format commands
            run_now = true;
        }
    }
}

// Control a run
void run(int predelay, int stepdelay, int exposureduration, int postexposure)
{
    delay(predelay);
    digitalWrite(SHUT_OUT, HIGH); // shutter opened before step delay
    digitalWrite(SHUT_LED, HIGH); // may open during pre-delay in future
    digitalWrite(LEN_OUT, HIGH);
    digitalWrite(LEN_LED, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(EXPOSE_OUT, HIGH);
    digitalWrite(EXPOSE_LED, HIGH);
    delayMicroseconds(exposureduration);
    digitalWrite(EXPOSE_OUT, LOW);
    digitalWrite(EXPOSE_LED, LOW);
    digitalWrite(SHUT_OUT, LOW);
    digitalWrite(SHUT_LED, LOW);
    delay(postexposure);
    digitalWrite(LEN_OUT, LOW);
    digitalWrite(LEN_LED, LOW);
}

error: pathspec 'quick_release.ino' did not match any file(s) known to git


new file for main app code

new file for main app code

#include <CurieIMU.h>
#include <MadgwickAHRS.h>
#include <NewPing.h>

// ULTRASONIC
#define TRIGGER_PIN_1  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN_1     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN_2  10  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN_2     9  // Arduino pin tied to echo pin on the ultrasonic sensor.

#define DETECTION_THRESH 50 // Distance cutoff for passing
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar_1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar_2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// MADGEWICK
Madgwick filter;
unsigned long microsPerReading, microsPrevious;
float accelScale, gyroScale;

// SETUP
void setup() {
  Serial.begin(9600);

  // start the IMU and filter
  CurieIMU.begin();
  CurieIMU.setGyroRate(25);
  CurieIMU.setAccelerometerRate(25);
  filter.begin(25);

  // Set the accelerometer range to 2G
  CurieIMU.setAccelerometerRange(2);
  // Set the gyroscope range to 250 degrees/second
  CurieIMU.setGyroRange(250);

  // initialize variables to pace updates to correct rate
  microsPerReading = 1000000 / 25;
  microsPrevious = micros();
}

void loop() {
  int aix, aiy, aiz;
  int gix, giy, giz;
  float ax, ay, az;
  float gx, gy, gz;
  float doorAngle;
  int ping_1, ping_2;
  unsigned long microsNow;

  // check if it's time to read data and update the filter
  microsNow = micros();
  if (microsNow - microsPrevious >= microsPerReading) {

    // read raw data from CurieIMU
    CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);

    // convert from raw data to gravity and degrees/second units
    ax = convertRawAcceleration(aix);
    ay = convertRawAcceleration(aiy);
    az = convertRawAcceleration(aiz);
    gx = convertRawGyro(gix);
    gy = convertRawGyro(giy);
    gz = convertRawGyro(giz);

    // update the filter, which computes orientation
    filter.updateIMU(gx, gy, gz, ax, ay, az);

    // print the heading, pitch and roll
    doorAngle = filter.getYaw();
    Serial.print("Door Angle: ");
    Serial.println(doorAngle);

    ping_1 = sonar_1.ping_cm();
    ping_2 = sonar_2.ping_cm();
    printpings(ping_1, ping_2);

    // increment previous time, so we keep proper pace
    microsPrevious = microsPrevious + microsPerReading;
  }
}

float convertRawAcceleration(int aRaw) {
  // since we are using 2G range
  // -2g maps to a raw value of -32768
  // +2g maps to a raw value of 32767

  float a = (aRaw * 2.0) / 32768.0;
  return a;
}

float convertRawGyro(int gRaw) {
  // since we are using 250 degrees/seconds range
  // -250 maps to a raw value of -32768
  // +250 maps to a raw value of 32767

  float g = (gRaw * 250.0) / 32768.0;
  return g;
}

void printpings(int ping_1, int ping_2) {
  Serial.print("Ping 1: ");
  Serial.print(ping_1); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.print(" cm | Ping 2: ");
  Serial.print(ping_2); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println(" cm");
}

error: pathspec 'Turnstyle/Turnstyle.ino' did not match any file(s) known to git


add Catch Unit example

add Catch Unit example

/*
    Description: Control Catch Unit through PWM.
*/

#include <M5Stack.h>

const int servoPin = 26;
int freq = 50;
int ledChannel = 0;
int resolution = 10;
void setup() {
  // put your setup code here, to run once:
  M5.begin();
  M5.Power.begin();
  M5.Lcd.setCursor(100, 50, 4);
  M5.Lcd.println("Catch Unit");
  M5.Lcd.setCursor(40, 120, 4);
  M5.Lcd.println("Connect to the Port B");
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(servoPin, ledChannel);
}

void loop() {
  // High level 0.5ms is angle 0°
  // duty = 0.5/20ms = 0.025, 0.025*1023≈25
    ledcWrite(ledChannel, 25);
    delay(2000);
  // High level 1ms is angle 45°
  // duty = 1/20ms = 0.05, 0.05*1023≈50
    ledcWrite(ledChannel, 50);
    delay(2000);
}

error: pathspec 'examples/Unit/CATCH/CATCH.ino' did not match any file(s) known to git