A delivery capsule configured to be swallowed to deliver medication inside the gastrointestinal tract, including a pressure sensor for measuring the internal pressure in the delivery capsule; one or more compartments for storing medication; and a controller configured to control the release a dose of medication from a compartment responsive to the measurements of the pressure sensor.